theory SALSemantics = Semantics + TermCodegen: section {* SAL Semantics *} datatype tval = ILLEGAL | NAT nat types val = nat -- "or anything else, @{text nat} used in examples" loc = nat state = "loc \ tval" -- "value and type information are stored together" constdefs lift :: "(val \ val \ val) \ tval \ tval \ tval" "lift f a b \ (case a of ILLEGAL \ ILLEGAL | NAT m \ (case b of ILLEGAL \ ILLEGAL | NAT n \ NAT (f m n)))" (* other possibilities: (1) datatype type = ILLEGAL | NAT types state = "loc \ val * type" (2) datatype type = ILLEGAL | NAT types state = "(loc \ val) * (loc \ type)" (3) datatype 'a err = Err | OK 'a datatype tval = nat err types state = "loc \ tval" *) (*<*) --{* aus IMP/Natural.thy *} constdefs update :: "('a \ 'b) \ 'a \ 'b \ ('a \ 'b)" ("_/[_ ::= /_]" [900,0,0] 900) "update == fun_upd" syntax (xsymbols) update :: "('a \ 'b) \ 'a \ 'b \ ('a \ 'b)" ("_/[_ \ /_]" [900,0,0] 900) (*>*) datatype instr = SET loc val | ADD loc loc | INC loc | JMPEQ loc loc nat | JMPB nat types SALstate = "nat \ state" types SALform = "SALstate \ bool" types SALprogram = "(instr \ (SALform option)) list" constdefs cmd::"SALprogram \ (nat \ instr option)" "cmd p \ \ i. if (i < length p) then Some (fst (p!i)) else None" constdefs domC::"SALprogram \ (nat list)" "domC p \ upt 0 (length p)" constdefs an:: "SALprogram \ (nat \ SALform option)" "an p \ \ i. if (i < length p) then snd (p!i) else None" consts step::"SALprogram \ SALstate \ SALstate option" primrec "step p (i, m) = (case (cmd p i) of None \ None | Some ins \ (case ins of SET x n \ Some (i + 1, m[x \ NAT n]) | ADD x y \ Some (i + 1, m[x \ lift (op +) (m x) (m y)]) | INC x \ Some (i + 1, m[x \ lift (op +) (m x) (NAT 1)]) | JMPEQ x y t \ (if m x = m y then Some (i + t, m) else Some (i + 1, m)) | JMPB t \ Some (i - t, m)))" constdefs effS:: "SALprogram \ ((nat \ (loc \ tval)) \ (nat \ (loc \ tval))) set" "effS (p::SALprogram) \ {(s::SALstate,s'::SALstate). (step p s = Some s') }" (*<*) consts evaln::"SALprogram \ SALstate \ nat \ SALstate list" primrec evaln: "evaln p s 0 = []" "evaln p s (Suc n) = (case (step p s) of None \ [] | Some s' \ s' # (evaln p s' n))" constdefs showstate::"nat \ SALstate \ int \ (tval list)" "showstate mx s \ let (pc,m)=s in (int pc, (map m (upt 0 (Suc mx))))" (*>*) end