text
stringlengths
0
601k
let rec compare : type a b . a rep -> b rep -> ( string , ( a , b ) equal ) sum = fun a b -> match a , b with | I , I -> Inr Eq | Ar ( x , y ) , Ar ( s , t ) -> begin match compare x s with | Inl _ as e -> e | Inr Eq -> match compare y t with | Inl _ as e -> e | Inr Eq as e -> e end | I , Ar _ -> Inl " I <> Ar _ " | Ar...
type term = | C of int | Ab : string * ' a rep * term -> term | Ap of term * term | V of string
type _ ctx = | Cnil : rnil ctx | Ccons : ' t * string * ' x rep * ' e ctx -> ( ' t , ' x , ' e ) rcons ctx ; ;
type _ checked = | Cerror of string | Cok : ( ' e , ' t ) lam * ' t rep -> ' e checked
let rec lookup : type e . string -> e ctx -> e checked = fun name ctx -> match ctx with | Cnil -> Cerror ( " Name not found : " ^ name ) | Ccons ( l , s , t , rs ) -> if s = name then Cok ( Var l , t ) else match lookup name rs with | Cerror m -> Cerror m | Cok ( v , t ) -> Cok ( Shift v , t ) ; ;
let rec tc : type n e . n nat -> e ctx -> term -> e checked = fun n ctx t -> match t with | V s -> lookup s ctx | Ap ( f , x ) -> begin match tc n ctx f with | Cerror _ as e -> e | Cok ( f ' , ft ) -> match tc n ctx x with | Cerror _ as e -> e | Cok ( x ' , xt ) -> match ft with | Ar ( a , b ) -> begin match compare a ...
let ctx0 = Ccons ( Zero , " 0 " , I , Ccons ( Suc , " S " , Ar ( I , I ) , Ccons ( Add , " " , + Ar ( I , Ar ( I , I ) ) , Cnil ) ) )
let ex1 = Ab ( " x " , I , Ap ( Ap ( V " " , + V " x " ) , V " x " ) ) ; ;
let c1 = tc NZ ctx0 ex1 ; ;
let ex2 = Ap ( ex1 , C 3 ) ; ;
let c2 = tc NZ ctx0 ex2 ; ;
let eval_checked env = function | Cerror s -> failwith s | Cok ( e , I ) -> ( eval_lam env e : int ) | Cok _ -> failwith " Can only evaluate expressions of type I " ; ;
let v2 = eval_checked env0 c2 ; ;
type _ mode = | Pexp : pexp mode | Pval : pval mode
type ( ' a , ' b ) tarr = TARR
type ( _ , _ ) rel = | IntR : ( tint , int ) rel | IntTo : ( ' b , ' s ) rel -> ( ( tint , ' b ) tarr , int -> ' s ) rel
type ( _ , _ , _ ) lam = | Const : ( ' a , ' b ) rel * ' b -> ( pval , ' env , ' a ) lam | Var : ' a -> ( pval , ( ' a , ' t , ' e ) rcons , ' t ) lam | Shift : ( ' m , ' e , ' t ) lam -> ( ' m , ( ' a , ' q , ' e ) rcons , ' t ) lam | Lam : ' a * ( ' m , ( ' a , ' s , ' e ) rcons , ' t ) lam -> ( pval , ' e , ( ' s , ...
let ex1 = App ( Lam ( X , Var X ) , Const ( IntR , 3 ) )
let rec mode : type m e t . ( m , e , t ) lam -> m mode = function | Lam ( v , body ) -> Pval | Var v -> Pval | Const ( r , v ) -> Pval | Shift e -> mode e | App _ -> Pexp ; ;
type ( _ , _ ) sub = | Id : ( ' r , ' r ) sub | Bind : ' t * ( ' m , ' r2 , ' x ) lam * ( ' r , ' r2 ) sub -> ( ( ' t , ' x , ' r ) rcons , ' r2 ) sub | Push : ( ' r1 , ' r2 ) sub -> ( ( ' a , ' b , ' r1 ) rcons , ( ' a , ' b , ' r2 ) rcons ) sub
type ( _ , _ ) lam ' = Ex : ( ' m , ' s , ' t ) lam -> ( ' s , ' t ) lam ' ; ;
let rec subst : type m1 r t s . ( m1 , r , t ) lam -> ( r , s ) sub -> ( s , t ) lam ' = fun t s -> match t , s with | _ , Id -> Ex t | Const ( r , c ) , sub -> Ex ( Const ( r , c ) ) | Var v , Bind ( x , e , r ) -> Ex e | Var v , Push sub -> Ex ( Var v ) | Shift e , Bind ( _ , _ , r ) -> subst e r | Shift e , Push sub...
type ' a rlam = ( ( pexp , closed , ' a ) lam , ( pval , closed , ' a ) lam ) sum ; ;
let rec rule : type a b . ( pval , closed , ( a , b ) tarr ) lam -> ( pval , closed , a ) lam -> b rlam = fun v1 v2 -> match v1 , v2 with | Lam ( x , body ) , v -> begin match subst body ( Bind ( x , v , Id ) ) with Ex term -> match mode term with | Pexp -> Inl term | Pval -> Inr term end | Const ( IntTo b , f ) , Cons...
let rec onestep : type m t . ( m , closed , t ) lam -> t rlam = function | Lam ( v , body ) -> Inr ( Lam ( v , body ) ) | Const ( r , v ) -> Inr ( Const ( r , v ) ) | App ( e1 , e2 ) -> match mode e1 , mode e2 with | Pexp , _ -> begin match onestep e1 with | Inl e -> Inl ( App ( e , e2 ) ) | Inr v -> Inl ( App ( v , e2...
type ( ' env , ' a ) var = | Zero : ( ' a * ' env , ' a ) var | Succ : ( ' env , ' a ) var -> ( ' b * ' env , ' a ) var ; ;
type ( ' env , ' a ) typ = | Tint : ( ' env , int ) typ | Tbool : ( ' env , bool ) typ | Tvar : ( ' env , ' a ) var -> ( ' env , ' a ) typ ; ;
let f : type env a . ( env , a ) typ -> ( env , a ) typ -> int = fun ta tb -> match ta , tb with | Tint , Tint -> 0 | Tbool , Tbool -> 1 | Tvar var , tb -> 2 | _ -> . ; ;
type inkind = [ ` Link | ` Nonlink ]
type _ inline_t = | Text : string -> [ < inkind > ` Nonlink ] inline_t | Bold : ' a inline_t list -> ' a inline_t | Link : string -> [ < inkind > ` Link ] inline_t | Mref : string * [ ` Nonlink ] inline_t list -> [ < inkind > ` Link ] inline_t ; ;
let uppercase seq = let rec process : type a . a inline_t -> a inline_t = function | Text txt -> Text ( String . uppercase_ascii txt ) | Bold xs -> Bold ( List . map process xs ) | Link lnk -> Link lnk | Mref ( lnk , xs ) -> Mref ( lnk , List . map process xs ) in List . map process seq ; ;
type ast_t = | Ast_Text of string | Ast_Bold of ast_t list | Ast_Link of string | Ast_Mref of string * ast_t list ; ;
let inlineseq_from_astseq seq = let rec process_nonlink = function | Ast_Text txt -> Text txt | Ast_Bold xs -> Bold ( List . map process_nonlink xs ) | _ -> assert false in let rec process_any = function | Ast_Text txt -> Text txt | Ast_Bold xs -> Bold ( List . map process_any xs ) | Ast_Link lnk -> Link lnk | Ast_Mref...
type _ linkp = | Nonlink : [ ` Nonlink ] linkp | Maylink : inkind linkp ; ;
let inlineseq_from_astseq seq = let rec process : type a . a linkp -> ast_t -> a inline_t = fun allow_link ast -> match ( allow_link , ast ) with | ( Maylink , Ast_Text txt ) -> Text txt | ( Nonlink , Ast_Text txt ) -> Text txt | ( x , Ast_Bold xs ) -> Bold ( List . map ( process x ) xs ) | ( Maylink , Ast_Link lnk ) -...
type _ linkp2 = Kind : ' a linkp -> ( [ < inkind ] as ' a ) linkp2 ; ;
let rec process : type a . a linkp2 -> ast_t -> a inline_t = fun allow_link ast -> match ( allow_link , ast ) with | ( Kind _ , Ast_Text txt ) -> Text txt | ( x , Ast_Bold xs ) -> Bold ( List . map ( process x ) xs ) | ( Kind Maylink , Ast_Link lnk ) -> Link lnk | ( Kind Nonlink , Ast_Link _ ) -> assert false | ( Kind ...
module Add ( T : sig type two end ) = struct type _ t = | One : [ ` One ] t | Two : T . two t let add ( type a ) : a t * a t -> string = function | One , One -> " two " | Two , Two -> " four " end ; ;
module B : sig type ( _ , _ ) t = Eq : ( ' a , ' a ) t val f : ' a -> ' b -> ( ' a , ' b ) t end = struct type ( _ , _ ) t = Eq : ( ' a , ' a ) t let f t1 t2 = Obj . magic Eq end ; ;
let of_type : type a . a -> a = fun x -> match B . f x 4 with | Eq -> 5 ; ;
type _ constant = | Int : int -> int constant | Bool : bool -> bool constant
type ( _ , _ , _ ) binop = | Eq : ( ' a , ' a , bool ) binop | Leq : ( ' a , ' a , bool ) binop | Add : ( int , int , int ) binop
let eval ( type a ) ( type b ) ( type c ) ( bop ( : a , b , c ) binop ) ( x : a constant ) ( y : b constant ) : c constant = match bop , x , y with | Eq , Bool x , Bool y -> Bool ( if x then y else not y ) | Leq , Int x , Int y -> Bool ( x <= y ) | Leq , Bool x , Bool y -> Bool ( x <= y ) | Add , Int x , Int y -> Int (...
let _ = eval Eq ( Int 2 ) ( Int 3 )
type tag = [ ` TagA | ` TagB | ` TagC ] ; ;
type ' a poly = AandBTags : [ < ` TagA of int | ` TagB ] poly | ATag : [ < ` TagA of int ] poly ; ;
let intA = function ` TagA i -> i
let intB = function ` TagB -> 4 ; ;
let intAorB = function ` TagA i -> i | ` TagB -> 4 ; ;
type _ wrapPoly = WrapPoly : ' a poly -> ( [ < ` TagA of int | ` TagB ] as ' a ) wrapPoly ; ;
let example6 : type a . a wrapPoly -> ( a -> int ) = fun w -> match w with | WrapPoly ATag -> intA | WrapPoly _ -> intA ; ;
let _ = example6 ( WrapPoly AandBTags ) ` TagB ; ;
module F ( S : sig type ' a t end ) = struct type _ ab = A : int S . t ab | B : float S . t ab let f : int S . t ab -> float S . t ab -> string = fun ( l : int S . t ab ) ( r : float S . t ab ) -> match l , r with | A , B -> " f A B " end ; ;
module F ( S : sig type ' a t end ) = struct type a = int * int type b = int -> int type _ ab = A : a S . t ab | B : b S . t ab let f : a S . t ab -> b S . t ab -> string = fun l r -> match l , r with | A , B -> " f A B " end ; ;
type ( _ , _ ) t = Any : ( ' a , ' b ) t | Eq : ( ' a , ' a ) t ; ;
module M : sig type s = private [ > ` A ] val eq : ( s , [ ` A | ` B ] ) t struct type s = [ ` A | ` B ] let eq = Eq end ; ;
let f : ( M . s , [ ` A | ` B ] ) t -> string = function | Any -> " Any " ; ;
let ( ) = print_endline ( f M . eq ) ; ;
module N : sig type s = private < a : int ; . . > val eq : ( s , < a : int ; b : bool ) > t struct type s = < a : int ; b : bool > let eq = Eq end ; ;
let f : ( N . s , < a : int ; b : bool ) > t -> string = function | Any -> " Any " ; ;
type ( _ , _ ) comp = | Eq : ( ' a , ' a ) comp | Diff : ( ' a , ' b ) comp ; ;
module U = struct type t = T end ; ;
module M : sig type t = T val comp : ( U . t , t ) comp include U let comp = Eq end ; ;
module U = struct type t = { x : int } end ; ;
module M : sig type t = { x : int } val comp : ( U . t , t ) comp include U let comp = Eq end ; ;
type ' a t = T of ' a
type ' a s = S of ' a
type ( _ , _ ) eq = Refl : ( ' a , ' a ) eq ; ;
let f : ( int s , int t ) eq -> unit = function Refl -> ( ) ; ;
module M ( S : sig type ' a t = T of ' a type ' a s = T of ' a end ) =
type _ nat = Zero : [ ` Zero ] nat | Succ : ' a nat -> [ ` Succ of ' a ] nat ; ;
type ' a pre_nat = [ ` Zero | ` Succ of ' a ] ; ;
type aux = | Aux : [ ` Succ of [ [ [ [ ` <<< Zero ] pre_nat ] pre_nat ] pre_nat ] ] nat -> aux ; ;
let f ( Aux x ) = match x with | Succ Zero -> " 1 " | Succ ( Succ Zero ) -> " 2 " | Succ ( Succ ( Succ Zero ) ) -> " 3 " | Succ ( Succ ( Succ ( Succ Zero ) ) ) -> " 4 " | _ -> . ; ;
type _ t = C : ( ( ( ' a -> ' o ) -> ' o ) -> ( ' b -> ' o ) -> ' o ) t
let f : type a o . ( ( a -> o ) -> o ) t -> ( a -> o ) -> o = fun C k -> k ( fun x -> x ) ; ;
type ( _ , _ ) t = A : ( ' a , ' a ) t ; ;
module M ( A : sig module type T end ) ( B : sig module type T end ) = struct let f : ( ( module A . T ) , ( module B . T ) ) t -> string = function | B s -> s end ; ;
module A = struct module type T = sig end end ; ;
let x = N . f A ; ;
type ( ' a , ' result , ' visit_action ) context = | Local : ( ' a , ( ' a * insert ) as ' result , ' a local_visit_action ) context | Global : ( ' a , ' a , ' a visit_action ) context ; ;
let vexpr ( type visit_action ) : ( _ , _ , visit_action ) context -> _ -> visit_action = function | Local -> fun _ -> raise Exit | Global -> fun _ -> raise Exit ; ;
let vexpr ( type visit_action ) : ( ' a , ' result , visit_action ) context -> ' a -> visit_action = function | Local -> fun _ -> raise Exit | Global -> fun _ -> raise Exit ; ;
let vexpr ( type result ) ( type visit_action ) : ( unit , result , visit_action ) context -> unit -> visit_action = function | Local -> fun _ -> raise Exit | Global -> fun _ -> raise Exit ; ;
module A = struct type nil = Cstr end ; ;
type _ s = | Nil : nil s | Cons : ' t s -> ( ' h -> ' t ) s
type ( ' stack , ' typ ) var = | Head : ( ( ' typ -> _ ) s , ' typ ) var | Tail : ( ' tail s , ' typ ) var -> ( ( _ -> ' tail ) s , ' typ ) var
type _ lst = | CNil : nil lst | CCons : ' h * ( ' t lst ) -> ( ' h -> ' t ) lst ; ;
let rec get_var : type stk ret . ( stk s , ret ) var -> stk lst -> ret = fun n s -> match n , s with | Head , CCons ( h , _ ) -> h | Tail n ' , CCons ( _ , t ) -> get_var n ' t ; ;
type ' a t = [ < ` Foo | ` Bar ] as ' a ; ;
type ' a s = [ < ` Foo | ` Bar | ` Baz > ` Bar ] as ' a ; ;
type ' a first = First : ' a second -> ( ' b t as ' a ) first
type aux = Aux : ' a t second * ( ' a -> int ) -> aux ; ;
let it : ' a . [ < ` Bar | ` Foo > ` Bar ] as ' a = ` Bar ; ;
let g ( Aux ( Second , f ) ) = f it ; ;
type ( _ , _ ) eqp = Y : ( ' a , ' a ) eqp | N : string -> ( ' a , ' b ) eqp
let f : ( ' a list , ' a ) eqp -> unit = function N s -> print_string s ; ;
module rec A : sig type t = B . t list end = struct type t = B . t list end struct type t = A . t let eq = Y end ; ;
type ( _ , _ ) t = | Nil : ( ' tl , ' tl ) t | Cons : ' a * ( ' b , ' tl ) t -> ( ' a * ' b , ' tl ) t ; ;
let get1 ( Cons ( x , _ ) : ( _ * ' a , ' a ) t ) = x ; ;