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 ) | _ , ... |
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 ) | Tup... |
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_o... |
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 i... |
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 el... |
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 ... |
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 ... |
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 ... |
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 ... |
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 Non... |
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_ex... |
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_ide... |
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 i... |
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 t... |
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 ... |
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 .... |
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 map... |
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... |
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_p... |
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_alterna... |
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 ) ; ass... |
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 -> ( ... |
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... |
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 . fprin... |
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 -> ... |
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_d... |
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 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 wit... |
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 ty... |
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 ... |
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 ) ; se... |
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 ... |
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... |
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 .... |
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 ( var... |
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 ) , ... |
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.