theory i140618a__mem_write_execute_state_list imports Complex_Main "~~/src/HOL/Library/Code_Target_Nat" begin (*16-bit dataword and a little notation.*) type_synonym b8T = "bool * bool * bool * bool * bool * bool * bool * bool" type_synonym b16T = "b8T * b8T" notation (input) False ("0\<^sub>B") notation (input) True ("1\<^sub>B") abbreviation hex00 :: b8T ("00\") where "hex00 == (0\<^sub>B,0\<^sub>B,0\<^sub>B,0\<^sub>B,0\<^sub>B,0\<^sub>B,0\<^sub>B,0\<^sub>B)" abbreviation hex55 :: b8T ("55\") where "hex55 == (0\<^sub>B,1\<^sub>B,0\<^sub>B,1\<^sub>B,0\<^sub>B,1\<^sub>B,0\<^sub>B,1\<^sub>B)" abbreviation hexAA :: b8T ("AA\") where "hexAA == (1\<^sub>B,0\<^sub>B,1\<^sub>B,0\<^sub>B,1\<^sub>B,0\<^sub>B,1\<^sub>B,0\<^sub>B)" abbreviation hex16 :: "b8T => b8T => b16T" where "hex16 b1 b0 == (b1, b0)" notation hex16 ("x\__" [1000, 1000] 1000) (*Indexed, fixed size b16T list. The list idx won't always match the pair idx.*) type_synonym idxed_flist = "(nat * b16T) list * nat" primrec idx0s_app_flist :: "(nat * b16T) list \ nat \ (nat * b16T) list" where "idx0s_app_flist nlist 0 = nlist" |"idx0s_app_flist nlist (Suc n) = idx0s_app_flist ((n,x\00\00\) # nlist) n" definition create_idxed_flist :: "nat \ idxed_flist" where "create_idxed_flist lsize = (idx0s_app_flist [] lsize, lsize)" value "create_idxed_flist 0x10" value "length (fst (create_idxed_flist 0x10)) = 0x10" (*The write function.*) primrec write_idxed_flist :: "idxed_flist \ nat \ b16T \ idxed_flist option" where "write_idxed_flist (flist, lsize) idx data = ( if (idx >= lsize \ length flist \ lsize) then None else Some (flist[idx := (idx, data)], lsize) )" (*Two sequential writes done manually.*) value "write_idxed_flist (the(write_idxed_flist (create_idxed_flist 0x10) 5 x\55\55\)) 3 x\AA\AA\" (*Automate execution of a list of writes, to generate a list of states.*) fun execute_list :: "(idxed_flist option) list \ (nat * b16T) list \ (idxed_flist option) list" where "execute_list [] _ = []" |"execute_list state_list [] = state_list" |"execute_list [s] ((idx,data) # xs) = execute_list ((write_idxed_flist (the s) idx data) # [s]) xs" |"execute_list (s # ss) ((idx,data) # xs) = execute_list ((write_idxed_flist (the s) idx data) # (s # ss)) xs" (*Adding the third condition sped up the termination proof a lot. It probably needs more conditions to speed it up more.*) (*Execute a list of three instructions. The states are shown last to first.*) abbreviation "three_write_list == [(5, x\55\55\), (3, x\AA\AA\), (0x10, x\55\55\)]" value "execute_list [Some (create_idxed_flist 0xF)] three_write_list" (*Prove something trivial. The value of proving this wasn't trivial. It showed me I needed the condition "length flist \ lsize" in my function above.*) lemma "idx < lsize \ length flist = lsize \ write_idxed_flist (flist, lsize) idx dw \ None" by(simp) (******************************************************************************) end