text
stringlengths
12
786k
let encoding = let open Data_encoding in def " tx_rollup_id " ~ title " : A tx rollup handle " ~ description : " A tx rollup notation as given to an RPC or inside scripts , is a base58 \ tx rollup hash " @@ splitted ~ binary : Hash . encoding ~ json : ( conv to_b58check ( fun s -> match of_b58check s with | Ok s -> s | Error _ -> Json . cannot_destruct " Invalid tx rollup notation . " ) string )
let originated_tx_rollup nonce = let data = Data_encoding . Binary . to_bytes_exn Origination_nonce . encoding nonce in Hash . hash_bytes [ data ]
let rpc_arg = let construct = to_b58check in let destruct hash = Result . map_error ( fun _ -> " Cannot parse tx rollup id " ) ( of_b58check hash ) in RPC_arg . make ~ descr " : A tx rollup identifier encoded in b58check . " ~ name " : tx_rollup_id " ~ construct ~ destruct ( )
module Index = struct type t = tx_rollup let path_length = 1 let to_path c l = let raw_key = Data_encoding . Binary . to_bytes_exn encoding c in let ( ` Hex key ) = Hex . of_bytes raw_key in key :: l let of_path = function | [ key ] -> Option . bind ( Hex . to_bytes ( ` Hex key ) ) ( Data_encoding . Binary . of_bytes_opt encoding ) | _ -> None let rpc_arg = rpc_arg let encoding = encoding let compare = compare end
let custom_root = ( RPC_path . ( open_root / " context " / " tx_rollup " ) : RPC_context . t RPC_path . context )
module S = struct let state = RPC_service . get_service ~ description " : Access the state of a rollup . " ~ query : RPC_query . empty ~ output : Tx_rollup_state . encoding RPC_path . ( custom_root /: Tx_rollup . rpc_arg / " state " ) let inbox = RPC_service . get_service ~ description " : Get the inbox of a transaction rollup " ~ query : RPC_query . empty ~ output : Tx_rollup_inbox . encoding RPC_path . ( custom_root /: Tx_rollup . rpc_arg / " inbox " ) end
let register ( ) = let open Services_registration in opt_register1 ~ chunked : false S . state ( fun ctxt tx_rollup ( ) ( ) -> Tx_rollup_state . find ctxt tx_rollup >|=? snd ) ; opt_register1 ~ chunked : false S . inbox ( fun ctxt tx_rollup ( ) ( ) -> Tx_rollup_inbox . find ctxt tx_rollup ~ level ` : Current >|=? snd )
let state ctxt block tx_rollup = RPC_context . make_call1 S . state ctxt block tx_rollup ( ) ( )
let inbox ctxt block tx_rollup = RPC_context . make_call1 S . inbox ctxt block tx_rollup ( ) ( )
type t = { fees_per_byte : Tez_repr . t ; inbox_ema : int ; last_inbox_level : Raw_level_repr . t option ; }
let initial_state = { fees_per_byte = Tez_repr . zero ; inbox_ema = 0 ; last_inbox_level = None }
let encoding : t Data_encoding . t = let open Data_encoding in conv ( fun { last_inbox_level ; fees_per_byte ; inbox_ema } -> ( last_inbox_level , fees_per_byte , inbox_ema ) ) ( fun ( last_inbox_level , fees_per_byte , inbox_ema ) -> { last_inbox_level ; fees_per_byte ; inbox_ema } ) ( obj3 ( req " last_inbox_level " ( option Raw_level_repr . encoding ) ) ( req " fees_per_byte " Tez_repr . encoding ) ( req " inbox_ema " int31 ) )
let pp fmt { fees_per_byte ; last_inbox_level ; inbox_ema } = Format . fprintf fmt " Tx_rollup : fees_per_byte = % a ; inbox_ema % d ; last_inbox_level = % a " Tez_repr . pp fees_per_byte inbox_ema ( Format . pp_print_option Raw_level_repr . pp ) last_inbox_level
let update_fees_per_byte : t -> final_size : int -> hard_limit : int -> t = fun ( { fees_per_byte ; inbox_ema ; _ } as state ) ~ final_size ~ hard_limit -> let threshold_increase = 90 in let threshold_decrease = 80 in let variation_factor = 5L in let inbox_ema_multiplier = 165 in let inbox_ema = ( ( final_size * inbox_ema_multiplier ) + ( inbox_ema * ( 10000 - inbox_ema_multiplier ) ) ) / 10000 in let percentage = inbox_ema * 100 / hard_limit in let computation = let open Compare . Int in if threshold_decrease < percentage && percentage <= threshold_increase then ok fees_per_byte else Tez_repr . ( fees_per_byte *? variation_factor >>? fun x -> x /? 100L ) >>? fun variation -> let variation = if Tez_repr . ( variation = zero ) then Tez_repr . one_mutez else variation in if threshold_increase < percentage then Tez_repr . ( fees_per_byte +? variation ) else if percentage < threshold_decrease && Tez_repr . ( zero < fees_per_byte ) then Tez_repr . ( fees_per_byte -? variation ) else ok fees_per_byte in match computation with | Ok fees_per_byte -> { state with fees_per_byte ; inbox_ema } | Error _ -> { state with fees_per_byte = Tez_repr . max_mutez ; inbox_ema }
let fees { fees_per_byte ; _ } size = Tez_repr . ( fees_per_byte *? Int64 . of_int size )
let last_inbox_level { last_inbox_level ; _ } = last_inbox_level
let append_inbox t level = { t with last_inbox_level = Some level }
module Internal_for_tests = struct let make : fees_per_byte : Tez_repr . t -> inbox_ema : int -> last_inbox_level : Raw_level_repr . t option -> t = fun ~ fees_per_byte ~ inbox_ema ~ last_inbox_level -> { fees_per_byte ; inbox_ema ; last_inbox_level } let get_inbox_ema : t -> int = fun { inbox_ema ; _ } -> inbox_ema end
type error += | Tx_rollup_already_exists of Tx_rollup_repr . t | Tx_rollup_does_not_exist of Tx_rollup_repr . t
let init : Raw_context . t -> Tx_rollup_repr . t -> Raw_context . t tzresult Lwt . t = fun ctxt tx_rollup -> Storage . Tx_rollup . State . mem ctxt tx_rollup >>=? fun ( ctxt , already_exists ) -> fail_when already_exists ( Tx_rollup_already_exists tx_rollup ) >>=? fun ( ) -> Storage . Tx_rollup . State . init ctxt tx_rollup Tx_rollup_state_repr . initial_state >|=? fst
let find : Raw_context . t -> Tx_rollup_repr . t -> ( Raw_context . t * Tx_rollup_state_repr . t option ) tzresult Lwt . t = Storage . Tx_rollup . State . find
let get : Raw_context . t -> Tx_rollup_repr . t -> ( Raw_context . t * Tx_rollup_state_repr . t ) tzresult Lwt . t = fun ctxt tx_rollup -> find ctxt tx_rollup >>=? fun ( ctxt , state ) -> match state with | Some state -> return ( ctxt , state ) | None -> fail ( Tx_rollup_does_not_exist tx_rollup )
let assert_exist : Raw_context . t -> Tx_rollup_repr . t -> Raw_context . t tzresult Lwt . t = fun ctxt tx_rollup -> Storage . Tx_rollup . State . mem ctxt tx_rollup >>=? fun ( ctxt , tx_rollup_exists ) -> fail_unless tx_rollup_exists ( Tx_rollup_does_not_exist tx_rollup ) >>=? fun ( ) -> return ctxt
let update : Raw_context . t -> Tx_rollup_repr . t -> Tx_rollup_state_repr . t -> Raw_context . t tzresult Lwt . t = fun ctxt tx_rollup t -> Storage . Tx_rollup . State . update ctxt tx_rollup t >>=? fun ( ctxt , _ ) -> return ctxt
let ( ) = let open Data_encoding in register_error_kind ` Permanent ~ id " : tx_rollup_already_exists " ~ title " : Transaction rollup was already created " ~ description : " The protocol tried to originate the same transaction rollup twice " ~ pp ( : fun ppf addr -> Format . fprintf ppf " Transaction rollup % a is already used for an existing transaction \ rollup . This should not happen , and indicates there is a bug in the \ protocol . If you can , please report this bug \ ( https :// gitlab . com / tezos / tezos /-/ issues . ) " Tx_rollup_repr . pp addr ) ( obj1 ( req " rollup_address " Tx_rollup_repr . encoding ) ) ( function Tx_rollup_already_exists rollup -> Some rollup | _ -> None ) ( fun rollup -> Tx_rollup_already_exists rollup ) ; register_error_kind ` Temporary ~ id " : tx_rollup_does_not_exist " ~ title " : Transaction rollup does not exist " ~ description " : An invalid transaction rollup address was submitted " ~ pp ( : fun ppf addr -> Format . fprintf ppf " Invalid transaction rollup address % a " Tx_rollup_repr . pp addr ) ( obj1 ( req " rollup_address " Tx_rollup_repr . encoding ) ) ( function Tx_rollup_does_not_exist rollup -> Some rollup | _ -> None ) ( fun rollup -> Tx_rollup_does_not_exist rollup )
let fresh_tx_rollup_from_current_nonce ctxt = Raw_context . increment_origination_nonce ctxt >|? fun ( ctxt , nonce ) -> ( ctxt , Tx_rollup_repr . originated_tx_rollup nonce )
let originate ctxt = fresh_tx_rollup_from_current_nonce ctxt >>?= fun ( ctxt , tx_rollup ) -> Tx_rollup_state_storage . init ctxt tx_rollup >|=? fun ctxt -> ( ctxt , tx_rollup )
let update_tx_rollups_at_block_finalization : Raw_context . t -> Raw_context . t tzresult Lwt . t = fun ctxt -> let level = ( Raw_context . current_level ctxt ) . level in Storage . Tx_rollup . fold ctxt level ~ init ( : ok ctxt ) ~ f ( : fun tx_rollup ctxt -> ctxt >>?= fun ctxt -> Tx_rollup_state_storage . get ctxt tx_rollup >>=? fun ( ctxt , state ) -> Tx_rollup_inbox_storage . get ~ level ( ` : Level level ) ctxt tx_rollup >>=? fun ( ctxt , inbox ) -> let hard_limit = Constants_storage . tx_rollup_hard_size_limit_per_inbox ctxt in let state = Tx_rollup_state_repr . update_fees_per_byte state ~ final_size : inbox . cumulated_size ~ hard_limit in Storage . Tx_rollup . State . add ctxt tx_rollup state >|=? fun ( ctxt , _ , _ ) -> ctxt )
let script_parse_test sc scdec octx = ; ;
let script_serialize_test sc scdec octx = ; ;
let script_verify_is_spendable_test sc octx = ; ;
let script_verify_spendable_by_test prefix sc adr octx = ; ;
let tx_parse_test raw octx = ; ;
let tx_parse_and_check_hash raw hash octx = ; ;
let tx_parse_segwit_test raw dest sizes vsize hashes ? prefix ( : prefix ( = Params . of_network XTN ) . prefixes ) octx = ; ;
let tlist = [ ( [ Script . OP_DUP ; Script . OP_HASH160 ; Script . OP_DATA ( 20 , Hex . to_string ( ` Hex " 89ABCDEFABBAABBAABBAABBAABBAABBAABBAABBA " ) ) ; Script . OP_EQUALVERIFY ; Script . OP_CHECKSIG ] , 25 ) ; ( [ Script . OP_DUP ; Script . OP_HASH160 ; Script . OP_DATA ( 20 , Hex . to_string ( ` Hex " 89ABCDEFABBAABBAABBAABBAABBAABBAABBAABBA " ) ) ; Script . OP_EQUALVERIFY ; Script . OP_CHECKSIG ] , 25 ) ; ( [ Script . OP_DUP ; Script . OP_HASH160 ; Script . OP_DATA ( 20 , Hex . to_string ( ` Hex " 89ABCDEFABBAABBAABBAABBAABBAABBAABBAABBA " ) ) ; Script . OP_EQUALVERIFY ; Script . OP_CHECKSIG ] , 25 ) ; { pubkeyhash = 0x00 ; scripthash = 0x05 ; hrp = " bc " } ( [ Script . OP_DUP ; Script . OP_HASH160 ; Script . OP_DATA ( 20 , Hex . to_string ( ` Hex " 89ABCDEFABBAABBAABBAABBAABBAABBAABBAABBA " ) ) ; Script . OP_EQUALVERIFY ; Script . OP_CHECKSIG ] , 25 ) " 1DYwPTpZuLjY2qApmJdHaSAuWRvEF5skCN " ; ( ` Hex " 0100000000010115e180dc28a2327e687facc33f10f2a20da717e5548406f7ae8b4c811072f8560100000000ffffffff0100b4f505000000001976a9141d7cd6c75c2e86f4cbf98eaed221b30bd9a0b92888ac02483045022100df7b7e5cda14ddf91290e02ea10786e03eb11ee36ec02dd862fe9a326bbcb7fd02203f5b4496b667e6e281cc654a2da9e4f08660c620a1051337fa8965f727eb19190121038262a6c6cec93c2d3ecd6c6072efea86d02ff8e3328bbd0242b20af3425990ac00000000 " ) " miCsSagJ7RQDCMcBUaKFKEryaLnxbhGAPt " ( 85 , 110 ) 113 ( " d869f854e1f8788bcff294cc83b280942a8c728de71eb709a2c29d10bfe21b7c " , " 976015741ba2fc60804dd63167326b1a1f7e94af2b66f4a0fd95b38c18ee729b " ) ; ( ` Hex " 0100000000010115e180dc28a2327e687facc33f10f2a20da717e5548406f7ae8b4c811072f8560200000000ffffffff0188b3f505000000001976a9141d7cd6c75c2e86f4cbf98eaed221b30bd9a0b92888ac02483045022100f9d3fe35f5ec8ceb07d3db95adcedac446f3b19a8f3174e7e8f904b1594d5b43022074d995d89a278bd874d45d0aea835d3936140397392698b7b5bbcdef8d08f2fd012321038262a6c6cec93c2d3ecd6c6072efea86d02ff8e3328bbd0242b20af3425990acac00000000 " ) " miCsSagJ7RQDCMcBUaKFKEryaLnxbhGAPt " ( 85 , 112 ) 113 ( " 78457666f82c28aa37b74b506745a7c7684dc7842a52a457b09f09446721e11c " , " 12b1b3fe5e29f136bed2996d39e935442b619feb0a12d30ca832d29246689aa9 " ) ; ( ` Hex " 0100000000010115e180dc28a2327e687facc33f10f2a20da717e5548406f7ae8b4c811072f85603000000171600141d7cd6c75c2e86f4cbf98eaed221b30bd9a0b928ffffffff019caef505000000001976a9141d7cd6c75c2e86f4cbf98eaed221b30bd9a0b92888ac02483045022100f764287d3e99b1474da9bec7f7ed236d6c81e793b20c4b5aa1f3051b9a7daa63022016a198031d5554dbb855bdbe8534776a4be6958bd8d530dc001c32b828f6f0ab0121038262a6c6cec93c2d3ecd6c6072efea86d02ff8e3328bbd0242b20af3425990ac00000000 " ) " miCsSagJ7RQDCMcBUaKFKEryaLnxbhGAPt " ( 108 , 110 ) 136 ( " 8139979112e894a14f8370438a471d23984061ff83a9eba0bc7a34433327ec21 " , " 6bf4e4dfb860cf0906f49c836700b130ac78cc391c72a0911c94cdec4dcb10ec " ) ; ( ` Hex " 0100000000010115e180dc28a2327e687facc33f10f2a20da717e5548406f7ae8b4c811072f856040000002322002001d5d92effa6ffba3efa379f9830d0f75618b13393827152d26e4309000e88b1ffffffff0188b3f505000000001976a9141d7cd6c75c2e86f4cbf98eaed221b30bd9a0b92888ac02473044022038421164c6468c63dc7bf724aa9d48d8e5abe3935564d38182addf733ad4cd81022076362326b22dd7bfaf211d5b17220723659e4fe3359740ced5762d0e497b7dcc012321038262a6c6cec93c2d3ecd6c6072efea86d02ff8e3328bbd0242b20af3425990acac00000000 " ) " miCsSagJ7RQDCMcBUaKFKEryaLnxbhGAPt " ( 120 , 111 ) 148 ( " 954f43dbb30ad8024981c07d1f5eb6c9fd461e2cf1760dd1283f052af746fc88 " , " a5947589e2762107ff650958ba0e3a3cf341f53281d15593530bf9762c4edab1 " ) ; ( ` Hex " 0200000000010140d43a99926d43eb0e619bf0b3d83b4a31f60c176beecfb9d35bf45e54d0f7420100000017160014a4b4ca48de0b3fffc15404a1acdc8dbaae226955ffffffff0100e1f5050000000017a9144a1154d50b03292b3024370901711946cb7cccc387024830450221008604ef8f6d8afa892dee0f31259b6ce02dd70c545cfcfed8148179971876c54a022076d771d6e91bed212783c9b06e0de600fab2d518fad6f15a2b191d7fbd262a3e0121039d25ab79f41f75ceaf882411fd41fa670a4c672c23ffaf0e361a969cde0692e800000000 " ) " 38Segwituno6sUoEkh57ycM6K7ej5gvJhM " ( 106 , 110 ) 134 ( " c586389e5e4b3acb9d6c8be1c19ae8ab2795397633176f5a6442a261bbdefc3a " , " b759d39a8596b70b3a46700b83e1edb247e17ba58df305421864fe7a9ac142ea " ) ~ prefix ( : Params . of_network BTC ) . prefixes ; ] ; ;
type ' a ty = Ty of repr
let obj ( Ty ty ) = ty
let repr ty = Ty ( ty )
let print ( Ty ty ) = Format . asprintf " % a " %! Pprintast . core_type ty
let domains = function | Ty { Parsetree . ptyp_desc = Parsetree . Ptyp_arrow ( _ , arg , ret ) ; _ } -> ( Ty arg , Ty ret ) | _ -> invalid_arg " Ty . domains "
let curry ( Ty arg ) ( Ty ret ) = Ty { Parsetree . ptyp_desc = Parsetree . Ptyp_arrow ( Asttypes . Nolabel , arg , ret ) ; ptyp_loc = Location . none ; ptyp_loc_stack = [ ] ; ptyp_attributes = [ ] }
let pair2 ( Ty t1 ) ( Ty t2 ) = Ty { Parsetree . ptyp_desc = Parsetree . Ptyp_tuple [ t1 ; t2 ] ; ptyp_loc = Location . none ; ptyp_loc_stack = [ ] ; ptyp_attributes = [ ] }
let pair3 ( Ty t1 ) ( Ty t2 ) ( Ty t3 ) = Ty { Parsetree . ptyp_desc = Parsetree . Ptyp_tuple [ t1 ; t2 ; t3 ] ; ptyp_loc = Location . none ; ptyp_loc_stack = [ ] ; ptyp_attributes = [ ] }
let pair4 ( Ty t1 ) ( Ty t2 ) ( Ty t3 ) ( Ty t4 ) = Ty { Parsetree . ptyp_desc = Parsetree . Ptyp_tuple [ t1 ; t2 ; t3 ; t4 ] ; ptyp_loc = Location . none ; ptyp_loc_stack = [ ] ; ptyp_attributes = [ ] }
let lst ( Ty ty ) = Ty { Parsetree . ptyp_desc = Parsetree . Ptyp_constr ( { Asttypes . txt = Longident . Lident " list " ; loc = Location . none } , [ ty ] ) ; ptyp_loc = Location . none ; ptyp_loc_stack = [ ] ; ptyp_attributes = [ ] }
module Impl = Snarky . Snark . Make ( Snarky . Backends . Mnt4 . Default )
type nonrec t = { a : bool ; b : field_var }
type nonrec ' a u = { a1 : ' a ; b1 : bool } type nonrec ( ' a , ' b ) v_var = { a2 : ' a ; b2 : ' b } and ( ' a , ' b ) v = { a2 : ' a ; b2 : ' b } let v_typ x___3 x___2 = { Snarky . Types . Typ . store = ( fun { a2 ; b2 } -> Snarky . Typ_monads . Store . bind ( x___3 . Snarky . Types . Typ . store a2 ) ~ f ( : fun a2 -> Snarky . Typ_monads . Store . bind ( x___2 . Snarky . Types . Typ . store b2 ) ~ f ( : fun b2 -> Snarky . Typ_monads . Store . return { a2 ; b2 } ) ) ) ; Snarky . Types . Typ . read = ( fun { a2 ; b2 } -> Snarky . Typ_monads . Read . bind ( x___3 . Snarky . Types . Typ . read a2 ) ~ f ( : fun a2 -> Snarky . Typ_monads . Read . bind ( x___2 . Snarky . Types . Typ . read b2 ) ~ f ( : fun b2 -> Snarky . Typ_monads . Read . return { a2 ; b2 } ) ) ) ; Snarky . Types . Typ . alloc = Snarky . Typ_monads . Alloc . bind x___3 . Snarky . Types . Typ . alloc ~ f ( : fun a2 -> Snarky . Typ_monads . Alloc . bind x___2 . Snarky . Types . Typ . alloc ~ f ( : fun b2 -> Snarky . Typ_monads . Alloc . return { a2 ; b2 } ) ) ; Snarky . Types . Typ . check = ( fun { a2 ; b2 } -> Snarky . Checked . bind ( x___3 . Snarky . Types . Typ . check a2 ) ~ f ( : fun ( ) -> Snarky . Checked . bind ( x___2 . Snarky . Types . Typ . check b2 ) ~ f ( : fun ( ) -> Snarky . Checked . return ( ) ) ) ) } end
let x __implicit3__ __implicit1__ x = let typ x___6 = v_typ x___6 x___6 in Snarky . exists ( typ __implicit3__ ) ~ compute : ( let open As_prover in fun ( ) -> { a2 = ( let typ x___4 = x___4 in As_prover . read ( typ __implicit1__ ) x ) ; b2 = ( let typ x___5 = x___5 in As_prover . read ( typ __implicit1__ ) x ) } )
let y ( ) = x Typ . boolean { Snarky . Types . Typ . store = ( fun x -> Snarky . Typ_monads . Store . return x ) ; Snarky . Types . Typ . read = ( fun x -> Snarky . Typ_monads . Read . return x ) ; Snarky . Types . Typ . alloc = ( let open Snarky . Typ_monads . Alloc in map alloc ~ f ( : fun _ -> failwith " cannot allocate this type . " ) ) ; Snarky . Types . Typ . check = ( fun _ -> Snarky . Checked . return ( ) ) } true
type id = string list [ @@ deriving show , eq , ord ]
type vtype = | TUnbound of string * int option * Loc . t option | TId of id * Loc . t option | TComposed of id * t list * Loc . t option | TArrow of t * t * Loc . t option | TLink of t | TExpAlt of t list | TInt of int * Loc . t option
let rec unlink t = match t with | { contents = TLink e } -> unlink e | { contents = TComposed ( id , elems , _ ) } -> { contents = TComposed ( id , List . map unlink elems , None ) } | { contents = TArrow ( t1 , t2 , _ ) } -> { contents = TArrow ( unlink t1 , unlink t2 , None ) } | { contents = TExpAlt elems } -> { contents = TExpAlt ( List . map unlink elems ) } | { contents = TId ( id , _ ) } -> { contents = TId ( id , None ) } | { contents = TInt ( n , _ ) } -> { contents = TInt ( n , None ) } | { contents = TUnbound ( name , level , _ ) } -> { contents = TUnbound ( name , level , None ) }
let compare a b = compare ( unlink a ) ( unlink b )
let gensym_counter = ref 0
let gensym : unit -> string = fun ( ) -> let n = ! gensym_counter in let ( ) = incr gensym_counter in " ' " ^ string_of_int n
let current_level_val = ref 1
let current_level ( ) = ! current_level_val
let rec makeArrowType ( last : t ) ( types : t list ) : t = match types with | [ ] -> last | h :: t -> ref ( TArrow ( h , makeArrowType last t , None ) )
let rec stripArrow ( typ : t ) : t list * t = match typ with | { contents = TArrow ( t1 , t2 , _ ) } -> let args , last = stripArrow t2 in t1 :: args , last | _ -> [ ] , typ
let newvar : unit -> t = fun ( ) -> ref ( TUnbound ( gensym ( ) , Some ( current_level ( ) ) , None ) )
let base ( t : t ) : id = match ! t with | TId ( id , _ ) -> id | TComposed ( id , _ , _ ) -> id | _ -> failwith " Typ . base : this type does not have a base type "
let newinst ( t : t ) : t = let rec copy table t = try List . find ( fun ( key , _ ) -> key == t ) table |> snd , table with | Not_found -> match ! t with | TUnbound ( s , level , loc ) -> let o = ref ( TUnbound ( s , level , loc ) ) in o , ( t , o ) :: table | TId ( id , loc ) -> let o = ref ( TId ( id , loc ) ) in o , ( t , o ) :: table | TInt ( n , loc ) -> let o = ref ( TInt ( n , loc ) ) in o , ( t , o ) :: table | TComposed ( id , elems , loc ) -> let elems ' , table ' = copyList table elems in let o = ref ( TComposed ( id , elems ' , loc ) ) in o , ( t , o ) :: table ' | TArrow ( t1 , t2 , loc ) -> let t1 ' , table ' = copy table t1 in let t2 ' , table ' = copy table ' t2 in let o = ref ( TArrow ( t1 ' , t2 ' , loc ) ) in o , ( t , o ) :: table ' | TLink link -> let link ' , table ' = copy table link in let o = ref ( TLink link ' ) in o , ( t , o ) :: table ' | TExpAlt elems -> let elems ' , table ' = copyList table elems in let o = ref ( TExpAlt elems ' ) in o , ( t , o ) :: table ' and copyList table l = let l ' , table ' = List . fold_left ( fun ( ol , table ) t -> let o , table ' = copy table t in o :: ol , table ' ) ( [ ] , table ) l in List . rev l ' , table ' in copy [ ] t |> fst
let rec fixType table t = try List . find ( fun key -> equal key t ) table , table with | _ -> match t with | { contents = TUnbound ( _ , _ , _ ) } -> t , t :: table | { contents = TComposed ( id , elems , loc ) } -> let elems ' , table ' = fixTypeList table elems in ref ( TComposed ( id , elems ' , loc ) ) , table ' | { contents = TArrow ( t1 , t2 , loc ) } -> let t1 ' , table ' = fixType table t1 in let t2 ' , table ' = fixType table ' t2 in ref ( TArrow ( t1 ' , t2 ' , loc ) ) , table ' | { contents = TLink link } -> let link ' , table ' = fixType table link in ref ( TLink link ' ) , table ' | { contents = TExpAlt elems } -> let elems ' , table ' = fixTypeList table elems in ref ( TExpAlt elems ' ) , table ' | _ -> t , table let tl ' , table ' = List . fold_left ( fun ( ol , table ) t -> let o , table ' = fixType table t in o :: ol , table ' ) ( [ ] , table ) tl in List . rev tl ' , table '
let fixOptType table ot = match ot with | None -> None , table | Some t -> let t ' , table ' = fixType table t in Some t ' , table '
let rec isUnbound ( t : t ) : bool = match t with | { contents = TUnbound ( _ , _ , _ ) } -> true | { contents = TId ( _ , _ ) } -> false | { contents = TInt ( _ , _ ) } -> false | { contents = TComposed ( _ , elems , _ ) } -> List . exists isUnbound elems | { contents = TArrow ( t1 , t2 , _ ) } -> isUnbound t1 || isUnbound t2 | { contents = TLink t } -> isUnbound t | { contents = TExpAlt _ } -> true
let rec location ( t : t ) : Loc . t = match t with | { contents = TUnbound ( _ , _ , Some loc ) } -> loc | { contents = TId ( _ , Some loc ) } -> loc | { contents = TComposed ( _ , elems , Some loc ) } -> List . fold_left ( fun s a -> Loc . merge s ( location a ) ) loc elems | { contents = TArrow ( t1 , t2 , Some loc ) } -> loc |> Loc . merge ( location t1 ) |> Loc . merge ( location t2 ) | { contents = TLink t } -> location t | { contents = TExpAlt elems } -> List . fold_left ( fun s a -> Loc . merge s ( location a ) ) Loc . default elems | { contents = TInt ( _ , Some loc ) } -> loc | { contents = TUnbound ( _ , _ , None ) } { | contents = TId ( _ , None ) } { | contents = TComposed ( _ , _ , None ) } { | contents = TArrow ( _ , _ , None ) } { | contents = TInt ( _ , None ) } -> Loc . default
let getLevel = function | None -> current_level ( ) | Some n -> n
let pickLoc ( loc1 : Loc . t option ) ( loc2 : Loc . t option ) : Loc . t option = match loc1 , loc2 with | None , _ -> loc2 | _ , None -> loc1 | Some l1 , _ when l1 = Loc . default -> loc2 | _ , Some l2 when l2 = Loc . default -> loc1 | _ -> loc1
let rec unify ( t1 : t ) ( t2 : t ) : bool = if t1 == t2 then true else match t1 , t2 with | { contents = TInt ( n1 , _ ) } , { contents = TInt ( n2 , _ ) } when n1 = n2 -> true | { contents = TUnbound ( n1 , level1 , loc1 ) } , { contents = TUnbound ( _ , level2 , loc2 ) } -> let loc = pickLoc loc1 loc2 in let level = min ( getLevel level1 ) ( getLevel level2 ) in let n = if n1 = " " then gensym ( ) else n1 in let t = TUnbound ( n , Some level , loc ) in t1 := t ; t2 := TLink t1 ; true | { contents = TLink tlink } , t | t , { contents = TLink tlink } -> unify t tlink | ( { contents = TUnbound _ } as tu ) , t | t , ( { contents = TUnbound _ } as tu ) -> tu := TLink t ; true | { contents = TComposed ( n1 , elems1 , _ ) } , { contents = TComposed ( n2 , elems2 , _ ) } when n1 = n2 && List . length elems1 = List . length elems2 -> List . for_all2 unify elems1 elems2 | { contents = TArrow ( a1 , a2 , _ ) } , { contents = TArrow ( b1 , b2 , _ ) } -> unify a1 b1 && unify a2 b2 | { contents = TId ( id1 , _ ) } , { contents = TId ( id2 , _ ) } when id1 = id2 -> true | { contents = TExpAlt _ as tp1 } , { contents = TExpAlt _ as tp2 } when equal_vtype tp1 tp2 -> t2 := TLink t1 ; true | ( { contents = TExpAlt alt } as tu ) , t | t , ( { contents = TExpAlt alt } as tu ) -> let rec loop alt = match alt with | [ ] -> false | first_alt :: alt_rest -> if unify t first_alt then ( tu := TLink first_alt ; true ) else loop alt_rest in loop alt | { contents = tp1 } , { contents = tp2 } when equal_vtype tp1 tp2 -> true | _ -> false
let rec join ( sep : string ) ( id : string list ) : string = match id with | [ ] -> " " | [ name ] -> name | h :: t -> h ^ sep ^ join sep t
let rec getTupleName ( typ : t ) : string = match ! typ with | TId ( id , _ ) -> join " _ " id | TComposed ( id , elems , _ ) -> ( join " _ " id :: " _ " :: List . map getTupleName elems ) @ [ " _ " ] |> join " _ " | TLink e -> getTupleName e | TArrow ( e1 , e2 , _ ) -> getTupleName e1 ^ " __ " ^ getTupleName e2 | _ -> failwith " There should be no other types here "
let getTupleName ( typ : t ) : string = " _ " ^ getTupleName typ
let arrayTypeAndSize typ = match ! typ with | TComposed ( [ " array " ] , [ t ; { contents = TInt ( n , _ ) } ] , _ ) -> t , n | _ -> failwith " arraySize : invalid input "
let getSubTypes ( typ : t ) : t list = match ( ! unlink typ ) with | TComposed ( _ , elems , _ ) -> elems | _ -> [ ]
let isArray ( typ : t ) : bool = match ( ! unlink typ ) with | TComposed ( [ " array " ] , _ , _ ) -> true | _ -> false
let isTuple typ = match ! typ with | TComposed ( [ " tuple " ] , _ , _ ) -> true | _ -> false
let isSimpleType ( typ : t ) : bool = match ! typ with | TId ( [ " real " ] , _ ) -> true | TId ( [ " fix16 " ] , _ ) -> true | TId ( [ " int " ] , _ ) -> true | TId ( [ " bool " ] , _ ) -> true | TId ( [ " unit " ] , _ ) -> true | TId ( [ " void " ] , _ ) -> true | TId ( [ " string " ] , _ ) -> true | TId ( [ " abstract " ] , _ ) -> true | _ -> false
let isRealType ( typ : t ) : bool = match ! typ with | TId ( [ " real " ] , _ ) -> true | TId ( [ " fix16 " ] , _ ) -> true | _ -> false
let isSimpleOpType ( typ : t option ) : bool = match typ with | Some t -> isSimpleType t | _ -> true
let isTupleOpType ( typ : t option ) : bool = match typ with | Some t -> isTuple t | _ -> true
let first ( t : t list ) : t = match t with | h :: _ -> h | _ -> failwith " Typ . first : invalid type "
let makeListOpt t = match t with | Some t -> Some [ t ] | None -> None
module Const = struct let ( |-> ) a b = ref ( TArrow ( a , b , None ) ) let empty = ref ( TId ( [ " " ] , None ) ) let type_type = ref ( TId ( [ " type " ] , None ) ) let unit_type = ref ( TId ( [ " unit " ] , None ) ) let bool_type = ref ( TId ( [ " bool " ] , None ) ) let int_type = ref ( TId ( [ " int " ] , None ) ) let real_type = ref ( TId ( [ " real " ] , None ) ) let string_type = ref ( TId ( [ " string " ] , None ) ) let fix16_type = ref ( TId ( [ " fix16 " ] , None ) ) let num_type ( ) = ref ( TExpAlt [ real_type ; int_type ; fix16_type ] ) let freal_type ( ) = ref ( TExpAlt [ real_type ; fix16_type ] ) let freal_freal ( ) = let t = freal_type ( ) in t |-> t let freal_freal_freal ( ) = let t = freal_type ( ) in t |-> ( t |-> t ) let real_real ( ) = real_type |-> real_type let real_real_real ( ) = real_type |-> ( real_type |-> real_type ) let a_a ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in a |-> a let a_a_a ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in a |-> ( a |-> a ) let num_num ( ) = let num = num_type ( ) in num |-> num let num_num_num ( ) = let num = num_type ( ) in num |-> ( num |-> num ) let num_num_bool ( ) = let num = num_type ( ) in num |-> ( num |-> bool_type ) let a_a_bool ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in a |-> ( a |-> bool_type ) let int_int_int ( ) = int_type |-> ( int_type |-> int_type ) let num_num_num_num ( ) = let num = num_type ( ) in num |-> ( num |-> ( num |-> num ) ) let a_a_a_a ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in a |-> ( a |-> ( a |-> a ) ) let bool_bool ( ) = bool_type |-> bool_type let bool_bool_bool ( ) = bool_type |-> ( bool_type |-> bool_type ) let num_int ( ) = num_type ( ) |-> int_type let num_real ( ) = num_type ( ) |-> real_type let num_fix16 ( ) = num_type ( ) |-> fix16_type let num_string ( ) = num_type ( ) |-> string_type let array_size ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in let array_type = ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) in array_type |-> int_type let array_get ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in let array_type = ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) in array_type |-> ( int_type |-> a ) let array_set ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in let array_type = ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) in array_type |-> ( int_type |-> ( a |-> unit_type ) ) let array_make ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in let array_type = ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) in int_type |-> ( a |-> array_type ) let unit_real ( ) = unit_type |-> real_type let unit_real ( ) = unit_type |-> int_type let int_unit ( ) = int_type |-> unit_type let wrap_array ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in let array_type = ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) in array_type |-> array_type let tuple_real_real ( ) = ref ( TComposed ( [ " tuple " ] , [ real_type ; real_type ] , None ) ) let real_tuple_real_real ( ) = real_type |-> tuple_real_real ( ) let some_array ( ) = let a = ref ( TUnbound ( " ' a " , None , None ) ) in let size = ref ( TUnbound ( " ' size " , None , None ) ) in ref ( TComposed ( [ " array " ] , [ a ; size ] , None ) ) end
module Impl = Snarky . Snark . Make ( Snarky . Backends . Mnt4 . Default )
module Alias_alias = struct include struct type nonrec ( ' a , ' b ) u_var = ' a -> ' a and ( ' a , ' b ) u = ' a -> ' a let u_typ x___2 x___1 = Typ . fn x___2 x___2 end include struct type nonrec ( ' a , ' b ) v_var = ( ' a , ' a ) u_var and ( ' a , ' b ) v = ( ' a , ' a ) u let v_typ x___4 x___3 x___5 = u_typ x___4 x___5 end let f ( x : ( int , bool ) u_var ) : ( int , int ) u_var = x let g ( x : ( int , int ) v_var ) : ( int , bool ) v_var = x let h ( x : ( int , bool ) v_var ) : ( int , int ) u_var = x let i ( x : ( bool , bool ) u_var ) : ( bool , unit ) v_var = x end
module Alias_opaque = struct type nonrec ( ' a , ' b ) u type nonrec ( ' a , ' b ) v = ( ' a , ' a ) u let f ( x : ( int , int ) v ) : ( int , bool ) v = x let g ( x : ( int , bool ) v ) : ( int , int ) u = x let h ( x : ( bool , bool ) u ) : ( bool , unit ) v = x end
module Alias_record = struct include struct type nonrec ( ' a , ' b ) u_var = { a : ' a ; b : ' b } and ( ' a , ' b ) u = { a : ' a ; b : ' b } let u_typ x___11 x___10 = { Snarky . Types . Typ . store = ( fun { a ; b } -> Snarky . Typ_monads . Store . bind ( x___11 . Snarky . Types . Typ . store a ) ~ f ( : fun a -> Snarky . Typ_monads . Store . bind ( x___10 . Snarky . Types . Typ . store b ) ~ f ( : fun b -> Snarky . Typ_monads . Store . return { a ; b } ) ) ) ; Snarky . Types . Typ . read = ( fun { a ; b } -> Snarky . Typ_monads . Read . bind ( x___11 . Snarky . Types . Typ . read a ) ~ f ( : fun a -> Snarky . Typ_monads . Read . bind ( x___10 . Snarky . Types . Typ . read b ) ~ f ( : fun b -> Snarky . Typ_monads . Read . return { a ; b } ) ) ) ; Snarky . Types . Typ . alloc = Snarky . Typ_monads . Alloc . bind x___11 . Snarky . Types . Typ . alloc ~ f ( : fun a -> Snarky . Typ_monads . Alloc . bind x___10 . Snarky . Types . Typ . alloc ~ f ( : fun b -> Snarky . Typ_monads . Alloc . return { a ; b } ) ) ; Snarky . Types . Typ . check = ( fun { a ; b } -> Snarky . Checked . bind ( x___11 . Snarky . Types . Typ . check a ) ~ f ( : fun ( ) -> Snarky . Checked . bind ( x___10 . Snarky . Types . Typ . check b ) ~ f ( : fun ( ) -> Snarky . Checked . return ( ) ) ) ) } end include struct type nonrec ( ' a , ' b ) v_var = ( ' a , ' a ) u_var and ( ' a , ' b ) v = ( ' a , ' a ) u let v_typ x___13 x___12 = u_typ x___13 x___13 end let f ( x : ( int , int ) v_var ) : ( int , bool ) v_var = x let g ( x : ( int , bool ) v_var ) : ( int , int ) u_var = x let h ( x : ( bool , bool ) u_var ) : ( bool , unit ) v_var = x end
module Alias_variant = struct type nonrec ( ' a , ' b ) u = A | B | C of ' a | D of ' b type nonrec ( ' a , ' b ) v = ( ' a , ' a ) u let f ( x : ( int , int ) v ) : ( int , bool ) v = x let g ( x : ( int , bool ) v ) : ( int , int ) u = x let h ( x : ( bool , bool ) u ) : ( bool , unit ) v = x end
module Kind = struct type t = | Var | Constr | Arrow | Tuple | Other let to_int = function | Var -> 0 | Constr -> 1 | Arrow -> 2 | Tuple -> 3 | Other -> 4 let of_int = function | 0 -> Var | 1 -> Constr | 2 -> Arrow | 3 -> Tuple | 4 -> Other | _ -> assert false let to_string = function | Var -> " variable " | Constr -> " constructor " | Arrow -> " arrow " | Tuple -> " tuple " | Other -> " other " let compare t1 t2 = CCInt . compare ( to_int t1 ) ( to_int t2 ) let equal t1 t2 = compare t1 t2 = 0 let hash = CCHash . poly module Map = CCMap . Make ( struct type nonrec t = t let compare = compare end ) module HMap = CCHashtbl . Make ( struct type nonrec t = t let equal = equal let hash = hash end ) module MSet = CCMultiSet . Make ( struct type nonrec t = t let compare = compare end ) let pp = Fmt . of_to_string to_string end
module Kind ' = struct type t = | Var | Constr of LongIdent . t | Arrow | Tuple | Other let to_int = function | Var -> 0 | Constr _ -> 1 | Arrow -> 2 | Tuple -> 3 | Other -> 4 let compare t1 t2 = match t1 , t2 with | Var , Var | Arrow , Arrow | Tuple , Tuple | Other , Other -> 0 | Constr lid1 , Constr lid2 -> LongIdent . compare lid1 lid2 | _ -> CCInt . compare ( to_int t1 ) ( to_int t2 ) let equal t1 t2 = compare t1 t2 = 0 let hash = CCHash . poly module Map = CCMap . Make ( struct type nonrec t = t let compare = compare end ) module HMap = CCHashtbl . Make ( struct type nonrec t = t let equal = equal let hash = hash end ) module MSet = CCMultiSet . Make ( struct type nonrec t = t let compare = compare end ) end
module rec Base : sig type t = | Var of Variable . t | FrozenVar of Variable . t | Constr of LongIdent . t * t Array . t | Arrow of NSet . t * t | Tuple of NSet . t | Other of Int . t val kind : t -> Kind . t val kind ' : t -> Kind ' . t val compare : t CCOrd . t val equal : t -> t -> Bool . t val hash : t CCHash . t type t = | Var of Variable . t | FrozenVar of Variable . t | Constr of LongIdent . t * t Array . t | Arrow of NSet . t * t | Tuple of NSet . t | Other of Int . t let kind : t -> Kind . t = function | Var _ | FrozenVar _ -> Var | Constr _ -> Constr | Arrow _ -> Arrow | Tuple _ -> Tuple | Other _ -> Other let kind ' : t -> Kind ' . t = function | Var _ | FrozenVar _ -> Var | Constr ( lid , _ ) -> Constr lid | Arrow _ -> Arrow | Tuple _ -> Tuple | Other _ -> Other let to_int t = Kind . to_int @@ kind t let rec compare t1 t2 = if t1 == t2 then 0 else let open CCOrd . Infix in match t1 , t2 with | Var var1 , Var var2 -> Variable . compare var1 var2 | Constr ( lid1 , params1 ) , Constr ( lid2 , params2 ) -> LongIdent . compare lid1 lid2 <?> ( CCArray . compare compare , params1 , params2 ) | Arrow ( param1 , ret1 ) , Arrow ( param2 , ret2 ) -> let cmp = compare in cmp ret1 ret2 <?> ( NSet . compare , param1 , param2 ) | Tuple elts1 , Tuple elts2 -> NSet . compare elts1 elts2 | Other i1 , Other i2 -> CCInt . compare i1 i2 | _ -> CCInt . compare ( to_int t1 ) ( to_int t2 ) let equal t1 t2 = compare t1 t2 = 0 let hash = Hashtbl . hash end type elt = Base . t type t val compare : t CCOrd . t val of_list : elt List . t -> t val of_iter : elt Iter . t -> t val to_iter : t -> elt Iter . t val as_array : t -> elt Array . t val empty : t val is_empty : t -> Bool . t val length : t -> Int . t val singleton : elt -> t val is_singleton : t -> elt Option . t val union : t -> t -> t val add : elt -> t -> t val fold : ( elt -> ' a -> ' a ) -> t -> ' a -> ' a val map : ( elt -> elt ) -> t -> t val pp : elt Fmt . t -> t Fmt . t type elt = Base . t type t = elt Array . t let compare = CCArray . compare Base . compare let sort = CCArray . sort Base . compare let of_list lst = let t = CCArray . of_list lst in sort t ; t let of_iter it = let t = Iter . to_array it in sort t ; t let to_iter = Iter . of_array let as_array = CCFun . id let empty = [ ] || let is_empty t = t = [ ] || let length = CCArray . length let singleton elt = [ | elt ] | let is_singleton = function | [ | elt ] | -> Some elt | _ -> None let union t1 t2 = let t = CCArray . append t1 t2 in sort t ; t let add elt t = union ( singleton elt ) t let fold fn t acc = CCArray . fold_left ( CCFun . flip fn ) acc t let map fn t = of_iter @@ Iter . map fn @@ to_iter t let pp pp_ty ppf = function | [ ] || -> Fmt . string ppf " ( ) " | [ | elt ] | -> pp_ty ppf elt | t -> Fmt . pf ppf " [ @< 2 >% a ] " @ Fmt . ( array ~ sep ( : any " *@ " ) pp_ty ) t end
module HMap = CCHashtbl . Make ( Base )
module Map = CCMap . Make ( Base )
module Set = CCSet . Make ( Base )
module MSet = CCMultiSet . Make ( Base )
module Hashcons = struct type elt = t type t = elt HMap . t let make ( ) = HMap . create 17 let hashcons t ty = match HMap . find_opt t ty with | Some ty -> ty | None -> HMap . add t ty ty ; ty end
module Env = struct type t = { var_gen : Variable . Gen . t ; hcons : Hashcons . t ; } let make ( ? hcons = Hashcons . make ( ) ) namespace = { var_gen = Variable . Gen . make namespace ; hcons ; } end
let hashcons env ty = Hashcons . hashcons env . Env . hcons ty