8 people like it.

Universal Type

Implements a type into which any other type can be embedded. Check out this link for a discussion: http://ocaml.janestreet.com/?q=node/18

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
type t = (unit -> unit) * (unit -> unit)

type Binder<'a> = ('a -> t)*(t -> 'a option)

let embed<'a> (): Binder<'a> =
    let r = ref None
    let put x = (fun () -> r := Some x), (fun () -> r := None) 
    let get (f, g) = f (); let res = !r in g (); res in
    put, get

let binder<'a> : Binder<'a> = embed ()

let (of_int, to_int) = binder<int>
let (of_string, to_string) = binder<string>
let (of_float, to_float) = binder<float>

let heterogenous_list = [ of_int 3; of_float 4. ; of_int 5; of_int 7 ; of_string "F#" ]
    
let just_floats = List.choose to_float heterogenous_list
let just_ints =  List.choose to_int heterogenous_list
type t = (unit -> unit) * (unit -> unit)

Full name: Script.t
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
type Binder<'a> = ('a -> t) * (t -> 'a option)

Full name: Script.Binder<_>
type 'T option = Option<'T>

Full name: Microsoft.FSharp.Core.option<_>
val embed : unit -> Binder<'a>

Full name: Script.embed
val r : 'a option ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
union case Option.None: Option<'T>
val put : ('a -> (unit -> unit) * (unit -> unit))
val x : 'a
union case Option.Some: Value: 'T -> Option<'T>
val get : ((unit -> unit) * (unit -> unit) -> 'a option)
val f : (unit -> unit)
val g : (unit -> unit)
val res : 'a option
val binder<'a> : Binder<'a>

Full name: Script.binder
val of_int : (int -> t)

Full name: Script.of_int
val to_int : (t -> int option)

Full name: Script.to_int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val of_string : (string -> t)

Full name: Script.of_string
val to_string : (t -> string option)

Full name: Script.to_string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val of_float : (float -> t)

Full name: Script.of_float
val to_float : (t -> float option)

Full name: Script.to_float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val heterogenous_list : t list

Full name: Script.heterogenous_list
val just_floats : float list

Full name: Script.just_floats
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val choose : chooser:('T -> 'U option) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.choose
val just_ints : int list

Full name: Script.just_ints
Raw view New version

More information

Link:http://fssnip.net/6W
Posted:6 years ago
Author:Ademar Gonzalez
Tags: types , universal , heterogeneous , container