doc-src/Tutorial/CodeGen/CodeGenIf.thy
author wenzelm
Tue, 07 May 2002 14:24:30 +0200
changeset 13104 df7aac8543c9
parent 5377 efb799c5ed3c
permissions -rw-r--r--
clarified eq_thm; added eq_thm_prop;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5377
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
     1
CodeGenIf = List +
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
     2
types 'v binop = 'v => 'v => 'v
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
     3
      'v test = 'v => bool
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
     4
datatype ('a,'v) expr = Vex 'a 
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
     5
                      | Cex 'v
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
     6
                      | Bex ('v binop) (('a,'v) expr) (('a,'v) expr)
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
     7
                      | Ifex ('v test) (('a,'v)expr) (('a,'v)expr) (('a,'v)expr)
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
     8
consts value :: ('a => 'v) => ('a,'v)expr => 'v
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
     9
primrec value expr
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    10
"value env (Vex a) = env a"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    11
"value env (Cex v) = v"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    12
"value env (Bex f e1 e2) = f (value env e1) (value env e2)"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    13
"value env (Ifex t b e1 e2) =
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    14
   (if t(value env b) then value env e1 else value env e2)"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    15
datatype ('a,'v) instr = Load 'a
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    16
                       | Const 'v
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    17
                       | Apply ('v binop)
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    18
                       | Jump nat
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    19
                       | Test ('v test) nat
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    20
consts exec :: "('a => 'v) * 'v list * (('a,'v) instr) list => 'v list"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    21
recdef exec "measure(%(s,vs,is).length is)"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    22
simpset "simpset() addsimps [diff_less_Suc]"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    23
"exec(s, vs, []) = vs"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    24
"exec(s, vs, i#is) = (case i of
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    25
    Load a   => exec(s,(s a)#vs,is)
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    26
  | Const v  => exec(s,v#vs,is)
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    27
  | Apply f  => exec(s, (f (hd vs) (hd(tl vs)))#(tl(tl vs)), is)
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    28
  | Jump n   => exec(s, vs, drop n is)
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    29
  | Test t n => (if t(hd vs) then exec(s,tl vs, drop n is)
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    30
                             else exec(s,tl vs, is)))"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    31
consts comp :: ('a,'v) expr => (('a,'v) instr) list
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    32
primrec comp expr
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    33
"comp (Vex a) = [Load a]"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    34
"comp (Cex v) = [Const v]"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    35
"comp (Bex f e1 e2) = (comp e2)@(comp e1)@[Apply f]"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    36
"comp (Ifex t b e1 e2) = (let is1 = comp e1; is2 = comp e2
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    37
                          in comp b @ [Test t (Suc(length is2))] @
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    38
                             is2 @ [Jump (length is1)] @ is1)"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    39
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    40
consts wf :: ('a,'v)instr list => bool
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    41
primrec wf list
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    42
"wf [] = True"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    43
"wf (i#is) = (wf is & (case i of Load a => True | Const v => True
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    44
  | Apply f  => True | Jump n => n <= length is | Test t n => n <= length is))"
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    45
efb799c5ed3c *** empty log message ***
nipkow
parents:
diff changeset
    46
end