text
stringlengths 0
601k
|
---|
let never _ = assert false |
let digest ~ kind ( ? off = 0 ) ? len buf = let len = match len with Some len -> len | None -> Bigstringaf . length buf - off in let ctx = SHA1 . empty in let ctx = match kind with | ` A -> SHA1 . feed_string ctx ( Fmt . str " commit % d \ 000 " len ) | ` B -> SHA1 . feed_string ctx ( Fmt . str " tree % d \ 000 " len ) | ` C -> SHA1 . feed_string ctx ( Fmt . str " blob % d \ 000 " len ) | ` D -> SHA1 . feed_string ctx ( Fmt . str " tag % d \ 000 " len ) in let ctx = SHA1 . feed_bigstring ctx ~ off ~ len buf in SHA1 . get ctx |
let ( >>= ) = sched . bind |
let read fd buf ~ off ~ len = let len = input fd buf off len in return len |
let first_pass fpath = let ic = open_in ( Fpath . to_string fpath ) in let zw = De . make_window ~ bits : 15 in let allocate _ = zw in First_pass . check_header sched read ic >>= fun ( max , _ , _ ) -> seek_in ic 0 ; let decoder = First_pass . decoder ~ o : z ~ allocate ( ` Channel ic ) in let children = Hashtbl . create 0x100 in let where = Hashtbl . create 0x100 in let weight = Hashtbl . create 0x100 in let length = Hashtbl . create 0x100 in let carbon = Hashtbl . create 0x100 in let matrix = Array . make max Verify . unresolved_node in let rec go decoder = match First_pass . decode decoder with | ` Await _ | ` Peek _ -> assert false | ` Entry ( { First_pass . kind = Base _ ; offset ; size ; consumed ; _ } , decoder ) -> let n = First_pass . count decoder - 1 in Hashtbl . add weight offset size ; Hashtbl . add length offset size ; Hashtbl . add carbon offset consumed ; Hashtbl . add where offset n ; matrix . ( n ) <- Verify . unresolved_base ~ cursor : offset ; go decoder | ` Entry ( { First_pass . kind = Ofs { sub = s ; source ; target } ; offset ; size ; consumed ; _ ; } , decoder ) -> let n = First_pass . count decoder - 1 in replace weight Int64 . ( sub offset ( Int64 . of_int s ) ) source ; replace weight offset target ; Hashtbl . add length offset size ; Hashtbl . add carbon offset consumed ; Hashtbl . add where offset n ; ( try let vs = Hashtbl . find children ( ` Ofs Int64 . ( sub offset ( of_int s ) ) ) in Hashtbl . replace children ( ` Ofs Int64 . ( sub offset ( of_int s ) ) ) ( offset :: vs ) with _ -> Hashtbl . add children ( ` Ofs Int64 . ( sub offset ( of_int s ) ) ) [ offset ] ) ; go decoder | ` Entry ( { First_pass . kind = Ref { ptr ; target ; source } ; offset ; size ; consumed ; _ ; } , decoder ) -> let n = First_pass . count decoder - 1 in replace weight offset ( Stdlib . max target source ) ; Hashtbl . add length offset size ; Hashtbl . add carbon offset consumed ; Hashtbl . add where offset n ; ( try let vs = Hashtbl . find children ( ` Ref ptr ) in Hashtbl . replace children ( ` Ref ptr ) ( offset :: vs ) with _ -> Hashtbl . add children ( ` Ref ptr ) [ offset ] ) ; go decoder | ` End hash -> close_in ic ; return ( Ok hash ) | ` Malformed err -> return ( Error ( ` Msg err ) ) in go decoder >>= function | Error _ as err -> return err | Ok hash -> let where ~ cursor = Hashtbl . find where cursor in let children ~ cursor ~ uid = match ( Hashtbl . find_opt children ( ` Ofs cursor ) , Hashtbl . find_opt children ( ` Ref uid ) ) with | Some a , Some b -> List . sort_uniq compare ( a @ b ) | Some x , None | None , Some x -> x | None , None -> [ ] in let weight ~ cursor = Hashtbl . find weight cursor in let oracle = { Carton . Dec . where ; children ; digest ; weight } in return ( Ok ( hash , oracle , matrix , length , carbon ) ) |
let map ~ max fd ~ pos len = let len = Stdlib . min len ( Int64 . to_int ( Int64 . sub max pos ) ) in let res = Unix . map_file fd ~ pos Bigarray . char Bigarray . c_layout false [ | len ] | in Bigarray . array1_of_genarray res |
let second_pass fpath ( hash , oracle , matrix ) = let open Fiber in let fd = Unix . openfile ( Fpath . to_string fpath ) Unix . [ O_RDONLY ] 0o644 in let max = ( Unix . LargeFile . fstat fd ) . Unix . LargeFile . st_size in let map fd ~ pos len = map ~ max fd ~ pos len in let pack = Carton . Dec . make fd ~ allocate ~ z ~ uid_ln : SHA1 . length ~ uid_rw : SHA1 . of_raw_string never in Verify . verify ~ threads ( : Fiber . get_concurrency ( ) ) pack ~ map ~ oracle ~ verbose : ignore ~ matrix >>= fun ( ) -> match Array . for_all Verify . is_resolved matrix with | false -> return ( R . error_msgf " Thin PACK file " ) | true -> return ( Ok ( hash , matrix ) ) |
let pp_kind ppf = function | ` A -> Fmt . string ppf " commit " | ` B -> Fmt . string ppf " tree " | ` C -> Fmt . string ppf " blob " | ` D -> Fmt . string ppf " tag " |
let pp_delta ppf status = match Verify . source_of_status status with | Some uid -> Fmt . pf ppf " % d % a " ( Verify . depth_of_status status ) SHA1 . pp uid | None -> ( ) |
let verify_hash ~ memory hash = let max = Bigstringaf . length memory in let hash ' = SHA1 . of_raw_string ( Bigstringaf . substring memory ~ off ( : max - ( 2 * SHA1 . length ) ) ~ len : SHA1 . length ) in SHA1 . equal hash hash ' |
let verify ~ verbose fpath hash length carbon matrix = let fd = Unix . openfile ( Fpath . to_string fpath ) Unix . [ O_RDONLY ] 0o644 in let len = ( Unix . fstat fd ) . Unix . st_size in let memory = Unix . map_file fd ~ pos : 0L Bigarray . char Bigarray . c_layout false [ | len ] | in let memory = Bigarray . array1_of_genarray memory in Unix . close fd ; let idx = Carton . Dec . Idx . make memory ~ uid_ln : SHA1 . length ~ uid_rw : SHA1 . to_raw_string ~ uid_wr : SHA1 . of_raw_string in if not ( verify_hash ~ memory hash ) then return ( R . error_msgf " Invalid PACK hash " ) else match verbose with | false -> if Array . for_all ( Carton . Dec . Idx . exists idx . <> Verify . uid_of_status ) matrix then return ( Ok ( ) ) else return ( R . error_msgf " Invalid PACK file " ) | true -> ( let chains = Hashtbl . create 0x10 in let f status = let uid = Verify . uid_of_status status in let kind = Verify . kind_of_status status in let offset = Verify . offset_of_status status in let ( size : Carton . Dec . weight ) = Hashtbl . find length offset in let size_in_pack = Hashtbl . find carbon offset in let depth = Verify . depth_of_status status in ( match Hashtbl . find chains depth with | v -> Hashtbl . replace chains depth ( succ v ) | exception _ -> Hashtbl . replace chains depth 1 ) ; match Carton . Dec . Idx . find idx uid with | Some ( _crc , offset ' ) when offset = offset ' -> Fmt . pr " % a % a % d % d % Ld % a \ n " %! SHA1 . pp uid pp_kind kind ( size :> int ) size_in_pack offset pp_delta status | _ -> Fmt . failwith " Invalid PACK file " in let pp_chain ppf ( depth , n ) = match depth with | 0 -> Fmt . pf ppf " non delta : % d objects \ n " %! n | _ -> Fmt . pf ppf " chain length = % d : % d objects \ n " %! depth n in try Array . iter f matrix ; let chains = List . sort_uniq ( fun ( a , _ ) ( b , _ ) -> compare a b ) ( ( List . of_seq . <> Hashtbl . to_seq ) chains ) in Fmt . pr " % a " %! ( Fmt . list ~ sep : Fmt . nop pp_chain ) chains ; Fmt . pr " % a : ok \ n " %! Fpath . pp Fpath . ( set_ext " pack " ( base fpath ) ) ; return ( Ok ( ) ) with _exn -> return ( R . error_msgf " Invalid PACK file " ) ) |
let ( >>? ) x f = let open Fiber in x >>= function Ok x -> f x | Error err -> return ( Error err ) |
let run ~ verbose fpath = let pack = Fpath . set_ext " pack " fpath in Bos . OS . File . must_exist pack |> Fiber . return >>? fun pack -> first_pass pack |> Scheduler . prj >>? fun ( hash , oracle , matrix , length , carbon ) -> second_pass pack ( hash , oracle , matrix ) >>? fun ( hash , matrix ) -> verify ~ verbose fpath hash length carbon matrix |> Scheduler . prj |
let run verbose fpath = match Fiber . run ( run ~ verbose fpath ) with | Ok ( ) -> Ok ( ) | Error ( ` Msg err ) -> Error ( Fmt . str " % s . " err ) |
let verbose = let doc = " After verifying the pack , show list of objects contained in the pack and \ histogram of delta chain length . " in Arg . ( value & flag & info [ " v " ; " verbose " ] ~ doc ) |
let fpath = let parser x = match Fpath . of_string x with | Ok v when Sys . file_exists x -> Ok v | Ok v -> R . error_msgf " % a not found " Fpath . pp v | Error _ as err -> err in Arg . conv ( parser , Fpath . pp ) |
let fpath = let doc = " The idx files to verify . " in Arg . ( required & pos ~ rev : true 0 ( some fpath ) None & info [ ] ~ doc ) |
let cmd = let doc = " Validate packed Git archive files " in let man = [ ` S Manpage . s_description ; ` P " Reads given idx file for packed Git archive created with the ( $ i , git ) \ ( $ i , pack - objets ) command and verifies idx file and the corresponding \ pack file . " ; ] in let info = Cmd . info " verify - pack " ~ doc ~ man in Cmd . v info Term . ( const run $ verbose $ fpath ) |
let ( ) = exit @@ Cmd . eval_result cmd |
let parse s = Pb_parsing_parser . proto_ Pb_parsing_lexer . lexer ( Lexing . from_string s ) s |
let ( ) = let proto = parse " message M { string no_label = 1 ; } " in match Pb_parsing_util . finalize_proto_value proto with | _ -> assert ( assertfalse ) assertfalse | exception E . Compilation_error _ -> ( ) |
let ( ) = let proto = parse " \ syntax = " \ proto2 " ; \ \ message M { \ string no_label = 1 ; \ } " in match Pb_parsing_util . finalize_proto_value proto with | _ -> assert ( assertfalse ) assertfalse | exception E . Compilation_error _ -> ( ) |
let ( ) = let proto = parse " \ syntax = " \ proto3 " ; \ \ message M { \ string no_label = 1 ; \ repeated string r = 2 ; \ message S { \ string s_no_label = 3 ; \ repeated string s_r = 4 ; \ } \ } " in match Pb_parsing_util . finalize_proto_value proto with | _ -> ( ) | exception E . Compilation_error _ -> assert ( assertfalse ) assertfalse |
let ( ) = let proto = parse " \ syntax = " \ proto3 " ; \ \ message M { \ required string no_label = 1 ; \ } " in match Pb_parsing_util . finalize_proto_value proto with | _ -> assert ( assertfalse ) assertfalse | exception E . Compilation_error _ -> ( ) |
let ( ) = let proto = parse " \ syntax = " \ proto3 " ; \ \ message M { message S { \ required string no_label = 1 ; \ } } " in match Pb_parsing_util . finalize_proto_value proto with | _ -> assert ( assertfalse ) assertfalse | exception E . Compilation_error _ -> ( ) |
let ( ) = let proto = parse " \ syntax = " \ proto3 " ; \ \ enum E { EN1 = 1 ; EN2 = 2 ; } \ message M { } \ " in match Pb_parsing_util . finalize_proto_value proto with | _ -> assert ( assertfalse ) assertfalse | exception E . Compilation_error _ -> ( ) |
let ( ) = let proto = parse " \ syntax = " \ proto3 " ; \ \ message M { \ enum E { EN1 = 1 ; EN2 = 2 ; } \ } " in match Pb_parsing_util . finalize_proto_value proto with | _ -> assert ( assertfalse ) assertfalse | exception E . Compilation_error _ -> ( ) |
let ( ) = let proto = parse " \ syntax = " \ proto3 " ; \ \ message M { \ enum E { EN0 = 0 ; EN2 = 2 ; } \ } " in match Pb_parsing_util . finalize_proto_value proto with | _ -> ( ) | exception E . Compilation_error _ -> assert ( assertfalse ) assertfalse |
let ( ) = print_endline " Verify syntax invariants . . . Ok " |
let failwith str = raise ( Failure str ) |
let ( ) @ a b = let a = List . rev a in let rec f a b = match b with | [ ] -> a | h :: t -> f ( h :: a ) t in List . rev ( f a b ) |
let reserved = [ " and " ; " always " ; " assign " ; " attribute " ; " begin " ; " buf " ; " bufif0 " ; " bufif1 " ; " case " ; " cmos " ; " deassign " ; " default " ; " defparam " ; " disable " ; " else " ; " endattribute " ; " end " ; " endcase " ; " endfunction " ; " endprimitive " ; " endmodule " ; " endtable " ; " endtask " ; " event " ; " for " ; " force " ; " forever " ; " fork " ; " function " ; " highhz0 " ; " highhz1 " ; " if " ; " initial " ; " inout " ; " input " ; " integer " ; " join " ; " large " ; " medium " ; " module " ; " nand " ; " negedge " ; " nor " ; " not " ; " notif0 " ; " notif1 " ; " nmos " ; " or " ; " output " ; " parameter " ; " pmos " ; " posedge " ; " primitive " ; " pulldown " ; " pullup " ; " pull0 " ; " pull1 " ; " rcmos " ; " reg " ; " release " ; " repeat " ; " rnmos " ; " rpmos " ; " rtran " ; " rtranif0 " ; " rtranif1 " ; " scalared " ; " small " ; " specify " ; " specparam " ; " strong0 " ; " strong1 " ; " supply0 " ; " supply1 " ; " table " ; " task " ; " tran " ; " tranif0 " ; " tranif1 " ; " time " ; " tri " ; " triand " ; " trior " ; " trireg " ; " tri0 " ; " tri1 " ; " vectored " ; " wait " ; " wand " ; " weak0 " ; " weak1 " ; " while " ; " wire " ; " wor " ] |
let iteri f l = let rec iteri n l = match l with | [ ] -> ( ) | h :: t -> f n h ; iteri ( n + 1 ) t in iteri 0 l |
let folds c s = List . fold_left ( fun s n -> if s = " " then n else n ^ c ^ s ) " " s |
let write circuit = let inputs = Circuit . inputs circuit in let outputs = Circuit . outputs circuit in let buf = Buffer . create ( 10 * 1024 ) in let name = Circuit . mangle_names reserved prefix circuit in let write_signal_decl typ signal = let write typ name ass = if width signal = 1 then bprintf buf " % s % s % s ; \ n " typ name ass else bprintf buf " % s [ % d : 0 ] % s % s ; \ n " typ ( width signal - 1 ) name ass in let names = names signal in let name = name ( uid signal ) in let name0 = name 0 in write typ name0 " " ; if List . length names > 1 then iteri ( fun i _ -> write " wire " ( name ( i + 1 ) ) ( " = " ^ name0 ) ) ( List . tl names ) in let write_mem_decl s = match s with | Signal_mem ( id , mid , r , sp ) -> bprintf buf " reg [ % d : 0 ] % s [ 0 :% d ] ; \ n " ( width s - 1 ) ( name mid 0 ) ( sp . mem_size - 1 ) | _ -> failwith " can only write memories here " in bprintf buf " module % s ( " ( Circuit . name circuit ) ; bprintf buf " % s " ( folds " , " ( List . map ( fun s -> List . hd ( names s ) ) ( inputs @ outputs ) ) ) ; bprintf buf " ) ; \ n " ; List . iter ( write_signal_decl " input " ) inputs ; List . iter ( write_signal_decl " output " ) outputs ; let is_internal_signal s = not ( Circuit . is_input circuit s ) && not ( Circuit . is_output circuit s ) && not ( s = empty ) in let internal_signals = Circuit . filter is_internal_signal outputs in List . iter ( fun s -> if is_mem s then write_mem_decl s ; write_signal_decl ( if is_op Signal_mux s || is_reg s then " reg " else " wire " ) s ) internal_signals ; let write_logic s = let dep = List . nth ( deps s ) in let nameuid u = name u 0 in let name s = name ( uid s ) 0 in let edge s = if s = vdd then " posedge " else " negedge " in let cat s = fst ( List . fold_left ( fun ( s , i ) d -> if i = 0 then name d , i + 1 else s ^ " , " ^ name d , i + 1 ) ( " " , 0 ) ( deps s ) ) in let mux s = let deps = deps s in let sel = List . hd deps in let vals = List . tl deps in let nvals = List . length vals in let name_s = name s in bprintf buf " always @*\ n case ( % s ) \ n " ( name sel ) ; iteri ( fun i d -> if ( i + 1 ) <> nvals then ( bprintf buf " % i ' d % i : % s <= % s ; \ n " ( width sel ) i name_s ( name d ) ) else ( bprintf buf " default : % s <= % s ; \ n " name_s ( name d ) ) ) vals ; bprintf buf " endcase \ n " in let reg s = let d = List . hd ( deps s ) in match s with | Signal_reg ( _ , r ) -> begin if r . reg_reset = empty then bprintf buf " always ( @% s % s ) \ n " ( edge r . reg_clock_level ) ( name r . reg_clock ) else begin bprintf buf " always ( @% s % s , % s % s ) \ n " ( edge r . reg_clock_level ) ( name r . reg_clock ) ( edge r . reg_reset_level ) ( name r . reg_reset ) ; bprintf buf " if ( % s == % i ) " ( name r . reg_reset ) ( if r . reg_reset_level = vdd then 1 else 0 ) ; bprintf buf " % s <= % s ; else \ n " ( name s ) ( name r . reg_reset_value ) end ; if r . reg_clear <> empty then begin bprintf buf " if ( % s == % i ) " ( name r . reg_clear ) ( if r . reg_clear_level = vdd then 1 else 0 ) ; bprintf buf " % s <= % s ; else \ n " ( name s ) ( name r . reg_clear_value ) end ; if r . reg_enable = vdd then bprintf buf " % s <= % s ; \ n " ( name s ) ( name d ) else bprintf buf " if ( % s ) % s <= % s ; \ n " ( name r . reg_enable ) ( name s ) ( name d ) end | _ -> failwith " Cannot write non - register value here " in let mem s = match s with | Signal_mem ( id , muid , r , m ) -> begin let d = dep 0 in if r . reg_reset = empty then bprintf buf " always ( @% s % s ) \ n " ( edge r . reg_clock_level ) ( name r . reg_clock ) else begin bprintf buf " always ( @% s % s , % s % s ) \ n " ( edge r . reg_clock_level ) ( name r . reg_clock ) ( edge r . reg_reset_level ) ( name r . reg_reset ) ; bprintf buf " if ( % s == % i ) \ n " ( name r . reg_reset ) ( if r . reg_reset_level = vdd then 1 else 0 ) ; bprintf buf " begin \ n " ; for i = 0 to m . mem_size - 1 do bprintf buf " % s [ % i ] <= % s ; \ n " ( nameuid muid ) i ( name r . reg_reset_value ) done ; bprintf buf " end \ n " ; bprintf buf " else \ n " ; end ; if r . reg_clear <> empty then begin bprintf buf " if ( % s == % i ) \ n " ( name r . reg_clear ) ( if r . reg_clear_level = vdd then 1 else 0 ) ; bprintf buf " begin \ n " ; for i = 0 to m . mem_size - 1 do bprintf buf " % s [ % i ] <= % s ; \ n " ( nameuid muid ) i ( name r . reg_clear_value ) done ; bprintf buf " end \ n " ; bprintf buf " else \ n " ; end ; if r . reg_enable = vdd then bprintf buf " % s [ % s ] <= % s ; \ n " ( nameuid muid ) ( name m . mem_write_address ) ( name d ) else bprintf buf " if ( % s ) % s [ % s ] <= % s ; \ n " ( name r . reg_enable ) ( nameuid muid ) ( name m . mem_write_address ) ( name d ) ; bprintf buf " assign % s = % s [ % s ] ; \ n " ( name s ) ( nameuid muid ) ( name m . mem_read_address ) end | _ -> failwith " Cannot write non - memory value here " in let inst s = match s with | Signal_inst ( id , iuid , i ) -> begin let rec folds = function | [ ] -> " " | s :: [ ] -> s | h :: t -> h ^ " , " ^ ( folds t ) in let generic ( n , g ) = " . " ^ n ^ " ( " ^ ( match g with | ParamString ( s ) -> " " " \ ^ s ^ " " " \ | ParamInt ( i ) -> string_of_int i | ParamBool ( b ) -> if b then " 1 " else " 0 " | ParamFloat ( f ) -> string_of_float f ) ^ " ) " in let inport ( n , s ) = " . " ^ n ^ " ( " ^ ( name s ) ^ " ) " in let outport ( n , ( w , l ) ) = if width s = 1 then " . " ^ n ^ " ( " ^ ( name s ) ^ " ) " else " . " ^ n ^ " ( " ^ ( name s ) ^ " [ " ^ string_of_int ( w + l - 1 ) ^ " " : ^ string_of_int l ^ " ] ) " in let generics = List . map generic i . inst_generics in let inports = List . map inport i . inst_inputs in let outports = List . map outport i . inst_outputs in bprintf buf " % s \ n " i . inst_name ; if i . inst_generics <> [ ] then bprintf buf " ( # % s ) \ n " ( folds generics ) ; bprintf buf " % s \ n " ( nameuid iuid ) ; bprintf buf " ( % s ) ; \ n " ( folds ( inports @ outports ) ) ; end | _ -> failwith " expecting an instantiation " in match s with | Signal_empty -> failwith " Can ' t write empty signal " | Signal_const ( _ , v ) -> bprintf buf " assign % s = % i ' b % s ; \ n " ( name s ) ( width s ) v | Signal_wire ( _ , d ) -> bprintf buf " assign % s = % s ; \ n " ( name s ) ( name ! d ) | Signal_select ( _ , h , l ) -> bprintf buf " assign % s = % s [ % d :% d ] ; \ n " ( name s ) ( name ( dep 0 ) ) h l | Signal_op ( _ , op ) -> begin let op2 op = bprintf buf " assign % s = % s % s % s ; \ n " ( name s ) ( name ( dep 0 ) ) op ( name ( dep 1 ) ) in match op with | Signal_add -> op2 " " + | Signal_sub -> op2 " " - | Signal_mulu -> op2 " " * | Signal_muls -> bprintf buf " assign % s = $ signed ( % s ) * $ signed ( % s ) ; \ n " ( name s ) ( name ( dep 0 ) ) ( name ( dep 1 ) ) | Signal_and -> op2 " " & | Signal_or -> op2 " " | | Signal_xor -> op2 " " ^ | Signal_eq -> op2 " " == | Signal_not -> bprintf buf " assign % s = ~ % s ; \ n " ( name s ) ( name ( dep 0 ) ) | Signal_lt -> op2 " " < | Signal_cat -> bprintf buf " assign % s = { % s } ; \ n " ( name s ) ( cat s ) | Signal_mux -> mux s end | Signal_reg ( _ , _ ) -> reg s | Signal_mem ( _ , _ , _ , _ ) -> mem s | Signal_inst ( _ ) -> inst s in List . iter write_logic ( internal_signals @ outputs ) ; bprintf buf " endmodule \ n " ; buf |
module Testbench = struct let write clocks resets circuit = let buf = Buffer . create ( 10 * 1024 ) in let inputs = Circuit . inputs circuit in let outputs = Circuit . outputs circuit in let name s = List . hd ( names s ) in let write_signal_decl t s = if width s = 1 then bprintf buf " % s % s ; \ n " t ( name s ) else bprintf buf " % s [ % i : 0 ] % s ; \ n " t ( width s ) ( name s ) in let write_reg = write_signal_decl " reg " in let write_wire = write_signal_decl " wire " in bprintf buf " module % s_tb ; \ n " ( Circuit . name circuit ) ; List . iter write_reg inputs ; List . iter write_wire outputs ; let write_assoc s = folds " , " ( List . map ( fun s -> " . " ^ ( name s ) ^ " ( " ^ ( name s ) ^ " ) " ) s ) in bprintf buf " /* design under test */\ n " ; bprintf buf " % s the_dut ( \ n " ( Circuit . name circuit ) ; bprintf buf " % s \ n " ( write_assoc ( inputs @ outputs ) ) ; bprintf buf " ) ; \ n " ; let clocks , inputs = List . partition ( fun s -> List . mem ( name s ) clocks ) inputs in let resets , inputs = List . partition ( fun s -> List . mem ( name s ) resets ) inputs in bprintf buf " /* stimulus */\ n " ; bprintf buf " initial begin \ n " ; bprintf buf " /* async reset */\ n " ; List . iter ( fun s -> bprintf buf " % s <= 0 ; \ n " ( name s ) ; ) clocks ; List . iter ( fun s -> bprintf buf " % s <= 1 ; \ n " ( name s ) ; ) resets ; bprintf buf " # 10 ; \ n " ; List . iter ( fun s -> bprintf buf " % s <= 0 ; \ n " ( name s ) ; ) resets ; bprintf buf " # 10 ; \ n " ; bprintf buf " /* clocking */\ n " ; bprintf buf " while ( 1 ) begin \ n " ; List . iter ( fun s -> bprintf buf " % s <= 1 ; \ n " ( name s ) ; ) clocks ; List . iter ( fun s -> bprintf buf " $ scanf ( " \%% b " , \ % s ) ; \ n " ( name s ) ; ) inputs ; bprintf buf " # 10 ; \ n " ; List . iter ( fun s -> bprintf buf " % s <= 0 ; \ n " ( name s ) ; ) clocks ; bprintf buf " # 10 ; \ n " ; bprintf buf " end \ n " ; bprintf buf " end \ n " ; bprintf buf " endmodule \ n " ; buf end |
let write chan circ = let b = Verilog . write circ in output_string chan ( Buffer . contents b ) |
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 } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.