text
stringlengths 12
786k
|
---|
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 ) |
let add_waiter t cb = let w = Lwt_dllist . add_l cb t in Hook . Node w |
let wake { enqueue ; ctx } r = if Cancel . Fiber_context . clear_cancel_fn ctx then ( enqueue ( Ok r ) ; true ) else false |
let wake_all ( t : _ t ) v = try while true do let waiter = Lwt_dllist . take_r t in ignore ( wake waiter v : bool ) done with Lwt_dllist . Empty -> ( ) |
let rec wake_one t v = match Lwt_dllist . take_opt_r t with | None -> ` Queue_empty | Some waiter -> if wake waiter v then ` Ok else wake_one t v |
let await_internal ~ mutex ( t ' : a t ) id ( ctx : Cancel . fiber_context ) enqueue = match Cancel . Fiber_context . get_error ctx with | Some ex -> Option . iter Mutex . unlock mutex ; enqueue ( Error ex ) | None -> let resolved_waiter = ref Hook . null in let enqueue x = Ctf . note_read ~ reader : id ctx . tid ; enqueue x in let cancel ex = Hook . remove ! resolved_waiter ; enqueue ( Error ex ) in Cancel . Fiber_context . set_cancel_fn ctx cancel ; let waiter = { enqueue ; ctx } in match mutex with | None -> resolved_waiter := add_waiter t waiter | Some mutex -> resolved_waiter := add_waiter_protected ~ mutex t waiter ; Mutex . unlock mutex |
let await ~ mutex waiters id = Suspend . enter_unchecked ( await_internal ~ mutex waiters id ) |
let all_equal ~ equal ~ compare ls = Option . value_map ( List . hd ls ) ls ~ default : true ~ f ( : fun h -> List . equal equal [ h ] ( List . find_all_dups ~ compare ls ) ls ) |
module Make ( Engine : Intf . Engine . S ) S ( Event_router : Intf . Dsl . Event_router_intf with module Engine := Engine ) Engine ( Network_state : Intf . Dsl . Network_state_intf with module Engine := Engine and module Event_router := Event_router ) Event_router = struct open Network_state module Node = Engine . Network . Node type ' a predicate_result = | Predicate_passed | Predicate_continuation of ' a | Predicate_failure of Error . t type predicate = | Network_state_predicate : ( Network_state . t -> ' a predicate_result ) predicate_result * ( ' a -> Network_state . t -> ' a predicate_result ) predicate_result -> predicate | Event_predicate : ' b Event_type . t * ' a * ( ' a -> Node . t -> ' b -> ' a predicate_result ) predicate_result -> predicate type t = { description : string ; predicate : predicate ; soft_timeout : Network_time_span . t ; hard_timeout : Network_time_span . t } let with_timeouts ? soft_timeout ? hard_timeout t = { t with soft_timeout = Option . value soft_timeout ~ default : t . soft_timeout ; hard_timeout = Option . value hard_timeout ~ default : t . hard_timeout } let network_state ~ description ( ~ f : Network_state . t -> bool ) bool : t = let check ( ) ( state : Network_state . t ) t = if f state then Predicate_passed else Predicate_continuation ( ) in { description ; predicate = Network_state_predicate ( check ( ) , check ) check ; soft_timeout = Literal ( Time . Span . of_hr 1 . 0 ) 0 ; hard_timeout = Literal ( Time . Span . of_hr 2 . 0 ) 0 } let nodes_to_initialize nodes = let open Network_state in network_state ~ description : ( nodes |> List . map ~ f : Node . id |> String . concat ~ sep " , : " |> Printf . sprintf [ " % s ] s to initialize " ) ~ f ( : fun ( state : Network_state . t ) t -> List . for_all nodes ~ f ( : fun node -> String . Map . find state . node_initialization ( Node . id node ) node |> Option . value ~ default : false ) ) |> with_timeouts ~ soft_timeout ( : Literal ( Time . Span . of_min 10 . 0 ) 0 ) 0 ~ hard_timeout ( : Literal ( Time . Span . of_min 15 . 0 ) 0 ) 0 let node_to_initialize node = nodes_to_initialize [ node ] let blocks_to_be_produced n = let init state = Predicate_continuation state . blocks_generated in let check init_blocks_generated state = if state . blocks_generated - init_blocks_generated >= n then Predicate_passed else Predicate_continuation init_blocks_generated in let soft_timeout_in_slots = ( 2 * n ) n + 1 in { description = Printf . sprintf " % d blocks to be produced " n ; predicate = Network_state_predicate ( init , check ) check ; soft_timeout = Slots soft_timeout_in_slots ; hard_timeout = Slots ( soft_timeout_in_slots * 2 ) 2 } let nodes_to_synchronize ( nodes : Node . t list ) list = let check ( ) state = let all_best_tips_equal = all_equal ~ equal [ :% equal : State_hash . t option ] option ~ compare [ :% compare : State_hash . t option ] option in let best_tips = List . map nodes ~ f ( : fun node -> String . Map . find state . best_tips_by_node ( Node . id node ) node ) in if List . for_all best_tips ~ f : Option . is_some && all_best_tips_equal best_tips then Predicate_passed else Predicate_continuation ( ) in let soft_timeout_in_slots = 8 * 3 in let formatted_nodes = nodes |> List . map ~ f ( : fun node -> " " " \ ^ Node . id node ^ ) " " " \ |> String . concat ~ sep " , : " in { description = Printf . sprintf " % s to synchronize " formatted_nodes ; predicate = Network_state_predicate ( check ( ) , check ) check ; soft_timeout = Slots soft_timeout_in_slots ; hard_timeout = Slots ( soft_timeout_in_slots * 2 ) 2 } let signed_command_to_be_included_in_frontier ~ txn_hash ( ~ node_included_in : [ ` Any_node | ` Node of Node . t ] ) = let check ( ) state = let blocks_with_txn_set_opt = Map . find state . blocks_including_txn txn_hash in match blocks_with_txn_set_opt with | None -> Predicate_continuation ( ) | Some blocks_with_txn_set -> ( match node_included_in with | ` Any_node -> Predicate_passed | ` Node n -> let blocks_seen_by_n = Map . find state . blocks_seen_by_node ( Node . id n ) n |> Option . value ~ default : State_hash . Set . empty in let intersection = State_hash . Set . inter blocks_with_txn_set blocks_seen_by_n in if State_hash . Set . is_empty intersection then Predicate_continuation ( ) else Predicate_passed ) in let soft_timeout_in_slots = 8 in { description = Printf . sprintf " signed command with hash % s " ( Transaction_hash . to_base58_check txn_hash ) txn_hash ; predicate = Network_state_predicate ( check ( ) , check ) check ; soft_timeout = Slots soft_timeout_in_slots ; hard_timeout = Slots ( soft_timeout_in_slots * 2 ) 2 } let ledger_proofs_emitted_since_genesis ~ num_proofs = let open Network_state in let check ( ) ( state : Network_state . t ) t = if state . snarked_ledgers_generated >= num_proofs then Predicate_passed else Predicate_continuation ( ) in let description = Printf . sprintf [ " % d ] d snarked_ledgers to be generated since genesis " num_proofs in { description ; predicate = Network_state_predicate ( check ( ) , check ) check ; soft_timeout = Slots 15 ; hard_timeout = Slots 20 } let snapp_to_be_included_in_frontier ~ has_failures ~ parties = let command_matches_parties cmd = let open User_command in match cmd with | Parties p -> Parties . equal p parties | Signed_command _ -> false in let check ( ) _node ( breadcrumb_added : Event_type . Breadcrumb_added . t ) t = let snapp_opt = List . find breadcrumb_added . user_commands ~ f ( : fun cmd_with_status -> cmd_with_status . With_status . data |> User_command . forget_check |> command_matches_parties ) in match snapp_opt with | Some cmd_with_status -> let actual_status = cmd_with_status . With_status . status in let successful = match actual_status with | Transaction_status . Applied -> not has_failures | Failed _ -> has_failures in if successful then Predicate_passed else Predicate_failure ( Error . createf " Unexpected status in matching payment : % s " ( Transaction_status . to_yojson actual_status |> Yojson . Safe . to_string ) ) | None -> Predicate_continuation ( ) in let soft_timeout_in_slots = 8 in let is_first = ref true in { description = sprintf " snapp with fee payer % s and other parties ( % s ) s " ( Signature_lib . Public_key . Compressed . to_base58_check parties . fee_payer . body . public_key ) ( Parties . Call_forest . Tree . fold_forest ~ init " " : parties . other_parties ~ f ( : fun acc party -> let str = Signature_lib . Public_key . Compressed . to_base58_check party . body . public_key in if ! is_first then ( is_first := false ; str ) else acc ^ " , " ^ str ) ) ; predicate = Event_predicate ( Event_type . Breadcrumb_added , ( ) , check ) check ; soft_timeout = Slots soft_timeout_in_slots ; hard_timeout = Slots ( soft_timeout_in_slots * 2 ) 2 } end |
let refl = Filename . concat Filename . current_dir_name " reflector . exe " |
let ( ) = let oc = Unix . open_process_out ( refl ^ " - i2o " ) in let pid = Unix . process_out_pid oc in let ( pid1 , status1 ) = Unix . waitpid [ WNOHANG ] pid in assert ( pid1 = 0 ) ; assert ( status1 = WEXITED 0 ) ; output_string oc " aa \ n " ; close_out oc ; let rec busywait ( ) = let ( pid2 , status2 ) = Unix . waitpid [ WNOHANG ] pid in if pid2 = 0 then begin Unix . sleepf 0 . 001 ; busywait ( ) end else begin assert ( pid2 = pid ) ; assert ( status2 = WEXITED 0 ) end in busywait ( ) |
module Steps = struct type t = [ ` PerZ of float | ` Flat of int ] let to_int t z = match t with ` PerZ mm -> Int . max 2 ( Float . to_int ( z . / mm ) ) | ` Flat n -> n end |
module Edge = struct type t = float -> Vec3 . t let translate p = Fn . compose ( Vec3 . add p ) let scale s = Fn . compose ( Vec3 . scale s ) let mirror ax = Fn . compose ( Vec3 . mirror ax ) let rotate r = Fn . compose ( Vec3 . rotate r ) let rotate_about_pt r p = Fn . compose ( Vec3 . rotate_about_pt r p ) let quaternion q = Fn . compose ( Vec3 . quaternion q ) let quaternion_about_pt q p = Fn . compose ( Vec3 . quaternion_about_pt q p ) let point_at_z ( ? max_iter = 100 ) ( ? tolerance = 0 . 001 ) t z = let bez_frac = Util . bisection_exn ~ max_iter ~ tolerance ~ f ( : fun s -> Vec3 . get_z ( t s ) . - z ) 0 . 1 . in t bez_frac end |
module EdgeDrawer = struct type drawer = Vec3 . t -> Edge . t type t = { top : drawer ; bot : drawer } let make ( ? max_iter = 100 ) ( ? tolerance = 0 . 001 ) ( ~ get_bez : bool -> Vec3 . t -> Edge . t ) Points . { top_left ; top_right ; bot_left ; bot_right ; _ } = let find_between lp rp ( x , y , _ ) = let ( ( dx , dy , _ ) as diff ) = Vec3 . sub rp lp in let get_major , target = if Float . ( abs dx > abs dy ) then ( Vec3 . get_x , x ) else ( Vec3 . get_y , y ) in let ml = get_major lp and mr = get_major rp in if Float . ( target > ml && target < mr ) || Float . ( target < ml && target > mr ) then let get s = Vec3 . ( add lp ( mul_scalar diff s ) ) in let pos = Util . bisection_exn ~ max_iter ~ tolerance ~ f ( : fun s -> get_major ( get s ) . - target ) 0 . 1 . in get pos else if Float . ( abs ( target . - ml ) < abs ( target . - mr ) ) then lp else rp in { top = find_between top_left top_right >> get_bez true ; bot = find_between bot_left bot_right >> get_bez false } let map ~ f t = { top = f t . top ; bot = f t . bot } let translate p = map ~ f ( : fun d start -> d start >> Vec3 . add p ) let scale s = map ~ f ( : fun d start -> d start >> Vec3 . scale s ) let mirror ax = map ~ f ( : fun d start -> d start >> Vec3 . mirror ax ) let rotate r = map ~ f ( : fun d start -> d start >> Vec3 . rotate r ) let rotate_about_pt r p = map ~ f ( : fun d start -> d start >> Vec3 . rotate_about_pt r p ) let quaternion q = map ~ f ( : fun d start -> d start >> Vec3 . quaternion q ) let quaternion_about_pt q p = map ~ f ( : fun d start -> d start >> Vec3 . quaternion_about_pt q p ) end |
module Edges = struct type t = { top_left : Edge . t ; top_right : Edge . t ; bot_left : Edge . t ; bot_right : Edge . t } [ @@ deriving scad ] let map ~ f t = { top_left = f t . top_left ; top_right = f t . top_right ; bot_left = f t . bot_left ; bot_right = f t . bot_right } let of_clockwise_list_exn = function | [ top_left ; top_right ; bot_right ; bot_left ] -> { top_left ; top_right ; bot_left ; bot_right } | _ -> failwith " Expect list of length 4 , with edges beziers in clockwise order . " let of_clockwise_list l = try Ok ( of_clockwise_list_exn l ) with Failure e -> Error e let get t = function | ` TL -> t . top_left | ` TR -> t . top_right | ` BL -> t . bot_left | ` BR -> t . bot_right end |
type config = { d1 : float ; d2 : float ; z_off : float ; thickness : float ; clearance : float ; n_steps : Steps . t ; n_facets : int ; eyelet_config : Eyelet . config option } |
let default = { d1 = 2 . ; d2 = 5 . ; z_off = 0 . ; thickness = 3 . 5 ; clearance = 1 . 5 ; n_steps = ` Flat 4 ; n_facets = 1 ; eyelet_config = None } |
type t = { scad : Scad . d3 ; start : Points . t ; foot : Points . t ; edge_drawer : EdgeDrawer . t ; edges : Edges . t ; screw : Eyelet . t option } |
let swing_face ( ? step = Float . pi . / 24 . ) key_origin face = let quat = Quaternion . make ( KeyHole . Face . direction face ) and free , pivot , rock_z , z_sign = let ortho = Vec3 . ( normalize ( face . points . centre <-> key_origin ) ) in if Float . ( Vec3 . get_z ortho > 0 . ) then ( face . points . top_right , Vec3 . ( div ( face . points . bot_left <+> face . points . bot_right ) ( - 2 . , - 2 . , - 2 . ) ) , Vec3 . get_z face . points . bot_right , 1 . ) else ( face . points . bot_right , Vec3 . ( div ( face . points . top_left <+> face . points . top_right ) ( - 2 . , - 2 . , - 2 . ) ) , Vec3 . get_z face . points . top_right , - 1 . ) in let diff a = Vec3 . ( get_z ( Vec3 . quaternion_about_pt ( quat ( a . * z_sign ) ) pivot free ) . - rock_z ) . * z_sign in let rec find_angle a last_diff = if Float . ( a < pi ) then let d = diff a in if Float . ( d < last_diff ) then a . - step else find_angle ( a . + step ) d else a . - step in let q = quat @@ ( find_angle step ( Vec3 . get_z free . - rock_z ) . * z_sign ) in let face ' = KeyHole . Face . quaternion_about_pt q pivot face in let ortho ' = Vec3 . quaternion_about_pt q pivot key_origin |> Vec3 . sub face ' . points . centre |> Vec3 . normalize in ( face ' , ortho ' ) |
let poly_siding ( ? x_off = 0 . ) ( ? y_off = 0 . ) ( ? z_off = 0 . ) ( ? clearance = 1 . 5 ) ( ? n_steps = ` Flat 4 ) ( ? n_facets = 1 ) ( ? d1 = 2 . ) ( ? d2 = 5 . ) ? thickness ? eyelet_config side ( key : _ KeyHole . t ) = let start_face = KeyHole . Faces . face key . faces side and thickness = Option . value ~ default : key . config . thickness thickness in let pivoted_face , ortho = swing_face key . origin start_face in let cleared_face = KeyHole . Face . translate ( Vec3 . map ( ( . * ) clearance ) ortho ) pivoted_face in let xy = Vec3 . ( normalize ( mul ortho ( 1 . , 1 . , 0 . ) ) ) and z_hop = ( Float . max 0 . ( Vec3 . get_z ortho ) . * key . config . thickness ) . + z_off and top_offset = Vec3 . ( mul ( 1 . , 1 . , 0 . ) ( cleared_face . points . bot_right <-> cleared_face . points . top_right ) ) in let get_bez top ( ( x , y , z ) as start ) = let jog , d1 , plus = let half_delta = ( d2 . - d1 ) . / 2 . in if top then ( thickness , d1 . + Float . max half_delta 0 . , top_offset ) else ( 0 . , d1 . + Float . min half_delta 0 . , ( 0 . , 0 . , 0 . ) ) in let p1 = Vec3 . ( start <-> mul ortho ( 0 . 01 , 0 . 01 , 0 . 01 ) ) and p2 = Vec3 . ( mul xy ( d1 . + jog , d1 . + jog , 0 . ) |> add ( x . + x_off , y . + y_off , z . + z_hop ) |> add plus ) and p3 = Vec3 . ( add ( mul xy ( d2 . + jog , d2 . + jog , 0 . ) ) ( x . + x_off , y . + y_off , 0 . ) |> add plus ) in ( p3 , Bezier . quad_vec3 ~ p1 ~ p2 ~ p3 ) in let cw_points = let n = n_facets - 1 in Util . fill_points ~ init : ( Util . fill_points ~ n cleared_face . points . bot_left cleared_face . points . bot_right ) ~ n cleared_face . points . top_right cleared_face . points . top_left in let corners = List . filteri ~ f ( : fun i _ -> i = 0 || i = n_facets || i = n_facets + 1 || i = 3 + ( ( n_facets - 1 ) * 2 ) ) and steps = let adjust ( _ , _ , z ) = let lowest_z = let f m ( _ , _ , z ) = Float . min m z in Points . fold ~ f ~ init : Float . max_value cleared_face . points in Float . ( to_int ( ( 1 . . + ( ( z . - lowest_z ) . / z ) ) . * of_int ( Steps . to_int n_steps z ) ) ) in ` Ragged ( List . map ~ f : adjust cw_points ) and end_ps , bezs = List . foldi ~ f ( : fun i ( ends , bs ) p -> let e , b = get_bez ( i > n_facets ) p in ( e :: ends , b :: bs ) ) ~ init ( [ ] , : [ ] ) ( List . rev cw_points ) in let foot = Points . of_clockwise_list_exn ( corners end_ps ) in let screw = let f config = let open Eyelet in let placement = let n = Vec3 . negate xy in match config with | { hole = Through ; _ } -> Normal n | { hole = Inset _ ; outer_rad ; _ } -> let offset = outer_rad . + Vec3 . ( norm ( sub foot . top_left foot . bot_left ) . / 4 . ) in Point Vec3 . ( mean [ foot . top_left ; foot . top_right ] <+> mul_scalar n offset ) in make ~ placement config foot . bot_left foot . bot_right in Option . map ~ f eyelet_config in { scad = Scad . hull [ start_face . scad ; cleared_face . scad ] :: Bezier . prism_exn bezs steps :: Option . value_map ~ default [ ] : ~ f ( : fun s -> [ s . scad ] ) screw |> Scad . union |> Fn . flip Scad . difference ( Option . value_map ~ default [ ] : ~ f ( : fun s -> Option . to_list s . cut ) screw ) ; start = start_face . points ; foot ; edge_drawer = EdgeDrawer . make ~ get_bez ( : fun top start -> snd ( get_bez top start ) ) cleared_face . points ; edges = Edges . of_clockwise_list_exn ( corners bezs ) ; screw } |
let poly_of_config ? x_off ? y_off { d1 ; d2 ; z_off ; thickness ; clearance ; n_steps ; n_facets ; eyelet_config } = poly_siding ~ d1 ~ d2 ? x_off ? y_off ~ z_off ~ thickness ~ clearance ~ n_steps ~ n_facets ? eyelet_config |
let column_drop ? z_off ? clearance ? n_steps ? n_facets ? d1 ? d2 ? thickness ? eyelet_config ~ spacing ~ columns side idx = let key , face , hanging = let c : _ Column . t = Map . find_exn columns idx in match side with | ` North -> let key = snd @@ Map . max_elt_exn c . keys in let edge_y = Vec3 . get_y key . faces . north . points . centre in ( key , key . faces . north , Float . ( ( <= ) edge_y ) ) | ` South -> let key = Map . find_exn c . keys 0 in let edge_y = Vec3 . get_y key . faces . south . points . centre in ( key , key . faces . south , Float . ( ( >= ) edge_y ) ) in let x_dodge = match Map . find columns ( idx + 1 ) with | Some next_c -> let right_x = Vec3 . get_x face . points . top_right and next_face = KeyHole . Faces . face ( snd @@ Map . max_elt_exn next_c . keys ) . faces side in let diff = if hanging ( Vec3 . get_y next_face . points . centre ) then right_x . - Vec3 . get_x next_face . points . bot_left else . - spacing in if Float . ( diff > 0 . ) then diff . + spacing else Float . max 0 . ( spacing . + diff ) | _ -> 0 . in poly_siding ~ x_off ( : x_dodge . * - 1 . ) ? z_off ? clearance ? d1 ? d2 ? thickness ? n_steps ? n_facets ? eyelet_config side key |
let drop_of_config ~ spacing { d1 ; d2 ; z_off ; thickness ; clearance ; n_steps ; n_facets ; eyelet_config } = column_drop ~ d1 ~ d2 ~ z_off ~ thickness ~ clearance ~ n_steps ~ n_facets ~ spacing ? eyelet_config |
let start_direction { start = { top_left ; top_right ; _ } ; _ } = Vec3 . normalize Vec3 . ( top_left <-> top_right ) |
let foot_direction { foot = { top_left ; top_right ; _ } ; _ } = Vec3 . normalize Vec3 . ( top_left <-> top_right ) |
let to_scad t = t . scad |
module Mnemonic = struct let new_random = Bip39 . of_entropy ( Hacl . Rand . gen 32 ) let to_sapling_key mnemonic = let seed_64_to_seed_32 ( seed_64 : bytes ) : bytes = assert ( Bytes . length seed_64 = 64 ) ; let first_32 = Bytes . sub seed_64 0 32 in let second_32 = Bytes . sub seed_64 32 32 in let seed_32 = Bytes . create 32 in for i = 0 to 31 do Bytes . set seed_32 i ( Char . chr ( Char . code ( Bytes . get first_32 i ) lxor Char . code ( Bytes . get second_32 i ) ) ) done ; seed_32 in Spending_key . of_seed ( seed_64_to_seed_32 ( Bip39 . to_seed mnemonic ) ) let words_pp = Format . ( pp_print_list ~ pp_sep : pp_print_space pp_print_string ) end |
let to_uri unencrypted cctxt sapling_key = if unencrypted then Tezos_signer_backends . Unencrypted . make_sapling_key sapling_key >>?= return else Tezos_signer_backends . Encrypted . encrypt_sapling_key cctxt sapling_key |
let from_uri ( cctxt : # Client_context . full ) uri = Tezos_signer_backends . Encrypted . decrypt_sapling_key cctxt uri |
let register ( cctxt : # Client_context . full ) ( ? force = false ) ( ? unencrypted = false ) mnemonic name = let sk = Mnemonic . to_sapling_key mnemonic in to_uri unencrypted cctxt sk >>=? fun sk_uri -> let key = { sk = sk_uri ; path = [ Spending_key . child_index sk ] ; address_index = Viewing_key . default_index ; } in Sapling_key . add ~ force cctxt name key >>=? fun ( ) -> return @@ Viewing_key . of_sk sk |
let derive ( cctxt : # Client_context . full ) ( ? force = false ) ( ? unencrypted = false ) src_name dst_name child_index = Sapling_key . find cctxt src_name >>=? fun k -> from_uri cctxt k . sk >>=? fun src_sk -> let child_index = Int32 . of_int child_index in let dst_sk = Spending_key . derive_key src_sk child_index in to_uri unencrypted cctxt dst_sk >>=? fun dst_sk_uri -> let dst_key = { sk = dst_sk_uri ; path = child_index :: k . path ; address_index = Viewing_key . default_index ; } in let _ = force in Sapling_key . add ~ force : true cctxt dst_name dst_key >>=? fun ( ) -> let path = String . concat " " / ( List . map Int32 . to_string ( List . rev dst_key . path ) ) in return ( path , Viewing_key . of_sk dst_sk ) |
let find_vk cctxt name = Sapling_key . find cctxt name >>=? fun k -> from_uri cctxt k . sk >>=? fun sk -> return ( Viewing_key . of_sk sk ) |
let new_address ( cctxt : # Client_context . full ) name index_opt = Sapling_key . find cctxt name >>=? fun k -> let index = match index_opt with | None -> k . address_index | Some i -> Viewing_key . index_of_int64 ( Int64 . of_int i ) in from_uri cctxt k . sk >>=? fun sk -> return ( Viewing_key . of_sk sk ) >>=? fun vk -> let ( corrected_index , address ) = Viewing_key . new_address vk index in Sapling_key . update cctxt name { k with address_index = Viewing_key . index_succ corrected_index } >>=? fun ( ) -> return ( sk , corrected_index , address ) |
let export_vk cctxt name = find_vk cctxt name >>=? fun vk -> return ( Data_encoding . Json . construct Viewing_key . encoding vk ) |
type locked_key = | Locked of string | Unlocked of ( string * Keypair . t ) t | Hd_account of Mina_numbers . Hd_index . t |
type t = { cache : locked_key Public_key . Compressed . Table . t ; path : string } |
let get_privkey_filename public_key = Public_key . Compressed . to_base58_check public_key |
let get_path { path ; cache } public_key = let filename = Public_key . Compressed . Table . find cache public_key |> Option . bind ~ f ( : function | Locked file | Unlocked ( file , _ ) _ -> Option . return file | Hd_account _ -> Option . return ( Public_key . Compressed . to_base58_check public_key ^ " . index ) " ) |> Option . value ~ default ( : get_privkey_filename public_key ) public_key in path ^/ filename |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.