text
stringlengths
0
601k
let gauss a b x = let xb = Vec . sub x b in . ~- ( exp ( dot xb ( symv a xb ) . / 2 . 0 ) )
let dgauss a b x = symv ~ alpha ( : gauss a b x ) a ( Vec . sub x b )
let gnuplot f = let oc = Unix . open_process_out " gnuplot " in let ppf = formatter_of_out_channel oc in f ppf ; pp_print_flush ppf ( ) ; print_endline " Press Enter to exit gnuplot " ; ignore ( read_line ( ) ) ; ignore ( Unix . close_process_out oc )
let splot_fun ( ? option = " " ) ( ? n = 10 ) ( ? x1 = - 10 . 0 ) ( ? x2 = 10 . 0 ) ( ? y1 = - 10 . 0 ) ( ? y2 = 10 . 0 ) ppf f = let cx = ( x2 . - x1 ) . / float n in let cy = ( y2 . - y1 ) . / float n in fprintf ppf " splot ' ' - % s @\ n " option ; for i = 0 to n do for j = 0 to n do let x = cx . * float i . + x1 in let y = cy . * float j . + y1 in let z = f [ % vec [ x ; y ] ] in fprintf ppf " % g % g % g @\ n " x y z ; done ; fprintf ppf " @\ n " done ; fprintf ppf " end @\ n "
let steepest_descent ppf ~ loops ~ eta df f x = fprintf ppf " splot ' ' - with linespoints linetype 2 title ' ' @\ n \ % a 0 @\ n " pp_rfvec x ; for i = 1 to loops do axpy ~ alpha ( . :~- eta ) ( df x ) x ; fprintf ppf " % a 0 @\ n " pp_rfvec x done ; fprintf ppf " end @\ n "
let ( ) = let a = [ % mat [ - 0 . 1 , 0 . 1 ; 0 . 1 , - 0 . 2 ] ] in let b = [ % vec [ 1 . 0 ; 3 . 0 ] ] in let ( x1 , x2 , y1 , y2 ) = ( - 2 . 0 , 3 . 0 , 0 . 0 , 5 . 0 ) in gnuplot ( fun ppf -> fprintf ppf " set multiplot @\ n \ set view 0 , 0 # Fix a view @\ n \ unset ztics # Don ' t show ztics @\ n \ unset clabel # Don ' t show contour labels @\ n \ set ticslevel 0 @\ n \ set xrange [ % g :% g ] @\ n \ set yrange [ % g :% g ] @\ n " x1 x2 y1 y2 ; steepest_descent ppf ~ loops : 100 ~ eta : 2 . 0 ( dgauss a b ) ( gauss a b ) [ % vec [ 0 . 0 ; 1 . 0 ] ] ; fprintf ppf " set contour # Plot contour @\ n \ unset xtics # Don ' t show xtics twice @\ n \ unset ytics # Don ' t show xtics twice @\ n \ unset surface # Don ' t show a 3D surface @\ n \ set cntrparam levels 15 # Levels of contour @\ n " ; splot_fun ppf ( gauss a b ) ~ n : 50 ~ x1 ~ x2 ~ y1 ~ y2 ~ option " : w l lt 1 title ' ' " )
module type core = sig type t val t : t Ctypes . typ val ctype : t Ctypes . typ val null : t val ctype_opt : t option Ctypes . typ val pp : Format . formatter -> t -> unit val array : t list -> t Ctypes . CArray . t val array_opt : t list -> t option Ctypes . CArray . t end
module type S = sig include core val to_ptr : t -> nativeint val unsafe_from_ptr : nativeint -> t end
module type S_non_dispatchable = sig include core val to_int64 : t -> int64 val unsafe_from_int64 : int64 -> t end
module Make ( ) : S = struct type self type t = self Ctypes . structure Ctypes . ptr let t : t Ctypes . typ = Ctypes . ptr ( Ctypes . structure " " ) let ctype = t let null = Ctypes . ( coerce @@ ptr void ) t Ctypes . null let ctype_opt = let read v = if v = null then None else Some v in let write = function | None -> null | Some x -> x in Ctypes . view ~ read ~ write t let pp ppf ( x : t ) = Format . fprintf ppf " % s " @@ Nativeint . to_string @@ Ctypes . ( raw_address_of_ptr @@ coerce t ( ptr void ) x ) let array l = let a = Ctypes . CArray . of_list t l in Gc . finalise ( fun _ -> let _kept_alive = ref l in ( ) ) a ; a let array_opt l = let a = Ctypes . CArray . of_list ctype_opt ( List . map ( fun x -> Some x ) l ) in Gc . finalise ( fun _ -> let _kept_alive = ref l in ( ) ) a ; a let to_ptr x = Ctypes . raw_address_of_ptr @@ Ctypes . coerce t Ctypes . ( ptr void ) x let unsafe_from_ptr x = Ctypes . coerce Ctypes . ( ptr void ) t @@ Ctypes . ptr_of_raw_address x end
module Make_non_dispatchable ( ) : S_non_dispatchable = struct type self type t = int64 let t : t Ctypes . typ = Ctypes . int64_t let ctype = t let null = 0L let ctype_opt = let read v = if v = null then None else Some v in let write = function | None -> null | Some x -> x in Ctypes . view ~ read ~ write t let pp ppf ( x : t ) = Format . fprintf ppf " % s " @@ Nativeint . to_string @@ Ctypes . ( raw_address_of_ptr @@ coerce t ( ptr void ) x ) let array l = let a = Ctypes . CArray . of_list t l in Gc . finalise ( fun _ -> let _kept_alive = ref l in ( ) ) a ; a let array_opt l = let a = Ctypes . CArray . of_list ctype_opt ( List . map ( fun x -> Some x ) l ) in Gc . finalise ( fun _ -> let _kept_alive = ref l in ( ) ) a ; a let to_int64 x = x let unsafe_from_int64 x = x end
module type intlike = sig type t val zero : t val ctype : t Ctypes . typ end
let integer_opt ( type a ) ( module I : intlike with type t = a ) = let read x = if x = I . zero then None else Some x in let write = function None -> I . zero | Some x -> x in Ctypes . view ~ read ~ write I . ctype
let integer_opt ' zero ctype = let read x = if x = zero then None else Some x in let write = function None -> zero | Some x -> x in Ctypes . view ~ read ~ write ctype
let uint_32_t = Ctypes . view ~ read ( : U32 . to_int ) ~ write ( : U32 . of_int ) Ctypes . uint32_t
let uint_16_t = Ctypes . view ~ read ( : U16 . to_int ) ~ write ( : U16 . of_int ) Ctypes . uint16_t
module Int = struct type t = int let zero = 0 let pp = Format . pp_print_int let ctype = Ctypes . int end
module Uint_8_t = struct open U8 type t = U8 . t let ctype = Ctypes . uint8_t let zero = of_int 0 let of_int = of_int let to_int = to_int let to_string = to_string let pp ppf x = Format . pp_print_string ppf ( to_string x ) end
let bool_32 = let true ' = U32 . of_int Vk__Const . true ' and false ' = U32 . of_int Vk__Const . false ' in Ctypes . view ~ read ( : ( ) = true ' ) ~ write ( : fun x -> if x then true ' else false ' ) Ctypes . uint32_t
let bool_32_opt = let true ' = U32 . of_int Vk__Const . true ' and false ' = U32 . of_int Vk__Const . false ' in Ctypes . view ~ read ( : fun x -> if U32 . zero = x then None else if x = true ' then Some true else Some false ) ~ write ( : function None -> U32 . zero | Some x -> if x then true ' else false ' ) Ctypes . uint32_t
module Size_t_0 = struct let of_int = S . of_int let to_int = S . to_int let zero = of_int 0 let to_string = S . to_string let pp ppf x = Format . fprintf ppf " % s " ( S . to_string x ) type t = S . t let ctype = Ctypes . size_t end
module Size_t = struct include Size_t_0 let ctype_opt = integer_opt ( module Size_t_0 ) end
let size_t_opt = integer_opt ( module Size_t )
module Uint_32_t_0 = struct let zero = 0 let of_int x = x let to_int x = x let to_string = string_of_int let pp ppf x = Format . fprintf ppf " % d " x type t = int let ctype = uint_32_t end
module Uint_32_t = struct include Uint_32_t_0 let ctype_opt = integer_opt ( module Uint_32_t_0 ) end
module Uint_16_t_0 = struct let zero = 0 let of_int x = x let to_int x = x let to_string = string_of_int let pp ppf x = Format . fprintf ppf " % d " x type t = int let ctype = uint_16_t end
module Uint_16_t = struct include Uint_16_t_0 let ctype_opt = integer_opt ( module Uint_16_t_0 ) end
module Bool_32 = struct type t = bool let t = bool let ctype = bool_32 let ctype_opt = bool_32_opt let zero = true let pp = Format . pp_print_bool end
module Int_32_t = struct let zero = 0 let of_int x = x let to_int x = x let to_string = string_of_int let pp ppf x = Format . fprintf ppf " % d " x type t = int let read = Int32 . to_int let write = Int32 . of_int let ctype = Ctypes . view ~ read ~ write Ctypes . int32_t let ctype_opt = integer_opt ' zero ctype end
module Int_64_t = struct let zero = 0 let of_int x = x let to_int x = x let to_string = string_of_int let pp ppf x = Format . fprintf ppf " % d " x type t = int let read = Int64 . to_int let write = Int64 . of_int let ctype = Ctypes . view ~ read ~ write Ctypes . int64_t let ctype_opt = integer_opt ' zero ctype end
module Uint_64_t = struct let of_int = U64 . of_int let to_int = U64 . to_int let zero = of_int 0 let to_string = U64 . to_string let pp ppf x = Format . fprintf ppf " % s " ( U64 . to_string x ) type t = U64 . t let ctype = Ctypes . uint64_t let ctype_opt = integer_opt ' zero ctype end
module type aliased = sig type t val zero : t val ctype : t Ctypes . typ val ctype_opt : t option Ctypes . typ val of_int : int -> t val to_int : t -> int val to_string : t -> string end
module type alias = sig type x type t = private x val make : x -> t val ctype : t Ctypes . typ val ctype_opt : t option Ctypes . typ val of_int : int -> t val zero : t val to_int : t -> int val pp : Format . formatter -> t -> unit end
module Alias ( X : aliased ) : alias with type x := X . t = struct type t = X . t let make x = x let zero = X . zero let ctype = X . ctype let ctype_opt = X . ctype_opt let of_int = X . of_int let to_int = X . to_int let pp ppf x = Format . fprintf ppf " % s " ( X . to_string x ) end
module Float = struct type t = float let pp = Format . pp_print_float let ctype = Ctypes . float end
module Double = struct type t = float let pp = Format . pp_print_float let ctype = Ctypes . double end
module Void = struct type t = void let ctype = Ctypes . void let pp = Vk__helpers . Pp . abstract end
module Cametallayer = struct type m type t = m Ctypes . structure let ctype : t Ctypes . typ = Ctypes . structure " CAmetallayer " let pp = Vk__helpers . Pp . abstract end
type cametallayer = Cametallayer . t Ctypes . structure
module type Config = sig val shift : int val char_of_digit : int -> char val digit_of_char : char -> int end
module type S = sig val encode : Buffer . t -> int -> unit val decode : char Stream . t -> int end
module Make ( C : Config ) = struct let vlq_base = 1 lsl C . shift let vlq_base_mask = vlq_base - 1 let vlq_continuation_bit = vlq_base let vlq_signed_of_int value = if value < 0 then ( ( - value ) lsl 1 ) + 1 else ( value lsl 1 ) + 0 let rec encode_vlq buf vlq = let digit = vlq land vlq_base_mask in let vlq = vlq lsr C . shift in if vlq = 0 then Buffer . add_char buf ( C . char_of_digit digit ) else begin Buffer . add_char buf ( C . char_of_digit ( digit lor vlq_continuation_bit ) ) ; encode_vlq buf vlq end let encode buf value = let vlq = vlq_signed_of_int value in encode_vlq buf vlq let decode = let rec helper ( acc , shift ) stream = let chr = try Stream . next stream with Stream . Failure -> raise Unexpected_eof in let digit = C . digit_of_char chr in let continued = ( digit land vlq_continuation_bit ) != 0 in let acc = acc + ( digit land vlq_base_mask ) lsl shift in if continued then helper ( acc , shift + C . shift ) stream else acc in fun stream -> let acc = helper ( 0 , 0 ) stream in let abs = acc / 2 in if acc land 1 = 0 then abs else ( - abs ) end
module Base64 = Make ( struct let shift = 5 let base64 = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 " +/ let char_of_digit digit = if 0 <= digit && digit < String . length base64 then base64 . [ digit ] else failwith ( Printf . sprintf " Must be between 0 and 63 : % d " digit ) let digit_of_char chr = try String . index base64 chr with Not_found -> raise ( Invalid_base64 chr ) end )
let code_rev = let a = Array . make 255 ( - 1 ) in for i = 0 to String . length code - 1 do a . ( Char . code code . [ i ] ) <- i done ; a
let vlq_base = 1 lsl vlq_base_shift
let vlq_base_mask = vlq_base - 1
let toVLQSigned v = if v < 0 then ( - v lsl 1 ) + 1 else v lsl 1
let fromVLQSigned v = let is_neg = v land 1 = 1 in let shift = v lsr 1 in if is_neg then - shift else shift
let add_char buf x = Buffer . add_char buf code . [ x ]
let rec encode ' buf x = let digit = x land vlq_base_mask in let rest = x lsr vlq_base_shift in if rest = 0 then add_char buf digit else ( add_char buf ( digit lor vlq_continuation_bit ) ; encode ' buf rest )
let encode b x = let vql = toVLQSigned x in encode ' b vql
let encode_l b l = List . iter ~ f ( : encode b ) l
let rec decode ' acc s start pos = let digit = code_rev . ( Char . code s . [ pos ] ) in if digit = - 1 then invalid_arg " Vql64 . decode ' " ; let cont = digit land vlq_continuation_bit = vlq_continuation_bit in let digit = digit land vlq_base_mask in let acc = acc + ( digit lsl ( ( pos - start ) * vlq_base_shift ) ) in if cont then decode ' acc s start ( succ pos ) else acc , succ pos
let decode s p = let d , i = decode ' 0 s p p in fromVLQSigned d , i
let decode_l s ~ pos ~ len = let rec aux pos acc len = if len = 0 then List . rev acc else if len < 0 then invalid_arg " Vlq64 . decode_l " else let d , i = decode s pos in let len = len - ( i - pos ) in aux i ( d :: acc ) len in aux pos [ ] len
module Make ( V : HashCmp ) ( L : Lattice ) ( R : Result ) = struct type v = V . t * L . t type r = R . t type d = Leaf of r | Branch of V . t * L . t * t * t and t = { id : int ; d : d } let equal x y = x . id = y . id let rec to_string t = match t . d with | Leaf r -> R . to_string r | Branch ( v , l , t , f ) -> Printf . sprintf " B ( % s = % s , % s , % s ) " ( V . to_string v ) ( L . to_string l ) ( to_string t ) ( to_string f ) type this_t = t module M = struct module Table = Weak . Make ( struct type t = this_t let hash t = match t . d with | Leaf r -> 101 * R . hash r | Branch ( v , l , t , f ) -> 191 * ( V . hash v ) + 877 * ( L . hash l ) + 3511 * t . id + 3559 * f . id let equal a b = match a . d , b . d with | Leaf r1 , Leaf r2 -> R . compare r1 r2 = 0 | Branch ( vx , lx , tx , fx ) , Branch ( vy , ly , ty , fy ) -> V . compare vx vy = 0 && tx . id = ty . id && fx . id = fy . id && L . compare lx ly = 0 | _ , _ -> false end ) type t = { table : Table . t ; mutable sym : int } let create size = { table = Table . create size ; sym = 1 } let clear t = Table . clear t . table let gensym t = let s = t . sym in t . sym <- t . sym + 1 ; s let get t d = try Table . find t . table { id = 0 ; d } with Not_found -> Table . merge t . table { id = gensym t ; d } let remove t d = Table . remove t . table { id = 0 ; d } end let manager = M . create 100 let mk_leaf r = M . get manager ( Leaf r ) let mk_branch v l t f = if equal t f then begin t end else M . get manager ( Branch ( v , l , t , f ) ) let rec fold g h t = match t . d with | Leaf r -> g r | Branch ( v , l , t , f ) -> h ( v , l ) ( fold g h t ) ( fold g h f ) let destruct g h t = match t . d with | Leaf r -> g r | Branch ( v , l , t , f ) -> h ( v , l ) t f let rec iter ( ? order ` = Pre ) g h t = match t . d with | Leaf r -> g r | Branch ( v , l , t , f ) -> match order with | ` Pre -> h ( v , l ) t f ; iter ~ order g h t ; iter ~ order g h f | ` Post -> iter ~ order g h t ; iter ~ order g h f ; h ( v , l ) t f let const r = mk_leaf r let atom ( v , l ) t f = mk_branch v l ( const t ) ( const f ) let restrict lst = let rec loop xs u = match xs , u . d with | [ ] , _ | _ , Leaf _ -> u | ( v , l ) :: xs ' , Branch ( v ' , l ' , t , f ) -> match V . compare v v ' with | 0 -> if L . subset_eq l l ' then loop xs ' t else loop xs f | - 1 -> loop xs ' u | 1 -> mk_branch v ' l ' ( loop xs t ) ( loop xs f ) | _ -> assert false in loop ( List . sort ( fun ( u , _ ) ( v , _ ) -> V . compare u v ) lst ) let peek t = match t . d with | Leaf r -> Some r | Branch _ -> None let rec map_r g = fold ( fun r -> const ( g r ) ) ( fun ( v , l ) t f -> mk_branch v l t f ) let rec prod x y = match x . d , y . d with | Leaf r , _ -> if R . ( compare r zero ) = 0 then x else if R . ( compare r one ) = 0 then y else map_r ( R . prod r ) y | _ , Leaf r -> if R . ( compare zero r ) = 0 then y else if R . ( compare one r ) = 0 then x else map_r ( fun x -> R . prod x r ) x | Branch ( vx , lx , tx , fx ) , Branch ( vy , ly , ty , fy ) -> begin match V . compare vx vy with | 0 -> begin match L . meet ~ tight : true lx ly with | Some ( l ) -> mk_branch vx l ( prod tx ty ) ( prod fx fy ) | None -> begin match L . compare lx ly with | 0 -> assert false | - 1 -> mk_branch vx lx ( prod tx ( restrict [ ( vx , lx ) ] y ) ) ( prod fx y ) | 1 -> mk_branch vy ly ( prod ( restrict [ ( vy , ly ) ] x ) ty ) ( prod x fy ) | _ -> assert false end end | - 1 -> mk_branch vx lx ( prod tx y ) ( prod fx y ) | 1 -> mk_branch vy ly ( prod x ty ) ( prod x fy ) | _ -> assert false end let rec sum x y = match x . d , y . d with | Leaf r , _ -> if R . ( compare r zero ) = 0 then y else map_r ( R . sum r ) y | _ , Leaf r -> if R . ( compare zero r ) = 0 then x else map_r ( fun x -> R . sum x r ) x | Branch ( vx , lx , tx , fx ) , Branch ( vy , ly , ty , fy ) -> begin match V . compare vx vy with | 0 -> begin match L . join ~ tight : true lx ly with | Some ( l ) -> mk_branch vx l ( sum tx ty ) ( sum fx fy ) | None -> begin match L . compare lx ly with | 0 -> assert false | - 1 -> mk_branch vx lx ( sum tx ( restrict [ ( vx , lx ) ] y ) ) ( sum fx y ) | 1 -> mk_branch vy ly ( sum ( restrict [ ( vy , ly ) ] x ) ty ) ( sum x fy ) | _ -> assert false end end | - 1 -> mk_branch vx lx ( sum tx y ) ( sum fx y ) | 1 -> mk_branch vy ly ( sum x ty ) ( sum x fy ) | _ -> assert false end module VH = Hashtbl . Make ( struct type t = v let equal ( v1 , l1 ) ( v2 , l2 ) = V . compare v1 v2 = 0 && L . compare l1 l2 = 0 let hash ( v , l ) = 191 * ( V . hash v ) + 877 * ( L . hash l ) end ) let rec support t = let s = VH . create 20 in iter ( fun _ -> ( ) ) ( fun v _ _ -> VH . replace s v ( ) ) t ; VH . fold ( fun v ( ) acc -> v :: acc ) s [ ] let to_dot t = let open Format in let buf = Buffer . create 200 in let fmt = formatter_of_buffer buf in let seen = Hashtbl . create ~ random : true 20 in let rank = VH . create 20 in pp_set_margin fmt ( 1 lsl 29 ) ; fprintf fmt " digraph tdk { @\ n " ; let rec loop t = if not ( Hashtbl . mem seen t . id ) then begin Hashtbl . add seen t . id ( ) ; match t . d with | Leaf r -> fprintf fmt " % d [ shape = box label " =\% s " ] ; \@\ n " t . id ( R . to_string r ) | Branch ( v , l , a , b ) -> begin try Hashtbl . add ( VH . find rank ( v , l ) ) t . id ( ) with Not_found -> let s = Hashtbl . create ~ random : true 10 in Hashtbl . add s t . id ( ) ; VH . add rank ( v , l ) s end ; fprintf fmt " % d [ label " =\% s = % s " ] ; \@\ n " t . id ( V . to_string v ) ( L . to_string l ) ; fprintf fmt " % d -> % d ; @\ n " t . id a . id ; fprintf fmt " % d -> % d [ style " =\ dashed " ] ; \@\ n " t . id b . id ; loop a ; loop b end in loop t ; VH . iter ( fun _ s -> fprintf fmt " { rank = same ; " ; Hashtbl . iter ( fun x ( ) -> fprintf fmt " % d " x ) s ; fprintf fmt " ; } @\ n " ) rank ; fprintf fmt " } . " ; @ Buffer . contents buf end
let network = [ " host " ]
let download_cache = Obuilder_spec . Cache . v " opam - archives " ~ target " :/ home / opam . / opam / download - cache "
let dune_cache = Obuilder_spec . Cache . v " opam - dune - cache " ~ target " :/ home / opam . / cache / dune "
let cache = [ download_cache ; dune_cache ]
type t0 = { voodoo_do : Git . Commit_id . t ; voodoo_prep : Git . Commit_id . t ; voodoo_gen : Git . Commit_id . t ; }
module Op = struct type voodoo = t0 type t = No_context let id = " voodoo - repository " let pp f _ = Fmt . pf f " voodoo - repository " let auto_cancel = false module Key = struct type t = Git . Commit . t let digest = Git . Commit . hash end module Value = struct type t = voodoo let to_yojson commit = let hash = Git . Commit_id . hash commit in let repo = Git . Commit_id . repo commit in let gref = Git . Commit_id . gref commit in ` Assoc [ ( " hash " , ` String hash ) ; ( " repo " , ` String repo ) ; ( " gref " , ` String gref ) ] let of_yojson_exn json = let open Yojson . Safe . Util in let hash = json |> member " hash " |> to_string in let gref = json |> member " gref " |> to_string in let repo = json |> member " repo " |> to_string in Git . Commit_id . v ~ repo ~ gref ~ hash let marshal { voodoo_do ; voodoo_prep ; voodoo_gen } = ` Assoc [ ( " do " , to_yojson voodoo_do ) ; ( " prep " , to_yojson voodoo_prep ) ; ( " gen " , to_yojson voodoo_gen ) ; ] |> Yojson . Safe . to_string let unmarshal t = let json = Yojson . Safe . from_string t in let open Yojson . Safe . Util in let voodoo_do = json |> member " do " |> of_yojson_exn in let voodoo_prep = json |> member " prep " |> of_yojson_exn in let voodoo_gen = json |> member " gen " |> of_yojson_exn in { voodoo_do ; voodoo_prep ; voodoo_gen } end let voodoo_prep_paths = Fpath . [ v " voodoo - prep . opam " ; v " src / voodoo - prep " / ] let voodoo_do_paths = Fpath . [ v " voodoo - do . opam " ; v " voodoo - lib . opam " ; v " src / voodoo - do " ; / v " src / voodoo " / ] let voodoo_gen_paths = Fpath . [ v " voodoo - gen . opam " ; v " src / voodoo - gen " ; / v " src / voodoo " ; / v " src / voodoo - web " / ] let get_oldest_commit_for ~ job ~ dir ~ from paths = let paths = List . map Fpath . to_string paths in let cmd = " git " :: " log " :: " - n " :: " 1 " :: " -- format = format :% H " :: from :: " " -- :: paths in let cmd = ( " " , Array . of_list cmd ) in Current . Process . check_output ~ cwd : dir ~ job ~ cancellable : false cmd |> Lwt_result . map String . trim let with_hash ~ id hash = Git . Commit_id . v ~ repo ( : Git . Commit_id . repo id ) ~ gref ( : Git . Commit_id . gref id ) ~ hash let build No_context job commit = let open Lwt . Syntax in let ( let ** ) = Lwt_result . bind in let * ( ) = Current . Job . start ~ level : Harmless job in Git . with_checkout ~ job commit @@ fun dir -> let id = Git . Commit . id commit in let from = Git . Commit_id . hash id in let ** voodoo_prep = get_oldest_commit_for ~ job ~ dir ~ from voodoo_prep_paths in let ** voodoo_do = get_oldest_commit_for ~ job ~ dir ~ from voodoo_do_paths in let ** voodoo_gen = get_oldest_commit_for ~ job ~ dir ~ from voodoo_gen_paths in Current . Job . log job " Prep commit : % s " voodoo_prep ; Current . Job . log job " Do commit : % s " voodoo_do ; Current . Job . log job " Gen commit : % s " voodoo_gen ; let voodoo_prep = with_hash ~ id voodoo_prep in let voodoo_do = with_hash ~ id voodoo_do in let voodoo_gen = with_hash ~ id voodoo_gen in Lwt . return_ok { voodoo_prep ; voodoo_do ; voodoo_gen } end
module VoodooCache = Current_cache . Make ( Op )
let v ( ) = let daily = Current_cache . Schedule . v ~ valid_for ( : Duration . of_day 1 ) ( ) in let git = Git . clone ~ schedule : daily ~ gref " : main " " https :// github . com / ocaml - doc / voodoo . git " in let open Current . Syntax in Current . component " voodoo " |> let > git = git in VoodooCache . get No_context git
type t = { voodoo_do : Git . Commit_id . t ; voodoo_prep : Git . Commit_id . t ; voodoo_gen : Git . Commit_id . t ; config : Config . t ; }
let v config = let open Current . Syntax in let + { voodoo_do ; voodoo_prep ; voodoo_gen } = v ( ) in { voodoo_do ; voodoo_prep ; voodoo_gen ; config }
let remote_uri commit = let repo = Git . Commit_id . repo commit in let commit = Git . Commit_id . hash commit in repo ^ " " # ^ commit
let digest t = let key = Fmt . str " % s \ n % s \ n % s \ n % s \ n " ( Git . Commit_id . hash t . voodoo_prep ) ( Git . Commit_id . hash t . voodoo_do ) ( Git . Commit_id . hash t . voodoo_gen ) ( Config . odoc t . config ) in Digest . ( string key |> to_hex )
module Prep = struct type voodoo = t type t = Git . Commit_id . t let v { voodoo_prep ; _ } = voodoo_prep let spec ~ base t = let open Obuilder_spec in base |> Spec . add [ run ~ network " sudo apt - get update && sudo apt - get install - yy m4 pkg - config " ; run ~ network ~ cache " opam pin - ny % s && opam depext - iy voodoo - prep " ( remote_uri t ) ; run " cp ( $ opam config var bin ) / voodoo - prep / home / opam " ; ] let digest = Git . Commit_id . hash let commit = Fun . id end
module Do = struct type voodoo = t type t = { commit : Git . Commit_id . t ; config : Config . t } let v { voodoo_do ; config ; _ } = { commit = voodoo_do ; config } let spec ~ base t = let open Obuilder_spec in base |> Spec . add [ run ~ network " sudo apt - get update && sudo apt - get install - yy m4 " ; run ~ network ~ cache " opam pin - ny odoc % s && opam depext - iy odoc && opam exec -- odoc -- version " ( Config . odoc t . config ) ; run ~ network ~ cache " opam pin - ny % s && opam depext - iy voodoo - do " ( remote_uri t . commit ) ; run " cp ( $ opam config var bin ) / odoc ( $ opam config var bin ) / voodoo - do / home / opam " ; ] let digest t = Git . Commit_id . hash t . commit ^ Config . odoc t . config let commit t = t . commit end
module Gen = struct type voodoo = t type t = { commit : Git . Commit_id . t ; config : Config . t } let v { voodoo_gen ; config ; _ } = { commit = voodoo_gen ; config } let spec ~ base t = let open Obuilder_spec in base |> Spec . add [ run ~ network " sudo apt - get update && sudo apt - get install - yy m4 && opam repo set - url default \ https :// github . com / ocaml / opam - repository . git # c1ab98721ec2efc4c65d0c5a65a85c826925db6c " ; run ~ network ~ cache " opam pin - ny odoc % s && opam depext - iy odoc && opam exec -- odoc -- version " ( Config . odoc t . config ) ; run ~ network ~ cache " opam pin - ny % s && opam depext - iy voodoo - gen " ( remote_uri t . commit ) ; run " cp ( $ opam config var bin ) / odoc ( $ opam config var bin ) / voodoo - gen / home / opam " ; ] let digest t = Git . Commit_id . hash t . commit ^ Config . odoc t . config let commit t = t . commit end
type ballot = Yay | Nay | Pass
let ballot_encoding = let of_int8 = function | 0 -> Yay | 1 -> Nay | 2 -> Pass | _ -> invalid_arg " ballot_of_int8 " in let to_int8 = function | Yay -> 0 | Nay -> 1 | Pass -> 2 in let open Data_encoding in splitted ~ binary : ( conv to_int8 of_int8 int8 ) ~ json : ( string_enum [ " yay " , Yay ; " nay " , Nay ; " pass " , Pass ; ] )
let recorded_proposal_count_for_delegate ctxt proposer = Storage . Vote . Proposals_count . find ctxt proposer >|=? Option . value ~ default : 0
let record_proposal ctxt proposal proposer = recorded_proposal_count_for_delegate ctxt proposer >>=? fun count -> Storage . Vote . Proposals_count . add ctxt proposer ( count + 1 ) >>= fun ctxt -> Storage . Vote . Proposals . add ctxt ( proposal , proposer ) >|= ok
let get_proposals ctxt = Storage . Vote . Proposals . fold ctxt ~ order ` : Sorted ~ init ( : ok Protocol_hash . Map . empty ) ~ f ( : fun ( proposal , delegate ) acc -> Storage . Vote . Listings . get ctxt delegate >>=? fun weight -> Lwt . return ( acc >|? fun acc -> let previous = match Protocol_hash . Map . find proposal acc with | None -> 0L | Some x -> x in Protocol_hash . Map . add proposal ( Int64 . add weight previous ) acc ) )
let clear_proposals ctxt = Storage . Vote . Proposals_count . clear ctxt >>= fun ctxt -> Storage . Vote . Proposals . clear ctxt
type ballots = { yay : int64 ; nay : int64 ; pass : int64 }
let ballots_encoding = let open Data_encoding in conv ( fun { yay ; nay ; pass } -> ( yay , nay , pass ) ) ( fun ( yay , nay , pass ) -> { yay ; nay ; pass } ) @@ obj3 ( req " yay " int64 ) ( req " nay " int64 ) ( req " pass " int64 )
let get_ballots ctxt = Storage . Vote . Ballots . fold ctxt ~ order ` : Sorted ~ f ( : fun delegate ballot ( ballots : ballots tzresult ) -> Storage . Vote . Listings . get ctxt delegate >>=? fun weight -> let count = Int64 . add weight in Lwt . return ( ballots >|? fun ballots -> match ballot with | Yay -> { ballots with yay = count ballots . yay } | Nay -> { ballots with nay = count ballots . nay } | Pass -> { ballots with pass = count ballots . pass } ) ) ~ init ( : ok { yay = 0L ; nay = 0L ; pass = 0L } )
let listings_encoding = Data_encoding . ( list ( obj2 ( req " pkh " Signature . Public_key_hash . encoding ) ( req " voting_power " int64 ) ) )
let update_listings ctxt = Storage . Vote . Listings . clear ctxt >>= fun ctxt -> Stake_storage . fold ctxt ( ctxt , 0L ) ~ order ` : Sorted ~ f ( : fun ( delegate , stake ) ( ctxt , total ) -> let weight = Tez_repr . to_mutez stake in Storage . Vote . Listings . init ctxt delegate weight >>=? fun ctxt -> return ( ctxt , Int64 . add total weight ) ) >>=? fun ( ctxt , total ) -> Storage . Vote . Voting_power_in_listings . add ctxt total >>= fun ctxt -> return ctxt
let get_voting_power_free ctxt owner = Storage . Vote . Listings . find ctxt owner >|=? Option . value ~ default : 0L
let get_voting_power ctxt owner = let open Raw_context in consume_gas ctxt ( Storage_costs . read_access ~ path_length : 4 ~ read_bytes : 8 ) >>?= fun ctxt -> Storage . Vote . Listings . find ctxt owner >|=? function | None -> ( ctxt , 0L ) | Some power -> ( ctxt , power )
let get_total_voting_power ctxt = let open Raw_context in consume_gas ctxt ( Storage_costs . read_access ~ path_length : 2 ~ read_bytes : 8 ) >>?= fun ctxt -> get_total_voting_power_free ctxt >|=? fun total_voting_power -> ( ctxt , total_voting_power )
let get_current_quorum ctxt = Storage . Vote . Participation_ema . get ctxt >|=? fun participation_ema -> let quorum_min = Constants_storage . quorum_min ctxt in let quorum_max = Constants_storage . quorum_max ctxt in let quorum_diff = Int32 . sub quorum_max quorum_min in Int32 . ( add quorum_min ( div ( mul participation_ema quorum_diff ) 100_00l ) )
let init ctxt ~ start_position = let participation_ema = Constants_storage . quorum_max ctxt in Storage . Vote . Participation_ema . init ctxt participation_ema >>=? fun ctxt -> Voting_period_storage . init_first_period ctxt ~ start_position
let test_proto_files = [ " main . ml " ; " main . mli " ]
let test_proto_TEZOS_PROTOCOL = { { | " modules " : [ " Main " ] , " expected_env_version " : 3 } } |
type period = { index : int ; kind : string ; start_position : int ; position : int ; remaining : int ; }
let period_type : period Check . typ = Check . convert ( fun { index ; kind ; start_position ; position ; remaining } -> ( index , kind , start_position , position , remaining ) ) Check . ( tuple5 int string int int int )
let decode_period json = let index = JSON . ( json |-> " voting_period " |-> " index " |> as_int ) in let kind = JSON . ( json |-> " voting_period " |-> " kind " |> as_string ) in let start_position = JSON . ( json |-> " voting_period " |-> " start_position " |> as_int ) in let position = JSON . ( json |-> " position " |> as_int ) in let remaining = JSON . ( json |-> " remaining " |> as_int ) in { index ; kind ; start_position ; position ; remaining }
let get_current_period client = let * json = RPC . Votes . get_current_period client in return ( decode_period json )
let get_successor_period client = let * json = RPC . Votes . get_successor_period client in return ( decode_period json )
let check_current_period client expected_period = let * period = get_current_period client in Check . ( ( period = expected_period ) period_type ) ~ error_msg " : expected current_period = % R , got % L " ; unit
let check_successor_period client expected_period = let * period = get_successor_period client in Check . ( ( period = expected_period ) period_type ) ~ error_msg " : expected successor_period = % R , got % L " ; unit
type level = { level : int ; level_position : int ; cycle : int ; cycle_position : int ; expected_commitment : bool ; }
let level_type : level Check . typ = Check . convert ( fun { level ; level_position ; cycle ; cycle_position ; expected_commitment } -> ( level , level_position , cycle , cycle_position , expected_commitment ) ) Check . ( tuple5 int int int int bool )
let decode_level json = let level = JSON . ( json |-> " level " |> as_int ) in let level_position = JSON . ( json |-> " level_position " |> as_int ) in let cycle = JSON . ( json |-> " cycle " |> as_int ) in let cycle_position = JSON . ( json |-> " cycle_position " |> as_int ) in let expected_commitment = JSON . ( json |-> " expected_commitment " |> as_bool ) in { level ; level_position ; cycle ; cycle_position ; expected_commitment }
let get_current_level client = let * json = RPC . get_current_level client in return ( decode_level json )