text
stringlengths
0
601k
let newinst ( t : t ) : t = let rec copy table t = try List . find ( fun ( key , _ ) -> key == t ) table |> snd , table with | Not_found -> match ! t with | TUnbound ( s , level , loc ) -> let o = ref ( TUnbound ( s , level , loc ) ) in o , ( t , o ) :: table | TId ( id , loc ) -> let o = ref ( TId ( id , loc ) ) in o , ( t , o ) :: table | TInt ( n , loc ) -> let o = ref ( TInt ( n , loc ) ) in o , ( t , o ) :: table | TComposed ( id , elems , loc ) -> let elems ' , table ' = copyList table elems in let o = ref ( TComposed ( id , elems ' , loc ) ) in o , ( t , o ) :: table ' | TArrow ( t1 , t2 , loc ) -> let t1 ' , table ' = copy table t1 in let t2 ' , table ' = copy table ' t2 in let o = ref ( TArrow ( t1 ' , t2 ' , loc ) ) in o , ( t , o ) :: table ' | TLink link -> let link ' , table ' = copy table link in let o = ref ( TLink link ' ) in o , ( t , o ) :: table ' | TExpAlt elems -> let elems ' , table ' = copyList table elems in let o = ref ( TExpAlt elems ' ) in o , ( t , o ) :: table ' and copyList table l = let l ' , table ' = List . fold_left ( fun ( ol , table ) t -> let o , table ' = copy table t in o :: ol , table ' ) ( [ ] , table ) l in List . rev l ' , table ' in copy [ ] t |> fst
let rec fixType table t = try List . find ( fun key -> equal key t ) table , table with | _ -> match t with | { contents = TUnbound ( _ , _ , _ ) } -> t , t :: table | { contents = TComposed ( id , elems , loc ) } -> let elems ' , table ' = fixTypeList table elems in ref ( TComposed ( id , elems ' , loc ) ) , table ' | { contents = TArrow ( t1 , t2 , loc ) } -> let t1 ' , table ' = fixType table t1 in let t2 ' , table ' = fixType table ' t2 in ref ( TArrow ( t1 ' , t2 ' , loc ) ) , table ' | { contents = TLink link } -> let link ' , table ' = fixType table link in ref ( TLink link ' ) , table ' | { contents = TExpAlt elems } -> let elems ' , table ' = fixTypeList table elems in ref ( TExpAlt elems ' ) , table ' | _ -> t , table let tl ' , table ' = List . fold_left ( fun ( ol , table ) t -> let o , table ' = fixType table t in o :: ol , table ' ) ( [ ] , table ) tl in List . rev tl ' , table '
let fixOptType table ot = match ot with | None -> None , table | Some t -> let t ' , table ' = fixType table t in Some t ' , table '
let rec isUnbound ( t : t ) : bool = match t with | { contents = TUnbound ( _ , _ , _ ) } -> true | { contents = TId ( _ , _ ) } -> false | { contents = TInt ( _ , _ ) } -> false | { contents = TComposed ( _ , elems , _ ) } -> List . exists isUnbound elems | { contents = TArrow ( t1 , t2 , _ ) } -> isUnbound t1 || isUnbound t2 | { contents = TLink t } -> isUnbound t | { contents = TExpAlt _ } -> true
let rec location ( t : t ) : Loc . t = match t with | { contents = TUnbound ( _ , _ , Some loc ) } -> loc | { contents = TId ( _ , Some loc ) } -> loc | { contents = TComposed ( _ , elems , Some loc ) } -> List . fold_left ( fun s a -> Loc . merge s ( location a ) ) loc elems | { contents = TArrow ( t1 , t2 , Some loc ) } -> loc |> Loc . merge ( location t1 ) |> Loc . merge ( location t2 ) | { contents = TLink t } -> location t | { contents = TExpAlt elems } -> List . fold_left ( fun s a -> Loc . merge s ( location a ) ) Loc . default elems | { contents = TInt ( _ , Some loc ) } -> loc | { contents = TUnbound ( _ , _ , None ) } { | contents = TId ( _ , None ) } { | contents = TComposed ( _ , _ , None ) } { | contents = TArrow ( _ , _ , None ) } { | contents = TInt ( _ , None ) } -> Loc . default
let getLevel = function | None -> current_level ( ) | Some n -> n
let pickLoc ( loc1 : Loc . t option ) ( loc2 : Loc . t option ) : Loc . t option = match loc1 , loc2 with | None , _ -> loc2 | _ , None -> loc1 | Some l1 , _ when l1 = Loc . default -> loc2 | _ , Some l2 when l2 = Loc . default -> loc1 | _ -> loc1
let rec unify ( t1 : t ) ( t2 : t ) : bool = if t1 == t2 then true else match t1 , t2 with | { contents = TInt ( n1 , _ ) } , { contents = TInt ( n2 , _ ) } when n1 = n2 -> true | { contents = TUnbound ( n1 , level1 , loc1 ) } , { contents = TUnbound ( _ , level2 , loc2 ) } -> let loc = pickLoc loc1 loc2 in let level = min ( getLevel level1 ) ( getLevel level2 ) in let n = if n1 = " " then gensym ( ) else n1 in let t = TUnbound ( n , Some level , loc ) in t1 := t ; t2 := TLink t1 ; true | { contents = TLink tlink } , t | t , { contents = TLink tlink } -> unify t tlink | ( { contents = TUnbound _ } as tu ) , t | t , ( { contents = TUnbound _ } as tu ) -> tu := TLink t ; true | { contents = TComposed ( n1 , elems1 , _ ) } , { contents = TComposed ( n2 , elems2 , _ ) } when n1 = n2 && List . length elems1 = List . length elems2 -> List . for_all2 unify elems1 elems2 | { contents = TArrow ( a1 , a2 , _ ) } , { contents = TArrow ( b1 , b2 , _ ) } -> unify a1 b1 && unify a2 b2 | { contents = TId ( id1 , _ ) } , { contents = TId ( id2 , _ ) } when id1 = id2 -> true | { contents = TExpAlt _ as tp1 } , { contents = TExpAlt _ as tp2 } when equal_vtype tp1 tp2 -> t2 := TLink t1 ; true | ( { contents = TExpAlt alt } as tu ) , t | t , ( { contents = TExpAlt alt } as tu ) -> let rec loop alt = match alt with | [ ] -> false | first_alt :: alt_rest -> if unify t first_alt then ( tu := TLink first_alt ; true ) else loop alt_rest in loop alt | { contents = tp1 } , { contents = tp2 } when equal_vtype tp1 tp2 -> true | _ -> false
let rec join ( sep : string ) ( id : string list ) : string = match id with | [ ] -> " " | [ name ] -> name | h :: t -> h ^ sep ^ join sep t
let rec getTupleName ( typ : t ) : string = match ! typ with | TId ( id , _ ) -> join " _ " id | TComposed ( id , elems , _ ) -> ( join " _ " id :: " _ " :: List . map getTupleName elems ) @ [ " _ " ] |> join " _ " | TLink e -> getTupleName e | TArrow ( e1 , e2 , _ ) -> getTupleName e1 ^ " __ " ^ getTupleName e2 | _ -> failwith " There should be no other types here "
let getTupleName ( typ : t ) : string = " _ " ^ getTupleName typ
let arrayTypeAndSize typ = match ! typ with | TComposed ( [ " array " ] , [ t ; { contents = TInt ( n , _ ) } ] , _ ) -> t , n | _ -> failwith " arraySize : invalid input "
let getSubTypes ( typ : t ) : t list = match ( ! unlink typ ) with | TComposed ( _ , elems , _ ) -> elems | _ -> [ ]
let isArray ( typ : t ) : bool = match ( ! unlink typ ) with | TComposed ( [ " array " ] , _ , _ ) -> true | _ -> false
let isTuple typ = match ! typ with | TComposed ( [ " tuple " ] , _ , _ ) -> true | _ -> false
let isSimpleType ( typ : t ) : bool = match ! typ with | TId ( [ " real " ] , _ ) -> true | TId ( [ " fix16 " ] , _ ) -> true | TId ( [ " int " ] , _ ) -> true | TId ( [ " bool " ] , _ ) -> true | TId ( [ " unit " ] , _ ) -> true | TId ( [ " void " ] , _ ) -> true | TId ( [ " string " ] , _ ) -> true | TId ( [ " abstract " ] , _ ) -> true | _ -> false
let isRealType ( typ : t ) : bool = match ! typ with | TId ( [ " real " ] , _ ) -> true | TId ( [ " fix16 " ] , _ ) -> true | _ -> false
let isSimpleOpType ( typ : t option ) : bool = match typ with | Some t -> isSimpleType t | _ -> true
let isTupleOpType ( typ : t option ) : bool = match typ with | Some t -> isTuple t | _ -> true
let first ( t : t list ) : t = match t with | h :: _ -> h | _ -> failwith " Typ . first : invalid type "
let makeListOpt t = match t with | Some t -> Some [ t ] | None -> None
module Const = struct let ( |-> ) a b = ref ( TArrow ( a , b , None ) ) let empty = ref ( TId ( [ " " ] , None ) ) let type_type = ref ( TId ( [ " type " ] , None ) ) let unit_type = ref ( TId ( [ " unit " ] , None ) ) let bool_type = ref ( TId ( [ " bool " ] , None ) ) let int_type = ref ( TId ( [ " int " ] , None ) ) let real_type = ref ( TId ( [ " real " ] , None ) ) let string_type = ref ( TId ( [ " string " ] , None ) ) let fix16_type = ref ( TId ( [ " fix16 " ] , None ) ) let num_type ( ) = ref ( TExpAlt [ real_type ; int_type ; fix16_type ] ) let freal_type ( ) = ref ( TExpAlt [ real_type ; fix16_type ] ) let freal_freal ( ) = let t = freal_type ( ) in t |-> t let freal_freal_freal ( ) = let t = freal_type ( ) in t |-> ( t |-> t ) let real_real ( ) = real_type |-> real_type let real_real_real ( ) = real_type |-> ( real_type |-> real_type ) let a_a ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in a |-> a let a_a_a ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in a |-> ( a |-> a ) let num_num ( ) = let num = num_type ( ) in num |-> num let num_num_num ( ) = let num = num_type ( ) in num |-> ( num |-> num ) let num_num_bool ( ) = let num = num_type ( ) in num |-> ( num |-> bool_type ) let a_a_bool ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in a |-> ( a |-> bool_type ) let int_int_int ( ) = int_type |-> ( int_type |-> int_type ) let num_num_num_num ( ) = let num = num_type ( ) in num |-> ( num |-> ( num |-> num ) ) let a_a_a_a ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in a |-> ( a |-> ( a |-> a ) ) let bool_bool ( ) = bool_type |-> bool_type let bool_bool_bool ( ) = bool_type |-> ( bool_type |-> bool_type ) let num_int ( ) = num_type ( ) |-> int_type let num_real ( ) = num_type ( ) |-> real_type let num_fix16 ( ) = num_type ( ) |-> fix16_type let num_string ( ) = num_type ( ) |-> string_type let array_size ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in let array_type = ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) in array_type |-> int_type let array_get ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in let array_type = ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) in array_type |-> ( int_type |-> a ) let array_set ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in let array_type = ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) in array_type |-> ( int_type |-> ( a |-> unit_type ) ) let array_make ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in let array_type = ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) in int_type |-> ( a |-> array_type ) let unit_real ( ) = unit_type |-> real_type let unit_real ( ) = unit_type |-> int_type let int_unit ( ) = int_type |-> unit_type let wrap_array ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in let array_type = ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) in array_type |-> array_type let tuple_real_real ( ) = ref ( TComposed ( [ " tuple " ] , [ real_type ; real_type ] , None ) ) let real_tuple_real_real ( ) = real_type |-> tuple_real_real ( ) let some_array ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) end
module Impl = Snarky . Snark . Make ( Snarky . Backends . Mnt4 . Default )
module Alias_alias = struct include struct type nonrec ( ' a , ' b ) u_var = ' a -> ' a and ( ' a , ' b ) u = ' a -> ' a let u_typ x___2 x___1 = Typ . fn x___2 x___2 end include struct type nonrec ( ' a , ' b ) v_var = ( ' a , ' a ) u_var and ( ' a , ' b ) v = ( ' a , ' a ) u let v_typ x___4 x___3 x___5 = u_typ x___4 x___5 end let f ( x : ( int , bool ) u_var ) : ( int , int ) u_var = x let g ( x : ( int , int ) v_var ) : ( int , bool ) v_var = x let h ( x : ( int , bool ) v_var ) : ( int , int ) u_var = x let i ( x : ( bool , bool ) u_var ) : ( bool , unit ) v_var = x end
module Alias_opaque = struct type nonrec ( ' a , ' b ) u type nonrec ( ' a , ' b ) v = ( ' a , ' a ) u let f ( x : ( int , int ) v ) : ( int , bool ) v = x let g ( x : ( int , bool ) v ) : ( int , int ) u = x let h ( x : ( bool , bool ) u ) : ( bool , unit ) v = x end
module Alias_record = struct include struct type nonrec ( ' a , ' b ) u_var = { a : ' a ; b : ' b } and ( ' a , ' b ) u = { a : ' a ; b : ' b } let u_typ x___11 x___10 = { Snarky . Types . Typ . store = ( fun { a ; b } -> Snarky . Typ_monads . Store . bind ( x___11 . Snarky . Types . Typ . store a ) ~ f ( : fun a -> Snarky . Typ_monads . Store . bind ( x___10 . Snarky . Types . Typ . store b ) ~ f ( : fun b -> Snarky . Typ_monads . Store . return { a ; b } ) ) ) ; Snarky . Types . Typ . read = ( fun { a ; b } -> Snarky . Typ_monads . Read . bind ( x___11 . Snarky . Types . Typ . read a ) ~ f ( : fun a -> Snarky . Typ_monads . Read . bind ( x___10 . Snarky . Types . Typ . read b ) ~ f ( : fun b -> Snarky . Typ_monads . Read . return { a ; b } ) ) ) ; Snarky . Types . Typ . alloc = Snarky . Typ_monads . Alloc . bind x___11 . Snarky . Types . Typ . alloc ~ f ( : fun a -> Snarky . Typ_monads . Alloc . bind x___10 . Snarky . Types . Typ . alloc ~ f ( : fun b -> Snarky . Typ_monads . Alloc . return { a ; b } ) ) ; Snarky . Types . Typ . check = ( fun { a ; b } -> Snarky . Checked . bind ( x___11 . Snarky . Types . Typ . check a ) ~ f ( : fun ( ) -> Snarky . Checked . bind ( x___10 . Snarky . Types . Typ . check b ) ~ f ( : fun ( ) -> Snarky . Checked . return ( ) ) ) ) } end include struct type nonrec ( ' a , ' b ) v_var = ( ' a , ' a ) u_var and ( ' a , ' b ) v = ( ' a , ' a ) u let v_typ x___13 x___12 = u_typ x___13 x___13 end let f ( x : ( int , int ) v_var ) : ( int , bool ) v_var = x let g ( x : ( int , bool ) v_var ) : ( int , int ) u_var = x let h ( x : ( bool , bool ) u_var ) : ( bool , unit ) v_var = x end
module Alias_variant = struct type nonrec ( ' a , ' b ) u = A | B | C of ' a | D of ' b type nonrec ( ' a , ' b ) v = ( ' a , ' a ) u let f ( x : ( int , int ) v ) : ( int , bool ) v = x let g ( x : ( int , bool ) v ) : ( int , int ) u = x let h ( x : ( bool , bool ) u ) : ( bool , unit ) v = x end
module Kind = struct type t = | Var | Constr | Arrow | Tuple | Other let to_int = function | Var -> 0 | Constr -> 1 | Arrow -> 2 | Tuple -> 3 | Other -> 4 let of_int = function | 0 -> Var | 1 -> Constr | 2 -> Arrow | 3 -> Tuple | 4 -> Other | _ -> assert false let to_string = function | Var -> " variable " | Constr -> " constructor " | Arrow -> " arrow " | Tuple -> " tuple " | Other -> " other " let compare t1 t2 = CCInt . compare ( to_int t1 ) ( to_int t2 ) let equal t1 t2 = compare t1 t2 = 0 let hash = CCHash . poly module Map = CCMap . Make ( struct type nonrec t = t let compare = compare end ) module HMap = CCHashtbl . Make ( struct type nonrec t = t let equal = equal let hash = hash end ) module MSet = CCMultiSet . Make ( struct type nonrec t = t let compare = compare end ) let pp = Fmt . of_to_string to_string end
module Kind ' = struct type t = | Var | Constr of LongIdent . t | Arrow | Tuple | Other let to_int = function | Var -> 0 | Constr _ -> 1 | Arrow -> 2 | Tuple -> 3 | Other -> 4 let compare t1 t2 = match t1 , t2 with | Var , Var | Arrow , Arrow | Tuple , Tuple | Other , Other -> 0 | Constr lid1 , Constr lid2 -> LongIdent . compare lid1 lid2 | _ -> CCInt . compare ( to_int t1 ) ( to_int t2 ) let equal t1 t2 = compare t1 t2 = 0 let hash = CCHash . poly module Map = CCMap . Make ( struct type nonrec t = t let compare = compare end ) module HMap = CCHashtbl . Make ( struct type nonrec t = t let equal = equal let hash = hash end ) module MSet = CCMultiSet . Make ( struct type nonrec t = t let compare = compare end ) end
module rec Base : sig type t = | Var of Variable . t | FrozenVar of Variable . t | Constr of LongIdent . t * t Array . t | Arrow of NSet . t * t | Tuple of NSet . t | Other of Int . t val kind : t -> Kind . t val kind ' : t -> Kind ' . t val compare : t CCOrd . t val equal : t -> t -> Bool . t val hash : t CCHash . t type t = | Var of Variable . t | FrozenVar of Variable . t | Constr of LongIdent . t * t Array . t | Arrow of NSet . t * t | Tuple of NSet . t | Other of Int . t let kind : t -> Kind . t = function | Var _ | FrozenVar _ -> Var | Constr _ -> Constr | Arrow _ -> Arrow | Tuple _ -> Tuple | Other _ -> Other let kind ' : t -> Kind ' . t = function | Var _ | FrozenVar _ -> Var | Constr ( lid , _ ) -> Constr lid | Arrow _ -> Arrow | Tuple _ -> Tuple | Other _ -> Other let to_int t = Kind . to_int @@ kind t let rec compare t1 t2 = if t1 == t2 then 0 else let open CCOrd . Infix in match t1 , t2 with | Var var1 , Var var2 -> Variable . compare var1 var2 | Constr ( lid1 , params1 ) , Constr ( lid2 , params2 ) -> LongIdent . compare lid1 lid2 <?> ( CCArray . compare compare , params1 , params2 ) | Arrow ( param1 , ret1 ) , Arrow ( param2 , ret2 ) -> let cmp = compare in cmp ret1 ret2 <?> ( NSet . compare , param1 , param2 ) | Tuple elts1 , Tuple elts2 -> NSet . compare elts1 elts2 | Other i1 , Other i2 -> CCInt . compare i1 i2 | _ -> CCInt . compare ( to_int t1 ) ( to_int t2 ) let equal t1 t2 = compare t1 t2 = 0 let hash = Hashtbl . hash end type elt = Base . t type t val compare : t CCOrd . t val of_list : elt List . t -> t val of_iter : elt Iter . t -> t val to_iter : t -> elt Iter . t val as_array : t -> elt Array . t val empty : t val is_empty : t -> Bool . t val length : t -> Int . t val singleton : elt -> t val is_singleton : t -> elt Option . t val union : t -> t -> t val add : elt -> t -> t val fold : ( elt -> ' a -> ' a ) -> t -> ' a -> ' a val map : ( elt -> elt ) -> t -> t val pp : elt Fmt . t -> t Fmt . t type elt = Base . t type t = elt Array . t let compare = CCArray . compare Base . compare let sort = CCArray . sort Base . compare let of_list lst = let t = CCArray . of_list lst in sort t ; t let of_iter it = let t = Iter . to_array it in sort t ; t let to_iter = Iter . of_array let as_array = CCFun . id let empty = [ ] || let is_empty t = t = [ ] || let length = CCArray . length let singleton elt = [ | elt ] | let is_singleton = function | [ | elt ] | -> Some elt | _ -> None let union t1 t2 = let t = CCArray . append t1 t2 in sort t ; t let add elt t = union ( singleton elt ) t let fold fn t acc = CCArray . fold_left ( CCFun . flip fn ) acc t let map fn t = of_iter @@ Iter . map fn @@ to_iter t let pp pp_ty ppf = function | [ ] || -> Fmt . string ppf " ( ) " | [ | elt ] | -> pp_ty ppf elt | t -> Fmt . pf ppf " [ @< 2 >% a ] " @ Fmt . ( array ~ sep ( : any " *@ " ) pp_ty ) t end
module HMap = CCHashtbl . Make ( Base )
module Map = CCMap . Make ( Base )
module Set = CCSet . Make ( Base )
module MSet = CCMultiSet . Make ( Base )
module Hashcons = struct type elt = t type t = elt HMap . t let make ( ) = HMap . create 17 let hashcons t ty = match HMap . find_opt t ty with | Some ty -> ty | None -> HMap . add t ty ty ; ty end
module Env = struct type t = { var_gen : Variable . Gen . t ; hcons : Hashcons . t ; } let make ( ? hcons = Hashcons . make ( ) ) namespace = { var_gen = Variable . Gen . make namespace ; hcons ; } end
let hashcons env ty = Hashcons . hashcons env . Env . hcons ty
let var env v = hashcons env @@ Var v
let frozen_var env v = hashcons env @@ FrozenVar v
let constr env lid params = hashcons env @@ match params with | [ ] || when lid = LongIdent . unit -> Tuple NSet . empty | params -> Constr ( lid , params )
let arrows env params ret = hashcons env @@ match ret with | Arrow ( params ' , ret ) -> Arrow ( NSet . union params params ' , ret ) | _ -> Arrow ( params , ret )
let arrow env param ret = hashcons env @@ match param , ret with | Tuple elts , Arrow ( params , ret ) -> Arrow ( NSet . union elts params , ret ) | _ , Arrow ( params , ret ) -> Arrow ( NSet . add param params , ret ) | Tuple elts , _ -> Arrow ( elts , ret ) | _ , _ -> Arrow ( NSet . singleton param , ret )
let tuple env elts = let aux = function | Tuple elts -> elts | elt -> NSet . singleton elt in let elts = NSet . fold ( fun elt -> NSet . union @@ aux elt ) elts NSet . empty in hashcons env @@ match NSet . is_singleton elts with | Some elt -> elt | None -> Tuple elts
let other env x = hashcons env @@ Other ( CCHash . poly x )
let rec freeze_variables env ( t : t ) = match t with | Var v -> FrozenVar v | Constr ( lid , t ) -> constr env lid ( Array . map ( freeze_variables env ) t ) | Arrow ( args , t ) -> arrows env ( NSet . map ( freeze_variables env ) args ) ( freeze_variables env t ) | Tuple ts -> tuple env ( NSet . map ( freeze_variables env ) ts ) | Other _ | FrozenVar _ -> hashcons env t
let of_outcometree of_outcometree env var ( out_ty : Outcometree . out_type ) = match out_ty with | Otyp_var ( _ , name ) -> var @@ Some name | Otyp_constr ( id , params ) -> params |> Iter . of_list |> Iter . map of_outcometree |> Iter . to_array |> constr env @@ LongIdent . of_outcometree id | Otyp_arrow ( _ , param , ret ) -> arrow env ( of_outcometree param ) ( of_outcometree ret ) | Otyp_tuple elts -> elts |> Iter . of_list |> Iter . map of_outcometree |> NSet . of_iter |> tuple env | Otyp_alias ( out_ty , _ ) -> of_outcometree out_ty | Otyp_object _ | Otyp_class _ | Otyp_variant _ | Otyp_module _ | Otyp_attribute _ | Otyp_stuff _ | Otyp_poly _ | Otyp_abstract | Otyp_open | Otyp_manifest _ | Otyp_record _ | Otyp_sum _ -> other env out_ty
let of_parsetree of_parsetree env var ( parse_ty : Parsetree . core_type ) = match parse_ty . ptyp_desc with | Ptyp_any -> var None | Ptyp_var name -> var @@ Some name | Ptyp_constr ( id , params ) -> params |> Iter . of_list |> Iter . map of_parsetree |> Iter . to_array |> constr env id . txt | Ptyp_arrow ( _ , param , ret ) -> arrow env ( of_parsetree param ) ( of_parsetree ret ) | Ptyp_tuple elts -> elts |> Iter . of_list |> Iter . map of_parsetree |> NSet . of_iter |> tuple env | Ptyp_alias ( parse_ty , _ ) | Ptyp_poly ( _ , parse_ty ) -> of_parsetree parse_ty | Ptyp_object _ | Ptyp_class _ | Ptyp_variant _ | Ptyp_package _ | Ptyp_extension _ -> other env parse_ty
let var ' bdgs ( env : Env . t ) = function | None -> var env @@ Variable . Gen . gen env . var_gen | Some name -> match String . HMap . get bdgs name with | Some v -> var env v | None -> let v = Variable . Gen . gen env . var_gen in String . HMap . add bdgs name v ; var env v
let of_outcometree ' , of_parsetree ' = let wrap fn ( env : Env . t ) x = let bdgs = String . HMap . create 17 in let var = var ' bdgs env in let rec fn ' x = fn fn ' env var x in bdgs , fn ' x in wrap of_outcometree , wrap of_parsetree
let of_outcometree , of_parsetree = let wrap fn env x = snd @@ fn env x in wrap of_outcometree ' , wrap of_parsetree '
let of_lexing , of_lexing ' = let wrap of_parsetree env lexbuf = let parse_ty = try Parse . core_type lexbuf with Syntaxerr . Error _ -> invalid_arg " Type . of_lexing " in of_parsetree env parse_ty in wrap of_parsetree , wrap of_parsetree '
let of_string , of_string ' = let wrap of_lexing env str = str |> Lexing . from_string |> of_lexing env in wrap of_lexing , wrap of_lexing '
let head = function | Arrow ( _ , ret ) -> ret | t -> t
let tail = function | Arrow ( params , _ ) -> params | _ -> NSet . empty
let iter = let iter_subs = function | Constr ( _ , params ) -> Iter . of_array params | Arrow ( params , ret ) -> Iter . snoc ( NSet . to_iter params ) ret | Tuple elts -> NSet . to_iter elts | _ -> Iter . empty in fun t k -> let rec aux t = k t ; iter_subs t aux in aux t
let rec iter_vars t k = match t with | Other _ | FrozenVar _ -> ( ) | Var var -> k var | Constr ( _ , params ) -> CCArray . iter ( fun t -> iter_vars t k ) params | Arrow ( params , ret ) -> Iter . flat_map iter_vars ( NSet . to_iter params ) k ; iter_vars ret k | Tuple elts -> Iter . flat_map iter_vars ( NSet . to_iter elts ) k
let rec pp ppf = function | Var var -> Fmt . pf ppf " ' % a " Variable . pp var | FrozenVar var -> Fmt . pf ppf " ^% a " Variable . pp var | Constr ( lid , [ ] ) || -> LongIdent . pp ppf lid | Constr ( lid , params ) -> Fmt . pf ppf " % a @ % a " pp_array params LongIdent . pp lid | Arrow ( params , ret ) -> Fmt . pf ppf " [ @< 2 >% a @ ->@ % a ] " @ ( NSet . pp pp_parens ) params pp_parens ret | Tuple elts -> Fmt . pf ppf " [ @< 2 >% a ] " @ ( NSet . pp pp_parens ) elts | Other i -> Fmt . pf ppf " other % i " i match ty with | Var _ | FrozenVar _ | Other _ | Constr _ -> pp ppf ty | Arrow _ -> Fmt . parens pp ppf ty | Tuple elts -> if NSet . length elts <= 1 then pp ppf ty else Fmt . parens pp ppf ty | [ ] || -> Fmt . string ppf " ( ) " | [ | elt ] | -> pp ppf elt | arr -> Fmt . pf ppf " [ @< 2 ( >% a ) ] " @ Fmt . ( array ~ sep ( : any " , " ) pp ) arr
type row_presence = { mutable rp_desc : row_presence_desc ; rp_id : int } | RpPresent | RpMaybe | RpAbsent | RpSubtract of row_presence | RpAny | RpRef of row_presence | RpReplace of row_presence
type type_expr = { mutable type_desc : type_desc ; type_id : int ; mutable type_depth : int ; type_mode : mode ; mutable type_alternate : type_expr } | Tvar of string option | Ttuple of type_expr list | Tarrow of type_expr * type_expr * explicitness * Ast_types . arg_label | Tctor of variant | Tpoly of type_expr list * type_expr | Tref of type_expr | Tconv of type_expr | Topaque of type_expr | Tother_mode of type_expr | Treplace of type_expr | Trow of row { row_tags : ( Path . t * row_presence * type_expr list ) Ident . Map . t ; row_closed : closed_flag ; row_rest : type_expr ; row_presence_proxy : row_presence }
type field_decl = { fld_ident : Ident . t ; fld_type : type_expr }
type ctor_args = Ctor_tuple of type_expr list | Ctor_record of type_decl { ctor_ident : Ident . t ; ctor_args : ctor_args ; ctor_ret : type_expr option } { tdec_params : type_expr list ; tdec_desc : type_decl_desc ; tdec_id : int ; tdec_ret : type_expr } | TAbstract | TAlias of type_expr | TRecord of field_decl list | TVariant of ctor_decl list | TOpen | TExtend of Path . t * ctor_decl list
type mapper = { type_expr : mapper -> type_expr -> type_expr ; type_desc : mapper -> type_desc -> type_desc ; variant : mapper -> variant -> variant ; row : mapper -> row -> row ; field_decl : mapper -> field_decl -> field_decl ; ctor_args : mapper -> ctor_args -> ctor_args ; ctor_decl : mapper -> ctor_decl -> ctor_decl ; type_decl : mapper -> type_decl -> type_decl ; type_decl_desc : mapper -> type_decl_desc -> type_decl_desc ; ident : mapper -> Ident . t -> Ident . t ; path : mapper -> Path . t -> Path . t }
let map_list ~ same ~ f = List . map ~ f ( : fun x -> let y = f x in if not ( phys_equal x y ) then same := false ; y )
let type_expr mapper typ = match typ . Type0 . type_desc with | Treplace typ -> typ | desc -> let alt_desc = typ . type_alternate . type_desc in let alt_alt_desc = typ . type_alternate . type_alternate . type_desc in let typ ' = Type1 . mkvar ~ mode : typ . type_mode typ . type_depth None in typ ' . type_desc <- Treplace typ ' ; typ ' . type_alternate . type_desc <- Treplace typ ' . type_alternate ; let is_stitched = phys_equal typ typ . type_alternate . type_alternate in if is_stitched then typ ' . type_alternate . type_alternate <- typ ' else typ ' . type_alternate . type_alternate . type_desc <- Treplace typ ' . type_alternate . type_alternate ; Type1 . set_replacement typ typ ' ; let type_desc = mapper . type_desc mapper desc in let alt_type_desc = mapper . type_desc mapper alt_desc in let alt_alt_type_desc = if is_stitched then type_desc else mapper . type_desc mapper alt_alt_desc in if phys_equal type_desc desc && phys_equal alt_type_desc alt_desc && phys_equal alt_alt_type_desc alt_alt_desc then ( typ ' . type_desc <- Tref typ ; typ ' . type_alternate . type_desc <- Tref typ . type_alternate ; typ ' . type_alternate . type_alternate . type_desc <- Tref typ . type_alternate . type_alternate ; typ ) else ( typ ' . type_desc <- type_desc ; typ ' . type_alternate . type_desc <- alt_type_desc ; typ ' . type_alternate . type_alternate . type_desc <- alt_alt_type_desc ; typ ' )
let type_desc mapper desc = match desc with | Tvar _ -> desc | Ttuple typs -> let same = ref true in let typs = map_list typs ~ same ~ f ( : mapper . type_expr mapper ) in if ! same then desc else Ttuple typs | Tarrow ( typ1 , typ2 , explicit , label ) -> let typ1 ' = mapper . type_expr mapper typ1 in let typ2 ' = mapper . type_expr mapper typ2 in if phys_equal typ1 ' typ1 && phys_equal typ2 ' typ2 then desc else Tarrow ( typ1 ' , typ2 ' , explicit , label ) | Tctor variant -> let variant ' = mapper . variant mapper variant in if phys_equal variant ' variant then desc else Tctor variant ' | Tpoly ( vars , typ ) -> let same = ref true in let vars = map_list vars ~ same ~ f ( : mapper . type_expr mapper ) in let typ ' = mapper . type_expr mapper typ in if ! same && phys_equal typ ' typ then desc else Tpoly ( vars , typ ' ) | Tref typ -> let typ ' = mapper . type_expr mapper typ in if phys_equal typ ' typ then desc else Tref typ ' | Tconv typ -> let typ ' = mapper . type_expr mapper typ in if phys_equal typ ' typ then desc else Tconv typ ' | Topaque typ -> let typ ' = mapper . type_expr mapper typ in if phys_equal typ ' typ then desc else Topaque typ ' | Tother_mode typ -> let typ ' = mapper . type_expr mapper typ in if phys_equal typ ' typ then desc else Tother_mode typ ' | Treplace typ -> typ . type_desc | Trow row -> let row ' = mapper . row mapper row in if phys_equal row ' row then desc else Trow row '
let variant mapper ( { var_ident = ident ; var_params } as variant ) = let var_ident = mapper . path mapper ident in let same = ref true in let var_params = map_list var_params ~ same ~ f ( : mapper . type_expr mapper ) in if ! same && phys_equal var_ident ident then variant else { var_ident ; var_params }
let row mapper ( { row_tags ; row_closed ; row_rest ; row_presence_proxy } as row ) = let same = ref true in let row_tags = Map . fold row_tags ~ init : Ident . Map . empty ~ f ( : fun ~ key ~ data ( : path , pres , args ) row_tags -> let key ' = mapper . ident mapper key in let path ' = mapper . path mapper path in let pres ' = match pres . rp_desc with | RpReplace pres -> pres | _ -> pres in let args = map_list ~ f ( : mapper . type_expr mapper ) ~ same args in if not ( phys_equal key ' key && phys_equal pres ' pres && phys_equal path ' path ) then same := false ; Map . set row_tags ~ key : key ' ~ data ( : path ' , pres ' , args ) ) in let row_rest ' = mapper . type_expr mapper row_rest in if ! same && phys_equal row_rest ' row_rest then row else { row_tags ; row_closed ; row_rest ; row_presence_proxy }
let field_decl mapper ( { fld_ident = ident ; fld_type = typ } as fld ) = let fld_type = mapper . type_expr mapper typ in let fld_ident = mapper . ident mapper ident in if phys_equal fld_type typ && phys_equal fld_ident ident then fld else { fld_ident ; fld_type }
let ctor_args mapper args = match args with | Ctor_tuple typs -> let same = ref true in let typs = map_list typs ~ same ~ f ( : mapper . type_expr mapper ) in if ! same then args else Ctor_tuple typs | Ctor_record decl -> let decl ' = mapper . type_decl mapper decl in if phys_equal decl ' decl then args else Ctor_record decl
let ctor_decl mapper ( { ctor_ident ; ctor_args = args ; ctor_ret } as decl ) = let ctor_ident = mapper . ident mapper ctor_ident in let ctor_args = mapper . ctor_args mapper args in match ctor_ret with | None -> if phys_equal ctor_args args && phys_equal ctor_ident decl . ctor_ident then decl else { ctor_ident ; ctor_args ; ctor_ret } | Some ret -> let ctor_ret = mapper . type_expr mapper ret in if phys_equal ctor_args args && phys_equal ctor_ret ret && phys_equal ctor_ident decl . ctor_ident then decl else { ctor_ident ; ctor_args ; ctor_ret = Some ctor_ret }
let type_decl mapper ( { tdec_params = params ; tdec_desc = desc ; tdec_id ; tdec_ret = ret } as decl ) = let same = ref true in let tdec_params = map_list params ~ same ~ f ( : mapper . type_expr mapper ) in let tdec_desc = mapper . type_decl_desc mapper desc in let tdec_ret = mapper . type_expr mapper ret in if ! same && phys_equal tdec_desc desc && phys_equal tdec_ret ret then decl else { tdec_params ; tdec_desc ; tdec_id ; tdec_ret }
let type_decl_desc mapper desc = match desc with | TAbstract | TOpen -> desc | TAlias typ -> let typ ' = mapper . type_expr mapper typ in if phys_equal typ ' typ then desc else TAlias typ ' | TRecord flds -> let same = ref true in let flds = map_list flds ~ same ~ f ( : mapper . field_decl mapper ) in if ! same then desc else TRecord flds | TVariant ctors -> let same = ref true in let ctors = map_list ctors ~ same ~ f ( : mapper . ctor_decl mapper ) in if ! same then desc else TVariant ctors | TExtend ( path , ctors ) -> let path ' = mapper . path mapper path in let same = ref true in let ctors = map_list ctors ~ same ~ f ( : mapper . ctor_decl mapper ) in if ! same && phys_equal path ' path then desc else TExtend ( path ' , ctors )
let path mapper path = match path with | Path . Pident ident -> let ident ' = mapper . ident mapper ident in if phys_equal ident ' ident then path else Path . Pident ident ' | Path . Pdot ( path1 , mode , str ) -> let path1 ' = mapper . path mapper path1 in if phys_equal path1 ' path1 then path else Path . Pdot ( path1 ' , mode , str ) | Path . Pocamldot ( path1 , mode , str , ocaml_name ) -> let path1 ' = mapper . path mapper path1 in if phys_equal path1 ' path1 then path else Path . Pocamldot ( path1 ' , mode , str , ocaml_name ) | Path . Papply ( path1 , path2 ) -> let path1 ' = mapper . path mapper path1 in let path2 ' = mapper . path mapper path2 in if phys_equal path1 ' path1 && phys_equal path2 ' path2 then path else Path . Papply ( path1 ' , path2 ' )
let ident ( _mapper : mapper ) ( ident : Ident . t ) = ident
let default_mapper = { type_expr ; type_desc ; variant ; row ; field_decl ; ctor_args ; ctor_decl ; type_decl ; type_decl_desc ; ident ; path }
type error = | Mk_wrong_mode of string * type_expr list * mode * type_expr | Mk_invalid of string * type_expr list * type_expr
let check_mode ~ pos ~ error_info mode typ = if not ( equal_mode mode typ . type_mode ) then let kind , typs = error_info ( ) in raise ( Error ( Ast_build . Loc . of_prim pos , Mk_wrong_mode ( kind , typs , mode , typ ) ) )
let get_mode mode typ = if equal_mode typ . type_mode mode then typ else let typ = typ . type_alternate in assert ( equal_mode typ . type_mode mode ) ; typ
let ( ) = Debug_print . get_mode := get_mode
let type_id = ref 0
let row_id = ref 0
let mk_rp rp_desc = incr row_id ; { rp_desc ; rp_id = ! row_id }
let rec repr typ = match typ . type_desc with Tref typ -> repr typ | _ -> typ
let rec rp_repr pres = match pres . rp_desc with RpRef pres -> rp_repr pres | _ -> pres
let rec rp_strip_subtract pres = match rp_repr pres with | { rp_desc = RpSubtract pres ; _ } -> rp_strip_subtract pres | pres -> pres
let set_rp_desc_fwd = ref ( fun _ _ -> assert false )
let rec row_repr_aux tags { row_tags ; row_rest ; row_closed ; row_presence_proxy = _ } = let tags = Map . merge_skewed tags row_tags ~ combine ( : fun ~ key : _ ( old_path , old_pres , old_args ) ( new_path , new_pres , new_args ) -> let old_pres = rp_repr old_pres in let new_pres = rp_repr new_pres in let subst_any old_pres ' = ! set_rp_desc_fwd old_pres ' ( RpRef new_pres ) ; ( new_path , old_pres , new_args ) in match old_pres . rp_desc with | RpSubtract old -> ( let old ' = rp_strip_subtract old in if not ( phys_equal old old ' ) then ! set_rp_desc_fwd old ( RpRef old ' ) ; match old ' . rp_desc with | RpAny -> subst_any old ' | _ -> ( old_path , old_pres , old_args ) ) | RpAny -> subst_any old_pres | _ -> ( old_path , old_pres , old_args ) ) in row_repr_typ_aux ( tags , row_closed ) row_rest let typ = repr typ in match typ . type_desc with | Topaque typ -> row_repr_typ_aux ( tags , row_closed ) typ | Trow row -> row_repr_aux tags row | _ -> ( tags , typ , row_closed )
let row_repr row = let tags , row_rest , row_closed = row_repr_aux Ident . Map . empty row in ( tags , row_rest , row_closed )
let row_repr_typ typ = let tags , row_rest , row_closed = row_repr_typ_aux ( Ident . Map . empty , Open ) typ in ( tags , row_rest , row_closed )
let rec checked_none = { type_desc = Tvar None ; type_id = - 1 ; type_depth = 0 ; type_mode = Checked ; type_alternate = prover_none } { type_desc = Tvar None ; type_id = - 2 ; type_depth = 0 ; type_mode = Prover ; type_alternate = checked_none }
let none = function Checked -> checked_none | Prover -> prover_none
let other_none = function Checked -> prover_none | Prover -> checked_none
let type_alternate { type_alternate = typ ; _ } = typ
let row_alternate { row_tags ; row_closed ; row_rest ; row_presence_proxy } = let row_tags = Map . map row_tags ~ f ( : fun ( path , pres , args ) -> ( path , pres , List . map ~ f : type_alternate args ) ) in { row_tags ; row_closed ; row_rest = row_rest . type_alternate ; row_presence_proxy }
let is_poly = function { type_desc = Tpoly _ ; _ } -> true | _ -> false
let is_valid { type_id ; _ } = type_id > 0
let is_invalid { type_id ; _ } = type_id <= 0
let equal { type_id = id1 ; _ } { type_id = id2 ; _ } = Int . equal id1 id2
let check_valid ~ pos ~ error_info typ = if is_invalid typ then let kind , typs = error_info ( ) in raise ( Error ( Ast_build . Loc . of_prim pos , Mk_invalid ( kind , typs , typ ) ) )
let failure_type_id = try Some ( Int . of_string ( Sys . getenv " MEJA_BREAK_TYPE_ID " ) ) with _ -> None