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 ...
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 ' |...
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 || isUn...
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 ...
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 =...
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 | _ ->...
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 ( [ " abstra...
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 ) )...
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...
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 -> Sn...
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 -> ...
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...
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 t...
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_variable...
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 ...
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 ...
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_ite...
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 p...
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 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 | ...
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_dec...
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 <- Trepla...
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 ' = mapp...
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 le...
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_recor...
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_...
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 ! sam...
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 de...
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...
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 o...
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