src/HOL/IMP/Collecting_Examples.thy
author nipkow
Thu, 28 Mar 2013 15:45:08 +0100
changeset 51566 8e97017538ba
parent 51391 408271602165
child 51698 c0af8bbc5825
permissions -rw-r--r--
improved pretty printing for state set acom
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48765
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
     1
theory Collecting_Examples
51566
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
     2
imports Collecting Vars
48765
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
     3
begin
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
     4
51566
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
     5
subsection "Pretty printing state sets"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
     6
48765
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
     7
text{* Tweak code generation to work with sets of non-equality types: *}
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
     8
declare insert_code[code del] union_coset_filter[code del]
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
     9
lemma insert_code [code]:  "insert x (set xs) = set (x#xs)"
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
    10
by simp
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
    11
51566
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    12
text{* Collect variables in acom: *}
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    13
fun cvars :: "'a acom \<Rightarrow> vname set" where
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    14
"cvars (SKIP {P})= {}" |
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    15
"cvars (x::=e {P}) = {x} \<union> vars e" |
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    16
"cvars (c1;c2) = cvars c1 \<union> cvars c2" |
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    17
"cvars (IF b THEN {P1} c1 ELSE {P2} c2 {Q}) = vars b \<union> cvars c1 \<union> cvars c2" |
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    18
"cvars ({I} WHILE b DO {P} c {Q}) = vars b \<union> cvars c"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    19
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    20
text{* Compensate for the fact that sets may now have duplicates: *}
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    21
definition compact :: "'a set \<Rightarrow> 'a set" where
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    22
"compact X = X"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    23
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    24
lemma [code]: "compact(set xs) = set(remdups xs)"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    25
by(simp add: compact_def)
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    26
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    27
definition "vars_acom = compact o cvars"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    28
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    29
text{* In order to display commands annotated with state sets, states must be
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    30
translated into a printable format as sets of variable-state pairs, for the
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    31
variables in the command: *}
51040
faf7f0d4f9eb tuned state display
nipkow
parents: 50986
diff changeset
    32
faf7f0d4f9eb tuned state display
nipkow
parents: 50986
diff changeset
    33
definition show_acom ::
51566
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    34
  "state set acom \<Rightarrow> (vname*val)set set acom" where
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    35
"show_acom C = map_acom (\<lambda>S. (\<lambda>s. (\<lambda>x. (x, s x)) ` (vars_acom C)) ` S) C"
51040
faf7f0d4f9eb tuned state display
nipkow
parents: 50986
diff changeset
    36
faf7f0d4f9eb tuned state display
nipkow
parents: 50986
diff changeset
    37
51566
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    38
subsection "Examples"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    39
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    40
definition "c0 = WHILE Less (V ''x'') (N 3)
48765
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
    41
                DO ''x'' ::= Plus (V ''x'') (N 2)"
51566
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    42
definition C0 :: "state set acom" where "C0 = anno {} c0"
48765
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
    43
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
    44
text{* Collecting semantics: *}
50821
nipkow
parents: 50766
diff changeset
    45
51566
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    46
value "show_acom (((step {<>}) ^^ 1) C0)"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    47
value "show_acom (((step {<>}) ^^ 2) C0)"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    48
value "show_acom (((step {<>}) ^^ 3) C0)"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    49
value "show_acom (((step {<>}) ^^ 4) C0)"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    50
value "show_acom (((step {<>}) ^^ 5) C0)"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    51
value "show_acom (((step {<>}) ^^ 6) C0)"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    52
value "show_acom (((step {<>}) ^^ 7) C0)"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    53
value "show_acom (((step {<>}) ^^ 8) C0)"
48765
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
    54
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
    55
text{* Small-step semantics: *}
51566
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    56
value "show_acom (((step {}) ^^ 0) (step {<>} C0))"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    57
value "show_acom (((step {}) ^^ 1) (step {<>} C0))"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    58
value "show_acom (((step {}) ^^ 2) (step {<>} C0))"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    59
value "show_acom (((step {}) ^^ 3) (step {<>} C0))"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    60
value "show_acom (((step {}) ^^ 4) (step {<>} C0))"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    61
value "show_acom (((step {}) ^^ 5) (step {<>} C0))"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    62
value "show_acom (((step {}) ^^ 6) (step {<>} C0))"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    63
value "show_acom (((step {}) ^^ 7) (step {<>} C0))"
8e97017538ba improved pretty printing for state set acom
nipkow
parents: 51391
diff changeset
    64
value "show_acom (((step {}) ^^ 8) (step {<>} C0))"
48765
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
    65
fb1ed5230abc special code with lists no longer necessary, use sets
nipkow
parents:
diff changeset
    66
end