text
stringlengths
0
601k
type foo += Foo of int * int option | Bar of int option ; ;
let x = Foo ( 3 , Some 4 ) , Bar ( Some 5 ) ; ;
type foo += Foo of string ; ;
let y = x ; ; ; ; ; ;
let x = Foo ( 3 , Some 4 ) , Bar ( Some 5 ) ; ;
type foo += Foo of string ; ;
let y = x ; ;
type foo = . . ; ;
type foo += Foo | Bar of int ; ;
let extension_name e = Obj . extension_name ( Obj . extension_constructor e ) ; ;
let extension_id e = Obj . extension_id ( Obj . extension_constructor e ) ; ;
let n1 = extension_name Foo ; ;
let n2 = extension_name ( Bar 1 ) ; ;
let t = ( extension_id ( Bar 2 ) ) = ( extension_id ( Bar 3 ) ) ; ;
let f = ( extension_id ( Bar 2 ) ) = ( extension_id Foo ) ; ;
let is_foo x = ( extension_id Foo ) = ( extension_id x )
type foo += Foo ; ;
let f = is_foo Foo ; ;
let _ = Obj . extension_constructor 7 ; ;
let _ = Obj . extension_constructor ( object method m = 3 end ) ; ;
module Msg : sig type ' a tag type result = Result : ' a tag * ' a -> result val write : ' a tag -> ' a -> unit val read : unit -> result type ' a tag += Int : int tag module type Desc = sig type t val label : string val write : t -> string val read : string -> t end module Define ( D : Desc ) : sig type ' a tag += C :...
let write_int i = Msg . write Msg . Int i ; ;
module StrM = Msg . Define ( struct type t = string let label = " string " let read s = s let write s = s end ) ; ;
type ' a Msg . tag += String = StrM . C ; ;
let write_string s = Msg . write String s ; ;
let read_one ( ) = let Msg . Result ( tag , body ) = Msg . read ( ) in match tag with Msg . Int -> print_int body | String -> print_string body | _ -> print_string " Unknown " ; ;
let sort ( type s ) set l = let module Set = ( val set : Set . S with type elt = s ) in Set . elements ( List . fold_right Set . add l Set . empty )
let make_set ( type s ) cmp = let module S = Set . Make ( struct type t = s let compare = cmp end ) in ( module S : Set . S with type elt = s )
let both l = List . map ( fun set -> sort set l ) [ make_set compare ; make_set ( fun x y -> compare y x ) ]
let ( ) = print_endline ( String . concat " " ( List . map ( String . concat " " ) / ( both [ " abc " ; " xyz " ; " def " ] ) ) )
module type S = sig type t val to_string : t -> string val apply : t -> t val x : t end
let create ( type s ) to_string apply x = let module M = struct type t = s let to_string = to_string let apply = apply let x = x end in ( module M : S with type t = s )
let forget ( type s ) x = let module M = ( val x : S with type t = s ) in ( module M : S )
let print x = let module M = ( val x : S ) in print_endline ( M . to_string M . x )
let apply x = let module M = ( val x : S ) in let module N = struct include M let x = apply x end in ( module N : S )
let ( ) = let int = forget ( create Int . to_string succ 0 ) in let str = forget ( create ( fun s -> s ) ( fun s -> s ^ s ) " X " ) in List . iter print ( List . map apply [ int ; apply int ; apply ( apply str ) ] )
module TypEq : sig type ( ' a , ' b ) t val apply : ( ' a , ' b ) t -> ' a -> ' b val refl : ( ' a , ' a ) t val sym : ( ' a , ' b ) t -> ( ' b , ' a ) t type ( ' a , ' b ) t = unit let apply _ = Obj . magic let refl = ( ) let sym ( ) = ( ) end
module rec Typ : sig module type PAIR = sig type t type t1 type t2 val eq : ( t , t1 * t2 ) TypEq . t val t1 : t1 Typ . typ val t2 : t2 Typ . typ end type ' a typ = | Int of ( ' a , int ) TypEq . t | String of ( ' a , string ) TypEq . t | Pair of ( module PAIR with type t = ' a ) module type PAIR = sig type t type t1 t...
let int = Int TypEq . refl
let str = String TypEq . refl
let pair ( type s1 ) ( type s2 ) t1 t2 = let module P = struct type t = s1 * s2 type t1 = s1 type t2 = s2 let eq = TypEq . refl let t1 = t1 let t2 = t2 end in let pair = ( module P : PAIR with type t = s1 * s2 ) in Pair pair
module rec Print : sig val to_string : ' a Typ . typ -> ' a -> string let to_string ( type s ) t x = match t with | Int eq -> Int . to_string ( TypEq . apply eq x ) | String eq -> Printf . sprintf " % S " ( TypEq . apply eq x ) | Pair p -> let module P = ( val p : PAIR with type t = s ) in let ( x1 , x2 ) = TypEq . app...
let ( ) = print_endline ( Print . to_string int 10 ) ; print_endline ( Print . to_string ( pair int ( pair str int ) ) ( 123 , ( " A " , 456 ) ) )
module type S1 = sig end
module type S2 = S1
let _f ( x : ( module S1 ) ) : ( module S2 ) = x
module X = struct module type S end
module Y = struct include X end
let _f ( x : ( module X . S ) ) : ( module Y . S ) = x
module type S3 = sig val x : bool end ; ;
let f = function | Some ( module M : S3 ) when M . x -> 1 | Some _ [ @ foooo ] -> 2 | None -> 3 ; ;
type ' a ty = | Int : int ty | Bool : bool ty
let fbool ( type t ) ( x : t ) ( tag : t ty ) = match tag with | Bool -> x ; ;
let fint ( type t ) ( x : t ) ( tag : t ty ) = match tag with | Int -> x > 0 ; ;
let f ( type t ) ( x : t ) ( tag : t ty ) = match tag with | Int -> x > 0 | Bool -> x
let g ( type t ) ( x : t ) ( tag : t ty ) = match tag with | Bool -> x | Int -> x > 0
let id x = x ; ;
let idb1 = ( fun id -> let _ = id true in id ) id ; ;
let idb2 : bool -> bool = id ; ;
let idb3 ( _ : bool ) = false ; ;
let g ( type t ) ( x : t ) ( tag : t ty ) = match tag with | Bool -> idb3 x | Int -> x > 0
let g ( type t ) ( x : t ) ( tag : t ty ) = match tag with | Bool -> idb2 x | Int -> x > 0
type ' a ty = | Int : int ty | String : string ty | List : ' a ty -> ' a list ty | Pair : ( ' a ty * ' b ty ) -> ( ' a * ' b ) ty ; ;
type variant = | VInt of int | VString of string | VList of variant list | VPair of variant * variant
let rec variantize : type t . t ty -> t -> variant = fun ty x -> match ty with | Int -> VInt x | String -> VString x | List ty1 -> VList ( List . map ( variantize ty1 ) x ) | Pair ( ty1 , ty2 ) -> VPair ( variantize ty1 ( fst x ) , variantize ty2 ( snd x ) )
let rec devariantize : type t . t ty -> variant -> t = fun ty v -> match ty , v with | Int , VInt x -> x | String , VString x -> x | List ty1 , VList vl -> List . map ( devariantize ty1 ) vl | Pair ( ty1 , ty2 ) , VPair ( x1 , x2 ) -> ( devariantize ty1 x1 , devariantize ty2 x2 ) | _ -> raise VariantMismatch ; ;
type ' a ty = | Int : int ty | String : string ty | List : ' a ty -> ' a list ty | Pair : ( ' a ty * ' b ty ) -> ( ' a * ' b ) ty | Record : ' a record -> ' a ty { path : string ; fields : ' a field_ list ; } | Field : ( ' a , ' b ) field -> ' a field_ { label : string ; field_type : ' b ty ; get : ( ' a -> ' b ) ; } ;...
type variant = | VInt of int | VString of string | VList of variant list | VPair of variant * variant | VRecord of ( string * variant ) list
let rec variantize : type t . t ty -> t -> variant = fun ty x -> match ty with | Int -> VInt x | String -> VString x | List ty1 -> VList ( List . map ( variantize ty1 ) x ) | Pair ( ty1 , ty2 ) -> VPair ( variantize ty1 ( fst x ) , variantize ty2 ( snd x ) ) | Record { fields } -> VRecord ( List . map ( fun ( Field { f...
type ' a ty = | Int : int ty | String : string ty | List : ' a ty -> ' a list ty | Pair : ( ' a ty * ' b ty ) -> ( ' a * ' b ) ty | Record : ( ' a , ' builder ) record -> ' a ty { path : string ; fields : ( ' a , ' builder ) field list ; create_builder : ( unit -> ' builder ) ; of_builder : ( ' builder -> ' a ) ; } | F...
let rec devariantize : type t . t ty -> variant -> t = fun ty v -> match ty , v with | Int , VInt x -> x | String , VString x -> x | List ty1 , VList vl -> List . map ( devariantize ty1 ) vl | Pair ( ty1 , ty2 ) , VPair ( x1 , x2 ) -> ( devariantize ty1 x1 , devariantize ty2 x2 ) | Record { fields ; create_builder ; of...
type my_record = { a : int ; b : string list ; }
let my_record = let fields = [ Field { label = " a " ; field_type = Int ; get = ( fun { a } -> a ) ; set = ( fun ( r , _ ) x -> r := Some x ) } ; Field { label = " b " ; field_type = List String ; get = ( fun { b } -> b ) ; set = ( fun ( _ , r ) x -> r := Some x ) } ; ] in let create_builder ( ) = ( ref None , ref None...
type ( _ , _ ) ty = | Int : ( int , _ ) ty | String : ( string , _ ) ty | List : ( ' a , ' e ) ty -> ( ' a list , ' e ) ty | Option : ( ' a , ' e ) ty -> ( ' a option , ' e ) ty | Pair : ( ( ' a , ' e ) ty * ( ' b , ' e ) ty ) -> ( ' a * ' b , ' e ) ty | Var : ( ' a , ' a -> ' e ) ty | Rec : ( ' a , ' a -> ' e ) ty -> ...
type _ ty_env = | Enil : unit ty_env | Econs : ( ' a , ' e ) ty * ' e ty_env -> ( ' a -> ' e ) ty_env ; ;
type ( _ , _ ) eq = Eq : ( ' a , ' a ) eq
let rec eq_sel : type a b c . ( a , b ) ty_sel -> ( a , c ) ty_sel -> ( b , c ) eq option = fun s1 s2 -> match s1 , s2 with | Thd , Thd -> Some Eq | Ttl s1 , Ttl s2 -> ( match eq_sel s1 s2 with None -> None | Some Eq -> Some Eq ) | _ -> None
let rec get_case : type a b e . ( b , a ) ty_sel -> ( string * ( e , b ) ty_case ) list -> string * ( a , e ) ty option = fun sel cases -> match cases with | ( name , TCnoarg sel ' ) :: rem -> begin match eq_sel sel sel ' with | None -> get_case sel rem | Some Eq -> name , None end | ( name , TCarg ( sel ' , ty ) ) :: ...
type variant = | VInt of int | VString of string | VList of variant list | VOption of variant option | VPair of variant * variant | VConv of string * variant | VSum of string * variant option
let may_map f = function Some x -> Some ( f x ) | None -> None
let rec variantize : type a e . e ty_env -> ( a , e ) ty -> a -> variant = fun e ty v -> match ty with | Int -> VInt v | String -> VString v | List t -> VList ( List . map ( variantize e t ) v ) | Option t -> VOption ( may_map ( variantize e t ) v ) | Pair ( t1 , t2 ) -> VPair ( variantize e t1 ( fst v ) , variantize e...
let rec devariantize : type t e . e ty_env -> ( t , e ) ty -> variant -> t = fun e ty v -> match ty , v with | Int , VInt x -> x | String , VString x -> x | List ty1 , VList vl -> List . map ( devariantize e ty1 ) vl | Pair ( ty1 , ty2 ) , VPair ( x1 , x2 ) -> ( devariantize e ty1 x1 , devariantize e ty2 x2 ) | Rec t ,...
let wrap_A t = Conv ( " ` A " , ( fun ( ` A x ) -> x ) , ( fun x -> ` A x ) , t ) ; ;
let ty a = Rec ( wrap_A ( Option ( Pair ( a , Var ) ) ) ) ; ;
let v = variantize Enil ( ty Int ) ; ;
let x = v ( ` A ( Some ( 1 , ` A ( Some ( 2 , ` A None ) ) ) ) ) ; ;
let triple t1 t2 t3 = Conv ( " Triple " , ( fun ( a , b , c ) -> ( a , ( b , c ) ) ) , ( fun ( a , ( b , c ) ) -> ( a , b , c ) ) , Pair ( t1 , Pair ( t2 , t3 ) ) )
let v = variantize Enil ( triple String Int Int ) ( " A " , 2 , 3 ) ; ;
let ty_abc = let proj = function ` A n -> " A " , Some ( Tdyn ( Int , n ) ) | ` B s -> " B " , Some ( Tdyn ( String , s ) ) | ` C -> " C " , None and inj : type c . ( int -> string -> noarg -> unit , c ) ty_sel * c -> [ ` A of int | ` B of string | ` C ] = function Thd , v -> ` A v | Ttl Thd , v -> ` B v | Ttl ( Ttl Th...
let v = variantize Enil ty_abc ( ` A 3 )
let a = devariantize Enil ty_abc v
type ' a vlist = [ ` Nil | ` Cons of ' a * ' a vlist ]
let ty_list : type a e . ( a , e ) ty -> ( a vlist , e ) ty = fun t -> let tcons = Pair ( Pop t , Var ) in Rec ( Sum { sum_proj = ( function ` Nil -> " Nil " , None | ` Cons p -> " Cons " , Some ( Tdyn ( tcons , p ) ) ) ; sum_cases = [ " Nil " , TCnoarg Thd ; " Cons " , TCarg ( Ttl Thd , tcons ) ] ; sum_inj = fun ( typ...
let v = variantize Enil ( ty_list Int ) ( ` Cons ( 1 , ` Cons ( 2 , ` Nil ) ) ) ; ;
type ( _ , _ ) ty = | Int : ( int , _ ) ty | String : ( string , _ ) ty | List : ( ' a , ' e ) ty -> ( ' a list , ' e ) ty | Option : ( ' a , ' e ) ty -> ( ' a option , ' e ) ty | Pair : ( ( ' a , ' e ) ty * ( ' b , ' e ) ty ) -> ( ' a * ' b , ' e ) ty | Var : ( ' a , ' a -> ' e ) ty | Rec : ( ' a , ' a -> ' e ) ty -> ...
let ty_abc : ( [ ` A of int | ` B of string | ` C ] , ' e ) ty = Sum ( ( function ` A n -> " A " , Some ( Tdyn ( Int , n ) ) | ` B s -> " B " , Some ( Tdyn ( String , s ) ) | ` C -> " C " , None ) , ( function " A " , Some ( Tdyn ( Int , n ) ) -> ` A n | " B " , Some ( Tdyn ( String , s ) ) -> ` B s | " C " , None -> `...
let ty_list : type a e . ( a , e ) ty -> ( a vlist , e ) ty = fun t -> let targ = Pair ( Pop t , Var ) in Rec ( Sum ( ( function ` Nil -> " Nil " , None | ` Cons p -> " Cons " , Some ( Tdyn ( targ , p ) ) ) , ( function " Nil " , None -> ` Nil | " Cons " , Some ( Tdyn ( Pair ( _ , Var ) , ( p : a * a vlist ) ) ) -> ` C...
type ( _ , _ ) ty = | Int : ( int , _ ) ty | String : ( string , _ ) ty | List : ( ' a , ' e ) ty -> ( ' a list , ' e ) ty | Option : ( ' a , ' e ) ty -> ( ' a option , ' e ) ty | Pair : ( ( ' a , ' e ) ty * ( ' b , ' e ) ty ) -> ( ' a * ' b , ' e ) ty | Var : ( ' a , ' a -> ' e ) ty | Rec : ( ' a , ' a -> ' e ) ty -> ...
let ty_abc : ( [ ` A of int | ` B of string | ` C ] as ' a , ' e ) ty = Sum ( object method proj = function ` A n -> " A " , Some ( Tdyn ( Int , n ) ) | ` B s -> " B " , Some ( Tdyn ( String , s ) ) | ` C -> " C " , None method cases = [ " A " , TCarg ( Thd , Int ) ; " B " , TCarg ( Ttl Thd , String ) ; " C " , TCnoarg...
type ' a vlist = [ ` Nil | ` Cons of ' a * ' a vlist ]