text
stringlengths
12
786k
let _ = Js . export " vult " ( object % js method main = Vultlib . main method version = Vultlib . version method generateJs = Vultlib . generateJs method generateC = Vultlib . generateC method generateLua = Vultlib . generateLua method checkCode = checkCode method generateJSCode = generateJSCode end )
type t = unit Pp . t
let quoted s = i ( sprintf " % S " s )
let surround delim a = let start , finish = match delim with | ` Paren -> ( i " ( " , i " ) " ) | ` Curly -> ( i " { " , i " } " ) | ` Square -> ( i " [ " , i " ] " ) in Pp . concat [ start ; a ; finish ]
module Name = struct let to_json t = sprintf " yojson_of_ % s " t let of_json t = sprintf " % s_of_yojson " t end
module Json = struct let invalid_pat name = ( ident " json " , Pp . textf " Json . error " \ invalid % s " \ json " name ) let typ = " Json . t " module Literal = struct let str n = sprintf " ` String % S " n let int i = sprintf " ` Int ( % d ) " i let null = " ` Null " let bool b = sprintf " ` Bool % b " b end let str = sprintf " ` String % s " let int = sprintf " ` Int % s " let bool = sprintf " ` Bool % s " end
module Gen = struct let record ~ delim fields = let sep = Pp . concat [ Pp . verbatim " ; " ; Pp . newline ] in Pp . text " { " ++ Pp . concat_map ~ sep fields ~ f ( : fun ( name , f ) -> Pp . concat [ Pp . textf " % s % s " name delim ; f ] ) ++ Pp . verbatim " } " let clause ~ delim l r = Pp . concat [ l ; Pp . verbatim ( sprintf " % s " delim ) ; r ] end
module Attr = struct type t = { name : string ; payload : w list } let make name payload = { name ; payload } let pp kind { name ; payload } = let kind = match kind with | ` Field -> " " @ | ` Type -> " " @@ in Pp . concat [ i kind ; i name ; Pp . space ; Pp . concat ~ sep : Pp . space payload ] |> surround ` Square end
module Type = struct let string = i " string " let int = i " int " let name = i let bool = i " bool " let gen_decl kw name body = Pp . concat [ Pp . textf " % s % s " = kw name ; Pp . newline ; body ] let and_ name body = gen_decl " and " name body let decl name body = gen_decl " type " name body let record fields = Gen . record ~ delim " " :: fields let field_attrs ~ field ~ attrs = match attrs with | [ ] -> field | attrs -> let attrs = Pp . concat_map attrs ~ sep : Pp . space ~ f ( : Attr . pp ` Field ) in Pp . concat [ field ; Pp . space ; attrs ] let var typ = Pp . textf " ' % s " typ let app typ = function | [ ] -> assert false | [ x ] -> Pp . concat [ x ; Pp . space ; typ ] | xs -> let args = let sep = Pp . verbatim " , " in Pp . concat [ Pp . verbatim " ( " ; Pp . concat ~ sep xs ; Pp . verbatim " ) " ] in Pp . concat [ args ; Pp . space ; typ ] let tuple fields = let sep = i " " * in i " ( " ++ Pp . concat ~ sep fields ++ i " ) " let rec_decls xs = match xs with | [ ] -> Pp . concat [ ] | ( name , body ) :: xs -> decl name body ++ newline ++ Pp . concat_map xs ~ sep : Pp . newline ~ f ( : fun ( name , body ) -> and_ name body ) let deriving td ~ record = let fields = if record then space ++ i " [ @@ yojson . allow_extra_fields ] " else space in Pp . concat [ td ; Pp . newline ; Pp . text " [ @@ deriving_inline yojson ] " ; fields ; space ; Pp . text " [ @@@ end ] " ] let opt_attr = ident " option [ @ yojson . option ] " let opt_field f = Pp . seq f opt_attr let default f def = Pp . concat [ f ; ident " [ @ default " ; ident def ; ident " ] " ] let key name = concat [ ident " [ @ key " ; quoted name ; ident " ] " ] let gen_variant ~ poly constrs = let sep = Pp . concat [ Pp . newline ; i " | " ] in Pp . concat_map constrs ~ sep ~ f ( : fun ( name , arg ) -> let name = let name = String . capitalize_ascii name in if poly then " ` " ^ name else name in match arg with | [ ] -> i name | xs -> let xs = match xs with | [ x ] -> x | xs -> tuple xs in Gen . clause ~ delim " : of " ( ident name ) xs ) let poly constrs = concat [ i " [ " ; gen_variant ~ poly : true constrs ; i " ] " ] let variant constrs = gen_variant ~ poly : false constrs end
let gen_module kw name body = Pp . concat [ Pp . textf " module % s % s " name kw ; Pp . newline ; body ; newline ; verbatim " end " ; newline ]
module Sig = struct let module_ name body = gen_module " : sig " name body let val_ name b = let sep = Pp . concat [ space ; i " " ; -> space ] in let b = Pp . concat ~ sep b in Pp . concat [ textf " val % s : " name ; b ; Pp . newline ] let assoc k v = Pp . concat [ Type . tuple [ k ; v ] ; Pp . space ; i " list " ] module Json = struct let arr typ = [ i typ ; i Json . typ ] let to_json typ = val_ ( Name . to_json typ ) ( arr typ ) let of_json typ = val_ ( Name . of_json typ ) ( List . rev ( arr typ ) ) end end
let warnings codes = seq ( textf " [ @@@ warning % S ] " codes ) newline
let opens names = Pp . concat_map names ~ f ( : fun name -> Pp . concat [ textf " open ! % s " name ; newline ] )
let module_ name body = gen_module " = struct " name body
let record fields = Gen . record ~ delim " " := fields
let match_ e clauses = let clauses = let sep = Pp . concat [ Pp . newline ; i " | " ] in Pp . concat_map ~ sep clauses ~ f ( : fun ( l , r ) -> Gen . clause ~ delim " " :-> l r ) in Pp . concat [ Pp . textf " match % s with " e ; Pp . newline ; clauses ]
let to_json t body = Pp . concat [ Pp . textf " let % s ( json : Json . t ) : t " = ( Name . to_json t ) ; Pp . newline ; Pp . hovbox ~ indent : 2 body ]
let of_json t body = Pp . concat [ Pp . textf " let % s ( t : t ) : Json . t " = ( Name . of_json t ) ; Pp . newline ; Pp . hovbox ~ indent : 2 body ]
let foo = ( ) ; ; *
let f x y = x ; ;
let 1 = 1 ; ; ; ;
type phone_number = int * int * int * int ; ;
type contact = { name : string ; phone_number : phone_number } ; ;
let nobody = { name = " " ; phone_number = ( 0 , 0 , 0 , 0 ) } ; ;
type database = { number_of_contacts : int ; contacts : contact array ; } ; ;
let make max_number_of_contacts = { number_of_contacts = 0 ; contacts = Array . make max_number_of_contacts nobody } ; ;
type query = { code : int ; contact : contact } ; ;
let insert db contact = if db . number_of_contacts >= Array . length db . contacts then ( false , db , nobody ) else let ( status , db , _ ) = search db contact in if status then ( false , db , contact ) else let cells i = in let db ' = { number_of_contacts = db . number_of_contacts + 1 ; contacts = Array . init ( Array . length db . contacts ) cells } in ( true , db ' , contact ) ; ;
let proof_of_bug = [ | { code = 0 ; contact = { name = " uno " ; phone_number = ( 1 , 1 , 1 , 1 ) } } ; { code = 0 ; contact = { name = " dos " ; phone_number = ( 2 , 2 , 2 , 2 ) } } ; { code = 1 ; contact = { name = " uno " ; phone_number = ( 0 , 0 , 0 , 0 ) } } ; { code = 0 ; contact = { name = " tres " ; phone_number = ( 3 , 3 , 3 , 3 ) } } ; { code = 2 ; contact = { name = " dos " ; phone_number = ( 0 , 0 , 0 , 0 ) } } ] | ; ;
let delete db contact = let ( status , db , contact ) = search db contact in if not status then ( false , db , contact ) else let cells i = if i = db . number_of_contacts - 1 then nobody else if db . contacts . ( i ) . name = contact . name then db . contacts . ( db . number_of_contacts - 1 ) else db . contacts . ( i ) in let db ' = { number_of_contacts = db . number_of_contacts - 1 ; contacts = Array . init ( Array . length db . contacts ) cells } in ( true , db ' , contact )
let update db contact = let ( status , _ , _ ) = engine db { code = 2 ; contact } in if status then let cells i = if db . contacts . ( i ) . name = contact . name then contact else db . contacts . ( i ) in let db ' = { number_of_contacts = db . number_of_contacts ; contacts = Array . init ( Array . length db . contacts ) cells } in ( true , db ' , contact ) else engine db { code = 0 ; contact }
let engine db { code ; contact } = if code = 0 then insert db contact else if code = 1 then delete db contact else if code = 2 then search db contact else if code = 3 then update db contact else ( false , db , nobody ) ; ;
let g x = x
let h x = x
let k x = x
let o x = x
module M = struct [ @@@ warning " - 32 " ] let f x = x let [ @ warning " + 32 " ] g x = x let [ @ warning " + 32 " ] h x = x and i x = x let j x = x and [ @ warning " + 32 " ] k x = x end
module F ( X : sig val x : int end ) = struct end
module G ( X : sig val x : int end ) = X
module H ( X : sig val x : int end ) = X
type story = { context : context ; perturbation : event ; adventure : event list ; conclusion : context ; } ; ;
let compatible_actions_for_character character context = match character with | { location = Restaurant } -> [ Eat ] | { location = Appartment } -> [ GoToRestaurant ] ; ;
let apply_action character = function | Eat -> { state = Happy ; location = character . location ; name = character . name } | GoToRestaurant -> { location = Restaurant ; state = character . state ; name = character . name } | Sleep -> { location = character . location ; state = Hungry ; name = character . name } ; ;
let compatible_actions context = let rec aux = function | [ ] -> [ ] | character :: cs -> let can_do = compatible_actions_for_character character context in let rec aux ' = function | [ ] -> [ ] | a :: actions -> Action ( character , a ) :: aux ' actions in aux ' can_do in aux context . characters ; ;
let possible_changes_for_character character = match character with | { state = Happy } -> [ Hungry ] | { state = Hungry } -> [ ] | { state = Tired } -> [ ] ; ;
let apply_change character state = { name = character . name ; state = state ; location = character . location } ; ;
let possible_changes context = let rec aux = function | [ ] -> [ ] | character :: cs -> let possible_changes = possible_changes_for_character character in let rec aux ' = function | [ ] -> [ ] | c :: changes -> Change ( character , c ) :: aux ' changes in aux ' possible_changes in aux context . characters ; ;
let character_of_event = function | Action ( character , _ ) -> character | Change ( character , _ ) -> character ; ;
let apply event context = let rec aux = function | [ ] -> assert false | character :: cs -> if character = character_of_event event then match event with | Action ( _ , action ) -> apply_action character action :: cs | Change ( _ , change ) -> apply_change character change :: cs else character :: aux cs in { characters = aux context . characters } ; ;
let rec is_one_of state states = match states with | [ ] -> false | state ' :: ss -> state = state ' || is_one_of state ss ; ;
let rec all_characters_are states = function | [ ] -> true | character :: cs -> is_one_of character . state states && all_characters_are states cs ; ;
let random_pick xs = List . nth xs ( Random . int ( List . length xs ) ) ; ;
let something_happens context = let what_can_happen = compatible_actions context @ possible_changes context in let event = random_pick what_can_happen in event , apply event context ; ;
let happy context = all_characters_are [ Happy ] context . characters ; ;
let rec end_story events context = if happy context then context , List . rev events else let event , context = something_happens context in end_story ( event :: events ) context ; ;
let make_story initial_context = let perturbation , context = something_happens initial_context in let conclusion , adventure = end_story [ ] context in { context = initial_context ; perturbation = perturbation ; adventure = adventure ; conclusion = conclusion } ; ;
let describe_location = function | Appartment -> " at home " | Restaurant -> " at the restaurant " ; ;
let describe_state = function | Happy -> " happy " | Hungry -> " hungry " | Tired -> " tired " ; ;
let describe character = character . name ^ " was " ^ describe_location character . location ^ " and was " ^ describe_state character . state ^ " . " ; ;
let tell_context context = let rec aux = function | [ ] -> " " | character :: characters -> describe character ^ aux characters in aux context . characters ; ;
let tell_action = function | Eat -> " ate " | GoToRestaurant -> " went to the restaurant " | Sleep -> " took a nap " ; ;
let tell_event = function | Action ( character , action ) -> character . name ^ " " ^ tell_action action ^ " . " | Change ( character , state ) -> character . name ^ " was made " ^ describe_state state ^ " . " ; ;
let rec tell_adventure = function | [ ] -> " " | event :: adventure -> tell_event event ^ tell_adventure adventure ; ;
let tell story = " Once upon a time , " ^ tell_context story . context ^ " One day , something wrong happened . " ^ tell_event story . perturbation ^ tell_adventure story . adventure ^ " At the end , the peace was restored . " ^ tell_context story . conclusion ; ;
let story = tell ( make_story { characters = [ { name = " Sophie " ; location = Appartment ; state = Happy } ; { name = " Socrate " ; location = Appartment ; state = Happy } ; ] } ) ; ;
let rec fact = function | 1 -> 1 | n -> n * ( fact [ @ tailcall ] ) ( n - 1 ) ; ; ^^^^^^^^^^^^^^^^^^^^^^^^ } ] |
let rec fact = function | 1 -> 1 | n -> n * ( fact [ @ tailcall true ] ) ( n - 1 ) ; ; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } ] |
let rec fact = function | 1 -> 1 | n -> n * ( fact [ @ tailcall false ] ) ( n - 1 ) ; ; } ] |
let rec fact_tail acc = function | 1 -> acc | n -> ( fact_tail [ @ tailcall ] ) ( n * acc ) ( n - 1 ) ; ; [ %% expect { | } ] |
let rec fact_tail acc = function | 1 -> acc | n -> ( fact_tail [ @ tailcall true ] ) ( n * acc ) ( n - 1 ) ; ; [ %% expect { | } ] |
let rec fact_tail acc = function | 1 -> acc | n -> ( fact_tail [ @ tailcall false ] ) ( n * acc ) ( n - 1 ) ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } ] |
let rec test x = ( test [ @ tailcall foobar ] ) x ; ; [ %% expect { | ^^^^^^^^ } ] |
let ( ) = try ( ) with Invalid_argument " Any " -> ( ) ; ; [ %% expect { | ^^^^^ } ] ; ; |
let ( ) = try ( ) with Match_failure ( " Any " , _ , _ ) -> ( ) ; ; [ %% expect { | ^^^^^^^^^^^ } ] ; ; |
let ( ) = try ( ) with Match_failure ( _ , 0 , _ ) -> ( ) ; ; [ %% expect { | ^^^^^^^ } ] ; ; |
type t = | Warn of string [ @ ocaml . warn_on_literal_pattern ] | Without_warning of string | Warn ' of nativeint [ @ ocaml . warn_on_literal_pattern ] | Deep of ( string * int ) list [ @ ocaml . warn_on_literal_pattern ] ; ; [ %% expect { |
type t = Warn of string | Without_warning of string | Warn ' of nativeint | Deep of ( string * int ) list } ] ; ; |
let f = function [ %% expect { | ^^^^^^^^^^ } ] ; ; |
let g = function [ %% expect { | ^^ } ] ; ; |
let h = function [ %% expect { | } ] ; ; |
let i = function [ %% expect { | } ] ; ; |
let j = function [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ } ] ; ; |
let f = ( fun x -> x + 1 ) [ @ inline never ]
let g x = ( f [ @ inlined ] ) x
let r = ref f
let i x = ( ! r [ @ inlined ] ) x
let j x y = x + y
let h x = ( j [ @ inlined ] ) x
let a x = let b = x + 1 in fun y -> y + b
let b x y = ( a [ @ inlined ] ) x y
let c x = x + 1 [ @@ inline never ]
let d x = ( c [ @ inlined ] ) x
let g ' x = ( f [ @ inlined hint ] ) x
let i ' x = ( ! r [ @ inlined hint ] ) x
let h ' x = ( j [ @ inlined hint ] ) x
let b ' x y = ( a [ @ inlined hint ] ) x y
let d ' x = ( c [ @ inlined hint ] ) x
let a = Lazy . force ( lazy " a " )
let b = Lazy . force ( lazy 1 )
let c = Lazy . force ( lazy 3 . 14 )