Theory Solidity_Evaluator
section‹ Solidty Evaluator and Code Generator Setup›
theory
Solidity_Evaluator
imports
Solidity_Main
"HOL-Library.Code_Target_Numeral"
"HOL-Library.Sublist"
"HOL-Library.Finite_Map"
begin
subsection‹Code Generator Setup and Local Tests›
subsubsection‹Utils›
definition FAILURE::"String.literal" where "FAILURE = STR ''Failure''"
definition "inta_of_int = int o nat_of_integer"
definition "nat_of_int = nat_of_integer"
fun astore :: "Identifier ⇒ Type ⇒ Valuetype ⇒ StorageT * Environment ⇒ StorageT * Environment"
where "astore i t v (s, e) = (fmupd i v s, (updateEnv i t (Storeloc i) e))"
subsubsection‹Valuetypes›
fun dump⇩V⇩a⇩l⇩u⇩e⇩t⇩y⇩p⇩e⇩s::"Types ⇒ Valuetype ⇒ String.literal" where
"dump⇩V⇩a⇩l⇩u⇩e⇩t⇩y⇩p⇩e⇩s (TSInt _) n = n"
| "dump⇩V⇩a⇩l⇩u⇩e⇩t⇩y⇩p⇩e⇩s (TUInt _) n = n"
| "dump⇩V⇩a⇩l⇩u⇩e⇩t⇩y⇩p⇩e⇩s TBool b = (if b = (STR ''True'') then STR ''true'' else STR ''false'')"
| "dump⇩V⇩a⇩l⇩u⇩e⇩t⇩y⇩p⇩e⇩s TAddr ad = ad"
paragraph‹Generalized Unit Tests›
lemma "createSInt 8 500 = STR ''-12''"
by(eval)
lemma "STR ''-92134039538802366542421159375273829975''
= createSInt 128 45648483135649456465465452123894894554654654654654646999465"
by(eval)
lemma "STR ''-128'' = createSInt 8 (-128)"
by(eval)
lemma "STR ''244'' = (createUInt 8 500)"
by(eval)
lemma "STR ''220443428915524155977936330922349307608''
= (createUInt 128 4564848313564945646546545212389489455465465465465464699946544654654654654168)"
by(eval)
lemma "less (TUInt 144) (TSInt 160) (STR ''5'') (STR ''8'') = Some(STR ''True'', TBool) "
by(eval)
subsubsection‹Load: Accounts›
fun load⇩A⇩c⇩c⇩o⇩u⇩n⇩t⇩s :: "Accounts ⇒ (Address × Balance) list ⇒ Accounts" where
"load⇩A⇩c⇩c⇩o⇩u⇩n⇩t⇩s acc [] = acc"
| "load⇩A⇩c⇩c⇩o⇩u⇩n⇩t⇩s acc ((ad, bal)#as) = load⇩A⇩c⇩c⇩o⇩u⇩n⇩t⇩s (updateBalance ad bal acc) as"
definition dump⇩A⇩c⇩c⇩o⇩u⇩n⇩t⇩s :: "Accounts ⇒ Address list ⇒ String.literal"
where
"dump⇩A⇩c⇩c⇩o⇩u⇩n⇩t⇩s acc = foldl (λ t a . String.implode ( (String.explode t)
@ (String.explode a)
@ ''.balance''
@ ''==''
@ String.explode (accessBalance acc a)
@ ''⏎''))
(STR '''')"
definition init⇩A⇩c⇩c⇩o⇩u⇩n⇩t::"(Address × Balance) list => Accounts" where
"init⇩A⇩c⇩c⇩o⇩u⇩n⇩t = load⇩A⇩c⇩c⇩o⇩u⇩n⇩t⇩s emptyAccount"
subsubsection‹Load: Store›
type_synonym Data⇩S⇩t⇩o⇩r⇩e = "(Location × String.literal) list"
fun show⇩S⇩t⇩o⇩r⇩e::"'a Store ⇒ 'a fset" where
"show⇩S⇩t⇩o⇩r⇩e s = (fmran (mapping s))"
subsubsection‹Load: Memory›