C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Variants and lists in Ocaml

By : Mahe babu
Date : November 21 2020, 11:01 PM
I wish this help you Is it possible to define a list using variants (or something else) which would contain integers and functions simultaneously? I am particularly interested in mixing functions and some other type in a list.
code :
``````type my_elt =
| Null
| Int of int
| Fun1 of (int-> unit)
| Fun2 of (int-> int)

let eval a =function
| Fun1 f -> f a;Null
| Fun2 f -> Int (f a)
|  _     -> Null

let leval a l = List.map (fun elt -> eval a elt ) l
;;
``````
``````let l=[Int 2;Fun1 (Printf.printf "%d");Fun2 ((+)2) ]
# leval 2 l;;
2- : my_elt list = [Null; Null; Int 4]
``````

Share :

## why does OCaml use subtyping for polymorphic variants?

By : Naren Nallapeta
Date : March 29 2020, 07:55 AM
Does that help To complement Gabriel's answer, one way to think about this is that subtyping provides a weak form of both universal and existential polymorphism. When both directions of parametric polymorphism are available, then the expressiveness of subtyping is mostly subsumed (especially when there is no depth subtyping). But that's not the case in Ocaml.
Ocaml replaces the universal aspect by actual universal polymorphism, but keeps subtyping to give you a form of existential quantification that it otherwise doesn't have. That is needed to form e.g. heterogeneous collections, such as a list in which you want to be able to store arbitrary objects that at least have an a method of the right type.

## Memory presentation for variants in OCaml

By : Devlin Wyatt
Date : March 29 2020, 07:55 AM
I hope this helps you . The runtime can't make the difference between 0, Apple and Empty but it doesn't need to. The compiler checks the types so that Apple will always be used with type t1 and not t2 or int. Once the types are checked by the compiler, you no longer need them because you are sure (you have a proof of it) that the integer 0 with the meaning Apple with never be used with another meaning.

## OCaml: type inference with polymorphic variants

By : user3545635
Date : March 29 2020, 07:55 AM
hope this fix your issue This is yet another instantiation of the let-polymorphism constraints, that hinders the usage of polymorphic recursive function. Since, OCaml 3.12 we have an explicit way to declare that your function is polymorphic.
Your case is a little bit more complex, since you have implicit type variable, that occurs inside the row-polymorphic type. Maybe there is a better way, but my approach is to make this type variable explicit, with the following type definition
code :
``````type 'a t = 'a constraint 'a = [< `A | `B]
``````
``````let rec f : 'a . 'a t -> int = function
| `A -> 0
| `B -> let _ = f `A  in 1
``````
``````module M : sig
val f : [< `A | `B] -> int
end = struct
let rec f : 'a . 'a t -> int = function
| `A -> 0
| `B -> let _ = f `A  in 1
end;;
``````
``````let rec f : 'a . ([< `A | `B] as 'a) -> int = function
| `A -> 0
| `B -> let _ = f `A  in 1
``````

## Equivalent to OCaml variants in Ruby

By : Corey
Date : March 29 2020, 07:55 AM
Hope this helps In languages that don't have a native support for sum types, the usual method is to use a product type with a tag and all the fields that appear in the sum type. In your case this would give:
code :
``````type tag = Sent | Paid | New
type status = {tag: tag; date : date_time; location: location}
``````

## OCaml polymorphic variants in pattern matching

By : Bissacot
Date : March 29 2020, 07:55 AM
wish helps you You cannot locally constrain the type of a case pattern. The type constraint : AST0.t enforces the other patterns' type to AST0.t too. That is why your code does not type check; `Inc is not included in AST0.t.
However, there is a neat feature in OCaml exactly for what you want to do. Use #AST0.t pattern alias, instead of the type constraint. See https://caml.inria.fr/pub/docs/manual-ocaml/lablexamples.html#sec46 for details:
code :
``````  (* I fixed several other trivial typing problems *)
let lower (ast : t list) =
let lower_one = function
| `Inc a -> [`Add (a, a, 1)]
| `Dec a -> [`Sub (a, a, 1)]
| `Cmp (a, b, c) -> [`Sub (13, a, b)]
| #AST0.t as x -> AST0.lower [x]     (* <--- problem line *)
in
List.concat @@ List.map lower_one ast
``````