Theory BoolProgs_Extras
theory BoolProgs_Extras
imports
BoolProgs
"HOL-Library.Mapping"
begin
context begin interpretation BoolProg_Syntax .
subsection ‹Macros›
text ‹Counters ≈ bounded natural numbers›
:: "nat list ⇒ bexp list ⇒ nat ⇒ nat ⇒ nat ⇒ com"
where
"set_counter vs bs _ 0 _ = vs ::= bs"
| "set_counter vs bs pos (Suc l) 0 = set_counter (pos#vs) (FF#bs) (Suc pos) l 0"
| "set_counter vs bs pos (Suc l) (Suc n) = set_counter (pos#vs) (TT#bs) (Suc pos) l n"
:: "nat ⇒ nat ⇒ nat ⇒ bexp" where
"counter_eq pos m n = (if n > m then FF else
let posT = [0..<n] in
let posF = [n..<m] in
let bexps = (map (λx. V (pos + x)) posT) @ (map (λx. Not (V (pos + x))) posF) in
foldl And TT bexps)"
:: "nat list ⇒ bexp list ⇒ nat ⇒ nat ⇒ com"
where
"inc_counter vs bs pos 0 = vs ::= bs"
| "inc_counter vs bs pos (Suc n) = IF V pos THEN inc_counter vs bs (Suc pos) n ELSE (pos#vs) ::= (TT#bs)"
:: "nat list ⇒ bexp list ⇒ bool ⇒ nat ⇒ com" where
"dec_counter_toggle vs bs False _ = vs ::= bs"
|"dec_counter_toggle vs bs True pos = ((pos - 1)#vs) ::= (FF#bs)"
:: "nat list ⇒ bexp list ⇒ bool ⇒ nat ⇒ nat ⇒ com"
where
"dec_counter' vs bs start pos 0 = dec_counter_toggle vs bs start pos"
| "dec_counter' vs bs start pos (Suc n) =
IF Not (V pos) THEN (dec_counter_toggle vs bs start pos)
ELSE dec_counter' vs bs True (Suc pos) n"
:: "nat list ⇒ bexp list ⇒ nat ⇒ nat ⇒ com" where
"dec_counter vs bs = dec_counter' vs bs False"
where
"array_access ctr act m = foldl (λbexp c. IF ctr c THEN act c ELSE bexp) SKIP [0..<m]"
where
"array_check ctr chk m = foldl (λbexp c. And (Or (Not (ctr c)) (chk c)) (Or (ctr c) bexp)) FF [0..<m]"
subsection ‹Gather statistics›
where
"stat' (a,t,c,g) instr = (case instr of
AssI _ _ ⇒ (Suc a,t,c,g)
| TestI _ _ ⇒ (a,Suc t,c,g)
| ChoiceI _ ⇒ (a,t,Suc c,g)
| GotoI _ ⇒ (a,t,c,Suc g))"
where
"stats ls = foldl stat' (0,0,0,0) ls"
subsection ‹Misc›
type_synonym = "(String.literal, bexp) mapping"
type_synonym = "(String.literal, nat ⇒ bexp) mapping"
:: "('a × 'b) list ⇒ ('a, 'b) mapping"
where
"mapping_from_list = foldl (λm (k,v). Mapping.update k v m) Mapping.empty"
end
end