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
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)
subsection‹Code Generator Setup and Local Tests›
subsubsection‹Utils›
definition EMPTY::"String.literal" where "EMPTY = STR ''''"
definition FAILURE::"String.literal" where "FAILURE = STR ''Failure''"
fun intersperse :: "String.literal ⇒ String.literal list ⇒ String.literal" where
"intersperse s [] = EMPTY"
| "intersperse s [x] = x"
| "intersperse s (x # xs) = x + s + intersperse s xs"
definition splitAt::"nat ⇒ String.literal ⇒ String.literal × String.literal" where
"splitAt n xs = (String.implode(take n (String.explode xs)), String.implode(drop n (String.explode xs)))"
fun splitOn':: "'a ⇒ 'a list ⇒ 'a list ⇒ 'a list list" where
"splitOn' x [] acc = [rev acc]"
| "splitOn' x (y#ys) acc = (if x = y then (rev acc)#(splitOn' x ys [])
else splitOn' x ys (y#acc))"
fun splitOn::"'a ⇒ 'a list ⇒ 'a list list" where
"splitOn x xs = splitOn' x xs []"
definition isSuffixOf::"String.literal ⇒ String.literal ⇒ bool" where
"isSuffixOf s x = suffix (String.explode s) (String.explode x)"
definition tolist :: "Location ⇒ String.literal list" where
"tolist s = map String.implode (splitOn (CHR ''.'') (String.explode s))"
abbreviation convert :: "Location ⇒ Location"
where "convert loc ≡ (if loc= STR ''True'' then STR ''true'' else
if loc=STR ''False'' then STR ''false'' else loc)"
definition ‹sorted_list_of_set' ≡ map_fun id id (folding_on.F insort [])›
lemma sorted_list_of_fset'_def': ‹sorted_list_of_set' = sorted_list_of_set›
apply(rule ext)
by (simp add: sorted_list_of_set'_def sorted_list_of_set_def sorted_key_list_of_set_def)
lemma sorted_list_of_set_sort_remdups' [code]:
‹sorted_list_of_set' (set xs) = sort (remdups xs)›
using sorted_list_of_fset'_def' sorted_list_of_set_sort_remdups
by metis
definition locations_map :: "Location ⇒ (Location, 'v) fmap ⇒ Location list" where
"locations_map loc = (filter (isSuffixOf ((STR ''.'')+loc))) ∘ sorted_list_of_set' ∘ fset ∘ fmdom"
definition locations :: "Location ⇒ 'v Store ⇒ Location list" where
"locations loc = locations_map loc ∘ mapping"
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"
subsubsection‹Memory›