text
stringlengths
0
601k
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 ) ...
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 ) i...
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...
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_presenc...
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...
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_...
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 a...
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 ...
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 -> tr...
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 ) ) || ...
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 ( Tre...
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_vari...
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 ( ...
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 ...
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_...
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
let get_replace typ = match ( repr typ ) . type_desc with Treplace typ -> Some typ | _ -> None
let get_rev_arrow_args typ = let rec go args typ = let typ = repr typ in match typ . type_desc with | Tarrow ( typ1 , typ2 , explicit , label ) -> go ( ( typ1 , explicit , label ) :: args ) typ2 | _ -> ( args , typ ) in go [ ] typ
let rec get_rev_implicits acc typ = match typ . type_desc with | Tarrow ( typ1 , typ2 , Implicit , label ) -> get_rev_implicits ( ( label , typ1 ) :: acc ) typ2 | _ -> ( acc , typ )
let get_implicits typ = let implicits , typ = get_rev_implicits [ ] typ in ( List . rev implicits , typ )
let get_rev_implicits typ = get_rev_implicits [ ] typ
let contains typ ~ in_ = let typ = repr typ in let equal in_ = phys_equal typ ( repr in_ ) in let rec contains in_ = let in_ = repr in_ in match in_ . type_desc with | Tvar _ -> false | Ttuple typs -> List . exists ~ f : equal typs || List . exists ~ f : contains typs | Tarrow ( typ1 , typ2 , _explicit , _label ) -> eq...
let rec get_same_mode typ1 typ2 = let typ1 = repr typ1 in let typ2 = repr typ2 in match ( typ1 . type_desc , typ2 . type_desc ) with | Tother_mode typ1 ' , Tother_mode typ2 ' -> ( match ( typ1 . type_mode , typ2 . type_mode ) with | Checked , Checked | Prover , Prover -> get_same_mode typ1 ' typ2 ' | Checked , Prover -...
let rec remove_opaques typ = let typ = repr typ in match typ . type_desc with Topaque typ -> remove_opaques typ | _ -> typ
let rec remove_mode_changes typ = let typ = repr typ in match typ . type_desc with | Tother_mode typ -> remove_mode_changes typ | _ -> typ
module Decl = struct let decl_id = ref 0 let next_id ( ) = incr decl_id ; ! decl_id let mk ~ name ~ params desc = incr decl_id ; let tdec_ret = Mk . ctor ~ mode ( : Ident . mode name ) 10000 ( Path . Pident name ) params in { tdec_params = params ; tdec_desc = desc ; tdec_id = ! decl_id ; tdec_ret } end
let report_error ppf = function | Mk_wrong_mode ( kind , typs , mode , typ ) -> fprintf ppf " [ @< hov > Internal error : Could not make a type % s from types ; @\ [ @< hov2 >% a ] ; @@\ The type % a was expected to have mode % a . ] " @ kind ( pp_print_list ~ pp_sep : pp_print_newline Debug_print . type_expr ) typs De...
let ( ) = Location . register_error_of_exn ( function | Error ( loc , err ) -> Some ( Location . error_of_printer loc report_error err ) | _ -> None )
let shared_computation = Bonsai_web_ui_typeahead . Typeahead . create ( module Data ) ~ all_options ( : Value . return Data . all ) ~ placeholder " : Select a value " ~ to_string ( : Bonsai . Value . return Data . to_string ) ; ;
let view_computation = let % sub _ , view , _ = shared_computation in return view ; ;
let view_and_inject_computation = let % sub _ , view , inject = shared_computation in return ( Value . both view inject ) ; ;
let view_and_result_computation = let % sub result , view , _ = shared_computation in return ( Value . both view result ) ; ; let handle = Handle . create ( Result_spec . vdom Fn . id ) view_computation in Handle . show handle ; [ % expect { | < div > < input type " = text " list " = bonsai_path_replaced_in_test " plac...
let shared_computation = Bonsai_web_ui_typeahead . Typeahead . create_multi ( module Data ) ~ all_options ( : Value . return Data . all ) ~ placeholder " : Select a value " ~ to_string : Data . to_string ~ split ( : String . split ~ on ' , ' ) : ; ;
let view_computation = let % sub _ , vdom , _ = shared_computation in return vdom ; ;
let view_and_set_computation = let % sub _ , vdom , set = shared_computation in return ( Value . both vdom set ) ; ;
let view_and_result_computation = let % sub result , vdom , _ = shared_computation in return ( Value . both vdom result ) ; ;
let input_value handle value = Handle . input_text handle ~ get_vdom : Fn . id ~ selector " : input " ~ text ( : Data . to_string value ) ; ; let handle = Handle . create ( Result_spec . vdom Fn . id ) view_computation in Handle . show handle ; [ % expect { | < div > < input type " = text " list " = bonsai_path_replace...
let preprocess ~ pp ~ ext text = let sourcefile = Filename . temp_file " caml " ext in begin try let oc = open_out_bin sourcefile in output_string oc text ; flush oc ; close_out oc with _ -> failwith " Preprocessing error " end ; let tmpfile = Filename . temp_file " camlpp " ext in let comm = Printf . sprintf " % s % s...
let parse_pp ~ parse ~ wrap ~ ext text = match ! Clflags . preprocessor with None -> parse ( Lexing . from_string text ) | Some pp -> let tmpfile = preprocess ~ pp ~ ext text in let ast_magic = if ext = " . ml " then Config . ast_impl_magic_number else Config . ast_intf_magic_number in let ic = open_in_bin tmpfile in l...
let nowarnings = ref false
let f txt = let error_messages = ref [ ] in let text = Jg_text . get_all txt . tw and env = ref ( Env . open_pers_signature " Pervasives " Env . initial ) in let tl , ew , end_message = Jg_message . formatted ~ title " : Warnings " ~ ppf : Format . err_formatter ( ) in Text . tag_remove txt . tw ~ tag " : error " ~ sta...
type error = [ ` TypeMismatchError of string | ` UnboundRegularVarInsideBoxError of Location . t * string | Env . error ]
type ' e lerror = ( [ > error ] as ' e ) Location . located
let check_equal ty1 ty2 msg = Result . ok_if_true ( [ % equal : Type . t ] ty1 ty2 ) ~ error ( ` : TypeMismatchError msg )
let with_error_location loc r = Result . map_error r ~ f ( : fun e -> Location . locate ~ loc e )
let fail_in loc err = Result . fail @@ Location . locate ~ loc @@ err
let mk_unbound_regular_var_inside_box_error box_expr_loc var_loc var = let var_name = Id . R . to_string var in let msg = [ % string " regular variable " ( \$ var_name ) " \ is accessed in box expression \ ( ( $ Location . pp_column_range box_expr_loc ) ) at \ ( $ Location . pp_column_range var_loc ) " ] in fail_in var...
let rec check_open delta gamma Location . { data = expr ; loc } typ = match expr with | Unit -> let exp_ty = PrettyPrinter . Str . of_type typ in with_error_location loc @@ check_equal typ Type . Unit [ % string " Expected $ exp_ty , but found Unit type " ] | Pair { e1 ; e2 } -> ( match typ with | Type . Prod { ty1 ; t...
let check expr typ = check_open Env . M . emp Env . R . emp expr typ
let infer expr = infer_open Env . M . emp Env . R . emp expr
type method_type = typ list * typ
type method_env = method_type SM . t
type attribute_env = typ SM . t
type class_type = attribute_env * method_env
type class_env = class_type SM . t
type variable_env = typ SM . t
let error ( location : ' a Location . t ) ( msg : string ) = raise ( Error ( sprintf " % s :\ n % s " ( Error . positions ( Location . startpos location ) ( Location . endpos location ) ) msg ) )
let errors ( locations : ' a Location . t list ) ( msg : string ) = raise ( Error ( sprintf " % s % s " ( List . fold_right ( fun location acc -> sprintf " % s :\ n % s " ( Error . positions ( Location . startpos location ) ( Location . endpos location ) ) acc ) locations " " ) msg ) )
let lookup ( msg : string ) ( id : identifier ) ( env : ' a SM . t ) = try SM . find ( Location . content id ) env with Not_found -> error id ( sprintf " % s % s is undefined " msg ( Location . content id ) )
let vlookup : identifier -> variable_env -> typ = lookup " variable "
let mlookup : identifier -> method_env -> method_type = lookup " method "
let alookup : identifier -> attribute_env -> typ = lookup " attribute "
let clookup : identifier -> class_env -> class_type = lookup " class "
let rec compatible ( typ1 : typ ) ( typ2 : typ ) ( instanceof : identifier -> identifier -> bool ) : bool = match typ1 , typ2 with | TypInt , TypInt | TypBool , TypBool | TypIntArray , TypIntArray -> true | Typ t1 , Typ t2 -> instanceof t1 t2 | _ , _ -> false
let rec type_to_string : typ -> string = function | TypInt -> " integer " | TypBool -> " boolean " | TypIntArray -> " int [ ] " | Typ t -> Location . content t
let rec typecheck_call ( cenv : class_env ) ( venv : variable_env ) ( vinit : S . t ) ( instanceof : identifier -> identifier -> bool ) ( o : expression ) ( callee : identifier ) ( expressions : expression list ) : typ = let o_type = typecheck_expression cenv venv vinit instanceof o in match o_type with | Typ t -> begi...
let rec typecheck_instruction ( cenv : class_env ) ( venv : variable_env ) ( vinit : S . t ) ( instanceof : identifier -> identifier -> bool ) ( inst : instruction ) : S . t = match inst with | ISetVar ( v , e ) -> let vinit = S . add ( Location . content v ) vinit in typecheck_expression_expecting cenv venv vinit inst...
let occurrences ( x : string ) ( bindings : ( identifier * ' a ) list ) : identifier list = List . map fst ( List . filter ( fun ( id , _ ) -> x = Location . content id ) bindings )
let map_of_association_list ( entity : string ) ( bindings : ( identifier * ' a ) list ) : ' a SM . t = try SM . of_association_list ( List . map ( fun ( id , data ) -> ( Location . content id , data ) ) bindings ) with SM . Duplicate x -> errors ( occurrences x bindings ) ( sprintf " % s % s is declared more than once...
let variable_map ( decls : ( identifier * typ ) list ) : variable_env = map_of_association_list " Variable " decls
let method_map ( decls : ( identifier * method_type ) list ) : method_env = map_of_association_list " Method " decls
let typecheck_method ( cenv : class_env ) ( venv : variable_env ) ( instanceof : identifier -> identifier -> bool ) ( m : metho ) : unit = let formals = m . formals and locals = m . locals in let mformals = variable_map formals and mlocals = variable_map locals in begin try let x = StringSet . choose ( StringSet . inte...
let typecheck_class ( cenv : class_env ) ( instanceof : identifier -> identifier -> bool ) ( ( name , c ) : identifier * clas ) : unit = let attribute_env , _ = clookup name cenv in let venv = SM . add " this " ( Typ name ) attribute_env in List . iter ( typecheck_method cenv venv instanceof ) ( List . map snd c . meth...
let extract_method_type ( m : metho ) : method_type = ( List . map snd m . formals , m . result )
let extract_class_type ( c : clas ) : class_type = ( variable_map c . attributes , method_map ( List . map ( fun ( id , m ) -> ( id , extract_method_type m ) ) c . methods ) )
let class_map ( decls : ( identifier * clas ) list ) : clas SM . t = map_of_association_list " Class " decls
let create_instanceof ( cmap : clas SM . t ) : identifier -> identifier -> bool = let rec instanceof id1 id2 = if id1 = id2 then true else try match ( SM . find id1 cmap ) . extends with | None -> false | Some id3 -> instanceof ( Location . content id3 ) id2 with Not_found -> false in fun id1 id2 -> instanceof ( Locati...
let add_method ( cmap : clas SM . t ) ( instanceof : identifier -> identifier -> bool ) : clas SM . t = let test_compatible_signature ( ( name , m ) : identifier * metho ) ( ( name ' , m ' ) : identifier * metho ) : unit = let typecheck_params ( typ : typ ) ( typ ' : typ ) : unit = if not ( compatible typ typ ' ( fun t...
let typecheck_program ( p : program ) : unit = let cmap = class_map p . defs in let instanceof = create_instanceof cmap in let cenv = add_method cmap instanceof |> SM . map extract_class_type in List . iter ( typecheck_class cenv instanceof ) p . defs ; let venv = SM . singleton " this " ( Typ p . name ) in typecheck_i...