text
stringlengths
12
786k
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
let mk ' ~ mode depth type_desc = incr type_id ; Option . iter failure_type_id ~ f ( : fun id -> assert ( ! type_id <> id ) ) ; { type_desc ; type_id = ! type_id ; type_depth = depth ; type_mode = mode ; type_alternate = other_none mode }
let stitch typ typ ' = assert ( equal_mode typ . type_mode ( other_mode typ ' . type_mode ) ) ; assert ( is_invalid typ . type_alternate ) ; assert ( is_invalid typ ' . type_alternate ) ; typ . type_alternate <- typ ' ; typ ' . type_alternate <- typ ; typ
let tri_stitch in_typ ptyp ctyp = assert ( equal_mode in_typ . type_mode Checked ) ; assert ( equal_mode ptyp . type_mode Prover ) ; assert ( equal_mode ctyp . type_mode Checked ) ; assert ( is_invalid in_typ . type_alternate ) ; assert ( is_invalid ptyp . type_alternate ) ; assert ( is_invalid ctyp . type_alternate ) ; in_typ . type_alternate <- ptyp ; ptyp . type_alternate <- ctyp ; ctyp . type_alternate <- ptyp ; in_typ
let are_stitched typ typ ' = ( phys_equal typ typ ' . type_alternate && phys_equal typ ' typ . type_alternate ) || let ctyp , ptyp , modes_match = match ( typ . type_mode , typ ' . type_mode ) with | Checked , Prover -> ( typ , typ ' , true ) | Prover , Checked -> ( typ ' , typ , true ) | _ -> ( typ , typ ' , false ) in modes_match && phys_equal ctyp . type_alternate ptyp && phys_equal ptyp ptyp . type_alternate . type_alternate
let mkvar ~ mode depth name = let typ = mk ' ~ mode depth ( Tvar name ) in let other_typ = mk ' ~ mode ( : other_mode mode ) depth ( Tvar name ) in if equal_mode mode Checked then let tri_typ = mk ' ~ mode depth ( Tvar name ) in tri_stitch typ other_typ tri_typ else stitch typ other_typ
module Mk = struct let var = mkvar let stitch ~ mode depth desc1 desc2 = stitch ( mk ' ~ mode depth desc1 ) ( mk ' ~ mode ( : other_mode mode ) depth desc2 ) let tri_stitch ~ mode depth desc1 desc2 desc3 = assert ( equal_mode mode Checked ) ; tri_stitch ( mk ' ~ mode : Checked depth desc1 ) ( mk ' ~ mode : Prover depth desc2 ) ( mk ' ~ mode : Checked depth desc3 ) let tuple ~ mode depth typs = let error_info ( ) = ( " tuple " , typs ) in let alts = List . map ~ f : type_alternate typs in let alt_alts = List . map ~ f : type_alternate alts in if List . for_all2_exn typs alt_alts ~ f ( : fun typ alt -> check_mode ~ pos : __POS__ ~ error_info mode typ ; check_mode ~ pos : __POS__ ~ error_info mode alt ; assert ( not ( is_poly typ ) ) ; check_valid ~ pos : __POS__ ~ error_info typ ; check_valid ~ pos : __POS__ ~ error_info alt ; phys_equal typ alt ) then stitch ~ mode depth ( Ttuple typs ) ( Ttuple alts ) else tri_stitch ~ mode depth ( Ttuple typs ) ( Ttuple alts ) ( Ttuple alt_alts ) let arrow ~ mode ( ? explicit = Explicit ) ( ? label = Nolabel ) depth typ1 typ2 = let error_info ( ) = ( " arrow " , [ typ1 ; typ2 ] ) in let alt1 = type_alternate typ1 in let alt2 = type_alternate typ2 in let alt_alt1 = type_alternate alt1 in let alt_alt2 = type_alternate alt2 in if List . for_all2_exn [ typ1 ; typ2 ] [ alt_alt1 ; alt_alt2 ] ~ f ( : fun typ alt -> check_mode ~ pos : __POS__ ~ error_info mode typ ; check_mode ~ pos : __POS__ ~ error_info mode alt ; check_valid ~ pos : __POS__ ~ error_info typ ; check_valid ~ pos : __POS__ ~ error_info alt ; assert ( not ( is_poly typ ) ) ; phys_equal typ alt ) then stitch ~ mode depth ( Tarrow ( typ1 , typ2 , explicit , label ) ) ( Tarrow ( alt1 , alt2 , explicit , label ) ) else tri_stitch ~ mode depth ( Tarrow ( typ1 , typ2 , explicit , label ) ) ( Tarrow ( alt1 , alt2 , explicit , label ) ) ( Tarrow ( alt_alt1 , alt_alt2 , explicit , label ) ) let ctor ~ mode depth path ? other_path ? tri_path params = let error_info ( ) = let desc = Format . ( fprintf str_formatter " ctor ( % a ) " Path . debug_print path ) ; Format . flush_str_formatter ( ) in ( desc , params ) in assert ( Option . is_some other_path || Option . is_none tri_path ) ; let other_path = Option . value ~ default : path other_path in let alts = List . map ~ f : type_alternate params in let alt_alts = List . map ~ f : type_alternate alts in if List . for_all2_exn params alt_alts ~ f ( : fun typ alt -> check_mode ~ pos : __POS__ ~ error_info mode typ ; check_mode ~ pos : __POS__ ~ error_info mode alt ; check_valid ~ pos : __POS__ ~ error_info typ ; check_valid ~ pos : __POS__ ~ error_info alt ; assert ( not ( is_poly typ ) ) ; phys_equal typ alt ) && Option . is_none tri_path then stitch ~ mode depth ( Tctor { var_ident = path ; var_params = params } ) ( Tctor { var_ident = other_path ; var_params = alts } ) else let tri_path = Option . value ~ default : path tri_path in tri_stitch ~ mode depth ( Tctor { var_ident = path ; var_params = params } ) ( Tctor { var_ident = other_path ; var_params = alts } ) ( Tctor { var_ident = tri_path ; var_params = alt_alts } ) let poly ~ mode depth vars typ = let error_info ( ) = ( " poly " , typ :: vars ) in check_valid ~ pos : __POS__ ~ error_info typ ; assert ( not ( is_poly typ ) ) ; check_mode ~ pos : __POS__ ~ error_info mode typ ; let alt = type_alternate typ in let alt_alt = type_alternate alt in check_valid ~ pos : __POS__ ~ error_info alt_alt ; let get_alt_var pos mode var = let alt = get_mode mode var in check_valid ~ pos : __POS__ ~ error_info alt ; match alt . type_desc with | Tvar _ -> alt | _ -> check_mode ~ pos ~ error_info Checked var ; assert ( not ( phys_equal alt . type_alternate var ) ) ; var in let alts = List . map ~ f ( : get_alt_var __POS__ ( other_mode mode ) ) vars in let alt_alts = List . map ~ f ( : get_alt_var __POS__ mode ) alts in if equal_mode mode Prover || List . for_all2_exn vars alt_alts ~ f ( : fun typ alt -> ( match ( typ . type_desc , alt . type_desc ) with | Tvar _ , Tvar _ -> ( ) | _ -> assert false ) ; phys_equal typ alt ) && phys_equal typ alt_alt then stitch ~ mode depth ( Tpoly ( vars , typ ) ) ( Tpoly ( vars , alt ) ) else tri_stitch ~ mode depth ( Tpoly ( vars , typ ) ) ( Tpoly ( vars , alt ) ) ( Tpoly ( vars , alt_alt ) ) let conv ~ mode depth typ1 typ2 = let error_info ( ) = ( " conv " , [ typ1 ; typ2 ] ) in check_valid ~ pos : __POS__ ~ error_info typ1 ; check_valid ~ pos : __POS__ ~ error_info typ2 ; assert ( not ( is_poly typ1 ) ) ; assert ( not ( is_poly typ2 ) ) ; check_mode ~ pos : __POS__ ~ error_info Checked typ1 ; check_mode ~ pos : __POS__ ~ error_info Prover typ2 ; let typ_stitched = if are_stitched typ1 typ2 then typ1 else stitch ~ mode : Checked depth typ1 . type_desc typ2 . type_desc in let typ = stitch ~ mode : Checked depth ( Tconv typ_stitched ) ( Tconv typ_stitched ) in get_mode mode typ let opaque ~ mode depth typ = let error_info ( ) = ( " opaque " , [ typ ] ) in check_valid ~ pos : __POS__ ~ error_info typ ; check_valid ~ pos : __POS__ ~ error_info typ . type_alternate . type_alternate ; assert ( not ( is_poly typ ) ) ; check_mode ~ pos : __POS__ ~ error_info Prover typ ; stitch ~ mode depth ( Topaque typ ) ( Topaque typ ) let other_mode ~ mode depth typ = let error_info ( ) = ( " other_mode " , [ typ ] ) in check_valid ~ pos : __POS__ ~ error_info typ ; check_valid ~ pos : __POS__ ~ error_info typ . type_alternate . type_alternate ; assert ( not ( is_poly typ ) ) ; stitch ~ mode depth ( Tother_mode typ ) ( Tother_mode typ ) let row ~ mode depth row = let row_alt = row_alternate row in match mode with | Prover -> let alt = stitch ~ mode : Prover depth ( Trow row ) ( Trow row_alt ) in opaque ~ mode depth alt | Checked -> let row_alt_alt = row_alternate row in let prover = stitch ~ mode : Prover depth ( Trow row_alt ) ( Trow row_alt_alt ) in tri_stitch ~ mode depth ( Trow row ) ( Topaque prover ) ( Topaque prover ) let row_of_ctor ~ mode depth ident args = let row_rest = var ~ mode depth None in let row_tags = Ident . Map . singleton ident ( Path . Pident ident , mk_rp RpPresent , args ) in row ~ mode depth { row_tags ; row_closed = Open ; row_rest ; row_presence_proxy = mk_rp RpPresent } end
type change = | Depth of ( type_expr * int ) | Desc of ( type_expr * type_desc ) | Replace of ( type_expr * type_desc ) | Row_presence of ( row_presence * row_presence_desc ) | Row_replace of ( row_presence * row_presence_desc )
let debug_print_change fmt = function | Depth ( typ , depth ) -> Format . fprintf fmt " depth ( id = % i , % i ) " typ . type_id depth | Desc ( typ , _ ) -> Format . fprintf fmt " desc ( id = % i , _ ) " typ . type_id | Replace ( typ , _ ) -> Format . fprintf fmt " replace ( id = % i , _ ) " typ . type_id | Row_presence ( pres , _ ) -> Format . fprintf fmt " row_presence ( id = % i , _ ) " pres . rp_id | Row_replace ( pres , _ ) -> Format . fprintf fmt " row_replace ( id = % i , _ ) " pres . rp_id
module Snapshot : sig type t val create : unit -> t val add_to_history : change -> unit val backtrack : t -> change list val filtered_backtrack : f ( : change -> bool ) -> t -> change list val debug_print : Format . formatter -> t -> unit val debug_print_latest : Format . formatter -> unit -> unit type node = Change of ( change * t ) | LinkedChange of t | NoChange and t = node ref let current = Weak . create 1 let add_to_history change = match Weak . get current 0 with | Some ptr -> let new_ptr = ref NoChange in ptr := Change ( change , new_ptr ) ; Weak . set current 0 ( Some new_ptr ) | None -> ( ) let create ( ) = match Weak . get current 0 with | Some ptr -> ptr | None -> let new_ptr = ref NoChange in Weak . set current 0 ( Some new_ptr ) ; new_ptr let rec collect snap = match ! snap with | Change ( change , ptr ) -> change :: collect ptr | LinkedChange ptr -> collect ptr | NoChange -> [ ] let debug_print fmt snap = let open Format in pp_print_list ~ pp_sep ( : fun fmt ( ) -> fprintf fmt " , , " ) @ debug_print_change fmt ( collect snap ) let debug_print_latest fmt ( ) = match Weak . get current 0 with | Some snap -> debug_print fmt snap | None -> Format . pp_print_list debug_print_change fmt [ ] let backtrack snap = let current = create ( ) in let rec backtrack changes ptr = match ! ptr with | Change ( change , ptr ' ) -> ptr := LinkedChange current ; backtrack ( change :: changes ) ptr ' | LinkedChange ptr ' -> ptr := LinkedChange current ; backtrack changes ptr ' | NoChange -> changes in backtrack [ ] snap let filtered_backtrack ~ f snap = let rec backtrack changes ptrs_to_clear ptr = match ! ptr with | Change ( change , ptr ' ) when f change -> backtrack ( change :: changes ) ( ptr :: ptrs_to_clear ) ptr ' | Change ( _change , ptr ' ) -> List . iter ptrs_to_clear ~ f ( : fun ptr ' -> ptr ' := LinkedChange ptr ) ; backtrack changes [ ] ptr ' | LinkedChange ptr ' -> backtrack changes ptrs_to_clear ptr ' | NoChange -> let current = create ( ) in List . iter ptrs_to_clear ~ f ( : fun ptr ' -> ptr ' := LinkedChange current ) ; changes in backtrack [ ] [ ] snap end
let revert = function | Depth ( typ , depth ) -> typ . type_depth <- depth ; typ . type_alternate . type_depth <- depth ; typ . type_alternate . type_alternate . type_depth <- depth | Desc ( typ , desc ) -> typ . type_desc <- desc | Replace ( typ , desc ) -> typ . type_desc <- desc | Row_presence ( pres , desc ) | Row_replace ( pres , desc ) -> pres . rp_desc <- desc
let backtrack snap = let changes = Snapshot . backtrack snap in List . iter ~ f : revert changes
let filtered_backtrack ~ f snap = let changes = Snapshot . filtered_backtrack ~ f snap in List . iter ~ f : revert changes
let fold ~ init ~ f typ = match typ . type_desc with | Tvar _ -> init | Ttuple typs -> List . fold ~ init ~ f typs | Tarrow ( typ1 , typ2 , _ , _ ) -> let acc = f init typ1 in f acc typ2 | Tctor variant -> List . fold ~ init ~ f variant . var_params | Tpoly ( typs , typ ) -> let acc = List . fold ~ init ~ f typs in f acc typ | Tref typ -> f init typ | Tconv typ -> f init typ | Topaque typ -> f init typ | Tother_mode typ -> f init typ | Treplace _ -> assert false | Trow { row_tags ; row_closed = _ ; row_rest ; row_presence_proxy = _ } -> let acc = Map . fold row_tags ~ init ~ f ( : fun ~ key : _ ~ data ( : _ , _ , args ) init -> List . fold ~ f ~ init args ) in f acc row_rest
let iter ~ f = fold ~ init ( ) : ~ f ( : fun ( ) -> f )
let rec copy_desc ~ f = function | Tvar _ as typ -> typ | Ttuple typs -> Ttuple ( List . map ~ f typs ) | Tarrow ( typ1 , typ2 , explicitness , label ) -> Tarrow ( f typ1 , f typ2 , explicitness , label ) | Tctor ( { var_params ; _ } as variant ) -> Tctor { variant with var_params = List . map ~ f var_params } | Tpoly ( typs , typ ) -> Tpoly ( List . map ~ f typs , f typ ) | Tref typ -> copy_desc ~ f typ . type_desc | Tconv typ -> Tconv ( f typ ) | Topaque typ -> Topaque ( f typ ) | Tother_mode typ -> Tother_mode ( f typ ) | Treplace _ -> assert false | Trow _ -> assert false
let rec equal_at_depth ~ get_decl ~ depth typ1 typ2 = let equal_at_depth = equal_at_depth ~ get_decl ~ depth in let typ1 = repr typ1 in let typ2 = repr typ2 in if Int . equal typ1 . type_id typ2 . type_id then true else match ( typ1 . type_desc , typ2 . type_desc ) with | Tvar _ , _ when typ1 . type_depth > depth -> true | _ , Tvar _ when typ2 . type_depth > depth -> true | Ttuple typs1 , Ttuple typs2 -> ( match List . for_all2 typs1 typs2 ~ f : equal_at_depth with | Ok b -> b | Unequal_lengths -> false ) | ( Tarrow ( typ1a , typ1b , explicitness1 , label1 ) , Tarrow ( typ2a , typ2b , explicitness2 , label2 ) ) -> equal_explicitness explicitness1 explicitness2 && equal_arg_label label1 label2 && equal_at_depth typ1a typ2a && equal_at_depth typ1b typ2b | ( Tctor ( { var_ident = path1 ; _ } as variant1 ) , Tctor ( { var_ident = path2 ; _ } as variant2 ) ) -> let decl1 = get_decl path1 in let decl2 = get_decl path2 in Int . equal decl1 . tdec_id decl2 . tdec_id && List . for_all2_exn ~ f : equal_at_depth variant1 . var_params variant2 . var_params | Tpoly ( typs1 , typ1 ) , Tpoly ( typs2 , typ2 ) -> ( match List . for_all2 typs1 typs2 ~ f : equal_at_depth with | Ok true -> equal_at_depth typ1 typ2 | _ -> false ) | _ , _ -> false
let set_depth depth typ = Snapshot . add_to_history ( Depth ( typ , typ . type_depth ) ) ; typ . type_depth <- depth ; typ . type_alternate . type_depth <- depth ; typ . type_alternate . type_alternate . type_depth <- depth
let update_depth depth typ = if typ . type_depth > depth then set_depth depth typ
let unify_depths typ1 typ2 = iter ~ f ( : update_depth typ1 . type_depth ) typ2 ; iter ~ f ( : update_depth typ2 . type_depth ) typ1
let set_rp_desc pres desc = Snapshot . add_to_history ( Row_presence ( pres , pres . rp_desc ) ) ; pres . rp_desc <- desc
let ( ) = set_rp_desc_fwd := set_rp_desc
let set_desc typ desc = Snapshot . add_to_history ( Desc ( typ , typ . type_desc ) ) ; typ . type_desc <- desc
let unsafe_set_single_replacement typ typ ' = Snapshot . add_to_history ( Replace ( typ , typ . type_desc ) ) ; typ . type_desc <- Treplace typ '
let set_replacement typ typ ' = let replace_one = unsafe_set_single_replacement in replace_one typ typ ' ; replace_one typ . type_alternate typ ' . type_alternate ; let alt_alt = typ . type_alternate . type_alternate in let alt_alt ' = typ ' . type_alternate . type_alternate in if ( not ( phys_equal typ alt_alt ) ) || not ( phys_equal typ ' alt_alt ' ) then replace_one alt_alt alt_alt '
let backtrack_replace = filtered_backtrack ~ f ( : function | Replace _ | Row_replace _ -> true | _ -> false )
let set_repr typ typ ' = assert ( equal_mode typ . type_mode typ ' . type_mode ) ; assert ( phys_equal typ typ . type_alternate = phys_equal typ ' typ ' . type_alternate ) ; set_desc typ ( Tref typ ' ) ; set_desc typ . type_alternate ( Tref typ ' . type_alternate ) ; set_desc typ . type_alternate . type_alternate ( Tref typ ' . type_alternate . type_alternate )
let rec choose_variable_name typ typ ' = match ( typ . type_desc , typ ' . type_desc ) with | Tvar ( Some name ) , Tvar None -> set_desc typ ' ( Tvar ( Some name ) ) | Tvar ( Some _ ) , Trow row -> let _ , row_rest , _ = row_repr row in choose_variable_name typ row_rest | Tvar _ , _ -> ( ) | _ -> assert false
let add_instance ~ unify typ typ ' = assert ( equal_mode typ . type_mode typ ' . type_mode ) ; assert ( phys_equal typ typ . type_alternate . type_alternate = phys_equal typ ' typ ' . type_alternate . type_alternate ) ; choose_variable_name typ typ ' ; match typ . type_alternate . type_desc with | Tvar _ -> choose_variable_name typ . type_alternate typ ' . type_alternate ; choose_variable_name typ . type_alternate . type_alternate typ ' . type_alternate . type_alternate ; choose_variable_name typ typ ' ; set_repr typ typ ' | _ -> assert ( equal_mode Checked typ . type_mode ) ; set_desc typ ( Tref typ ' ) ; unify typ . type_alternate typ ' . type_alternate ; if not ( phys_equal typ typ . type_alternate . type_alternate ) then unify typ . type_alternate . type_alternate typ ' . type_alternate . type_alternate
let flatten typ = let rec flatten typ = let typ = repr typ in match typ . type_desc with | Treplace typ ' -> ( match typ . type_alternate . type_desc with | Treplace _ -> ( ) | _ -> assert false ) ; typ ' | Tvar _ -> typ | Trow _ -> typ | desc -> ( match typ . type_alternate . type_desc with | Treplace alt -> assert ( not ( phys_equal typ typ . type_alternate . type_alternate ) ) ; assert ( equal_mode typ . type_mode Checked ) ; let typ ' = mk ' ~ mode : typ . type_mode typ . type_depth ( Tvar None ) in typ ' . type_alternate <- alt ; unsafe_set_single_replacement typ typ ' ; typ ' . type_desc <- copy_desc ~ f : flatten desc ; typ ' | Tvar _ -> assert false | _ -> let alt_desc = typ . type_alternate . type_desc in let alt_alt_desc = typ . type_alternate . type_alternate . type_desc in let typ ' = mkvar ~ mode : typ . type_mode typ . type_depth None in let stitched = phys_equal typ typ . type_alternate . type_alternate in if stitched then typ ' . type_alternate . type_alternate <- typ ' ; set_replacement typ typ ' ; typ ' . type_desc <- copy_desc ~ f : flatten desc ; typ ' . type_alternate . type_desc <- copy_desc ~ f : flatten alt_desc ; if not stitched then typ ' . type_alternate . type_alternate . type_desc <- copy_desc ~ f : flatten alt_alt_desc ; typ ' ) in let snap = Snapshot . create ( ) in let typ = flatten typ in backtrack snap ; typ
let type_vars ? depth typ = let deep_enough = match depth with | Some depth -> fun typ -> depth <= typ . type_depth | None -> fun _ -> true in let empty = Typeset . empty in let rec type_vars set typ = match typ . type_desc with | Tvar _ when deep_enough typ -> Set . add set typ | Tpoly ( vars , typ ) -> let poly_vars = List . fold ~ init : empty vars ~ f : type_vars in Set . union set ( Set . diff ( type_vars empty typ ) poly_vars ) | _ -> fold ~ init : set typ ~ f : type_vars in type_vars empty typ
let mk_option : ( Type0 . type_expr -> Type0 . type_expr ) ref = ref ( fun _ -> failwith " mk_option not initialised " )
let rec bubble_label_aux label typ = let { type_depth ; type_mode = mode ; _ } = typ in match ( repr typ ) . type_desc with | Tarrow ( typ1 , typ2 , explicit , arr_label ) when Int . equal ( compare_arg_label label arr_label ) 0 -> ( Some ( typ1 , explicit , arr_label ) , typ2 ) | Tarrow ( typ1 , typ2 , explicit , arr_label ) when match ( label , arr_label ) with | Labelled lbl , Optional arr_lbl -> String . equal lbl arr_lbl | _ -> false -> ( Some ( ! mk_option typ1 , explicit , arr_label ) , typ2 ) | Tarrow ( typ1 , typ2 , explicit , arr_label ) -> ( match bubble_label_aux label typ2 with | None , _ -> ( None , typ ) | res , typ2 -> ( res , Mk . arrow ~ mode ~ explicit ~ label : arr_label type_depth typ1 typ2 ) ) | _ -> ( None , typ )
let bubble_label label typ = let { type_depth ; type_mode = mode ; _ } = typ in match bubble_label_aux label typ with | Some ( typ1 , explicit , label ) , typ2 -> Mk . arrow ~ mode ~ explicit ~ label type_depth typ1 typ2 | None , typ -> typ
let discard_optional_labels typ = let rec go typ ' = let typ ' = repr typ ' in match typ ' . type_desc with | Tarrow ( _ , typ2 , _ , Optional _ ) -> go typ2 | Tarrow ( _ , _ , _ , _ ) -> typ | _ -> typ ' in go typ
let is_arrow typ = match ( repr typ ) . type_desc with | Tarrow _ | Tpoly ( _ , { type_desc = Tarrow _ ; _ } ) -> true | _ -> false
let is_var typ = match ( repr typ ) . type_desc with Tvar _ -> true | _ -> false
let is_replace typ = match ( repr typ ) . type_desc with Treplace _ -> true | _ -> false