text
stringlengths
12
786k
module Testbench = struct let write chan a b c = let b = Verilog . Testbench . write a b c in output_string chan ( Buffer . contents b ) end
module Hierarchy = struct open Signal . Types let vwrite = write let inst_name = function | Signal_inst ( _ , _ , i ) -> i . inst_name | _ -> failwith " expecting instantiation " let rec write ( ? transforms [ ] ) = database path circ = let name = Circuit . name circ in let f = open_out ( path ^ name ^ " . v " ) in let circ = List . fold_left ( fun circ fn -> Transform . rewrite_circuit fn circ ) circ transforms in vwrite f circ ; close_out f ; let insts = Circuit . search Circuit . id ( fun l s -> if is_inst s then s :: l else l ) [ ] ( Circuit . outputs circ ) in List . iter ( fun inst -> let name = inst_name inst in try match Circuit . Hierarchy . get database name with | Some ( c ) -> write ~ transforms : transforms database path c | None -> ( ) with | Circuit . Failure ( e ) -> failwith ( " error generating " ^ name ^ " : " ^ e ) ) insts end
let qualified_name m name = Conflict . qualified_name m . mod_namespace name
let pp_lwt_return fmt = function | Some lwt_module -> fprintf fmt " % s . return " lwt_module | _ -> assert false
type from_require_import = { import_from : string ; import_module : string }
let pp_from_require_import fmt fri = fprintf fmt " From % s Require Import % s . " fri . import_from fri . import_module
type from_require_export = { export_from : string ; export_module : string }
let pp_from_require_export fmt fre = fprintf fmt " From % s Require Export % s . " fre . export_from fre . export_module
type require = { require_module : string }
let pp_require fmt req = fprintf fmt " Require % s . " req . require_module
type constructor = { constructor_name : coq_name ; constructor_prototype : Repr . prototype_repr ; }
let pp_constructor fmt c = fprintf fmt " | [ @< hov 2 [ >@< hov 2 >% a % a % a % a % a ] @@ : % a ] " @ pp_coq_name c . constructor_name ( pp_if_not_empty pp_print_space ) c . constructor_prototype . prototype_type_args pp_type_args_list c . constructor_prototype . prototype_type_args ( pp_if_not_empty pp_print_space ) c . constructor_prototype . prototype_args pp_args_list c . constructor_prototype . prototype_args pp_type_repr c . constructor_prototype . prototype_ret_type
type inductive = { inductive_name : coq_name ; inductive_type_args : string list ; inductive_type : Repr . type_repr ; inductive_constructors : constructor list ; }
let pp_inductive fmt = function | [ ] -> ( ) | lind -> let pp_inductive_aux fmt ind = fprintf fmt " % a % a % a @ : % a ] :=@@ % a " pp_coq_name ind . inductive_name ( pp_if_not_empty pp_print_space ) ind . inductive_type_args pp_type_args_list ind . inductive_type_args pp_type_repr ind . inductive_type ( pp_print_list ~ pp_sep : pp_print_space pp_constructor ) ind . inductive_constructors in fprintf fmt " [ @< v [ >@< hov 2 > Inductive % a . ] " @ ( pp_print_list ~ pp_sep ( : fun fmt _ -> fprintf fmt " @ [ @< hov 2 > with " ) pp_inductive_aux ) lind
type field = { field_name : coq_name ; field_type : mono_type_repr }
type record = { record_name : coq_name ; record_type_args : string list ; record_fields : field list ; }
let pp_record fmt r = let pp_field fmt f = fprintf fmt " % a : % a " pp_coq_name f . field_name pp_mono_type_repr f . field_type in fprintf fmt " [ @< hov 2 > Record % a % a % a :=@ [ @< hov { >@ % a @ } . ] ] " @@ pp_coq_name r . record_name ( pp_if_not_empty ( fun fmt _ -> pp_print_text fmt " " ) ) r . record_type_args pp_type_args_list r . record_type_args ( pp_list ~ pp_sep ( : fun fmt _ -> fprintf fmt " @ ; " ) pp_field ) r . record_fields
type definition = { def_name : coq_name ; def_typeclass_args : string list ; def_prototype : Repr . prototype_repr ; def_body : Format . formatter -> unit -> unit ; }
let pp_definition fmt def = fprintf fmt " [ @< hov 2 [ >@< hov 2 > Definition % a % a % a % a % a % a ] @ :@ % a :=@ % a . ] " @ pp_coq_name def . def_name ( pp_list ~ pp_prefix ( : fun fmt _ -> pp_print_string fmt " ` { " ) ~ pp_suffix ( : fun fmt _ -> pp_print_string fmt " } " ) ~ pp_sep : pp_print_space pp_print_string ) def . def_typeclass_args ( pp_if_not_empty pp_print_space ) def . def_prototype . prototype_type_args pp_type_args_list def . def_prototype . prototype_type_args ( pp_if_not_empty pp_print_space ) def . def_prototype . prototype_args pp_args_list def . def_prototype . prototype_args pp_type_repr def . def_prototype . prototype_ret_type def . def_body ( )
type typeclass = { class_name : coq_name ; class_typeclass_args : string list ; class_args : ( string * Repr . type_repr ) list ; class_type : Repr . type_repr ; class_members : ( coq_name * Repr . type_repr ) list ; }
let pp_typeclass fmt cls = fprintf fmt " [ @< hov 2 [ >@< hov 2 > Class % a % a % a % a % a ] @ :@ % a :=@ [ @< v { > % a @ ] } . ] " @@ pp_coq_name cls . class_name ( pp_if_not_empty pp_print_space ) cls . class_typeclass_args ( pp_print_list ~ pp_sep : pp_print_space pp_print_string ) cls . class_typeclass_args ( pp_if_not_empty pp_print_space ) cls . class_args ( pp_print_list ~ pp_sep : pp_print_space ( fun fmt ( n , t ) -> fprintf fmt " ( % s : % a ) " n pp_type_repr t ) ) cls . class_args pp_type_repr cls . class_type ( pp_print_list ~ pp_sep ( : fun fmt _ -> fprintf fmt " @ ; " ) ( fun fmt ( m , t ) -> fprintf fmt " % a : % a " pp_coq_name m pp_type_repr t ) ) cls . class_members
type instance = { instance_name : coq_name ; instance_typeclass_args : string list ; instance_type : Repr . type_repr ; instance_members : ( coq_name * string ) list ; }
let pp_instance fmt inst = fprintf fmt " [ @< hov 2 > Instance % a % a @ : % a :=@ [ @< v { > % a @ } . ] ] " @@ pp_coq_name inst . instance_name ( pp_list ~ pp_prefix ( : fun fmt _ -> pp_print_string fmt " ` { " ) ~ pp_suffix ( : fun fmt _ -> pp_print_string fmt " } " ) ~ pp_sep ( : fun fmt _ -> pp_print_string fmt " , " ) pp_print_string ) inst . instance_typeclass_args pp_type_repr inst . instance_type ( pp_print_list ~ pp_sep ( : fun fmt _ -> fprintf fmt " @ ; " ) ( fun fmt ( m , v ) -> fprintf fmt " % a := % s " pp_coq_name m v ) ) inst . instance_members
type axiom = { axiom_name : coq_name ; axiom_typeclass_args : string list ; axiom_type : Repr . type_repr ; }
let pp_axiom fmt ax = fprintf fmt " [ @< hov 2 > Axiom % a @ : % a % a . ] " @ pp_coq_name ax . axiom_name ( pp_list ~ pp_prefix ( : fun fmt _ -> pp_print_text fmt " forall ` { " ) ~ pp_suffix ( : fun fmt _ -> pp_print_text fmt " } , " ) ~ pp_sep ( : fun fmt _ -> pp_print_text fmt " , @ " ) pp_print_text ) ax . axiom_typeclass_args pp_type_repr ax . axiom_type
type extract_constant = { constant_qualid : coq_name ; constant_type_vars : string list ; constant_target : string ; }
let pp_extract_constant fmt extr = let print_args_prod fmt = function | [ ] -> ( ) | [ x ] -> fprintf fmt " ' % s " x | args -> fprintf fmt " ( % a ) " ( pp_print_list ~ pp_sep ( : fun fmt _ -> pp_print_text fmt " , " ) ( fun fmt -> fprintf fmt " ' % s " ) ) args in fprintf fmt " [ @< hov 2 > Extract Constant % a % a @ => " \% a % s " . ] " \@ pp_coq_name extr . constant_qualid ( pp_list ~ pp_prefix ( : fun fmt _ -> pp_print_char fmt ' ' ) ~ pp_sep ( : fun fmt _ -> pp_print_string fmt " " ) ( fun fmt -> fprintf fmt " " ' \% s " " ) ) \ extr . constant_type_vars print_args_prod extr . constant_type_vars extr . constant_target
type extract_inductive = { inductive_qualid : coq_name ; inductive_target : string ; inductive_variants_target : string list ; }
let pp_extract_inductive fmt ind = fprintf fmt " [ @< hov 2 > Extract Inductive % a =>@ " \% s " \@ [ [ @< hov 2 >% a ] ] . ] " @@ pp_coq_name ind . inductive_qualid ind . inductive_target ( pp_list ~ pp_sep : pp_print_space ~ pp_prefix : pp_print_space ~ pp_suffix : pp_print_space ( fun fmt t -> fprintf fmt " " \% s " " \ t ) ) ind . inductive_variants_target
type coq_module = { coqmod_name : coq_name ; coqmod_content : t } | Section of string | Subsection of string | Comment of string | Block of t Lazylist . t | CompactedBlock of t Lazylist . t | CoqModule of coq_module | ConfigPrologue | FromRequireImport of from_require_import | FromRequireExport of from_require_export | Require of require | Definition of definition | Inductive of inductive list | Typeclass of typeclass | Instance of instance | Record of record | Axiom of axiom | ExtractConstant of extract_constant | ExtractInductive of extract_inductive
let exn_t = TParam ( CName " exn " , [ ] )
let monadic_may_raise_t may_raise t = if may_raise then map_codomain ( function | TParam ( m , [ t ] ) -> TParam ( m , [ TParam ( CName " sum " , [ t ; exn_t ] ) ] ) | _ -> assert false ) t else t
let may_raise_t may_raise t = if may_raise then map_codomain ( fun t -> TParam ( CName " sum " , [ t ; exn_t ] ) ) t else t
let rec pp_vernac fmt = function | Block l -> fprintf fmt " [ @< v >% a ] " @ ( pp_print_lazylist ~ pp_sep ( : fun fmt _ -> fprintf fmt " @ @ " ) pp_vernac ) l | CompactedBlock l -> fprintf fmt " [ @< v >% a ] " @ ( pp_print_lazylist ~ pp_sep : pp_print_space pp_vernac ) l | Comment str -> fprintf fmt " " str | Section str -> fprintf fmt " " str | Subsection str -> fprintf fmt " " str | ConfigPrologue -> pp_print_list ~ pp_sep : pp_print_space pp_print_string fmt [ " Set Implicit Arguments . " ; " Unset Strict Implicit . " ; " Set Contextual Implicit . " ; " Generalizable All Variables . " ; " Close Scope nat_scope . " ; ] | CoqModule m -> pp_module fmt m | FromRequireImport fri -> pp_from_require_import fmt fri | FromRequireExport fre -> pp_from_require_export fmt fre | Require req -> pp_require fmt req | Definition def -> pp_definition fmt def | Inductive ind -> pp_inductive fmt ind | Record rc -> pp_record fmt rc | Typeclass cls -> pp_typeclass fmt cls | Instance inst -> pp_instance fmt inst | Axiom ax -> pp_axiom fmt ax | ExtractConstant extr -> pp_extract_constant fmt extr | ExtractInductive ind -> pp_extract_inductive fmt ind fprintf fmt " [ @< v [ >@< v 2 > Module % a . @ % a ] @@ End % a . ] " @ pp_coq_name m . coqmod_name pp_vernac m . coqmod_content pp_coq_name m . coqmod_name
let block_of_list l = Block ( of_list l )
let compacted_block_of_list l = CompactedBlock ( of_list l )
let empty = function [ ] -> true | _ -> false
let ( @? ) cond f = if cond then f else fun x -> x
let proto_vars proto = List . map ( fun ( l , _ ) -> asprintf " % a " pp_arg_name l ) proto . prototype_args
let call_vars proto = List . map ( fun ( l , _ ) -> asprintf " % a " pp_arg_call l ) proto . prototype_args
let instance_member_body proto name = let tvars = proto . prototype_type_args in let vars = proto_vars proto in if 0 < List . length tvars then asprintf " [ @< h 2 > fun % a % a @ => % a % a ] " @ ( pp_list ~ pp_prefix ( : fun fmt _ -> pp_print_text fmt " " ) ~ pp_sep ( : fun fmt _ -> pp_print_text fmt " " ) ( fun fmt _ -> pp_print_text fmt " _ " ) ) tvars ( pp_list ~ pp_prefix ( : fun fmt _ -> pp_print_text fmt " " ) ~ pp_sep ( : fun fmt _ -> pp_print_text fmt " " ) pp_print_text ) vars pp_coq_name name ( pp_list ~ pp_prefix ( : fun fmt _ -> pp_print_text fmt " " ) ~ pp_sep ( : fun fmt _ -> pp_print_text fmt " " ) pp_print_text ) vars else of_coq_name name
let requires_vernac features models = let requires_freespec = Lazylist . push ( FromRequireImport { import_from = " FreeSpec . Core " ; import_module = " Core " } ) in let requires_io = Lazylist . push ( FromRequireImport { import_from = " SimpleIO " ; import_module = " IO_Monad " } ) in let requires_interface = Lazylist . push ( FromRequireImport { import_from = " CoqFFI " ; import_module = " Interface " } ) in Lazylist . push @@ CompactedBlock ( singleton ( FromRequireExport { export_from = " CoqFFI " ; export_module = " Extraction " } ) |> is_enabled features SimpleIO @? requires_io |> is_enabled features Interface @? requires_interface |> is_enabled features FreeSpec @? requires_freespec |++ List . map ( fun x -> Require { require_module = x } ) models )
let functions_vernac ~ rev_namespace conflicts m = let to_def f = let func_type = may_raise_t f . func_may_raise f . func_type in let func_name = Conflict . get_coq_value rev_namespace conflicts ~ value : f . func_name in match f . func_model with | Some model -> Definition { def_name = func_name ; def_typeclass_args = [ ] ; def_prototype = { prototype_type_args = [ ] ; prototype_args = [ ] ; prototype_ret_type = func_type ; } ; def_body = ( fun fmt _ -> pp_print_string fmt model ) ; } | _ -> Axiom { axiom_name = func_name ; axiom_typeclass_args = [ ] ; axiom_type = func_type ; } in let to_extr f = let target = qualified_name m ( Conflict . get_ocaml_value rev_namespace conflicts ~ value : f . func_name ) in let func_name = Conflict . get_coq_value rev_namespace conflicts ~ value : f . func_name in let func_extract f = if f . func_may_raise then let proto = type_repr_to_prototype_repr f . func_type in let pargs = proto_vars proto in let cargs = call_vars proto in asprintf " ( fun % a -> % a ) " ( pp_print_list ~ pp_sep ( : fun fmt _ -> pp_print_string fmt " " ) pp_print_string ) pargs ( pp_try_with ( pp_fun_call target cargs ) ) ( ) else if has_labelled_arg f . func_type then let proto = type_repr_to_prototype_repr f . func_type in let pargs = proto_vars proto in let cargs = call_vars proto in asprintf " ( fun % a -> % a ) " ( pp_print_list ~ pp_sep ( : fun fmt _ -> pp_print_string fmt " " ) pp_print_string ) pargs ( pp_fun_call ~ paren : false target cargs ) ( ) else asprintf " % a " pp_ocaml_name target in ExtractConstant { constant_qualid = func_name ; constant_type_vars = [ ] ; constant_target = func_extract f ; } in Lazylist . push_list [ Section " Pure functions " ; compacted_block_of_list @@ List . map to_def m . mod_functions ; compacted_block_of_list @@ List . map to_extr m . mod_functions ; ]
let variant_entry_to_constructor ~ rev_namespace conflicts owner v = { constructor_name = Conflict . get_coq_constructor rev_namespace conflicts ~ owner ~ cstr : v . variant_name ; constructor_prototype = v . variant_prototype ; }
let field_entry_to_field ~ rev_namespace owner conflicts ( r : field_entry ) = let field_name = Conflict . get_coq_field rev_namespace conflicts ~ owner ~ field : r . field_name in { field_name ; field_type = r . field_type }
let ind_type = let rec aux pos arity = if arity == 0 then type_sort_mono else TLambda { argtype = { position = pos ; kind = PositionedArg } ; domain = type_sort_mono ; codomain = aux ( pos + 1 ) ( arity - 1 ) ; } in aux 0
let type_entry_to_vernac ~ rev_namespace conflicts features t = let transparent = is_enabled features TransparentTypes in let type_name = Conflict . get_coq_type rev_namespace conflicts ~ ty : t . type_name in match ( t . type_value , t . type_model , transparent ) with | Variant l , None , true -> Inductive [ { inductive_name = type_name ; inductive_type_args = t . type_params ; inductive_constructors = List . map ( variant_entry_to_constructor ~ rev_namespace conflicts t . type_name ) l ; inductive_type = TMono ( ind_type t . type_arity ) ; } ; ] | Entry . Record r , None , true -> Record { record_name = type_name ; record_type_args = t . type_params ; record_fields = List . map ( field_entry_to_field ~ rev_namespace t . type_name conflicts ) r ; } | Alias mono , None , true -> Definition { def_name = type_name ; def_typeclass_args = [ ] ; def_prototype = { prototype_type_args = t . type_params ; prototype_args = [ ] ; prototype_ret_type = of_mono_type_repr [ ] ( ind_type t . type_arity ) ; } ; def_body = ( fun fmt _ -> pp_mono_type_repr fmt mono ) ; } | _ , Some m , _ -> Definition { def_name = type_name ; def_typeclass_args = [ ] ; def_prototype = { prototype_type_args = [ ] ; prototype_args = [ ] ; prototype_ret_type = of_mono_type_repr t . type_params ( ind_type t . type_arity ) ; } ; def_body = ( fun fmt _ -> pp_print_string fmt m ) ; } | _ -> Axiom { axiom_typeclass_args = [ ] ; axiom_name = type_name ; axiom_type = of_mono_type_repr t . type_params ( ind_type t . type_arity ) ; }
let io_primitives_vernac ~ rev_namespace conflicts m = let io_axiom_name name = Conflict . get_coq_helper rev_namespace conflicts name Name . io_helper in let to_axiom prim = let axiom_type = type_lift " IO " ( may_raise_t prim . prim_may_raise prim . prim_type ) in let axiom_name = io_axiom_name prim . prim_name in Axiom { axiom_typeclass_args = [ ] ; axiom_name ; axiom_type } in let to_extract_constant prim = let axiom_name = io_axiom_name prim . prim_name in let ocaml_name = Conflict . get_ocaml_value rev_namespace conflicts ~ value : prim . prim_name in let proto = type_repr_to_prototype_repr prim . prim_type in let pargs = proto_vars proto in let cargs = call_vars proto in let pp_call = pp_fun_call ( qualified_name m ocaml_name ) cargs in let body = asprintf " ( % a ) " ( if prim . prim_may_raise then pp_try_with pp_call else pp_call ) ( ) in ExtractConstant { constant_qualid = axiom_name ; constant_type_vars = [ ] ; constant_target = asprintf " [ @< h ( > fun % a k__ -> k__ % s ) ] " @ ( pp_list ~ pp_prefix ( : fun fmt _ -> pp_print_string fmt " " ) ~ pp_sep : pp_print_space pp_print_string ) pargs body ; } in let instance_vernac = let to_member prim = let proto = type_repr_to_prototype_repr prim . prim_type in let axiom_name = io_axiom_name prim . prim_name in let member_body = instance_member_body proto axiom_name in let member = Conflict . get_coq_value rev_namespace conflicts ~ value : prim . prim_name in ( member , member_body ) in let instance_name = Conflict . get_coq_helper rev_namespace conflicts m . mod_name Name . io_instance in Instance { instance_name ; instance_typeclass_args = [ ] ; instance_type = TMono ( TParam ( CName ( sprintf " Monad % s " m . mod_name ) , [ TParam ( CName " IO " , [ ] ) ] ) ) ; instance_members = List . map to_member m . mod_primitives ; } in Lazylist . push_list [ Subsection " [ IO ] Instance " ; compacted_block_of_list @@ List . map to_axiom m . mod_primitives ; compacted_block_of_list @@ List . map to_extract_constant m . mod_primitives ; instance_vernac ; ]
let interface_constructor_name rev_namespace conflicts name = Conflict . get_coq_helper rev_namespace conflicts ~ owner ( : List . hd rev_namespace ) name Name . interface_cstr
let interface_vernac ~ rev_namespace conflicts mod_name interface_name class_name instance_name all_prims vernacs = let interface_name = Conflict . get_coq_helper rev_namespace conflicts ~ owner : mod_name mod_name interface_name in let hof ( i , p ) = Repr . higher_order_monadic ( CPlaceholder i ) p . prim_type in let hoprims , prims = List . partition hof all_prims in let prim_to_constructor ( i , prim ) = let name = interface_constructor_name rev_namespace conflicts prim . prim_name in let prim_type = Repr . fill_placeholder i ( of_coq_name interface_name ) ( monadic_may_raise_t prim . prim_may_raise prim . prim_type ) in { constructor_name = name ; constructor_prototype = { prototype_type_args = [ ] ; prototype_args = [ ] ; prototype_ret_type = prim_type ; } ; } in let prim_to_inj_helper ( i , prim ) = let owner = prim . prim_name in let def_name = Conflict . get_coq_helper rev_namespace conflicts owner Name . inject_helper in let cstr = interface_constructor_name rev_namespace conflicts prim . prim_name in let inj_type = monadic_may_raise_t prim . prim_may_raise prim . prim_type in let proto = Repr . type_repr_to_prototype_repr ( Repr . fill_placeholder i " m " inj_type ) in Definition { def_name ; def_typeclass_args = [ asprintf " Inject % a m " pp_coq_name interface_name ] ; def_prototype = proto ; def_body = ( fun fmt _ -> fprintf fmt " inject ( % a % a ) " pp_coq_name cstr ( pp_print_list ~ pp_sep : pp_print_space pp_print_string ) ( proto_vars proto ) ) ; } in let hof_prim_to_inj ( i , prim ) = let owner = prim . prim_name in let axiom_name = Conflict . get_coq_helper rev_namespace conflicts owner Name . inject_helper in let inj_type = Repr . fill_placeholder i " m " @@ monadic_may_raise_t prim . prim_may_raise prim . prim_type in Axiom { axiom_name ; axiom_typeclass_args = [ asprintf " Inject % a m " pp_coq_name interface_name ] ; axiom_type = inj_type ; } in let inj_instance = let monad_name = of_coq_name ( Conflict . get_coq_helper rev_namespace conflicts mod_name class_name ) in let to_member ( _ , prim ) = let member = Conflict . get_coq_value rev_namespace conflicts ~ value : prim . prim_name in let proto = type_repr_to_prototype_repr prim . prim_type in let helper = Conflict . get_coq_helper rev_namespace conflicts prim . prim_name Name . inject_helper in let member_body = instance_member_body proto helper in ( member , member_body ) in let instance_name = Conflict . get_coq_helper rev_namespace conflicts mod_name instance_name in Instance { instance_name ; instance_typeclass_args = [ asprintf " Inject % a m " pp_coq_name interface_name ] ; instance_type = TMono ( TParam ( CName monad_name , [ TParam ( CName " m " , [ ] ) ] ) ) ; instance_members = List . map to_member all_prims ; } in let mod_inductive = Inductive [ { inductive_name = interface_name ; inductive_type_args = [ ] ; inductive_constructors = List . map prim_to_constructor prims ; inductive_type = TMono ( tlambda [ type_sort_mono ] type_sort_mono ) ; } ; ] in vernacs |++ [ Subsection " Interface datatype " ; mod_inductive ] |++ List . map prim_to_inj_helper prims |++ List . map hof_prim_to_inj hoprims |+ inj_instance
let lwt_primitives_vernac ~ rev_namespace conflicts lwt_module m vernacs = let to_lwt t = TParam ( CName " Lwt . t " , [ t ] ) in let axiom_name prim = let owner = prim . prim_name in Conflict . get_coq_helper rev_namespace conflicts owner Name . lwt_sync_helper in let to_axiom prim = Axiom { axiom_typeclass_args = [ ] ; axiom_name = axiom_name prim ; axiom_type = map_codomain to_lwt prim . prim_type ; } in let axioms = List . map to_axiom m . mod_primitives in let to_extract_target prim = let proto = type_repr_to_prototype_repr prim . prim_type in let pvars = proto_vars proto in let cvars = call_vars proto in let target = Conflict . get_ocaml_value rev_namespace conflicts ~ value : prim . prim_name in asprintf " ( fun % a -> % a % a ) " ( pp_list ~ pp_sep ( : fun fmt _ -> pp_print_string fmt " " ) pp_print_string ) pvars pp_lwt_return lwt_module ( pp_fun_call ( qualified_name m target ) cvars ) ( ) in let to_extract prim = ExtractConstant { constant_qualid = axiom_name prim ; constant_type_vars = [ ] ; constant_target = to_extract_target prim ; } in let extracts = List . map to_extract m . mod_primitives in let to_member prim = let proto = type_repr_to_prototype_repr prim . prim_type in let member_body = instance_member_body proto ( axiom_name prim ) in let member = Conflict . get_coq_value rev_namespace conflicts ~ value : prim . prim_name in ( member , member_body ) in let instance_name = let owner = m . mod_name in Conflict . get_coq_helper rev_namespace conflicts owner Name . lwt_sync_instance in let monad_name = let owner = m . mod_name in Conflict . get_coq_helper rev_namespace conflicts owner Name . prim_monad in let instance = Instance { instance_name ; instance_typeclass_args = [ ] ; instance_type = TMono ( TParam ( CName ( of_coq_name monad_name ) , [ TParam ( CName " Lwt . t " , [ ] ) ] ) ) ; instance_members = List . map to_member m . mod_primitives ; } in vernacs |++ [ Subsection " [ Lwt . t ] Instance " ; compacted_block_of_list axioms ; compacted_block_of_list extracts ; instance ; ]
let semantics_vernac ~ rev_namespace conflicts m vernacs = let axiom_name owner = Conflict . get_coq_helper rev_namespace conflicts owner Name . semantics_helper in let target_name value = Conflict . get_ocaml_value rev_namespace conflicts ~ value in let mod_name = String . uppercase_ascii m . mod_name in let mod_type = TParam ( CName mod_name , [ ] ) in let prim_target prim = let target_name = qualified_name m ( target_name prim . prim_name ) in if prim . prim_may_raise then let proto = type_repr_to_prototype_repr prim . prim_type in let args = call_vars proto in asprintf " ( fun % a -> % a ) " ( pp_list ~ pp_sep ( : fun fmt _ -> pp_print_string fmt " " ) pp_print_string ) args ( pp_try_with ( pp_fun_call target_name args ) ) ( ) else of_ocaml_name target_name in let semantics_name = Conflict . get_coq_helper rev_namespace conflicts m . mod_name Name . semantics in vernacs |++ [ Subsection " FreeSpec Semantics " ; compacted_block_of_list @@ List . map ( fun prim -> Axiom { axiom_typeclass_args = [ ] ; axiom_name = axiom_name prim . prim_name ; axiom_type = may_raise_t prim . prim_may_raise prim . prim_type ; } ) m . mod_primitives ; compacted_block_of_list @@ List . map ( fun prim -> ExtractConstant { constant_qualid = axiom_name prim . prim_name ; constant_type_vars = [ ] ; constant_target = prim_target prim ; } ) m . mod_primitives ; Definition { def_name = semantics_name ; def_typeclass_args = [ ] ; def_prototype = { prototype_type_args = [ ] ; prototype_args = [ ] ; prototype_ret_type = TMono ( TParam ( CName " semantics " , [ mod_type ] ) ) ; } ; def_body = ( fun fmt _ -> fprintf fmt " [ @< v 2 > bootstrap ( fun a e =>@ local [ @< v > match e in % s a \ return a with @ % a @ end ] ) ] " @@ mod_name ( pp_print_list ~ pp_sep : pp_print_space ( fun fmt prim -> let proto = type_repr_to_prototype_repr prim . prim_type in let args = proto_vars proto in fprintf fmt " [ @< hov 2 >| [ @< h >% a % a ] @@ => [ @< h >% a % a ] ] " @@ pp_coq_name ( interface_constructor_name rev_namespace conflicts prim . prim_name ) ( pp_print_list ~ pp_sep : pp_print_space pp_print_string ) args pp_coq_name ( axiom_name prim . prim_name ) ( pp_print_list ~ pp_sep : pp_print_space pp_print_string ) args ) ) m . mod_primitives ) ; } ; ]
let exceptions_vernac ~ rev_namespace conflicts m vernacs = let exception_vernac v e = let exn_name = e . exception_name in let exn_type = TParam ( CName " exn " , [ ] ) in let proxy_name = Conflict . get_coq_helper rev_namespace conflicts exn_name Name . exn_proxy_type in let proxy_constructor_name = Conflict . get_coq_helper rev_namespace conflicts exn_name Name . exn_proxy_cstr in let proxy_type = TParam ( CName ( of_coq_name proxy_name ) , [ ] ) in let proxy_proto = { e . exception_prototype with prototype_ret_type = TMono proxy_type } in let proxy_inductive = Inductive [ { inductive_name = proxy_name ; inductive_type_args = [ ] ; inductive_type = type_sort ; inductive_constructors = [ { constructor_name = proxy_constructor_name ; constructor_prototype = proxy_proto ; } ; ] ; } ; ] in let to_exn = Conflict . get_coq_helper rev_namespace conflicts exn_name Name . to_exn in let of_exn = Conflict . get_coq_helper rev_namespace conflicts exn_name Name . of_exn in let to_exn_axiom = Axiom { axiom_typeclass_args = [ ] ; axiom_name = to_exn ; axiom_type = TMono ( TLambda { argtype = { position = 0 ; kind = PositionedArg } ; domain = proxy_type ; codomain = exn_type ; } ) ; } in let of_exn_axiom = Axiom { axiom_typeclass_args = [ ] ; axiom_name = of_exn ; axiom_type = TMono ( TLambda { argtype = { position = 0 ; kind = PositionedArg } ; domain = exn_type ; codomain = TParam ( CName " option " , [ proxy_type ] ) ; } ) ; } in let vars = proto_vars proxy_proto in let enclose = function _ :: _ :: _ -> true | _ -> false in let to_exn_extract = ExtractConstant { constant_qualid = to_exn ; constant_type_vars = [ ] ; constant_target = asprintf " [ @< h ( > function | % a % a -> % s % a ) ] " @ pp_coq_name proxy_constructor_name ( pp_list ~ pp_prefix : pp_print_space ~ pp_sep : pp_print_space pp_print_string ) vars exn_name ( pp_list ~ enclose ~ pp_prefix ( : fun fmt _ -> pp_print_string fmt " ( " ) ~ pp_sep ( : fun fmt _ -> pp_print_string fmt " , " ) ~ pp_suffix ( : fun fmt _ -> pp_print_string fmt " ) " ) pp_print_string ) vars ; } in let of_exn_extract = ExtractConstant { constant_qualid = of_exn ; constant_type_vars = [ ] ; constant_target = asprintf " [ @< h ( > function | % s % a -> Some ( % a % a ) | _ -> None ) ] " @ exn_name ( pp_list ~ enclose ~ pp_prefix ( : fun fmt _ -> pp_print_string fmt " ( " ) ~ pp_sep ( : fun fmt _ -> pp_print_string fmt " , " ) ~ pp_suffix ( : fun fmt _ -> pp_print_string fmt " ) " ) pp_print_string ) vars pp_coq_name proxy_constructor_name ( pp_list ~ pp_prefix : pp_print_space ~ pp_sep : pp_print_space pp_print_string ) vars ; } in let instance_name = Conflict . get_coq_helper rev_namespace conflicts exn_name Name . exn_instance in let exn_instance = Instance { instance_name ; instance_typeclass_args = [ ] ; instance_type = TMono ( TParam ( CName " Exn " , [ TParam ( CName ( of_coq_name proxy_name ) , [ ] ) ] ) ) ; instance_members = [ ( unsafe_coq_name " to_exn " , of_coq_name to_exn ) ; ( unsafe_coq_name " of_exn " , of_coq_name of_exn ) ; ] ; } in v |++ [ Subsection ( sprintf " [ % s ] " exn_name ) ; proxy_inductive ; compacted_block_of_list [ to_exn_axiom ; of_exn_axiom ] ; compacted_block_of_list [ to_exn_extract ; of_exn_extract ] ; exn_instance ; ] in vernacs |+ Section " OCaml Exceptions " |+ Block ( List . fold_left exception_vernac ( of_list [ ] ) m . mod_exceptions )
let monad_vernac ~ rev_namespace conflicts mod_name prims = let prim_to_members prim = let prim_type = may_raise_t prim . prim_may_raise prim . prim_type in let prim_name = Conflict . get_coq_value rev_namespace conflicts ~ value : prim . prim_name in ( prim_name , type_lift " m " prim_type ) in let class_name = Conflict . get_coq_helper rev_namespace conflicts mod_name Name . prim_monad in Typeclass { class_name ; class_typeclass_args = [ ] ; class_args = [ ( " m " , TMono ( tlambda [ type_sort_mono ] type_sort_mono ) ) ] ; class_type = type_sort ; class_members = List . map prim_to_members prims ; }
let primitives_vernac ~ rev_namespace conflicts lwt_module features m vernacs = let prims = List . map ( fun p -> let t = p . prim_type in let i = Repr . fresh_placeholder t in let p = { p with prim_type = Repr . map_codomain ( fun x -> TParam ( CPlaceholder i , [ x ] ) ) t ; } in ( i , p ) ) m . mod_primitives in vernacs |++ [ Section " Impure Primitives " ; Subsection " Monad Definition " ; monad_vernac ~ rev_namespace conflicts m . mod_name m . mod_primitives ; ] |> is_enabled features SimpleIO @? io_primitives_vernac ~ rev_namespace conflicts m |> is_enabled features Interface @? interface_vernac ~ rev_namespace conflicts m . mod_name Name . interface_type Name . prim_monad Name . inject_instance prims |> is_enabled features FreeSpec @? semantics_vernac ~ rev_namespace conflicts m |> is_enabled features Lwt @? lwt_primitives_vernac ~ rev_namespace conflicts lwt_module m
let lwt_vernac ~ rev_namespace conflicts features m vernacs = let lwt_t = " Lwt . t " in let to_prim lwt = ( lwt . lwt_placeholder , { prim_name = lwt . lwt_name ; prim_type = lwt . lwt_type ; prim_may_raise = false ; prim_loc = lwt . lwt_loc ; } ) in let prims = List . map to_prim m . mod_lwt in let axiom_name lwt = let owner = lwt . lwt_name in Conflict . get_coq_helper rev_namespace conflicts owner Name . lwt_async_helper in let lwt_mono_t arg = TParam ( CName lwt_t , arg ) in let to_axiom lwt = Axiom { axiom_typeclass_args = [ ] ; axiom_name = axiom_name lwt ; axiom_type = Repr . fill_placeholder lwt . lwt_placeholder lwt_t lwt . lwt_type ; } in let axioms = List . map to_axiom m . mod_lwt in let member lwt = Conflict . get_coq_value rev_namespace conflicts ~ value : lwt . lwt_name in let to_member lwt = let member_body = instance_member_body ( type_repr_to_prototype_repr lwt . lwt_type ) ( axiom_name lwt ) in ( member lwt , member_body ) in let lwt_to_members lwt = ( member lwt , Repr . fill_placeholder lwt . lwt_placeholder " m " lwt . lwt_type ) in let class_name = Conflict . get_coq_helper rev_namespace conflicts m . mod_name Name . async_monad in let tclass = Typeclass { class_name ; class_typeclass_args = [ ] ; class_args = [ ( " m " , TMono ( tlambda [ type_sort_mono ] type_sort_mono ) ) ] ; class_type = type_sort ; class_members = List . map lwt_to_members m . mod_lwt ; } in let instance_name = Conflict . get_coq_helper rev_namespace conflicts m . mod_name Name . lwt_async_instance in let instance = Instance { instance_name ; instance_typeclass_args = [ ] ; instance_type = TMono ( TParam ( CName ( of_coq_name class_name ) , [ lwt_mono_t [ ] ] ) ) ; instance_members = List . map to_member m . mod_lwt ; } in let target_name lwt = qualified_name m ( Conflict . get_ocaml_value rev_namespace conflicts ~ value : lwt . lwt_name ) in let to_extract lwt = let constant_target = if has_labelled_arg lwt . lwt_type then let proto = type_repr_to_prototype_repr lwt . lwt_type in let pargs = proto_vars proto in let cargs = call_vars proto in asprintf " ( fun % a -> % a ) " ( pp_print_list ~ pp_sep ( : fun fmt _ -> pp_print_string fmt " " ) pp_print_string ) pargs ( pp_fun_call ~ paren : false ( target_name lwt ) cargs ) ( ) else of_ocaml_name @@ target_name lwt in ExtractConstant { constant_qualid = axiom_name lwt ; constant_type_vars = [ ] ; constant_target ; } in let extracts = List . map to_extract m . mod_lwt in vernacs |++ [ Section " Asynchronous Primitives " ; Subsection " Monad Definition " ; tclass ; Subsection " [ Lwt . t ] Instance " ; compacted_block_of_list axioms ; compacted_block_of_list extracts ; instance ; ] |> is_enabled features Interface @? interface_vernac ~ rev_namespace conflicts m . mod_name Name . async_interface_type Name . async_monad Name . async_inject_instance prims
let rec module_vernac ~ rev_namespace conflicts lwt_module features models m vernac = let rev_namespace = m . mod_name :: rev_namespace in vernac |> ( not ( empty m . mod_intro ) ) @? intros_vernac ~ rev_namespace conflicts lwt_module features models m |> ( not ( empty m . mod_exceptions ) ) @? exceptions_vernac ~ rev_namespace conflicts m |> ( not ( empty m . mod_functions ) ) @? functions_vernac ~ rev_namespace conflicts m |> ( not ( empty m . mod_primitives ) ) @? primitives_vernac ~ rev_namespace conflicts lwt_module features m |> ( not ( empty m . mod_lwt ) ) @? lwt_vernac ~ rev_namespace conflicts features m = let transparent = is_enabled features TransparentTypes in let type_entry_to_inductive t = let type_name = Conflict . get_coq_type rev_namespace conflicts ~ ty : t . type_name in match ( t . type_value , t . type_model , transparent ) with | Variant l , None , true -> { inductive_name = type_name ; inductive_type_args = t . type_params ; inductive_constructors = List . map ( variant_entry_to_constructor ~ rev_namespace conflicts t . type_name ) l ; inductive_type = TMono ( ind_type t . type_arity ) ; } | _ -> assert false in let mut_type_entries_to_ind t = [ Inductive ( List . map type_entry_to_inductive t ) ] in let type_entries_to_vernac = function | t :: _ as mut_types -> ( match ( t . type_value , t . type_model , transparent ) with | Variant _ , None , true -> mut_type_entries_to_ind mut_types | _ -> List . map ( type_entry_to_vernac ~ rev_namespace conflicts features ) mut_types ) | [ ] -> [ ] in let module_to_vernac ~ rev_namespace ( m : Mod . t ) = let coqmod_name = Conflict . get_coq_module conflicts ~ m : m . mod_name in [ CoqModule { coqmod_name ; coqmod_content = Block ( module_vernac ~ rev_namespace conflicts lwt_module features models m Lazylist . empty ) ; } ; ] in let intro_list_to_vernac ( l : intro list ) = List . flatten ( Mod . map_intro_list type_entries_to_vernac ( module_to_vernac ~ rev_namespace ) l ) in let to_extract = function | Right t -> Compat . concat_map ( fun t -> let inductive_qualid = Conflict . get_coq_type rev_namespace conflicts ~ ty : t . type_name in let inductive_target = of_ocaml_name ( qualified_name m ( Conflict . get_ocaml_type rev_namespace conflicts ~ ty : t . type_name ) ) in match ( t . type_value , t . type_model , transparent ) with | Variant l , None , true -> [ ( let variant_target v = of_ocaml_name ( qualified_name m ( Conflict . get_ocaml_constructor rev_namespace conflicts ~ owner : t . type_name ~ cstr : v . variant_name ) ) in ExtractInductive { inductive_qualid ; inductive_target ; inductive_variants_target = List . map variant_target l ; } ) ; ] | Record r , None , true -> let ns = String . concat " . " m . mod_namespace in ExtractInductive { inductive_qualid ; inductive_target ; inductive_variants_target = ( let tuple = asprintf " % a " ( pp_list ~ pp_sep ( : fun fmt _ -> fprintf fmt " , " ) ( fun fmt ( f : field_entry ) -> pp_print_text fmt f . field_name ) ) r in let fields = asprintf " % a " ( pp_list ~ pp_sep ( : fun fmt _ -> fprintf fmt " ; " ) ( fun fmt ( f : field_entry ) -> pp_print_text fmt f . field_name ) ) r in [ sprintf " % s . ( fun ( % s ) -> { % s } ) " ns tuple fields ] ) ; } :: List . map ( fun ( f : field_entry ) -> let constant_qualid = Conflict . get_coq_field rev_namespace conflicts ~ owner : t . type_name ~ field : f . field_name in ExtractConstant { constant_qualid ; constant_type_vars = [ ] ; constant_target = asprintf " % s . ( fun x -> x . % s ) " ns f . field_name ; } ) r | _ -> let type_params = fst ( pick_params t . type_arity ( make_params_pool t . type_params ) ) @ t . type_params in [ ExtractConstant { constant_qualid = inductive_qualid ; constant_type_vars = type_params ; constant_target = inductive_target ; } ; ] ) t | _ -> [ ] in vernacs |++ [ block_of_list @@ intro_list_to_vernac m . mod_intro ; compacted_block_of_list @@ Compat . concat_map to_extract m . mod_intro ; ]
let of_mod lwt_module features models conflicts m = Block ( of_list [ Comment " This file has been generated by coqffi . " ; ConfigPrologue ] |> requires_vernac features models |> module_vernac ~ rev_namespace [ ] : conflicts lwt_module features models m |+ Comment " The generated file ends here . " )
type choice ' a ' b = [ Left of ' a | Right of ' b ] ; match x with | Some x -> Some ( f x ) | None -> None end ; Some x -> x ] ; Left x -> x ] ; Left _ -> failwith ( " choice : " ^ symbol ) ] ; Sys . ocaml_version ; ( loc ) with Location . loc_ghost = True } ; let loc_at n lnum bolp = { Lexing . pos_fname = if lnum = - 1 then " " else fname ; Lexing . pos_lnum = lnum ; Lexing . pos_bol = bolp ; Lexing . pos_cnum = n } in { Location . loc_start = loc_at bp lnum bolp ; Location . loc_end = loc_at ep lnuml bolpl ; Location . loc_ghost = bp = 0 && ep = 0 } ; let loc = { Lexing . pos_fname = " _none_ " ; pos_lnum = 1 ; pos_bol = 0 ; pos_cnum = - 1 } in { Location . loc_start = loc ; Location . loc_end = loc ; Location . loc_ghost = True } ; { Location . txt = txt ; loc = loc } ; mkloc loc_none txt ; let mkli s = loop ( fun s -> Lident s ) where rec loop f = fun [ [ i :: il ] -> loop ( fun s -> Ldot ( f i ) s ) il | [ ] -> f s ] in match List . rev sl with [ [ ] -> None | [ s :: sl ] -> Some ( mkli s ( List . rev sl ) ) ] ; let rec not_extended = fun [ Lident _ -> True | Ldot li _ -> not_extended li | Lapply _ _ -> False ] in not_extended ; loop [ ] l where rec loop rev_l = fun [ [ x :: l ] -> match f x with [ Some s -> loop [ s :: rev_l ] l | None -> None ] | [ ] -> Some ( List . rev rev_l ) ] ; if lab = " " then Nolabel else if lab . [ 0 ] = ' ' ? then Optional ( String . sub lab 1 ( String . length lab - 1 ) ) else Labelled lab ; { pval_type = t ; pval_prim = p ; pval_loc = t . ptyp_loc ; pval_name = mkloc t . ptyp_loc vn ; pval_attributes = item_attributes } ; { pctf_desc = ctfd ; pctf_loc = loc ; pctf_attributes = item_attributes } ; { pcf_desc = cfd ; pcf_loc = loc ; pcf_attributes = item_attributes } ; IFDEF OCAML_VERSION < OCAML_4_08_0 THEN do { assert ( alg_attributes = [ ] ) ; { ptyp_desc = x ; ptyp_loc = loc ; ptyp_attributes = [ ] } } ELSE { ptyp_desc = x ; ptyp_loc = loc ; ptyp_loc_stack = [ ] ; ptyp_attributes = alg_attributes } END ; IFDEF OCAML_VERSION < OCAML_4_08_0 THEN { ppat_desc = x ; ppat_loc = loc ; ppat_attributes = [ ] } ELSE { ppat_desc = x ; ppat_loc = loc ; ppat_loc_stack = [ ] ; ppat_attributes = [ ] } END ; ELSE Parsetree . ( { attr_name = mkloc nameloc name ; attr_payload = PStr sl ; attr_loc = loc } ) ; Parsetree . ( { attr_name = mkloc nameloc name ; attr_payload = PSig si ; attr_loc = loc } ) ; Parsetree . ( { attr_name = mkloc nameloc name ; attr_payload = PTyp ty ; attr_loc = loc } ) ; Parsetree . ( { attr_name = mkloc nameloc name ; attr_payload = PPat p eopt ; attr_loc = loc } ) ; pexp_desc = pexp_desc ; pexp_loc = pexp_loc ; pexp_loc_stack = pexp_loc_stack ; pexp_attributes = pexp_attributes } = { pexp_desc = pexp_desc ; pexp_loc = pexp_loc ; pexp_loc_stack = pexp_loc_stack ; pexp_attributes = pexp_attributes @ [ attr ] } ; ptyp_desc = ptyp_desc ; ptyp_loc = ptyp_loc ; ptyp_loc_stack = ptyp_loc_stack ; ptyp_attributes = ptyp_attributes } = { ptyp_desc = ptyp_desc ; ptyp_loc = ptyp_loc ; ptyp_loc_stack = ptyp_loc_stack ; ptyp_attributes = ptyp_attributes @ [ attr ] } ; ppat_desc = ppat_desc ; ppat_loc = ppat_loc ; ppat_loc_stack = ppat_loc_stack ; ppat_attributes = ppat_attributes } = { ppat_desc = ppat_desc ; ppat_loc = ppat_loc ; ppat_loc_stack = ppat_loc_stack ; ppat_attributes = ppat_attributes @ [ attr ] } ; pmty_desc = pmty_desc ; pmty_loc = pmty_loc ; pmty_attributes = pmty_attributes } = { pmty_desc = pmty_desc ; pmty_loc = pmty_loc ; pmty_attributes = pmty_attributes @ [ attr ] } ; pmod_desc = module_expr_desc ; pmod_loc = pmod_loc ; pmod_attributes = pmod_attributes } = { pmod_desc = module_expr_desc ; pmod_loc = pmod_loc ; pmod_attributes = pmod_attributes @ [ attr ] } ; pcty_desc = pcty_desc ; pcty_loc = pcty_loc ; pcty_attributes = pcty_attributes } = { pcty_desc = pcty_desc ; pcty_loc = pcty_loc ; pcty_attributes = pcty_attributes @ [ attr ] } ; pcl_desc = pcl_desc ; pcl_loc = pcl_loc ; pcl_attributes = pcl_attributes } = { pcl_desc = pcl_desc ; pcl_loc = pcl_loc ; pcl_attributes = pcl_attributes @ attrs } ; IFDEF OCAML_VERSION < OCAML_4_14_0 THEN { pext_name = mkloc loc s ; pext_kind = Pext_decl ( Pcstr_tuple ed ) None ; pext_loc = loc ; pext_attributes = alg_attributes } ELSE { pext_name = mkloc loc s ; pext_kind = Pext_decl [ ] ( Pcstr_tuple ed ) None ; pext_loc = loc ; pext_attributes = alg_attributes } END ; Pexp_letexception exdef body ; Ppat_exception p ; END ; IFDEF OCAML_VERSION < OCAML_4_08_0 THEN { pexp_desc = x ; pexp_loc = loc ; pexp_attributes = [ ] } ELSE { pexp_desc = x ; pexp_loc = loc ; pexp_loc_stack = [ ] ; pexp_attributes = [ ] } END ; { pmty_desc = x ; pmty_loc = loc ; pmty_attributes = [ ] } ; { pmod_desc = x ; pmod_loc = loc ; pmod_attributes = [ ] } ; ; ELSE [ { pof_desc = Oinherit x ; pof_loc = loc ; pof_attributes = alg_attributes } :: fl ] ; END ; IFDEF OCAML_VERSION < OCAML_4_08_0 THEN do { assert ( alg_attributes = [ ] ) ; [ ( lab , x ) :: fl ] } ELSE [ { pof_desc = Otag ( mkloc loc lab ) x ; pof_loc = loc ; pof_attributes = alg_attributes } :: fl ] END ; [ ] ; assert ( inj = False ) ; let va = match va with [ Some False -> Contravariant | Some True -> Covariant | _ -> Invariant ] in va } ; ELSE let va = match va with [ Some False -> Contravariant | Some True -> Covariant | _ -> NoVariance ] in let inj = match inj with [ True -> Injective | False -> NoInjectivity ] in ( va , inj ) ; END ; do { assert ( [ ] = tyvars ) ; { pext_name = mkloc loc s ; pext_kind = Pext_decl ( Pcstr_tuple x ) rto ; pext_loc = loc ; pext_attributes = alg_attributes } } ; ELSE let tyvars = List . map ( mkloc loc ) tyvars in { pext_name = mkloc loc s ; pext_kind = Pext_decl tyvars ( Pcstr_tuple x ) rto ; pext_loc = loc ; pext_attributes = alg_attributes } ; END ; ELSE let x = match x with [ ( Ptype_record x ) -> Pcstr_record x | _ -> assert False ] in IFDEF OCAML_VERSION < OCAML_4_14_0 THEN { pext_name = mkloc loc s ; pext_kind = Pext_decl x rto ; pext_loc = loc ; pext_attributes = alg_attributes } ELSE { pext_name = mkloc loc s ; pext_kind = Pext_decl [ ] x rto ; pext_loc = loc ; pext_attributes = alg_attributes } END ; { pext_name = mkloc loc s ; pext_kind = Pext_rebind ( mkloc loc li ) ; pext_loc = loc ; pext_attributes = [ ] } ;
let params = List . map ( fun ( os , va ) -> match os with [ None -> ( ocaml_mktyp loc Ptyp_any , convert_camlp5_variance va ) | Some s -> ( ocaml_mktyp loc ( Ptyp_var s ) , convert_camlp5_variance va ) ] ) params in { ptyext_path = mkloc loc pathlid ; ptyext_params = params ; ptyext_constructors = ecstrs ; ptyext_private = priv ; ptyext_loc = loc ; ptyext_attributes = item_attributes } ; END ; let _ = if List . length params <> List . length variance then failwith " internal error : ocaml_type_declaration " else ( ) in let params = List . map2 ( fun os va -> match os with [ None -> ( ocaml_mktyp loc Ptyp_any , convert_camlp5_variance va ) | Some os -> ( ocaml_mktyp loc ( Ptyp_var os ) , convert_camlp5_variance va ) ] ) params variance in Right { ptype_params = params ; ptype_cstrs = cl ; ptype_kind = tk ; ptype_private = pf ; ptype_manifest = tm ; ptype_loc = loc ; ptype_name = mkloc loc tn ; ptype_attributes = attrs } ; Some ( fun d loc -> { pcty_desc = d ; pcty_loc = loc ; pcty_attributes = [ ] } ) ; Some ( fun { ? alg_attributes [ ] } = d loc -> { pcl_desc = d ; pcl_loc = loc ; pcl_attributes = alg_attributes } ) ; { pcstr_self = p ; pcstr_fields = cil } ; ELSE END ; let ( s , mt1 ) = mustSome " ocaml_pmty_functor " mt1 in let s = mustSome " ocaml_pmty_functor : s " s in Pmty_functor ( mkloc sloc s ) ( Some mt1 ) mt2 ; ELSE let mt1 = match mt1 with [ None -> Unit | Some ( idopt , mt ) -> Named ( mknoloc idopt ) mt ] in Pmty_functor mt1 mt2 ; END ; Some ( fun me -> Pmty_typeof me ) ; let lcl = List . map snd lcl in Pmty_with mt lcl ; Ptype_abstract ; Ptype_record ( List . map ( fun ( s , mf , ct , loc , attrs ) -> { pld_name = mkloc loc s ; pld_mutable = mf ; pld_type = ct ; pld_loc = loc ; pld_attributes = attrs } ) ltl ) ; try let ctl = List . map ( fun ( c , tl , loc , attrs ) -> IFDEF OCAML_VERSION < OCAML_4_14_0 THEN let ( tl , rto ) = match tl with [ ( Left ( tyvars , x ) , rto ) -> do { assert ( [ ] = tyvars ) ; ( Pcstr_tuple x , rto ) } | ( Right ( Ptype_record x ) , rto ) -> ( Pcstr_record x , rto ) | _ -> assert False ] in { pcd_name = mkloc loc c ; pcd_args = tl ; pcd_res = rto ; pcd_loc = loc ; pcd_attributes = attrs } ELSE let ( tyvars , tl , rto ) = match tl with [ ( Left ( tyvars , x ) , rto ) -> ( tyvars , Pcstr_tuple x , rto ) | ( Right ( Ptype_record x ) , rto ) -> ( [ ] , Pcstr_record x , rto ) | _ -> assert False ] in { pcd_name = mkloc loc c ; pcd_vars = List . map ( mkloc loc ) tyvars ; pcd_args = tl ; pcd_res = rto ; pcd_loc = loc ; pcd_attributes = attrs } END ) ctl in Some ( Ptype_variant ctl ) with [ Exit -> None ] ; Ptyp_arrow ( labelled lab ) t1 t2 ; IFDEF OCAML_VERSION < OCAML_4_11_0 THEN do { if ( not_extended_longident li ) then ( ) else failwith " #- types cannot contain extended - longidents ( with apply ) until ocaml 4 . 11 . 0 " ; Ptyp_class ( mknoloc li ) tl } ELSE Ptyp_class ( mknoloc li ) tl END ; IFDEF OCAML_VERSION < OCAML_4_06_0 THEN let ml = List . map ( fun ( s , t ) -> ( mkloc loc s , [ ] , t ) ) ml in Ptyp_object ml ( if is_open then Open else Closed ) ELSIFDEF OCAML_VERSION < OCAML_4_08_0 THEN let ml = List . map ( fun ( s , t ) -> Otag ( mkloc loc s ) [ ] t ) ml in Ptyp_object ml ( if is_open then Open else Closed ) ELSE Ptyp_object ml ( if is_open then Open else Closed ) END ; Some ( fun pt -> Ptyp_package pt ) ; Some ( fun loc cl t -> match cl with [ [ ] -> ( t . ptyp_desc , t . ptyp_attributes ) | _ -> ( Ptyp_poly ( List . map ( mkloc loc ) cl ) t , [ ] ) ] ) ; IFDEF OCAML_VERSION < OCAML_4_06_0 THEN let catl = List . map ( fun [ Left ( c , a , tl , attrs ) -> do { assert ( attrs = [ ] ) ; Rtag c [ ] a tl } | Right t -> Rinherit t ] ) catl in let clos = if clos then Closed else Open in Some ( Ptyp_variant catl clos sl_opt ) ELSIFDEF OCAML_VERSION < OCAML_4_08_0 THEN let catl = List . map ( fun [ Left ( c , a , tl , attrs ) -> do { assert ( attrs = [ ] ) ; Rtag ( mkloc loc c ) [ ] a tl } | Right t -> Rinherit t ] ) catl in let clos = if clos then Closed else Open in Some ( Ptyp_variant catl clos sl_opt ) ELSE let catl = List . map ( fun c -> let ( d , attrs ) = match c with | Left ( c , a , tl , attrs ) -> ( Rtag ( mkloc loc c ) a tl , attrs ) | Right t -> ( Rinherit t , [ ] ) end in catl in let clos = if clos then Closed else Open in Some ( Ptyp_variant catl clos sl_opt ) END ; ( mknoloc li , List . map ( fun ( li , t ) → ( mkloc t . ptyp_loc li , t ) ) ltl ) ; Pconst_char c ; Pconst_integer ( string_of_int i ) None ; Pconst_float s None ; IFDEF OCAML_VERSION < OCAML_4_11_0 THEN Const_string s None ELSE Const_string s loc None END ; IFDEF OCAML_VERSION < OCAML_4_11_0 THEN Pconst_string s so ELSE Pconst_string s loc so END ; fun [ Const_int i -> ocaml_pconst_int i | Const_char c -> ocaml_pconst_char c | IFDEF OCAML_VERSION < OCAML_4_11_0 THEN Const_string s so -> ocaml_pconst_string s loc_none so ELSE Const_string s loc so -> ocaml_pconst_string s loc so END | Const_float s -> ocaml_pconst_float s | Const_int32 i32 -> Pconst_integer ( Int32 . to_string i32 ) ( Some ' l ' ) | Const_int64 i64 -> Pconst_integer ( Int64 . to_string i64 ) ( Some ' L ' ) | Const_nativeint ni -> Pconst_integer ( Nativeint . to_string ni ) ( Some ' n ' ) ] ; Some ( fun s -> Const_int32 ( Int32 . of_string s ) ) ; Some ( fun s -> Const_int64 ( Int64 . of_string s ) ) ; Some ( fun s -> Const_nativeint ( Nativeint . of_string s ) ) ; Pexp_apply f ( List . map ( fun ( l , e ) -> ( labelled l , e ) ) lel ) ; Pexp_assert ( ocaml_mkexp loc ( Pexp_construct ( mkloc loc ( Lident " false " ) ) None ) ) ; Pexp_assert e ; match ot2 with | Some t2 -> Pexp_coerce e ot1 t2 | None -> match ot1 with | Some t1 -> Pexp_constraint e t1 | None -> failwith " internal error : ocaml_pexp_constraint " end end ; Pexp_construct ( mkloc loc li ) po ; fun [ Pexp_construct li po -> Some ( li . txt , li . loc , po , 0 ) | _ -> None ] ; let a = ocaml_mkexp loc ( Pexp_tuple al ) in IFDEF OCAML_VERSION < OCAML_4_08_0 THEN { pexp_desc = ocaml_pexp_construct li_loc li ( Some a ) True ; pexp_loc = loc ; pexp_attributes = [ ( mkloc loc " ocaml . explicit_arity " , PStr [ ] ) ] } ELSE { pexp_desc = ocaml_pexp_construct li_loc li ( Some a ) True ; pexp_loc = loc ; pexp_loc_stack = [ ] ; pexp_attributes = [ { attr_name = mkloc loc " ocaml . explicit_arity " ; attr_payload = PStr [ ] ; attr_loc = loc } ] } END ; Pexp_for i e1 e2 df e ; IFDEF OCAML_VERSION < OCAML_4_10_0 THEN { pc_lhs = p ; pc_guard = wo ; pc_rhs = e } ELSE let e = match e with [ { pexp_desc = Pexp_unreachable ; pexp_attributes = [ _ :: _ ] } -> failwith " Internal error : Pexp_unreachable ( parsed as ' . ' ) must not have attributes " | e -> e ] in { pc_lhs = p ; pc_guard = wo ; pc_rhs = e } END ; IFDEF OCAML_VERSION < OCAML_4_10_0 THEN match pel with [ [ { pc_lhs = p ; pc_guard = None ; pc_rhs = e } ] -> Pexp_fun ( labelled lab ) eo p e | pel -> if lab = " " && eo = None then Pexp_function pel else failwith " internal error : bad ast in ocaml_pexp_function " ] ELSE match pel with [ [ { pc_lhs = p ; pc_guard = None ; pc_rhs = { pexp_desc = Pexp_unreachable } } ] when lab = " " && eo = None -> Pexp_function pel | [ { pc_lhs = p ; pc_guard = None ; pc_rhs = e } ] -> Pexp_fun ( labelled lab ) eo p e | pel -> if lab = " " && eo = None then Pexp_function pel else failwith " internal error : bad ast in ocaml_pexp_function " ] END ; Some ( fun e -> Pexp_lazy e ) ; IFDEF OCAML_VERSION < OCAML_4_10_0 THEN Some ( fun i me e -> Pexp_letmodule ( mknoloc ( mustSome " ocaml_pexp_letmodule " i ) ) me e ) ELSE Some ( fun i me e -> Pexp_letmodule ( mknoloc i ) me e ) END ; Some ( fun loc s e -> Pexp_newtype ( mkloc loc s ) e ) ; Some ( fun cs -> Pexp_object cs ) ; IFDEF OCAML_VERSION < OCAML_4_08 THEN Some ( fun ovf me e -> let li = match me with [ { pmod_desc = Pmod_ident li } -> li | _ -> assert False ] in do { assert ( ovf = Fresh ) ; Pexp_open Fresh li e } ) ELSE Some ( fun ovf me e -> Pexp_open { popen_expr = me ; popen_override = ovf ; popen_loc = loc_none ; popen_attributes = [ ] } e ) END ; let sel = List . map ( fun ( s , e ) → ( mknoloc s , e ) ) sel in Pexp_override sel ; ( Some ( Right ( fun me -> Pexp_pack me , fun pt -> Ptyp_package pt ) ) : option ( choice ( ' a -> ' b -> ' c ) ' d ) ) ; Some ( fun e t -> Pexp_poly e t ) ; let lel = List . map ( fun ( li , loc , e ) → ( mkloc loc li , e ) ) lel in Pexp_record lel eo ; Pexp_send e ( mkloc loc s ) ; let pexp_variant_pat = fun [ Pexp_variant lab eo -> Some ( lab , eo ) | _ -> None ] in let pexp_variant ( lab , eo ) = Pexp_variant lab eo in Some ( pexp_variant_pat , pexp_variant ) ; IFDEF OCAML_VERSION < OCAML_4_10_0 THEN { pvb_pat = p ; pvb_expr = e ; pvb_loc = loc ; pvb_attributes = item_attributes } ELSE let p = match p with [ { ppat_desc = Ppat_constraint _ { ptyp_desc = Ptyp_poly _ _ } } -> p | { ppat_desc = Ppat_constraint { ppat_desc = Ppat_extension _ } _ } -> p | { ppat_desc = Ppat_constraint p1 t } as p0 -> let t = { ptyp_desc = Ptyp_poly [ ] t ; ptyp_loc = to_ghost_loc t . ptyp_loc ; ptyp_loc_stack = [ ] ; ptyp_attributes = [ ] } in { ( p0 ) with ppat_desc = Ppat_constraint p1 t } | p -> p ] in { pvb_pat = p ; pvb_expr = e ; pvb_loc = loc ; pvb_attributes = item_attributes } END ; ELSE END ; Some ( fun pl -> Ppat_array pl ) ; IFDEF OCAML_VERSION < OCAML_4_13_0 THEN let po = option_map ( fun ( _ , p ) -> p ) po in Ppat_construct ( mkloc loc li ) po ELSE Ppat_construct ( mkloc loc li ) po END ; fun [ Ppat_construct li po -> Some ( li . txt , li . loc , po , 0 ) | _ -> None ] ; IFDEF OCAML_VERSION < OCAML_4_08_0 THEN let a = ocaml_mkpat loc ( Ppat_tuple al ) in { ppat_desc = ocaml_ppat_construct li_loc li ( Some ( tyvl , a ) ) True ; ppat_loc = loc ; ppat_attributes = [ ( mkloc loc " ocaml . explicit_arity " , PStr [ ] ) ] } ELSE let a = ocaml_mkpat loc ( Ppat_tuple al ) in { ppat_desc = ocaml_ppat_construct li_loc li ( Some ( tyvl , a ) ) True ; ppat_loc = loc ; ppat_loc_stack = [ ] ; ppat_attributes = [ { attr_name = mkloc loc " ocaml . explicit_arity " ; attr_payload = PStr [ ] ; attr_loc = loc } ] } END ; Some ( fun p -> Ppat_lazy p ) ; let lpl = List . map ( fun ( li , loc , p ) → ( mkloc loc li , p ) ) lpl in Ppat_record lpl ( if is_closed then Closed else Open ) ; Some ( fun loc li -> Ppat_type ( mkloc loc li ) ) ; IFDEF OCAML_VERSION < OCAML_4_10_0 THEN Some ( fun loc s -> Ppat_unpack ( mkloc loc ( mustSome " ocaml_ppat_unpack " s ) ) , fun pt -> Ptyp_package pt ) ELSE Some ( fun loc s -> Ppat_unpack ( mkloc loc s ) , fun pt -> Ptyp_package pt ) END ; let ppat_variant_pat = fun [ Ppat_variant lab po -> Some ( lab , po ) | _ -> None ] in let ppat_variant ( lab , po ) = Ppat_variant lab po in Some ( ppat_variant_pat , ppat_variant ) ; Some ( fun ctl -> Psig_class_type ctl ) ; IFDEF OCAML_VERSION < OCAML_4_08_0 THEN do { assert ( item_attributes = [ ] ) ; assert ( alg_attributes = [ ] ) ; let ( tyvars , ed ) = mustLeft " ocaml_psig_exception ( record - types not allowed ) " ed in assert ( None = rto ) ; assert ( [ ] = tyvars ) ; Psig_exception { pext_name = mkloc loc s ; pext_kind = Pext_decl ( Pcstr_tuple ed ) None ; pext_loc = loc ; pext_attributes = [ ] } } ELSE let ec = match ed with [ Left ( tyvars , x ) -> ocaml_ec_tuple { ~ alg_attributes = alg_attributes } loc s tyvars ( x , rto ) | Right x -> ocaml_ec_record { ~ alg_attributes = alg_attributes } loc s ( x , rto ) ] in Psig_exception { ptyexn_constructor = ec ; ptyexn_attributes = item_attributes ; ptyexn_loc = loc } END ; Psig_include { pincl_mod = mt ; pincl_loc = loc ; pincl_attributes = item_attributes } ; IFDEF OCAML_VERSION < OCAML_4_10_0 THEN do { assert ( item_attributes = [ ] ) ; let s = mustSome " ocaml_psig_module " s in Psig_module { pmd_name = mkloc loc s ; pmd_type = mt ; pmd_attributes = [ ] ; pmd_loc = loc } } ELSE Psig_module { pmd_name = mkloc loc s ; pmd_type = mt ; pmd_attributes = item_attributes ; pmd_loc = loc } END ; ELSE Psig_modsubst { pms_name = mkloc loc s ; pms_manifest = mkloc loc li ; pms_attributes = item_attributes ; pms_loc = loc } ; END ; let pmtd = { pmtd_name = mkloc loc s ; pmtd_type = mto ; pmtd_attributes = item_attributes ; pmtd_loc = loc } in Psig_modtype pmtd ; IFDEF OCAML_VERSION < OCAML_4_13_0 THEN failwith " no ' module type ' substitution in this version of ocaml " ELSE let pmtd = { pmtd_name = mkloc loc s ; pmtd_type = mto ; pmtd_attributes = item_attributes ; pmtd_loc = loc } in Psig_modtypesubst pmtd END ; IFDEF OCAML_VERSION < OCAML_4_08 THEN do { assert ( item_attributes = [ ] ) ; Psig_open { popen_lid = mknoloc li ; popen_override = Fresh ; popen_loc = loc ; popen_attributes = [ ] } } ELSE Psig_open { popen_expr = mknoloc li ; popen_override = Fresh ; popen_loc = loc ; popen_attributes = item_attributes } END ; IFDEF OCAML_VERSION < OCAML_4_10_0 THEN let f ntl = let ntl = List . map ( fun ( ( s : option string ) , mt , attrs ) -> do { assert ( attrs = [ ] ) ; let s = mustSome " ocaml_psig_recmodule " s in { pmd_name = mknoloc s ; pmd_type = mt ; pmd_attributes = [ ] ; pmd_loc = loc_none } } ) ntl in Psig_recmodule ntl in Some f ELSE let f ntl = let ntl = List . map ( fun ( s , mt , attrs ) -> { pmd_name = mknoloc s ; pmd_type = mt ; pmd_attributes = attrs ; pmd_loc = loc_none } ) ntl in Psig_recmodule ntl in Some f END ; let stl = List . map ( fun ( s , t ) -> t ) stl in Psig_type ( if is_nonrec then Nonrecursive else Recursive ) stl ; IFDEF OCAML_VERSION < OCAML_4_10_0 THEN failwith " Only available in OCaml versions >= 4 . 10 . 0 " ELSE let stl = List . map ( fun ( s , t ) -> t ) stl in Psig_typesubst stl END ; Psig_value vd ; Some ( fun ctl -> Pstr_class_type ctl ) ; Pstr_eval e item_attributes ; IFDEF OCAML_VERSION < OCAML_4_08_0 THEN do { assert ( alg_attributes = [ ] ) ; assert ( item_attributes = [ ] ) ; let ( tyvars , ed ) = mustLeft " ocaml_pstr_exception ( record - types not allowed ) " ed in assert ( None = rto ) ; assert ( [ ] = tyvars ) ; Pstr_exception { pext_name = mkloc loc s ; pext_kind = Pext_decl ( Pcstr_tuple ed ) None ; pext_loc = loc ; pext_attributes = [ ] } } ELSE let ec = match ed with [ Left ( tyvars , x ) -> ocaml_ec_tuple { ~ alg_attributes = alg_attributes } loc s tyvars ( x , rto ) | Right x -> ocaml_ec_record { ~ alg_attributes = alg_attributes } loc s ( x , rto ) ] in Pstr_exception { ptyexn_constructor = ec ; ptyexn_attributes = item_attributes ; ptyexn_loc = loc } END ; IFDEF OCAML_VERSION < OCAML_4_08_0 THEN Some ( fun loc s li -> Pstr_exception { pext_name = mkloc loc s ; pext_kind = Pext_rebind ( mkloc loc li ) ; pext_loc = loc ; pext_attributes = [ ] } ) ELSE Some ( fun loc s li -> Pstr_exception { ptyexn_constructor = ocaml_ec_rebind loc s li ; ptyexn_attributes = [ ] ; END ; Some ( fun { ? item_attributes [ ] } = loc me -> Pstr_include { pincl_mod = me ; pincl_loc = loc ; pincl_attributes = item_attributes } ) ; let pmtd = { pmtd_name = mkloc loc s ; pmtd_type = mto ; pmtd_attributes = item_attributes ; pmtd_loc = loc } in Pstr_modtype pmtd ; IFDEF OCAML_VERSION < OCAML_4_10_0 THEN do { assert ( item_attributes = [ ] ) ; let s = mustSome " ocaml_pstr_module " s in let mb = { pmb_name = mkloc loc s ; pmb_expr = me ; pmb_attributes = [ ] ; pmb_loc = loc } in Pstr_module mb } ELSE let mb = { pmb_name = mkloc loc s ; pmb_expr = me ; pmb_attributes = item_attributes ; pmb_loc = loc } in Pstr_module mb END ; IFDEF OCAML_VERSION < OCAML_4_08 THEN do { assert ( item_attributes = [ ] ) ; assert ( ovflag = Fresh ) ; let li = match me with [ { pmod_desc = Pmod_ident { txt = li } } -> li | _ -> assert False ] in Pstr_open { popen_lid = mknoloc li ; popen_override = Fresh ; popen_loc = loc ; popen_attributes = [ ] } } ELSE Pstr_open { popen_expr = me ; popen_override = ovflag ; popen_loc = loc ; popen_attributes = item_attributes } END ; Pstr_primitive vd ; IFDEF OCAML_VERSION < OCAML_4_10_0 THEN let f mel = let mel = List . map ( fun ( a , b , c , attrs ) -> do { assert ( attrs = [ ] ) ; ( a , b , c ) } ) mel in Pstr_recmodule ( List . map ( fun ( ( s : option string ) , mt , me ) -> let s = mustSome " ocaml_pstr_recmodule " s in { pmb_name = mknoloc s ; pmb_expr = me ; pmb_attributes = [ ] ; pmb_loc = loc_none } ) mel ) in Some f ELSE let f mel = Pstr_recmodule ( List . map ( fun ( ( s : option string ) , mt , me , attrs ) -> { pmb_name = mknoloc s ; pmb_expr = me ; pmb_attributes = attrs ; pmb_loc = loc_none } ) mel ) in Some f END ; let stl = List . map ( fun ( s , t ) -> t ) stl in Pstr_type ( if is_nonrec then Nonrecursive else Recursive ) stl ; Some ( fun { ? item_attributes [ ] } = virt ( sl , sloc ) name expr loc variance -> let _ = if List . length sl <> List . length variance then failwith " internal error : ocaml_class_infos " else ( ) in let params = List . map2 ( fun os va -> ( ocaml_mktyp loc ( Ptyp_var os ) , convert_camlp5_variance va ) ) sl variance in { pci_virt = virt ; pci_params = params ; pci_name = mkloc loc name ; pci_expr = expr ; pci_loc = loc ; pci_attributes = item_attributes } ) ; ocaml_mkmod loc ( Pmod_constraint me mt ) ; let ( s , mt ) = mustSome " ocaml_pmod_functor " mt in let s = mustSome " ocaml_pmod_functor : s " s in Pmod_functor ( mknoloc s ) ( Some mt ) me ; ELSE let mt = match mt with [ None -> Unit | Some ( idopt , mt ) -> Named ( mknoloc idopt ) mt ] in Pmod_functor mt me ; END ; ( Some ( Right ( fun e -> Pmod_unpack e , fun pt -> Ptyp_package pt ) ) : option ( choice ( ' a -> ' b -> ' c ) ' d ) ) ; Some ( fun ( t1 , t2 , loc ) -> Pcf_constraint ( t1 , t2 ) ) ; fun loc ovflag ce pb -> Pcf_inherit ovflag ce ( option_map ( mkloc loc ) pb ) ; Some ( fun e -> Pcf_initializer e ) ; let pf = if pf then Private else Public in let ovf = if ovf then Override else Fresh in Pcf_method ( mkloc loc s , pf , Cfk_concrete ovf e ) ; let mf = if mf then Mutable else Immutable in let ovf = if ovf then Override else Fresh in Pcf_val ( mkloc loc s , mf , Cfk_concrete ovf e ) ; let ocaml_pcf ( s , mf , t , loc ) = let mf = if mf then Mutable else Immutable in Pcf_val ( mkloc loc s , mf , Cfk_virtual t ) in Some ocaml_pcf ; Pcf_method ( mkloc loc s , pf , Cfk_virtual t ) ; Some ( fun ce lel -> Pcl_apply ce ( List . map ( fun ( l , e ) -> ( labelled l , e ) ) lel ) ) ; Some ( fun li ctl -> Pcl_constr ( mknoloc li ) ctl ) ; Some ( fun ce ct -> Pcl_constraint ce ct ) ; Some ( fun lab ceo p ce -> Pcl_fun ( labelled lab ) ceo p ce ) ; Some ( fun rf pel ce -> Pcl_let rf pel ce ) ; ; ; ELSE Pcl_open { popen_expr = mknoloc li ; popen_override = ovf ; popen_loc = loc ; popen_attributes = [ ] } ce ; Pcty_open { popen_expr = mknoloc li ; popen_override = ovf ; popen_loc = loc ; popen_attributes = [ ] } ct ; END ; Some ( fun cs -> Pcl_structure cs ) ; Some ( fun ( t1 , t2 , loc ) -> Pctf_constraint ( t1 , t2 ) ) ; Pctf_inherit ct ; Pctf_method ( mkloc loc s , pf , Concrete , t ) ; Pctf_val ( mkloc loc s , mf , vf , t ) ; Pctf_method ( mkloc loc s , pf , Virtual , t ) ; Some ( fun li ltl -> Pcty_constr ( mknoloc li ) ltl ) ; Some ( fun lab t ot ct -> Pcty_arrow ( labelled lab ) t ct ) ; let f ( t , ctfl ) = let cs = { pcsig_self = t ; pcsig_fields = ctfl } in Pcty_signature cs in Some f ; Some ( fun b -> Pdir_bool b ) ; Pdir_int i None ; IFDEF OCAML_VERSION < OCAML_4_08_0 THEN x ELSE Some x END ; IFDEF OCAML_VERSION < OCAML_4_08_0 THEN Pdir_none ELSE None END ; IFDEF OCAML_VERSION < OCAML_4_08_0 THEN Ptop_dir s da ELSE Ptop_dir { pdir_name = mkloc loc s ; pdir_arg = match da with | Some da -> Some { pdira_desc = da ; pdira_loc = loc } | None -> None end ; pdir_loc = loc } END ; value ocaml_pwith_modtype : option ( Location . t -> Longident . t -> module_type -> with_constraint ) = IFDEF OCAML_VERSION < OCAML_4_13_0 THEN None ELSE Some ( fun loc li mt -> Pwith_modtype ( mkloc loc li ) mt ) END ; IFDEF OCAML_VERSION < OCAML_4_13_0 THEN None ELSE Some ( fun loc li mt -> Pwith_modtypesubst ( mkloc loc li ) mt ) END ; IFDEF OCAML_VERSION < OCAML_4_06_0 THEN Some ( fun loc li me -> Pwith_modsubst ( mkloc loc " " ) ( mkloc loc me ) ) ELSE Some ( fun loc li me -> Pwith_modsubst ( mkloc loc li ) ( mkloc loc me ) ) END ; Pwith_type ( mkloc loc i ) td ; Pwith_module ( mkloc loc mname ) ( mkloc loc me ) ; IFDEF OCAML_VERSION < OCAML_4_06_0 THEN Some ( fun loc lid td -> Pwith_typesubst td ) ELSE Some ( fun loc lid td -> Pwith_typesubst ( mkloc loc lid ) td ) END ; True ; False ; True ; fun [ Arg . Rest r -> Some r | _ -> None ] ; fun [ Arg . Set_string r -> Some r | _ -> None ] ; fun [ Arg . Set_int r -> Some r | _ -> None ] ; fun [ Arg . Set_float r -> Some r | _ -> None ] ; fun [ Arg . Symbol s f -> Some ( s , f ) | _ -> None ] ; fun [ Arg . Tuple t -> Some t | _ -> None ] ; fun [ Arg . Bool f -> Some f | _ -> None ] ; Char . escaped ; Hashtbl . mem ; List . rev_map ; List . sort ; set_binary_mode_out ; Char . uppercase_ascii ; " Bytes " ; Bytes . of_string s ; Bytes . to_string s ; String . capitalize_ascii ; String . contains ; Bytes . cat s1 s2 ; Bytes . copy ; Bytes . create ; Bytes . get ; Bytes . index ; Bytes . length ; String . lowercase_ascii ; Bytes . unsafe_set ; String . uncapitalize_ascii ; String . uppercase_ascii ; Bytes . set ; Bytes . sub ; Array . make ;
type additional_info = Dev | RC of int | Release
let string_of_additional_info = function | Dev -> " + dev " | RC n -> " ~ rc " ^ string_of_int n | Release -> " "
type t = { major : int ; minor : int ; additional_info : additional_info }
let pp f { major ; minor ; additional_info } = Format . fprintf f " % i . % i % s " major minor ( string_of_additional_info additional_info )
let to_string x = Format . asprintf " % a " pp x
let current = { major = 12 ; minor = 0 ; additional_info = Dev }
let current_string = to_string current
module Version_error = struct type t = { payload : Csexp . t option ; message : string } let payload t = t . payload let message t = t . message let create ? payload ~ message ( ) = { payload ; message } exception E of t let to_response_error { payload ; message } = Response . Error . create ~ kind : Invalid_request ? payload ~ message ( ) end
module Staged = struct type ( ' req , ' resp ) request = { encode_req : ' req -> Call . t ; decode_resp : Csexp . t -> ( ' resp , Response . Error . t ) result } type ' payload notification = { encode : ' payload -> Call . t } end
module Make ( Fiber : Fiber ) = struct module Handler = struct type ' state t = { menu : Menu . t ; handle_request : Menu . t -> ' state -> Types . Request . t -> Response . t Fiber . t ; handle_notification : Menu . t -> ' state -> Call . t -> ( unit , Response . Error . t ) result Fiber . t ; prepare_request : ' req ' resp . Menu . t -> ( ' req , ' resp ) Decl . Request . witness -> ( ( ' req , ' resp ) Staged . request , Version_error . t ) result ; prepare_notification : ' a . Menu . t -> ' a Decl . Notification . witness -> ( ' a Staged . notification , Version_error . t ) result } let handle_request t = t . handle_request t . menu let handle_notification t = t . handle_notification t . menu let prepare_request t = t . prepare_request t . menu let prepare_notification t = t . prepare_notification t . menu end module Builder = struct open Decl type ' s r_handler = | R : ( ' s -> ' req -> ' resp Fiber . t ) * ( ' req , ' resp ) Decl . Generation . t -> ' s r_handler type ' s n_handler = | N : ( ' s -> ' payload -> unit Fiber . t ) * ( ' payload , unit ) Decl . Generation . t -> ' s n_handler type packed = T : ' a Method_version . Map . t Univ_map . Key . t -> packed type ' state t = { mutable declared_requests : packed list Method_name . Map . t * Univ_map . t ; mutable declared_notifications : packed list Method_name . Map . t * Univ_map . t ; implemented_requests : ' state r_handler Method_version . Map . t Method_name . Table . t ; implemented_notifications : ' state n_handler Method_version . Map . t Method_name . Table . t } type ( _ , _ , _ ) field_witness = | Declared_requests : ( _ , Method_name . t * ( ' req , ' resp ) Decl . Generation . t Method_version . Map . t Univ_map . Key . t , ( ' req , ' resp ) Decl . Generation . t ) field_witness | Declared_notifs : ( _ , Method_name . t * ( ' a , unit ) Decl . Generation . t Method_version . Map . t Univ_map . Key . t , ( ' a , unit ) Decl . Generation . t ) field_witness | Impl_requests : ( ' state , string , ' state r_handler ) field_witness | Impl_notifs : ( ' state , string , ' state n_handler ) field_witness let get ( type st a b ) ( t : st t ) ( witness : ( st , a , b ) field_witness ) ( key : a ) : b Method_version . Map . t option = match witness with | Declared_requests -> let _ , key = key in let _ , table = t . declared_requests in Univ_map . find table key | Declared_notifs -> let _ , key = key in let _ , table = t . declared_notifications in Univ_map . find table key | Impl_requests -> Method_name . Table . find t . implemented_requests key | Impl_notifs -> Method_name . Table . find t . implemented_notifications key let set ( type st a b ) ( t : st t ) ( witness : ( st , a , b ) field_witness ) ( key : a ) ( value : b Method_version . Map . t ) = match witness with | Declared_requests -> let name , key = key in let known_keys , table = t . declared_requests in t . declared_requests <- ( Method_name . Map . add_multi known_keys name ( T key ) , Univ_map . set table key value ) | Declared_notifs -> let name , key = key in let known_keys , table = t . declared_notifications in t . declared_notifications <- ( Method_name . Map . add_multi known_keys name ( T key ) , Univ_map . set table key value ) | Impl_requests -> Method_name . Table . set t . implemented_requests key value | Impl_notifs -> Method_name . Table . set t . implemented_notifications key value let registered_procedures { declared_requests = declared_request_keys , declared_request_table ; declared_notifications = declared_notification_keys , declared_notification_table ; implemented_requests ; implemented_notifications } = let batch_declarations which declared_keys declaration_table = Method_name . Map . foldi declared_keys ~ init [ ] : ~ f ( : fun name keys acc -> let generations = List . fold_left keys ~ init [ ] : ~ f ( : fun acc ( T key ) -> match Univ_map . find declaration_table key with | None -> Code_error . raise " versioning : method found in versioning table without \ actually being declared " [ ( " method_ " , Dyn . String name ) ; ( " table " , Dyn . String ( " known_ " ^ which ^ " _table " ) ) ] | Some listing -> Method_version . Map . keys listing @ acc ) in ( name , generations ) :: acc ) in let declared_requests = batch_declarations " request " declared_request_keys declared_request_table in let declared_notifications = batch_declarations " notification " declared_notification_keys declared_notification_table in let batch_implementations table = Method_name . Table . foldi table ~ init [ ] : ~ f ( : fun name listing acc -> ( name , Method_version . Map . keys listing ) :: acc ) in let implemented_requests = batch_implementations implemented_requests in let implemented_notifications = batch_implementations implemented_notifications in List . concat [ declared_requests ; declared_notifications ; implemented_requests ; implemented_notifications ] let create ( ) = let declared_requests = ( Method_name . Map . empty , Univ_map . empty ) in let declared_notifications = ( Method_name . Map . empty , Univ_map . empty ) in let implemented_requests = Method_name . Table . create 16 in let implemented_notifications = Method_name . Table . create 16 in { declared_requests ; declared_notifications ; implemented_requests ; implemented_notifications } let register_generic t ~ method_ ~ generations ~ registry ~ registry_key ~ other ~ other_key ~ pack = let ( ) = match get t other other_key with | None -> ( ) | Some _ -> Code_error . raise " attempted to implement and declare method " [ ( " method " , Dyn . String method_ ) ] in let prior_registered_generations = match get t registry registry_key with | None -> Method_version . Map . empty | Some s -> s in let all_generations , duplicate_generations = List . fold_left generations ~ init ( : prior_registered_generations , Method_version . Set . empty ) ~ f ( : fun ( acc , dups ) ( n , gen ) -> match Method_version . Map . add acc n ( pack gen ) with | Error _ -> ( acc , Method_version . Set . add dups n ) | Ok acc ' -> ( acc ' , dups ) ) in if Method_version . Set . is_empty duplicate_generations then set t registry registry_key all_generations else Code_error . raise " attempted to register duplicate generations for RPC method " [ ( " method " , Dyn . String method_ ) ; ( " duplicated " , Method_version . Set . to_dyn duplicate_generations ) ] let declare_request t proc = register_generic t ~ method_ : proc . Request . decl . method_ ~ generations : proc . Request . generations ~ registry : Declared_requests ~ other : Impl_requests ~ registry_key ( : proc . Request . decl . method_ , proc . Request . decl . key ) ~ other_key : proc . Request . decl . method_ ~ pack ( : fun rc -> rc ) let declare_notification t proc = register_generic t ~ method_ : proc . Notification . decl . method_ ~ generations : proc . Notification . generations ~ registry : Declared_notifs ~ other : Impl_notifs ~ registry_key : ( proc . Notification . decl . method_ , proc . Notification . decl . key ) ~ other_key : proc . Notification . decl . method_ ~ pack ( : fun nc -> nc ) let implement_request t proc f = register_generic t ~ method_ : proc . Request . decl . method_ ~ generations : proc . Request . generations ~ registry : Impl_requests ~ other : Declared_requests ~ registry_key : proc . Request . decl . method_ ~ other_key ( : proc . Request . decl . method_ , proc . Request . decl . key ) ~ pack ( : fun r -> R ( f , r ) ) let implement_notification t proc f = register_generic t ~ method_ : proc . Notification . decl . method_ ~ generations : proc . Notification . generations ~ registry : Impl_notifs ~ other : Declared_notifs ~ registry_key : proc . Notification . decl . method_ ~ other_key ( : proc . Notification . decl . method_ , proc . Notification . decl . key ) ~ pack ( : fun n -> N ( f , n ) ) let lookup_method_generic t ~ menu ~ table ~ key ~ method_ k s = match ( get t table key , Method_name . Map . find menu method_ ) with | None , _ -> let payload = Sexp . record [ ( " method " , Atom method_ ) ] in k ( Version_error . create ~ message " : invalid method " ~ payload ( ) ) | _ , None -> let payload = Sexp . record [ ( " method " , Atom method_ ) ] in k ( Version_error . create ~ message " : remote and local have no common version for method " ~ payload ( ) ) | Some subtable , Some version -> s ( subtable , version ) let raise_version_bug ~ method_ ~ selected ~ verb ~ known = Code_error . raise " bug with version negotiation ; selected bad method version " [ ( " message " , Dyn . String ( " version is " ^ verb ) ) ; ( " method " , Dyn . String method_ ) ; ( " implemented versions " , Dyn . List ( List . map ~ f ( : fun i -> Dyn . Int i ) known ) ) ; ( " selected version " , Dyn . Int selected ) ] let to_handler t ~ session_version = let open Fiber . O in let handle_request menu state ( _id , ( n : Call . t ) ) = lookup_method_generic t ~ menu ~ table : Impl_requests ~ key : n . method_ ~ method_ : n . method_ ( fun e -> Fiber . return ( Error ( Version_error . to_response_error e ) ) ) ( fun ( handlers , version ) -> match Method_version . Map . find handlers version with | None -> raise_version_bug ~ method_ : n . method_ ~ selected : version ~ verb " : unimplemented " ~ known ( : Method_version . Map . keys handlers ) | Some ( R ( f , T gen ) ) -> ( match Conv . of_sexp gen . req ~ version ( : session_version state ) n . params with | Error e -> Fiber . return ( Error ( Response . Error . of_conv e ) ) | Ok req -> let + resp = f state ( gen . upgrade_req req ) in Ok ( Conv . to_sexp gen . resp ( gen . downgrade_resp resp ) ) ) ) in let handle_notification menu state ( n : Call . t ) = lookup_method_generic t ~ menu ~ table : Impl_notifs ~ key : n . method_ ~ method_ : n . method_ ( fun e -> Fiber . return ( Error ( Version_error . to_response_error e ) ) ) ( fun ( handlers , version ) -> match Method_version . Map . find handlers version with | None -> raise_version_bug ~ method_ : n . method_ ~ selected : version ~ verb " : unimplemented " ~ known ( : Method_version . Map . keys handlers ) | Some ( N ( f , T gen ) ) -> ( match Conv . of_sexp gen . req ~ version ( : session_version state ) n . params with | Error e -> Fiber . return ( Error ( Response . Error . of_conv e ) ) | Ok req -> let + ( ) = f state ( gen . upgrade_req req ) in Ok ( ) ) ) in let prepare_request ( type a b ) menu ( decl : ( a , b ) Decl . Request . witness ) : ( ( a , b ) Staged . request , Version_error . t ) result = let method_ = decl . Decl . method_ in lookup_method_generic t ~ menu ~ table : Declared_requests ~ key ( : method_ , decl . key ) ~ method_ ( fun e -> Error e ) ( fun ( decls , version ) -> match Method_version . Map . find decls version with | None -> raise_version_bug ~ method_ ~ selected : version ~ verb " : undeclared " ~ known ( : Method_version . Map . keys decls ) | Some ( T gen ) -> let encode_req ( req : a ) = { Call . method_ ; params = Conv . to_sexp gen . req ( gen . downgrade_req req ) } in let decode_resp sexp = match Conv . of_sexp gen . resp ~ version ( : 3 , 0 ) sexp with | Ok resp -> Ok ( gen . upgrade_resp resp ) | Error e -> Error ( Response . Error . of_conv e ) in Ok { Staged . encode_req ; decode_resp } ) in let prepare_notification ( type a ) menu ( decl : a Decl . Notification . witness ) : ( a Staged . notification , Version_error . t ) result = let method_ = decl . Decl . method_ in lookup_method_generic t ~ menu ~ table : Declared_notifs ~ key ( : method_ , decl . key ) ~ method_ ( fun e -> Error e ) ( fun ( decls , version ) -> match Method_version . Map . find decls version with | None -> raise_version_bug ~ method_ ~ selected : version ~ verb " : undeclared " ~ known ( : Method_version . Map . keys decls ) | Some ( T gen ) -> let encode ( req : a ) = { Call . method_ ; params = Conv . to_sexp gen . req ( gen . downgrade_req req ) } in Ok { Staged . encode } ) in fun ~ menu -> { Handler . menu ; handle_request ; handle_notification ; prepare_request ; prepare_notification } end end
module type S = sig type data module Lang : sig val register : Syntax . t -> data -> unit module Instance : sig type t = { syntax : Syntax . t ; data : data ; version : Syntax . Version . t } end val get_exn : string -> Instance . t end val load_exn : Path . t -> f ( : Lang . Instance . t -> ' a Decoder . t ) -> ' a val load : Path . t -> f ( : Lang . Instance . t -> ' a Decoder . t ) -> ' a Or_exn . t val parse_contents : Lexing . lexbuf -> f ( : Lang . Instance . t -> ' a Decoder . t ) -> ' a end
module Make ( Data : sig type t struct module Lang = struct type t = { syntax : Syntax . t ; data : Data . t } module Instance = struct type t = { syntax : Syntax . t ; data : Data . t ; version : Syntax . Version . t } end let langs = Table . create ( module String ) 32 let register syntax data = let name = Syntax . name syntax in if Table . mem langs name then Code_error . raise " Versioned_file . Lang . register : already registered " [ ( " name " , Dyn . string name ) ] ; Table . add_exn langs name { syntax ; data } let parse first_line : Instance . t = let { First_line . lang = name_loc , name ; version = ver_loc , ver } = first_line in let dune_lang_ver = Decoder . parse Syntax . Version . decode Univ_map . empty ( Atom ( ver_loc , Atom . of_string ver ) ) in match Table . find langs name with | None -> User_error . raise ~ loc : name_loc [ Pp . textf " Unknown language % S . " name ] ~ hints ( : User_message . did_you_mean name ~ candidates ( : Table . keys langs ) ) | Some t -> Syntax . check_supported ~ dune_lang_ver t . syntax ( ver_loc , dune_lang_ver ) ; { syntax = t . syntax ; data = t . data ; version = dune_lang_ver } let get_exn name : Instance . t = let t = Table . find_exn langs name in { syntax = t . syntax ; data = t . data ; version = Syntax . greatest_supported_version t . syntax } end let parse_lang_exn lb = let first_line = First_line . lex lb in let lang = Lang . parse first_line in ( lang , Parser . parse lb ~ mode : Many_as_one ) let parse_ast ( ( lang : Lang . Instance . t ) , ast ) ~ f = let parsing_context = Univ_map . singleton ( Syntax . key lang . syntax ) ( Active lang . version ) in Decoder . parse ( Decoder . enter ( f lang ) ) parsing_context ast let parse_contents lb ~ f = let ast = parse_lang_exn lb in parse_ast ast ~ f let load fn ~ f = Io . with_lexbuf_from_file fn ~ f ( : fun lb -> Result . try_with ( fun ( ) -> parse_contents lb ~ f ) ) let load_exn fn ~ f = Result . ok_exn ( load fn ~ f ) end
let no_more_lang = let open Decoder in let + ( _ : _ list ) = multi_field " lang " ( let + loc = loc and + _ = repeat raw in User_error . raise ~ loc [ Pp . text " The ( lang . . ) line cannot appear more than once . " ] ) in ( )
module Versioned_direct_stream_writer = struct module Direct_stream_writer = Pipe_rpc . Direct_stream_writer type ' input t = | T : { convert : ( ' input -> ' output ) ; writer : ' output Direct_stream_writer . t } -> ' input t let create ~ convert ~ writer = T { convert ; writer } let write ( T { convert ; writer } ) input = Direct_stream_writer . write writer ( convert input ) let write_without_pushback ( T { convert ; writer } ) input = Direct_stream_writer . write_without_pushback writer ( convert input ) let close ( T { convert = _ ; writer } ) = Direct_stream_writer . close writer let is_closed ( T { convert = _ ; writer } ) = Direct_stream_writer . is_closed writer let closed ( T { convert = _ ; writer } ) = Direct_stream_writer . closed writer end
let failed_conversion x = Error . create " type conversion failure " x [ % sexp_of : [ ` Msg | ` Query | ` Response | ` Error | ` State | ` Update ] * [ ` Rpc of string ] * [ ` Version of int ] * exn ]
let multiple_registrations x = Error . create " multiple rpc registrations " x [ % sexp_of : [ ` Rpc of string ] * [ ` Version of int ] ]
let unknown_version x = Error . create " unknown rpc version " x [ % sexp_of : string * int ]
module Callee_converts = struct module Rpc = struct module Simple = struct type ( ' query , ' response ) adapter = { adapt : ' state . ( ' state -> ' query -> ' response Deferred . t ) -> ' state Implementation . t } type ( ' query , ' response ) t = { name : string ; adapters : ( ' query , ' response ) adapter Int . Map . t } [ @@ deriving fields ] let create ~ name = { name ; adapters = Int . Map . empty } let wrap_error fn = ( fun state query -> fn state query >>| function | Ok value -> Ok value | Error error -> Error ( Error . to_string_hum error ) ) let add { name ; adapters } rpc adapter = if String . ( ) <> name ( Rpc . name rpc ) then Or_error . error " Rpc names don ' t agree " ( name , Rpc . name rpc ) [ % sexp_of : string * string ] else let version = Rpc . version rpc in match Map . find adapters version with | Some _ -> Or_error . error " Version already exists " ( name , version ) [ % sexp_of : string * int ] | None -> let adapters = Map . set adapters ~ key : version ~ data : adapter in Ok { name ; adapters } let add_rpc_version t old_rpc upgrade downgrade = let adapt fn = let adapted state old_query = fn state ( upgrade old_query ) >>| fun result -> downgrade result in Rpc . implement old_rpc adapted in add t old_rpc { adapt } let add_rpc_version_with_failure t old_rpc upgrade_or_error downgrade_or_error = let adapt fn = let adapted state old_query = let open Deferred . Result . Monad_infix in return ( upgrade_or_error old_query ) >>= fun query -> fn state query >>= fun response -> return ( downgrade_or_error response ) in Rpc . implement old_rpc ( wrap_error adapted ) in add t old_rpc { adapt } let add_version t ~ version ~ bin_query ~ bin_response upgrade downgrade = let rpc = Rpc . create ~ name : t . name ~ version ~ bin_query ~ bin_response in add_rpc_version t rpc upgrade downgrade let add_version_with_failure t ~ version ~ bin_query ~ bin_response upgrade downgrade = let rpc = Rpc . create ~ name : t . name ~ version ~ bin_query ~ bin_response in add_rpc_version_with_failure t rpc upgrade downgrade let implement t fn = Map . data t . adapters |> List . map ~ f ( : fun { adapt } -> adapt fn ) end module type S = sig type query type response val implement_multi : ? log_not_previously_seen_version ( : name : string -> int -> unit ) -> ( ' state -> version : int -> query -> response Deferred . t ) -> ' state Implementation . t list val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string type query type response end ) = struct let name = Model . name type ' s impl = ' s -> version : int -> Model . query -> Model . response Deferred . t type implementer = { implement : ' s . log_version ( : int -> unit ) -> ' s impl -> ' s Implementation . t } let registry : ( int , implementer * Any . t ) Hashtbl . t = Int . Table . create ~ size : 1 ( ) let implement_multi ? log_not_previously_seen_version f = let log_version = match log_not_previously_seen_version with | None -> ignore | Some f -> Memo . general ( f ~ name ) in List . map ( Hashtbl . data registry ) ~ f ( : fun ( i , _rpc ) -> i . implement ~ log_version f ) let rpcs ( ) = List . map ( Hashtbl . data registry ) ~ f ( : fun ( _ , rpc ) -> rpc ) let versions ( ) = Int . Set . of_list ( Hashtbl . keys registry ) module Register ( Version_i : sig type query [ @@ deriving bin_io ] type response [ @@ deriving bin_io ] val version : int val model_of_query : query -> Model . query val response_of_model : Model . response -> response end ) = struct open Version_i let rpc = Rpc . create ~ name ~ version ~ bin_query ~ bin_response let ( ) = let implement ~ log_version f = Rpc . implement rpc ( fun s q -> log_version version ; match Result . try_with ( fun ( ) -> Version_i . model_of_query q ) with | Error exn -> Error . raise ( failed_conversion ( ` Query , ` Rpc name , ` Version version , exn ) ) | Ok q -> f s ~ version q >>| fun r -> match Result . try_with ( fun ( ) -> Version_i . response_of_model r ) with | Ok r -> r | Error exn -> Error . raise ( failed_conversion ( ` Response , ` Rpc name , ` Version version , exn ) ) ) in match Hashtbl . find registry version with | None -> Hashtbl . set registry ~ key : version ~ data ( { : implement } , Any . Rpc rpc ) | Some _ -> Error . raise ( multiple_registrations ( ` Rpc name , ` Version version ) ) end end end module Pipe_rpc = struct module type S = sig type query type response type error val implement_multi : ? log_not_previously_seen_version ( : name : string -> int -> unit ) -> ( ' state -> version : int -> query -> ( response Pipe . Reader . t , error ) Result . t Deferred . t ) -> ' state Implementation . t list val implement_direct_multi : ? log_not_previously_seen_version ( : name : string -> int -> unit ) -> ( ' state -> version : int -> query -> response Versioned_direct_stream_writer . t -> ( unit , error ) Result . t Deferred . t ) -> ' state Implementation . t list val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string type query type response type error end ) = struct let name = Model . name type ' s impl = | Pipe of ( ' s -> version : int -> Model . query -> ( Model . response Pipe . Reader . t , Model . error ) Result . t Deferred . t ) | Direct of ( ' s -> version : int -> Model . query -> Model . response Versioned_direct_stream_writer . t -> ( unit , Model . error ) Result . t Deferred . t ) type implementer = { implement : ' s . log_version ( : int -> unit ) -> ' s impl -> ' s Implementation . t } let registry = Int . Table . create ~ size : 1 ( ) let implement_multi_gen ? log_not_previously_seen_version impl = let log_version = match log_not_previously_seen_version with | None -> ignore | Some f -> Memo . general ( f ~ name ) in List . map ( Hashtbl . data registry ) ~ f ( : fun ( i , _ ) -> i . implement ~ log_version impl ) let implement_multi ? log_not_previously_seen_version f = implement_multi_gen ? log_not_previously_seen_version ( Pipe f ) let implement_direct_multi ? log_not_previously_seen_version f = implement_multi_gen ? log_not_previously_seen_version ( Direct f ) let rpcs ( ) = List . map ( Hashtbl . data registry ) ~ f ( : fun ( _ , rpc ) -> rpc ) let versions ( ) = Int . Set . of_list ( Int . Table . keys registry ) module type Version_shared = sig type query [ @@ deriving bin_io ] type response [ @@ deriving bin_io ] type error [ @@ deriving bin_io ] val version : int val model_of_query : query -> Model . query val error_of_model : Model . error -> error val client_pushes_back : bool end module Make_shared ( Version_i : Version_shared ) ( Convert : sig val convert_elt ( : Model . response -> Version_i . response ) Or_error . t val convert_pipe : Model . response Pipe . Reader . t -> Version_i . response Pipe . Reader . t end ) = struct open Version_i open Convert let rpc = Pipe_rpc . create ~ name ~ version ~ bin_query ~ bin_response ~ bin_error ? client_pushes_back ( : Option . some_if client_pushes_back ( ) ) ( ) let wrapped_model_of_query q = match Version_i . model_of_query q with | exception exn -> Error . raise ( failed_conversion ( ` Response , ` Rpc name , ` Version version , exn ) ) | q -> q let wrapped_error_of_model error = match Version_i . error_of_model error with | error -> Error error | exception exn -> Error . raise ( failed_conversion ( ` Error , ` Rpc name , ` Version version , exn ) ) let implement ~ log_version impl = match impl with | Pipe f -> Pipe_rpc . implement rpc ( fun s q -> log_version version ; f s ~ version ( wrapped_model_of_query q ) >>= function | Ok pipe -> Monitor . handle_errors ( fun ( ) -> return ( Ok ( convert_pipe pipe ) ) ) ( fun exn -> Error . raise ( failed_conversion ( ` Response , ` Rpc name , ` Version version , exn ) ) ) | Error error -> return ( wrapped_error_of_model error ) ) | Direct f -> let convert_elt = Or_error . ok_exn convert_elt in Pipe_rpc . implement_direct rpc ( fun s q dsw -> let writer = Versioned_direct_stream_writer . create ~ convert : convert_elt ~ writer : dsw in f s ~ version ( wrapped_model_of_query q ) writer >>| function | Ok ( ) -> Ok ( ) | Error error -> wrapped_error_of_model error ) let ( ) = match Hashtbl . find registry version with | None -> Hashtbl . set registry ~ key : version ~ data ( { : implement } , Any . Pipe rpc ) | Some _ -> Error . raise ( multiple_registrations ( ` Rpc name , ` Version version ) ) end module Register_raw ( Version_i : sig include Version_shared val response_of_model : Model . response Pipe . Reader . t -> response Pipe . Reader . t end ) = Make_shared ( Version_i ) ( struct let convert_elt = Or_error . error_string " cannot use direct interface with Register_raw " let convert_pipe = Version_i . response_of_model end ) module Register ( Version_i : sig include Version_shared val response_of_model : Model . response -> response end ) = Make_shared ( Version_i ) ( struct let convert_elt = Ok Version_i . response_of_model let convert_pipe pipe = Pipe . map pipe ~ f : Version_i . response_of_model end ) end end module State_rpc = struct module type S = sig type query type state type update type error val implement_multi : ? log_not_previously_seen_version ( : name : string -> int -> unit ) -> ( ' connection_state -> version : int -> query -> ( state * update Pipe . Reader . t , error ) Result . t Deferred . t ) -> ' connection_state Implementation . t list val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string type query type state type update type error end ) = struct let name = Model . name type ' s impl = ' s -> version : int -> Model . query -> ( Model . state * Model . update Pipe . Reader . t , Model . error ) Result . t Deferred . t type implementer = { implement : ' s . log_version ( : int -> unit ) -> ' s impl -> ' s Implementation . t } let registry = Int . Table . create ~ size : 1 ( ) let implement_multi ? log_not_previously_seen_version f = let log_version = match log_not_previously_seen_version with | None -> ignore | Some f -> Memo . general ( f ~ name ) in List . map ( Hashtbl . data registry ) ~ f ( : fun ( i , _ ) -> i . implement ~ log_version f ) let rpcs ( ) = List . map ( Hashtbl . data registry ) ~ f ( : fun ( _ , rpc ) -> rpc ) let versions ( ) = Int . Set . of_list ( Int . Table . keys registry ) module type Version_shared = sig type query [ @@ deriving bin_io ] type state [ @@ deriving bin_io ] type update [ @@ deriving bin_io ] type error [ @@ deriving bin_io ] val version : int val model_of_query : query -> Model . query val state_of_model : Model . state -> state val error_of_model : Model . error -> error val client_pushes_back : bool end module Register_raw ( Version_i : sig include Version_shared val update_of_model : Model . state -> Model . update Pipe . Reader . t -> update Pipe . Reader . t end ) = struct open Version_i let rpc = State_rpc . create ~ name ~ version ~ bin_query ~ bin_state ~ bin_update ~ bin_error ? client_pushes_back ( : Option . some_if client_pushes_back ( ) ) ( ) let ( ) = let implement ~ log_version f = State_rpc . implement rpc ( fun s q -> log_version version ; match Version_i . model_of_query q with | exception exn -> Error . raise ( failed_conversion ( ` Response , ` Rpc name , ` Version version , exn ) ) | q -> f s ~ version q >>= function | Ok ( model_state , pipe ) -> let state = match Version_i . state_of_model model_state with | state -> state | exception exn -> Error . raise ( failed_conversion ( ` State , ` Rpc name , ` Version version , exn ) ) in Monitor . handle_errors ( fun ( ) -> return ( Ok ( state , Version_i . update_of_model model_state pipe ) ) ) ( fun exn -> Error . raise ( failed_conversion ( ` Update , ` Rpc name , ` Version version , exn ) ) ) | Error error -> return ( match Version_i . error_of_model error with | error -> Error error | exception exn -> Error . raise ( failed_conversion ( ` Error , ` Rpc name , ` Version version , exn ) ) ) ) in match Hashtbl . find registry version with | None -> Hashtbl . set registry ~ key : version ~ data ( { : implement } , Any . State rpc ) | Some _ -> Error . raise ( multiple_registrations ( ` Rpc name , ` Version version ) ) end module Register ( Version_i : sig include Version_shared val update_of_model : Model . update -> update end ) = struct include Register_raw ( struct include Version_i let update_of_model _state pipe = Pipe . map ~ f : update_of_model pipe end ) end end end module One_way = struct module type S = sig type msg val implement_multi : ? log_not_previously_seen_version ( : name : string -> int -> unit ) -> ( ' state -> version : int -> msg -> unit ) -> ' state Implementation . t list val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string type msg end ) = struct let name = Model . name type ' s impl = ' s -> version : int -> Model . msg -> unit type implementer = { implement : ' s . log_version ( : int -> unit ) -> ' s impl -> ' s Implementation . t ; } let registry : ( int , implementer * Any . t ) Hashtbl . t = Int . Table . create ~ size : 1 ( ) let implement_multi ? log_not_previously_seen_version f = let log_version = match log_not_previously_seen_version with | None -> ignore | Some f -> Memo . general ( f ~ name ) in List . map ( Hashtbl . data registry ) ~ f ( : fun ( i , _rpc ) -> i . implement ~ log_version f ) let rpcs ( ) = List . map ( Hashtbl . data registry ) ~ f ( : fun ( _ , rpc ) -> rpc ) let versions ( ) = Int . Set . of_list ( Hashtbl . keys registry ) module Register ( Version_i : sig type msg [ @@ deriving bin_io ] val version : int val model_of_msg : msg -> Model . msg end ) = struct open Version_i let rpc = One_way . create ~ name ~ version ~ bin_msg let ( ) = let implement ~ log_version f = One_way . implement rpc ( fun s q -> log_version version ; match Result . try_with ( fun ( ) -> Version_i . model_of_msg q ) with | Error exn -> Error . raise ( failed_conversion ( ` Msg , ` Rpc name , ` Version version , exn ) ) | Ok q -> f s ~ version q ) in match Hashtbl . find registry version with | None -> Hashtbl . set registry ~ key : version ~ data ( { : implement } , Any . One_way rpc ) | Some _ -> Error . raise ( multiple_registrations ( ` Rpc name , ` Version version ) ) end end end end
module Menu = struct module Model = struct let name = " __Versioned_rpc . Menu " type query = unit type response = Description . t list end include Callee_converts . Rpc . Make ( Model ) let rpc_name = Model . name module V1 = struct module T = struct let version = 1 type query = unit [ @@ deriving bin_io ] type response = ( string * int ) list [ @@ deriving bin_io ] let model_of_query q = q let response_of_model = List . map ~ f ( : fun { Description . name ; version } -> ( name , version ) ) end include T include Register ( T ) end module Current_version = V1 let add impls = let menu = List . map impls ~ f : Implementation . description in let menu_impls = implement_multi ( fun _ ~ version : _ ( ) -> return menu ) in impls @ menu_impls type t = Int . Set . t String . Table . t [ @@ deriving sexp_of ] let supported_rpcs t = let open List . Monad_infix in String . Table . to_alist t >>= fun ( name , versions ) -> Int . Set . to_list versions >>| fun version -> { Description . name ; version } let supported_versions t ~ rpc_name = Option . value ~ default : Int . Set . empty ( Hashtbl . find t rpc_name ) let of_entries entries = Hashtbl . map ~ f : Int . Set . of_list ( String . Table . of_alist_multi entries ) let request conn = Rpc . dispatch Current_version . rpc conn ( ) >>| fun result -> Result . map result ~ f : of_entries let create descriptions = List . map descriptions ~ f ( : fun { Description . name ; version } -> ( name , version ) ) |> of_entries end
module Connection_with_menu = struct type t = { connection : Connection . t ; menu : Menu . t } [ @@ deriving fields ] let create connection = let open Deferred . Or_error . Monad_infix in Menu . request connection >>| fun menu -> { connection ; menu } let create_directly connection menu = { connection ; menu } end
module Caller_converts = struct let most_recent_common_version ~ rpc_name ~ caller_versions ~ callee_versions ~ callee_menu = match Set . max_elt ( Set . inter callee_versions caller_versions ) with | Some version -> Ok version | None -> error_s [ % message " caller and callee share no common versions for rpc " ( rpc_name : string ) ( caller_versions : Int . Set . t ) ( callee_versions : Int . Set . t ) ( callee_menu : Menu . t ) ] let % expect_test " highest version number is taken in most_recent_common_version " = let rpc_name = " the - rpc " in let menu = Menu . of_entries [ rpc_name , 2 ] in let result = most_recent_common_version ~ rpc_name ~ caller_versions ( : Int . Set . of_list [ 1 ; 2 ; 3 ] ) ~ callee_versions ( : Int . Set . of_list [ 2 ] ) ~ callee_menu : menu in print_s [ % sexp ( result : int Or_error . t ) ] ; [ % expect { | ( Ok 2 ) } ] | ; ; let % expect_test " error from most_recent_common_version looks reasonable " = let the_rpc = " the - rpc " in let not_the_rpc = " other - rpc " in let menu = Menu . of_entries [ not_the_rpc , 1 ; not_the_rpc , 2 ] in let result = most_recent_common_version ~ rpc_name : the_rpc ~ caller_versions ( : Int . Set . of_list [ 1 ; 2 ; 3 ] ) ~ callee_versions ( : Menu . supported_versions menu ~ rpc_name : the_rpc ) ~ callee_menu : menu in print_s [ % sexp ( result : int Or_error . t ) ] ; [ % expect { | ( Error ( " caller and callee share no common versions for rpc " ( rpc_name the - rpc ) ( caller_versions ( 1 2 3 ) ) ( callee_versions ( ) ) ( callee_menu ( ( other - rpc ( 1 2 ) ) ) ) ) ) } ] | ; ; module Dispatch = struct module Make ( M : Monad ) = struct open M let with_specific_version ~ version ~ connection ~ name ~ query ~ dispatcher ~ registry = match Hashtbl . find registry version with | None -> return ( Error ( unknown_version ( name , version ) ) ) | Some ( dispatch , _rpc ) -> dispatcher dispatch connection query let with_version_menu { Connection_with_menu . connection ; menu } query ~ name ~ versions ~ registry ~ dispatcher = let callee_versions = Menu . supported_versions menu ~ rpc_name : name in let caller_versions = versions ( ) in match most_recent_common_version ~ rpc_name : name ~ caller_versions ~ callee_versions ~ callee_menu : menu with | Error e -> return ( Error e ) | Ok version -> with_specific_version ~ version ~ connection ~ name ~ query ~ registry ~ dispatcher end module Async = Make ( Deferred ) module Direct = Make ( Monad . Ident ) end module Rpc = struct module type S = sig type query type response val dispatch_multi : Connection_with_menu . t -> query -> response Or_error . t Deferred . t val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string type query type response end ) = struct let name = Model . name let registry = Int . Table . create ~ size : 1 ( ) let rpcs ( ) = List . map ( Hashtbl . data registry ) ~ f ( : fun ( _ , rpc ) -> rpc ) let versions ( ) = Int . Set . of_list ( Int . Table . keys registry ) let dispatch_multi conn_with_menu query = Dispatch . Async . with_version_menu conn_with_menu query ~ name ~ versions ~ registry ~ dispatcher : Fn . id module Register ' ( Version_i : sig type query [ @@ deriving bin_io ] type response [ @@ deriving bin_io ] val version : int val query_of_model : Model . query -> query val model_of_response : Model . query -> response -> Model . response end ) = struct open Version_i let rpc = Rpc . create ~ name ~ version ~ bin_query ~ bin_response let ( ) = let dispatch conn mq = match Result . try_with ( fun ( ) -> Version_i . query_of_model mq ) with | Error exn -> return ( Error ( failed_conversion ( ` Query , ` Rpc name , ` Version version , exn ) ) ) | Ok q -> Rpc . dispatch rpc conn q >>| fun result -> Result . bind result ~ f ( : fun r -> match Result . try_with ( fun ( ) -> Version_i . model_of_response mq r ) with | Ok r -> Ok r | Error exn -> Error ( failed_conversion ( ` Response , ` Rpc name , ` Version version , exn ) ) ) in match Hashtbl . find registry version with | None -> Hashtbl . set registry ~ key : version ~ data ( : dispatch , Any . Rpc rpc ) | Some _ -> Error . raise ( multiple_registrations ( ` Rpc name , ` Version version ) ) end module Register ( Version_i : sig type query [ @@ deriving bin_io ] type response [ @@ deriving bin_io ] val version : int val query_of_model : Model . query -> query val model_of_response : response -> Model . response end ) = Register ' ( struct include Version_i let model_of_response _ r = model_of_response r end ) end end module Pipe_rpc = struct module type S = sig type query type response type error val dispatch_multi : Connection_with_menu . t -> query -> ( response Or_error . t Pipe . Reader . t * Pipe_rpc . Metadata . t , error ) Result . t Or_error . t Deferred . t val dispatch_iter_multi : Connection_with_menu . t -> query -> f ( : response Pipe_rpc . Pipe_message . t -> Pipe_rpc . Pipe_response . t ) -> ( Pipe_rpc . Id . t , error ) Result . t Or_error . t Deferred . t val abort_multi : Connection_with_menu . t -> Pipe_rpc . Id . t -> unit Or_error . t val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string type query type response type error end ) = struct type dispatcher = { abort : Connection . t -> Pipe_rpc . Id . t -> unit ; dispatch : Connection . t -> Model . query -> ( Model . response Or_error . t Pipe . Reader . t * Pipe_rpc . Metadata . t , Model . error ) Result . t Or_error . t Deferred . t ; dispatch_iter : Connection . t -> Model . query -> f ( : Model . response Pipe_rpc . Pipe_message . t -> Pipe_rpc . Pipe_response . t ) -> ( Pipe_rpc . Id . t , Model . error ) Result . t Or_error . t Deferred . t } let name = Model . name let registry : ( dispatcher * Any . t ) Int . Table . t = Int . Table . create ~ size : 1 ( ) let rpcs ( ) = List . map ( Hashtbl . data registry ) ~ f ( : fun ( _ , rpc ) -> rpc ) let versions ( ) = Int . Set . of_list ( Int . Table . keys registry ) let dispatch_iter_multi conn_with_menu query ~ f = Dispatch . Async . with_version_menu conn_with_menu query ~ name ~ versions ~ registry ~ dispatcher ( : fun { dispatch_iter ; _ } conn query -> dispatch_iter conn query ~ f ) let dispatch_multi conn_with_menu query = Dispatch . Async . with_version_menu conn_with_menu query ~ name ~ versions ~ registry ~ dispatcher ( : fun { dispatch ; _ } conn query -> dispatch conn query ) let abort_multi conn_with_menu id = Dispatch . Direct . with_version_menu conn_with_menu id ~ name ~ versions ~ registry ~ dispatcher ( : fun { abort ; _ } conn id -> abort conn id ; Ok ( ) ) module type Version_shared = sig type query [ @@ deriving bin_io ] type response [ @@ deriving bin_io ] type error [ @@ deriving bin_io ] val version : int val query_of_model : Model . query -> query val model_of_error : error -> Model . error val client_pushes_back : bool end module Make_shared ( Version_i : Version_shared ) ( Convert : sig val convert_elt : ( Version_i . response -> Model . response ) Or_error . t val convert_pipe : Version_i . response Pipe . Reader . t -> Model . response Or_error . t Pipe . Reader . t end ) = struct open Version_i open Convert let rpc = Pipe_rpc . create ~ name ~ version ~ bin_query ~ bin_response ~ bin_error ? client_pushes_back ( : Option . some_if client_pushes_back ( ) ) ( ) let wrapped_query_of_model q = match Version_i . query_of_model q with | exception exn -> return ( Error ( failed_conversion ( ` Query , ` Rpc name , ` Version version , exn ) ) ) | q -> return ( Ok q ) let convert_result result ~ convert_ok = match result with | Error _ as e -> e | Ok ( Error e ) -> ( match Version_i . model_of_error e with | e ' -> Ok ( Error e ' ) | exception exn -> Error ( failed_conversion ( ` Error , ` Rpc name , ` Version version , exn ) ) ) | Ok ( Ok ok ) -> Ok ( Ok ( convert_ok ok ) ) let dispatch conn q = wrapped_query_of_model q >>=? fun q -> Pipe_rpc . dispatch rpc conn q >>| fun result -> convert_result result ~ convert_ok ( : fun ( pipe , id ) -> ( convert_pipe pipe , id ) ) let dispatch_iter conn q ~ f = let convert_elt = Or_error . ok_exn convert_elt in wrapped_query_of_model q >>=? fun q -> let convert_message ( m : _ Pipe_rpc . Pipe_message . t ) = match m with | Closed _ as closed -> closed | Update u -> Update ( convert_elt u ) in Pipe_rpc . dispatch_iter rpc conn q ~ f ( : fun message -> f ( convert_message message ) ) >>| fun result -> convert_result result ~ convert_ok : Fn . id let abort conn id = Pipe_rpc . abort rpc conn id let ( ) = match Hashtbl . find registry version with | None -> Hashtbl . set registry ~ key : version ~ data ( { : abort ; dispatch ; dispatch_iter } , Any . Pipe rpc ) | Some _ -> Error . raise ( multiple_registrations ( ` Rpc name , ` Version version ) ) end module Register_raw ( Version_i : sig include Version_shared val model_of_response : response Pipe . Reader . t -> Model . response Or_error . t Pipe . Reader . t end ) = Make_shared ( Version_i ) ( struct let convert_elt = Or_error . error_string " Cannot use Direct with Register_raw " let convert_pipe = Version_i . model_of_response end ) module Register ( Version_i : sig include Version_shared val model_of_response : response -> Model . response end ) = Make_shared ( Version_i ) ( struct let convert_elt = Ok Version_i . model_of_response let convert_pipe rs = Pipe . map rs ~ f ( : fun r -> match Version_i . model_of_response r with | r -> Ok r | exception exn -> Error ( failed_conversion ( ` Response , ` Rpc name , ` Version Version_i . version , exn ) ) ) end ) end end module State_rpc = struct module type S = sig type query type state type update type error val dispatch_multi : Connection_with_menu . t -> query -> ( state * update Or_error . t Pipe . Reader . t * State_rpc . Metadata . t , error ) Result . t Or_error . t Deferred . t val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string type query type state type update type error end ) = struct let name = Model . name let registry = Int . Table . create ~ size : 1 ( ) let rpcs ( ) = List . map ( Hashtbl . data registry ) ~ f ( : fun ( _ , rpc ) -> rpc ) let versions ( ) = Int . Set . of_list ( Int . Table . keys registry ) let dispatch_multi conn_with_menu query = Dispatch . Async . with_version_menu conn_with_menu query ~ name ~ versions ~ registry ~ dispatcher : Fn . id module type Version_shared = sig type query [ @@ deriving bin_io ] type state [ @@ deriving bin_io ] type update [ @@ deriving bin_io ] type error [ @@ deriving bin_io ] val version : int val query_of_model : Model . query -> query val model_of_state : state -> Model . state val model_of_error : error -> Model . error val client_pushes_back : bool end module Register_raw ( Version_i : sig include Version_shared val model_of_update : update Pipe . Reader . t -> Model . update Or_error . t Pipe . Reader . t end ) = struct open Version_i let rpc = State_rpc . create ~ name ~ version ~ bin_query ~ bin_state ~ bin_update ~ bin_error ? client_pushes_back ( : Option . some_if client_pushes_back ( ) ) ( ) let ( ) = let dispatch conn q = match Version_i . query_of_model q with | exception exn -> return ( Error ( failed_conversion ( ` Query , ` Rpc name , ` Version version , exn ) ) ) | q -> State_rpc . dispatch rpc conn q >>| fun result -> match result with | Error exn -> Error exn | Ok ( Error e ) -> ( match Version_i . model_of_error e with | e ' -> Ok ( Error e ' ) | exception exn -> Error ( failed_conversion ( ` Error , ` Rpc name , ` Version version , exn ) ) ) | Ok ( Ok ( state , pipe , id ) ) -> match Version_i . model_of_state state with | exception exn -> Error ( failed_conversion ( ` State , ` Rpc name , ` Version version , exn ) ) | state -> Ok ( Ok ( state , Version_i . model_of_update pipe , id ) ) in match Hashtbl . find registry version with | None -> Hashtbl . set registry ~ key : version ~ data ( : dispatch , Any . State rpc ) | Some _ -> Error . raise ( multiple_registrations ( ` Rpc name , ` Version version ) ) end module Register ( Version_i : sig include Version_shared val model_of_update : update -> Model . update end ) = struct include Register_raw ( struct include Version_i let model_of_update rs = Pipe . map rs ~ f ( : fun r -> match Version_i . model_of_update r with | r -> Ok r | exception exn -> Error ( failed_conversion ( ` Update , ` Rpc name , ` Version version , exn ) ) ) end ) end end end module One_way = struct module type S = sig type msg val dispatch_multi : Connection_with_menu . t -> msg -> unit Or_error . t val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string type msg end ) = struct let name = Model . name let registry = Int . Table . create ~ size : 1 ( ) let rpcs ( ) = List . map ( Hashtbl . data registry ) ~ f ( : fun ( _ , rpc ) -> rpc ) let versions ( ) = Int . Set . of_list ( Int . Table . keys registry ) let dispatch_multi conn_with_menu msg = Dispatch . Direct . with_version_menu conn_with_menu msg ~ name ~ versions ~ registry ~ dispatcher : Fn . id module Register ( Version_i : sig type msg [ @@ deriving bin_io ] val version : int val msg_of_model : Model . msg -> msg end ) = struct open Version_i let rpc = One_way . create ~ name ~ version ~ bin_msg let ( ) = let dispatch conn q = match Result . try_with ( fun ( ) -> Version_i . msg_of_model q ) with | Error exn -> Error ( failed_conversion ( ` Msg , ` Rpc name , ` Version version , exn ) ) | Ok q -> One_way . dispatch rpc conn q in match Hashtbl . find registry version with | None -> Hashtbl . set registry ~ key : version ~ data ( : dispatch , Any . One_way rpc ) | Some _ -> Error . raise ( multiple_registrations ( ` Rpc name , ` Version version ) ) end end end end
module Both_convert = struct module Plain = struct module type S = sig type caller_query type callee_query type caller_response type callee_response val dispatch_multi : Connection_with_menu . t -> caller_query -> caller_response Or_error . t Deferred . t val implement_multi : ? log_not_previously_seen_version ( : name : string -> int -> unit ) -> ( ' state -> version : int -> callee_query -> callee_response Deferred . t ) -> ' state Implementation . t list val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string module Caller : sig type query type response end module Callee : sig type query type response end end ) = struct open Model let name = name module Caller = Caller_converts . Rpc . Make ( struct let name = name include Caller end ) module Callee = Callee_converts . Rpc . Make ( struct let name = name include Callee end ) let % test _ = Int . Set . equal ( Caller . versions ( ) ) ( Callee . versions ( ) ) module Register ( Version : sig open Model val version : int type query [ @@ deriving bin_io ] type response [ @@ deriving bin_io ] val query_of_caller_model : Caller . query -> query val callee_model_of_query : query -> Callee . query val response_of_callee_model : Callee . response -> response val caller_model_of_response : response -> Caller . response end ) = struct include Callee . Register ( struct include Version let model_of_query = callee_model_of_query let response_of_model = response_of_callee_model end ) include Caller . Register ( struct include Version let query_of_model = query_of_caller_model let model_of_response = caller_model_of_response end ) let % test _ = Int . Set . equal ( Caller . versions ( ) ) ( Callee . versions ( ) ) end let dispatch_multi = Caller . dispatch_multi let implement_multi = Callee . implement_multi let versions ( ) = Caller . versions ( ) let rpcs ( ) = Caller . rpcs ( ) end end module Pipe_rpc = struct module type S = sig type caller_query type callee_query type caller_response type callee_response type caller_error type callee_error val dispatch_multi : Connection_with_menu . t -> caller_query -> ( caller_response Or_error . t Pipe . Reader . t * Pipe_rpc . Metadata . t , caller_error ) Result . t Or_error . t Deferred . t val dispatch_iter_multi : Connection_with_menu . t -> caller_query -> f ( : caller_response Pipe_rpc . Pipe_message . t -> Pipe_rpc . Pipe_response . t ) -> ( Pipe_rpc . Id . t , caller_error ) Result . t Or_error . t Deferred . t val abort_multi : Connection_with_menu . t -> Pipe_rpc . Id . t -> unit Or_error . t val implement_multi : ? log_not_previously_seen_version ( : name : string -> int -> unit ) -> ( ' state -> version : int -> callee_query -> ( callee_response Pipe . Reader . t , callee_error ) Result . t Deferred . t ) -> ' state Implementation . t list val implement_direct_multi : ? log_not_previously_seen_version ( : name : string -> int -> unit ) -> ( ' state -> version : int -> callee_query -> callee_response Versioned_direct_stream_writer . t -> ( unit , callee_error ) Result . t Deferred . t ) -> ' state Implementation . t list val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string module Caller : sig type query type response type error end module Callee : sig type query type response type error end end ) = struct open Model let name = name module Caller = Caller_converts . Pipe_rpc . Make ( struct let name = name include Caller end ) module Callee = Callee_converts . Pipe_rpc . Make ( struct let name = name include Callee end ) let % test _ = Int . Set . equal ( Caller . versions ( ) ) ( Callee . versions ( ) ) module type Version_shared = sig val version : int type query [ @@ deriving bin_io ] type response [ @@ deriving bin_io ] type error [ @@ deriving bin_io ] val query_of_caller_model : Model . Caller . query -> query val callee_model_of_query : query -> Model . Callee . query val error_of_callee_model : Model . Callee . error -> error val caller_model_of_error : error -> Model . Caller . error val client_pushes_back : bool end module Register_raw ( Version_i : sig include Version_shared val response_of_callee_model : Model . Callee . response Pipe . Reader . t -> response Pipe . Reader . t val caller_model_of_response : response Pipe . Reader . t -> Model . Caller . response Or_error . t Pipe . Reader . t end ) = struct include Callee . Register_raw ( struct include Version_i let model_of_query = callee_model_of_query let response_of_model = response_of_callee_model let error_of_model = error_of_callee_model end ) include Caller . Register_raw ( struct include Version_i let query_of_model = query_of_caller_model let model_of_response = caller_model_of_response let model_of_error = caller_model_of_error end ) end module Register ( Version_i : sig include Version_shared val response_of_callee_model : Model . Callee . response -> response val caller_model_of_response : response -> Model . Caller . response end ) = struct include Callee . Register ( struct include Version_i let model_of_query = callee_model_of_query let response_of_model = response_of_callee_model let error_of_model = error_of_callee_model end ) include Caller . Register ( struct include Version_i let query_of_model = query_of_caller_model let model_of_response = caller_model_of_response let model_of_error = caller_model_of_error end ) end let dispatch_multi = Caller . dispatch_multi let dispatch_iter_multi = Caller . dispatch_iter_multi let abort_multi = Caller . abort_multi let implement_multi = Callee . implement_multi let implement_direct_multi = Callee . implement_direct_multi let versions ( ) = Caller . versions ( ) let rpcs ( ) = Caller . rpcs ( ) end end module State_rpc = struct module type S = sig type caller_query type callee_query type caller_state type callee_state type caller_update type callee_update type caller_error type callee_error val dispatch_multi : Connection_with_menu . t -> caller_query -> ( caller_state * caller_update Or_error . t Pipe . Reader . t * State_rpc . Metadata . t , caller_error ) Result . t Or_error . t Deferred . t val implement_multi : ? log_not_previously_seen_version ( : name : string -> int -> unit ) -> ( ' state -> version : int -> callee_query -> ( callee_state * callee_update Pipe . Reader . t , callee_error ) Result . t Deferred . t ) -> ' state Implementation . t list val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string module Caller : sig type query type state type update type error end module Callee : sig type query type state type update type error end end ) = struct open Model let name = name module Caller = Caller_converts . State_rpc . Make ( struct let name = name include Caller end ) module Callee = Callee_converts . State_rpc . Make ( struct let name = name include Callee end ) let % test _ = Int . Set . equal ( Caller . versions ( ) ) ( Callee . versions ( ) ) module type Version_shared = sig val version : int type query [ @@ deriving bin_io ] type state [ @@ deriving bin_io ] type update [ @@ deriving bin_io ] type error [ @@ deriving bin_io ] val query_of_caller_model : Model . Caller . query -> query val callee_model_of_query : query -> Model . Callee . query val caller_model_of_state : state -> Model . Caller . state val state_of_callee_model : Model . Callee . state -> state val caller_model_of_error : error -> Model . Caller . error val error_of_callee_model : Model . Callee . error -> error val client_pushes_back : bool end module Register_raw ( Version_i : sig include Version_shared val caller_model_of_update : update Pipe . Reader . t -> Model . Caller . update Or_error . t Pipe . Reader . t val update_of_callee_model : Model . Callee . state -> Model . Callee . update Pipe . Reader . t -> update Pipe . Reader . t end ) = struct include Callee . Register_raw ( struct include Version_i let model_of_query = callee_model_of_query let state_of_model = state_of_callee_model let update_of_model = update_of_callee_model let error_of_model = error_of_callee_model end ) include Caller . Register_raw ( struct include Version_i let query_of_model = query_of_caller_model let model_of_state = caller_model_of_state let model_of_update = caller_model_of_update let model_of_error = caller_model_of_error end ) end module Register ( Version_i : sig include Version_shared val update_of_callee_model : Model . Callee . update -> update val caller_model_of_update : update -> Model . Caller . update end ) = struct include Callee . Register ( struct include Version_i let model_of_query = callee_model_of_query let state_of_model = state_of_callee_model let update_of_model = update_of_callee_model let error_of_model = error_of_callee_model end ) include Caller . Register ( struct include Version_i let query_of_model = query_of_caller_model let model_of_state = caller_model_of_state let model_of_update = caller_model_of_update let model_of_error = caller_model_of_error end ) end let dispatch_multi = Caller . dispatch_multi let implement_multi = Callee . implement_multi let versions ( ) = Caller . versions ( ) let rpcs ( ) = Caller . rpcs ( ) end end module One_way = struct module type S = sig type caller_msg type callee_msg val dispatch_multi : Connection_with_menu . t -> caller_msg -> unit Or_error . t val implement_multi : ? log_not_previously_seen_version ( : name : string -> int -> unit ) -> ( ' state -> version : int -> callee_msg -> unit ) -> ' state Implementation . t list val rpcs : unit -> Any . t list val versions : unit -> Int . Set . t val name : string end module Make ( Model : sig val name : string module Caller : sig type msg end module Callee : sig type msg end end ) = struct open Model let name = name module Caller = Caller_converts . One_way . Make ( struct let name = name include Caller end ) module Callee = Callee_converts . One_way . Make ( struct let name = name include Callee end ) let % test _ = Int . Set . equal ( Caller . versions ( ) ) ( Callee . versions ( ) ) module Register ( Version : sig open Model val version : int type msg [ @@ deriving bin_io ] val msg_of_caller_model : Caller . msg -> msg val callee_model_of_msg : msg -> Callee . msg end ) = struct include Callee . Register ( struct include Version let model_of_msg = callee_model_of_msg end ) include Caller . Register ( struct include Version let msg_of_model = msg_of_caller_model end ) let % test _ = Int . Set . equal ( Caller . versions ( ) ) ( Callee . versions ( ) ) end let dispatch_multi = Caller . dispatch_multi let implement_multi = Callee . implement_multi let versions ( ) = Caller . versions ( ) let rpcs ( ) = Caller . rpcs ( ) end end end
let from_just op msg = match op with | Some x -> x | None -> failwith @@ msg " ^: Expected Some . Got None . "
module Serialization ( S : sig type t val t : t Irmin . Type . t end ) = struct open S let pp = Irmin . Type . pp_json ~ minify : false t let of_string s = let decoder = Jsonm . decoder ( ` String s ) in let res = try Irmin . Type . decode_json t decoder with Invalid_argument s -> ( failwith @@ sprintf " AO_Value . of_string :\ \ Invalid_argument : % s " s ) in let _ = match res with | Ok _ -> ( ) | Error ( ` Msg str ) -> ( printf " Decoding error : % s \ n " str ; printf " While decoding : % s \ n " s ) in res end
module MakeVersionedDS ( Config : CONFIG ) ( OM : MERGEABLE ) ( AO_value : Irmin . Contents . Conv with type t = OM . t ) = struct module S = Irmin_git . AO ( Git_unix . FS ) ( AO_value ) include AO_value type adt = OM . t let create config = let level = Irmin . Private . Conf . key ~ doc " : The Zlib compression level . " " level " Irmin . Private . Conf . ( some int ) None in let root = Irmin . Private . Conf . get config Irmin . Private . Conf . root in let level = Irmin . Private . Conf . get config level in Git_unix . FS . create ? root ? level ( ) let create ( ) = create @@ Irmin_git . config Config . root module type MY_TREE = TAG_TREE with type value = AO_value . t let add_and_link : type a . ( module MY_TREE with type t = a ) -> S . t -> AO_value . t -> a -> ( K . t * a ) Lwt . t = fun ( module T ) t v tree -> ( S . add t v ) >>= fun k -> let tag = T . tag_of_hash k in T . add tree tag v >>= fun tree ' -> Lwt . return ( k , tree ' ) let of_adt : type a . ( module MY_TREE with type t = a ) -> adt -> a -> ( t * a ) Lwt . t = fun ( module T ) adt tr -> begin create ( ) >>= fun ao_store -> add_and_link ( module T ) ao_store adt tr >>= fun ( _ , tr ' ) -> Lwt . return ( adt , tr ' ) end let to_adt ( t : t ) : adt Lwt . t = Lwt . return t let merge ( ~ old : t Irmin . Merge . promise ) ( v1 : t ) ( v2 : t ) = if v1 = v2 then Irmin . Merge . ok v1 else begin let open Irmin . Merge . Infix in old ( ) >>=* fun old -> let old = from_just old " merge " in let v = OM . merge ~ ancestor : old v1 v2 in Irmin . Merge . ok v end let merge = Irmin . Merge . ( option ( v t merge ) ) end
module Make ( Config : CONFIG ) = struct module IWarehouse : IRMIN_DATA_STRUCTURE with type adt = Tpcc . Warehouse . t = struct module OM = Tpcc . Warehouse module AO_value : Irmin . Contents . Conv with type t = OM . t = struct type adt = OM . t type t = OM . t let t = let open Irmin . Type in let open Tpcc . Warehouse in record " t " ( fun w_id w_ytd -> { w_id ; w_ytd } ) |+ field " w_id " id ( fun t -> t . w_id ) |+ field " w_ytd " int32 ( fun t -> t . w_ytd ) |> sealr include Serialization ( struct type t = adt let t = t end ) end include MakeVersionedDS ( Config ) ( OM ) ( AO_value ) end module IDistrict : IRMIN_DATA_STRUCTURE with type adt = Tpcc . District . t = struct module OM = Tpcc . District module AO_value : Irmin . Contents . Conv with type t = OM . t = struct type adt = OM . t type t = OM . t let t = let open Irmin . Type in let open Tpcc . District in ( ( ( ( record " madt " ( fun d_id -> fun d_w_id -> fun d_ytd -> { d_id ; d_w_id ; d_ytd } ) ) |+ ( field " d_id " id ( fun t -> t . d_id ) ) ) |+ ( field " d_w_id " id ( fun t -> t . d_w_id ) ) ) |+ ( field " d_ytd " int32 ( fun t -> t . d_ytd ) ) ) |> sealr include Serialization ( struct type t = adt let t = t end ) end include MakeVersionedDS ( Config ) ( OM ) ( AO_value ) end module IOrder : IRMIN_DATA_STRUCTURE with type adt = Tpcc . Order . t = struct module OM = Tpcc . Order module AO_value : Irmin . Contents . Conv with type t = OM . t = struct type adt = OM . t type t = OM . t let t = let open Irmin . Type in let open Tpcc . Order in ( ( ( ( ( ( ( record " t " ( fun o_id -> fun o_w_id -> fun o_d_id -> fun o_c_id -> fun o_ol_cnt -> fun o_carrier_id -> { o_id ; o_w_id ; o_d_id ; o_c_id ; o_ol_cnt ; o_carrier_id } ) ) |+ ( field " o_id " id ( fun t -> t . o_id ) ) ) |+ ( field " o_w_id " id ( fun t -> t . o_w_id ) ) ) |+ ( field " o_d_id " id ( fun t -> t . o_d_id ) ) ) |+ ( field " o_c_id " id ( fun t -> t . o_c_id ) ) ) |+ ( field " o_ol_cnt " int32 ( fun t -> t . o_ol_cnt ) ) ) |+ ( field " o_carrier_id " bool ( fun t -> t . o_carrier_id ) ) ) |> sealr include Serialization ( struct type t = adt let t = t end ) end include MakeVersionedDS ( Config ) ( OM ) ( AO_value ) end module INewOrder : IRMIN_DATA_STRUCTURE with type adt = Tpcc . NewOrder . t = struct module OM = Tpcc . NewOrder module AO_value : Irmin . Contents . Conv with type t = OM . t = struct type adt = OM . t type t = OM . t let t = let open Irmin . Type in let open Tpcc . NewOrder in ( ( ( ( record " t " ( fun no_o_id -> fun no_d_id -> fun no_w_id -> { no_o_id ; no_d_id ; no_w_id } ) ) |+ ( field " no_o_id " id ( fun t -> t . no_o_id ) ) ) |+ ( field " no_d_id " id ( fun t -> t . no_d_id ) ) ) |+ ( field " no_w_id " id ( fun t -> t . no_w_id ) ) ) |> sealr include Serialization ( struct type t = adt let t = t end ) end include MakeVersionedDS ( Config ) ( OM ) ( AO_value ) end module IOrderLine : IRMIN_DATA_STRUCTURE with type adt = Tpcc . OrderLine . t = struct module OM = Tpcc . OrderLine module AO_value : Irmin . Contents . Conv with type t = OM . t = struct type adt = OM . t type t = OM . t let t = let open Irmin . Type in let open Tpcc . OrderLine in ( ( ( ( ( ( ( ( ( ( record " t " ( fun ol_o_id -> fun ol_d_id -> fun ol_w_id -> fun ol_num -> fun ol_amt -> fun ol_i_id -> fun ol_supply_w_id -> fun ol_qty -> fun ol_delivery_d -> { ol_o_id ; ol_d_id ; ol_w_id ; ol_num ; ol_amt ; ol_i_id ; ol_supply_w_id ; ol_qty ; ol_delivery_d } ) ) |+ ( field " ol_o_id " id ( fun t -> t . ol_o_id ) ) ) |+ ( field " ol_d_id " id ( fun t -> t . ol_d_id ) ) ) |+ ( field " ol_w_id " id ( fun t -> t . ol_w_id ) ) ) |+ ( field " ol_num " int32 ( fun t -> t . ol_num ) ) ) |+ ( field " ol_amt " int32 ( fun t -> t . ol_amt ) ) ) |+ ( field " ol_i_id " id ( fun t -> t . ol_i_id ) ) ) |+ ( field " ol_supply_w_id " id ( fun t -> t . ol_supply_w_id ) ) ) |+ ( field " ol_qty " int32 ( fun t -> t . ol_qty ) ) ) |+ ( field " ol_delivery_d " ( option float ) ( fun t -> t . ol_delivery_d ) ) ) |> sealr include Serialization ( struct type t = adt let t = t end ) end include MakeVersionedDS ( Config ) ( OM ) ( AO_value ) end module IItem : IRMIN_DATA_STRUCTURE with type adt = Tpcc . Item . t = struct module OM = Tpcc . Item module AO_value : Irmin . Contents . Conv with type t = OM . t = struct type adt = OM . t type t = OM . t let t = let open Irmin . Type in let open Tpcc . Item in ( ( ( ( record " t " ( fun i_id -> fun i_name -> fun i_price -> { i_id ; i_name ; i_price } ) ) |+ ( field " i_id " id ( fun t -> t . i_id ) ) ) |+ ( field " i_name " string ( fun t -> t . i_name ) ) ) |+ ( field " i_price " int32 ( fun t -> t . i_price ) ) ) |> sealr include Serialization ( struct type t = adt let t = t end ) end include MakeVersionedDS ( Config ) ( OM ) ( AO_value ) end module IHist : IRMIN_DATA_STRUCTURE with type adt = Tpcc . Hist . t = struct module OM = Tpcc . Hist module AO_value : Irmin . Contents . Conv with type t = OM . t = struct type adt = OM . t type t = OM . t let t = let open Irmin . Type in let open Tpcc . Hist in ( ( ( ( ( ( ( record " t " ( fun h_c_id -> fun h_c_d_id -> fun h_c_w_id -> fun h_d_id -> fun h_w_id -> fun h_amt -> { h_c_id ; h_c_d_id ; h_c_w_id ; h_d_id ; h_w_id ; h_amt } ) ) |+ ( field " h_c_id " id ( fun t -> t . h_c_id ) ) ) |+ ( field " h_c_d_id " id ( fun t -> t . h_c_d_id ) ) ) |+ ( field " h_c_w_id " id ( fun t -> t . h_c_w_id ) ) ) |+ ( field " h_d_id " id ( fun t -> t . h_d_id ) ) ) |+ ( field " h_w_id " id ( fun t -> t . h_w_id ) ) ) |+ ( field " h_amt " int32 ( fun t -> t . h_amt ) ) ) |> sealr include Serialization ( struct type t = adt let t = t end ) end include MakeVersionedDS ( Config ) ( OM ) ( AO_value ) end module IStock : IRMIN_DATA_STRUCTURE with type adt = Tpcc . Stock . t = struct module OM = Tpcc . Stock module AO_value : Irmin . Contents . Conv with type t = OM . t = struct type adt = OM . t type t = OM . t let t = let open Irmin . Type in let open Tpcc . Stock in ( ( ( ( ( ( record " t " ( fun s_i_id -> fun s_w_id -> fun s_qty -> fun s_ytd -> fun s_order_cnt -> { s_i_id ; s_w_id ; s_qty ; s_ytd ; s_order_cnt } ) ) |+ ( field " s_i_id " id ( fun t -> t . s_i_id ) ) ) |+ ( field " s_w_id " id ( fun t -> t . s_w_id ) ) ) |+ ( field " s_qty " int32 ( fun t -> t . s_qty ) ) ) |+ ( field " s_ytd " int32 ( fun t -> t . s_ytd ) ) ) |+ ( field " s_order_cnt " int32 ( fun t -> t . s_order_cnt ) ) ) |> sealr include Serialization ( struct type t = adt let t = t end ) end include MakeVersionedDS ( Config ) ( OM ) ( AO_value ) end module ICustomer : IRMIN_DATA_STRUCTURE with type adt = Tpcc . Customer . t = struct module OM = Tpcc . Customer module AO_value : Irmin . Contents . Conv with type t = OM . t = struct type adt = OM . t type t = OM . t let t = let open Irmin . Type in let open Tpcc . Customer in ( ( ( ( ( ( ( ( record " t " ( fun c_id -> fun c_d_id -> fun c_w_id -> fun c_bal -> fun c_ytd_payment -> fun c_payment_cnt -> fun c_delivery_cnt -> { c_id ; c_d_id ; c_w_id ; c_bal ; c_ytd_payment ; c_payment_cnt ; c_delivery_cnt } ) ) |+ ( field " c_id " id ( fun t -> t . c_id ) ) ) |+ ( field " c_d_id " id ( fun t -> t . c_d_id ) ) ) |+ ( field " c_w_id " id ( fun t -> t . c_w_id ) ) ) |+ ( field " c_bal " int32 ( fun t -> t . c_bal ) ) ) |+ ( field " c_ytd_payment " int32 ( fun t -> t . c_ytd_payment ) ) ) |+ ( field " c_payment_cnt " int32 ( fun t -> t . c_payment_cnt ) ) ) |+ ( field " c_delivery_cnt " int32 ( fun t -> t . c_delivery_cnt ) ) ) |> sealr include Serialization ( struct type t = adt let t = t end ) end include MakeVersionedDS ( Config ) ( OM ) ( AO_value ) end end
type _ witnesses = . .
type _ migration = . .
type _ migration += Undefined : _ migration
type ' a migration_info = { mutable next_version : ' a migration ; mutable previous_version : ' a migration ; }
module type Ast = sig module Parsetree : sig type structure type signature type toplevel_phrase type core_type type expression type pattern type case type type_declaration type type_extension type extension_constructor end module Config : sig val ast_impl_magic_number : string val ast_intf_magic_number : string end end
type ' a _types = ' a constraint ' a = < structure : _ ; signature : _ ; toplevel_phrase : _ ; core_type : _ ; expression : _ ; pattern : _ ; case : _ ; type_declaration : _ ; type_extension : _ ; extension_constructor : _ ; > ; ;
type ' a get_structure = ' x constraint ' a _types = < structure : ' x ; . . >
type ' a get_signature = ' x constraint ' a _types = < signature : ' x ; . . >
type ' a get_toplevel_phrase = ' x constraint ' a _types = < toplevel_phrase : ' x ; . . >
type ' a get_core_type = ' x constraint ' a _types = < core_type : ' x ; . . >
type ' a get_expression = ' x constraint ' a _types = < expression : ' x ; . . >
type ' a get_pattern = ' x constraint ' a _types = < pattern : ' x ; . . >
type ' a get_case = ' x constraint ' a _types = < case : ' x ; . . >
type ' a get_type_declaration = ' x constraint ' a _types = < type_declaration : ' x ; . . >