text
stringlengths 0
601k
|
---|
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 ) |
let d = Lazy . force ( lazy ' a ' ) |
let e = Lazy . force ( lazy ( fun x -> x + 1 ) ) |
let rec f ( x : int ) : int = g x and g x = f x |
let h = Lazy . force ( lazy f ) |
let i = Lazy . force ( lazy g ) |
let j = Lazy . force ( lazy 1L ) |
let k = Lazy . force ( lazy ( 1 , 2 ) ) |
let l = Lazy . force ( lazy [ | 3 . 14 ] ) | |
let m = Lazy . force ( lazy ( Sys . opaque_identity 3 . 14 ) ) |
let n = Lazy . force ( lazy None ) |
let p = fun x -> x |
let ( ) = Obj . set_field ( Obj . repr o ) 0 ( Obj . repr 3 ) ; Obj . set_field ( Obj . repr p ) 0 ( Obj . repr 3 ) ; Obj . set_field ( Obj . repr q ) 0 ( Obj . repr 3 ) ; Obj . set_field ( Obj . repr r ) 0 ( Obj . repr 3 ) |
let set v = Obj . set_field ( Obj . repr v ) 0 ( Obj . repr 3 ) [ @@ inline ] |
let ( ) = set o |
let opaque = Sys . opaque_identity ( 1 , 2 ) |
let set_opaque = Obj . set_field ( Obj . repr opaque ) 0 ( Obj . repr 3 ) |
type filesystem = ( string * node ) list | File | Dir of filesystem | Symlink of string list ; ; |
let rec print_path = function | [ ] -> ( ) | [ x ] -> ( print_string x ; print_newline ( ) ) | hd :: tl -> print_string ( hd ^ " " ) ; / print_path tl ; ; |
let rec print_file lvl name = if lvl = 0 then ( print_string name ; print_newline ( ) ) else ( print_string " | " ; print_file ( lvl - 1 ) name ) ; ; |
let rec print_symlink lvl name path = if lvl = 0 then ( print_string ( name ^ " -> " ) ; print_path path ) else ( print_string " | " ; print_symlink ( lvl - 1 ) name path ) ; ; |
let rec print_invalid_symlink lvl name = if lvl = 0 then ( print_string ( name ^ " -> " ) ; print_string " INVALID " ; print_newline ( ) ) else ( print_string " | " ; print_invalid_symlink ( lvl - 1 ) name ) ; ; |
let rec print_dir lvl name = if lvl = 0 then ( print_string ( " " / ^ name ) ; print_newline ( ) ) else ( print_string " | " ; print_dir ( lvl - 1 ) name ) ; ; |
let print_filesystem root = let rec aux lvl = function | [ ] -> ( ) | ( name , node ) :: tl -> match node with | File -> ( print_file lvl name ; aux lvl tl ) | Dir fs -> ( print_dir lvl name ; aux ( lvl + 1 ) fs ; aux lvl tl ) | Symlink path -> ( print_symlink lvl name path ; aux lvl tl ) in aux 0 root ; ; |
let rec resolve sym path = let rec aux acc path = match acc , path with | _ , [ ] -> List . rev acc | [ ] , " . . " :: tl -> aux acc tl | _ , " . . " :: tl -> aux ( List . tl acc ) tl | _ , hd :: tl -> aux ( hd :: acc ) tl in aux ( List . tl ( List . rev sym ) ) path ; ; |
let rec file_exists root path = let node_matches ( ( node1_name : string ) , node1 ) ( ( node2_name : string ) , node2 ) = match node1 , node2 with | ( File , File ) | ( Dir _ , Dir _ ) -> node1_name = node2_name | ( _ , _ ) -> false in match path with | [ ] -> false | [ target ] -> begin try let _ = List . find ( node_matches ( target , File ) ) root in true with Not_found -> false end | hd :: tl -> begin try match List . find ( node_matches ( hd , Dir [ ] ) ) root with | target , Dir fs -> file_exists fs ( List . tl path ) | _ , _ -> false with Not_found -> false end ; ; |
let print_filesystem root = let rec aux abs_path lvl = function | [ ] -> ( ) | ( name , node ) :: tl -> match node with | File -> ( print_file lvl name ; aux abs_path lvl tl ) | Dir fs -> ( print_dir lvl name ; aux ( name :: abs_path ) ( lvl + 1 ) fs ; aux abs_path lvl tl ) | Symlink path -> if file_exists root ( resolve ( List . rev ( name :: abs_path ) ) path ) then ( print_symlink lvl name path ; aux abs_path lvl tl ) else ( print_invalid_symlink lvl name ; aux abs_path lvl tl ) in aux [ ] 0 root ; ; |
type image = int -> int -> bool ; ; |
let all_white = fun x y -> false ; ; |
let all_black = fun x y -> true ; ; |
let checkers = fun x y -> y / 2 mod 2 = x / 2 mod 2 ; ; |
let square cx cy s = fun x y -> let minx = cx - s / 2 in let maxx = cx + s / 2 in let miny = cy - s / 2 in let maxy = cy + s / 2 in x >= minx && x <= maxx && y >= miny && y <= maxy ; ; |
let disk cx cy r = fun x y -> let x ' = x - cx in let y ' = y - cy in ( x ' * x ' + y ' * y ' ) <= r * r ; ; |
type blend = | Image of image | And of blend * blend | Or of blend * blend | Rem of blend * blend ; ; |
let display_image width height f_image = for row = 0 to height do for col = 0 to width do match f_image col row with | false -> print_char ' ' | true -> print_char ' ' # done ; print_newline ( ) done ; ; |
let rec render blend x y = match blend with | Image b -> b x y | And ( b1 , b2 ) -> ( ( render b1 ) x y ) && ( ( render b2 ) x y ) | Or ( b1 , b2 ) -> ( ( render b1 ) x y ) || ( ( render b2 ) x y ) | Rem ( b1 , b2 ) -> ( ( render b1 ) x y ) && not ( ( render b2 ) x y ) ; ; |
let display_blend width height blend = display_image width height ( render blend ) ; ; |
type ' a xlist = { mutable pointer : ' a cell } | Nil | List of ' a * ' a xlist ; ; |
let nil ( ) = { pointer = Nil } ; ; |
let cons elt rest = { pointer = List ( elt , rest ) } ; ; |
let head l = match l . pointer with | Nil -> raise Empty_xlist | List ( hd , tl ) -> hd |
let tail l = match l . pointer with | Nil -> raise Empty_xlist | List ( hd , tl ) -> tl |
let add a l = l . pointer <- List ( a , { pointer = l . pointer } ) |
let add a l = match l . pointer with | Nil -> l . pointer <- List ( a , nil ( ) ) | List ( hd , tl ) -> l . pointer <- List ( a , cons hd tl ) |
let chop l = match l . pointer with | Nil -> raise Empty_xlist | List ( hd , tl ) -> l . pointer <- tl . pointer |
let rec append l1 l2 = match l1 . pointer with | Nil -> l1 . pointer <- l2 . pointer | List ( hd , tl ) -> append tl l2 |
let rec filter p l = match l . pointer with | Nil -> ( ) | List ( hd , tl ) when p hd -> filter p tl | List ( hd , tl ) -> l . pointer <- tl . pointer ; filter p l |
module Tree = struct type ' a t = | Leaf of ' a | Node of ' a t * ' a * ' a t module Iterator = struct type ' a path = | Top | Left of ' a path * ' a * ' a t | Right of ' a t * ' a * ' a path type ' a iterator = Loc of ' a t * ' a path exception Fail let go_left ( Loc ( t , p ) ) = match p with let go_right ( Loc ( t , p ) ) = match p with | Top -> raise Fail | Left ( father , x , right ) -> Loc ( right , Right ( t , x , father ) ) | Right ( left , x , father ) -> raise Fail let go_up ( Loc ( t , p ) ) = match p with let go_first ( Loc ( t , p ) ) = match t with let go_second ( Loc ( t , p ) ) = match t with let focus ( Loc ( ( Leaf x | Node ( _ , x , _ ) ) , _ ) ) = x end end |
let bfs t = let rec aux results = function | [ ] -> List . rev results | l :: ls -> let results = ( Tree . Iterator . focus l ) :: results in try aux results ( ls @ [ Tree . Iterator . go_first l ; Tree . Iterator . go_second l ] ) with Tree . Iterator . Fail -> aux results ls in aux [ ] [ Tree . Iterator . Loc ( t , Tree . Iterator . Top ) ] |
module type MultiSet_S = sig type ' a t val occurrences : ' a t -> ' a -> int val empty : ' a t val insert : ' a t -> ' a -> ' a t val remove : ' a t -> ' a -> ' a t end |
module MultiSet : MultiSet_S = struct type ' a t = ( ' a * int ) list let occurrences s x = try List . assoc x s with Not_found -> 0 let empty = [ ] let insert s x = match occurrences s x with | 0 -> List . sort compare ( ( x , 1 ) :: s ) | i -> List . sort compare ( ( x , ( i + 1 ) ) :: ( List . remove_assoc x s ) ) let remove s x = match occurrences s x with | 0 -> s | 1 -> List . remove_assoc x s | i -> List . sort compare ( ( x , ( i - 1 ) ) :: ( List . remove_assoc x s ) ) |
let letters word = let rec aux s = function | 0 -> s | i -> aux ( MultiSet . insert s word . [ i - 1 ] ) ( i - 1 ) in aux MultiSet . empty ( String . length word ) ; ; |
let anagram word1 word2 = letters word1 = letters word2 ; ; |
module type DictSig = sig type ( ' key , ' value ) t val empty : ( ' key , ' value ) t val add : ( ' key , ' value ) t -> ' key -> ' value -> ( ' key , ' value ) t exception NotFound val lookup : ( ' key , ' value ) t -> ' key -> ' value val remove : ( ' key , ' value ) t -> ' key -> ( ' key , ' value ) t |
module Dict : DictSig = struct type ( ' key , ' value ) t = | Empty | Node of ( ' key , ' value ) t * ' key * ' value * ( ' key , ' value ) t let empty = Empty let rec add d k v = match d with | Empty -> Node ( Empty , k , v , Empty ) | Node ( l , k ' , v ' , r ) -> if k = k ' then Node ( l , k , v , r ) else if k < k ' then Node ( add l k v , k ' , v ' , r ) else Node ( l , k ' , v ' , add r k v ) exception NotFound let rec lookup d k = match d with | Empty -> raise NotFound | Node ( l , k ' , v ' , r ) -> if k = k ' then v ' else if k < k ' then lookup l k else lookup r k let rec remove d k = let rec merge l r = match l with | Empty -> r | Node ( ll , kk , vv , rr ) -> Node ( ll , kk , vv , merge rr r ) in match d with | Empty -> d | Node ( l , k ' , v ' , r ) -> if k = k ' then match l , r with | Empty , _ -> r | _ , Empty -> l | _ , _ -> merge l r else if k < k ' then Node ( ( remove l k ) , k ' , v ' , r ) else Node ( l , k ' , v ' , ( remove r k ) ) |
module type GenericTrie = sig type ' a char_table type ' a trie = Trie of ' a option * ' a trie char_table val empty : unit -> ' a trie val insert : ' a trie -> string -> ' a -> ' a trie val lookup : ' a trie -> string -> ' a option end |
module CharHashedType = struct type t = char let equal c1 c2 = c1 = c2 let hash c = Char . code c end ; ; |
module Trie : GenericTrie with type ' a char_table = ' a CharHashtbl . t = struct type ' a char_table = ' a CharHashtbl . t type ' a trie = Trie of ' a option * ' a trie char_table let empty ( ) = Trie ( None , CharHashtbl . create 100 ) let lookup trie w = let rec aux i ( Trie ( value , children ) ) = if i >= String . length w then value else try aux ( i + 1 ) ( CharHashtbl . find children w . [ i ] ) with Not_found -> None in aux 0 trie let insert trie w v = let rec aux i ( Trie ( value , children ) ) = if i >= String . length w then Trie ( Some v , children ) else try let subtrie = CharHashtbl . find children w . [ i ] in let newtrie = aux ( i + 1 ) subtrie in CharHashtbl . replace children w . [ i ] newtrie ; Trie ( value , children ) with Not_found -> begin CharHashtbl . add children w . [ i ] ( aux ( i + 1 ) ( empty ( ) ) ) ; Trie ( value , children ) end in aux 0 trie end ; ; |
let rec await r = if Atomic . get r then ( ) else ( cpu_relax ( ) ; await r ) = " caml_ml_domain_critical_section " |
let go ( ) = let in_crit = Atomic . make false in let woken = Atomic . make false in let d = spawn ( fun ( ) -> critical_section ( fun ( ) -> Atomic . set in_crit true ; wait ( ) ; Atomic . set in_crit false ) ) in await in_crit ; notify ( get_id d ) ; assert ( not ( Atomic . get in_crit ) ) ; join d ; let entered_crit = Atomic . make false in let woken = Atomic . make false in let d = spawn ( fun ( ) -> critical_section ( fun ( ) -> Atomic . set entered_crit true ; await woken ; wait ( ) ) ) in await entered_crit ; Atomic . set woken true ; notify ( get_id d ) ; join d ; let entered_crit = Atomic . make false in let in_second_crit = Atomic . make false in let d = spawn ( fun ( ) -> critical_section ( fun ( ) -> Atomic . set entered_crit true ; wait ( ) ; wait ( ) ) ; critical_section ( fun ( ) -> Atomic . set in_second_crit true ; wait ( ) ; Atomic . set in_second_crit false ) ) in await entered_crit ; notify ( get_id d ) ; await in_second_crit ; join ( spawn ( fun ( ) -> ( ) ) ) ; assert ( Atomic . get in_second_crit ) ; notify ( get_id d ) ; assert ( not ( Atomic . get in_second_crit ) ) ; join d ; let in_crit = Atomic . make false in let d = Domain . spawn ( fun ( ) -> critical_adjust ( + 1 ) ; Atomic . set in_crit true ; wait ( ) ; Atomic . set in_crit false ) in await in_crit ; for i = 1 to 10000 do assert ( Atomic . get in_crit ) done ; notify ( get_id d ) ; assert ( not ( Atomic . get in_crit ) ) ; join d |
let ( ) = for i = 1 to 1000 do go ( ) done ; print_endline " ok " |
type ' a waiter = { enqueue : ( ' a , exn ) result -> unit ; ctx : Cancel . Fiber_context . t ; } |
type ' a t = ' a waiter Lwt_dllist . t |
let add_waiter_protected ~ mutex t cb = let w = Lwt_dllist . add_l cb t in Hook . Node_with_mutex ( w , mutex ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.