src/HOL/IMP/Examples.thy
author berghofe
Fri, 01 Jul 2005 13:54:12 +0200
changeset 16633 208ebc9311f2
parent 16417 9bc16273c2d4
child 18372 2bffdf62fe7f
permissions -rw-r--r--
Implemented trick (due to Tobias Nipkow) for fine-tuning simplification of premises of congruence rules.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9243
22b460a0b676 disambiguated := ; added Examples (factorial)
oheimb
parents:
diff changeset
     1
(*  Title:      HOL/IMP/Examples.thy
22b460a0b676 disambiguated := ; added Examples (factorial)
oheimb
parents:
diff changeset
     2
    ID:         $Id$
22b460a0b676 disambiguated := ; added Examples (factorial)
oheimb
parents:
diff changeset
     3
    Author:     David von Oheimb, TUM
22b460a0b676 disambiguated := ; added Examples (factorial)
oheimb
parents:
diff changeset
     4
    Copyright   2000 TUM
22b460a0b676 disambiguated := ; added Examples (factorial)
oheimb
parents:
diff changeset
     5
*)
22b460a0b676 disambiguated := ; added Examples (factorial)
oheimb
parents:
diff changeset
     6
12431
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
     7
header "Examples"
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
     8
16417
9bc16273c2d4 migrated theory headers to new format
haftmann
parents: 12431
diff changeset
     9
theory Examples imports Natural begin
12431
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    10
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    11
constdefs  
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    12
  factorial :: "loc => loc => com"
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    13
  "factorial a b == b :== (%s. 1);
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    14
                    \<WHILE> (%s. s a ~= 0) \<DO>
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    15
                    (b :== (%s. s b * s a); a :== (%s. s a - 1))"
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    16
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    17
declare update_def [simp]
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    18
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    19
subsection "An example due to Tony Hoare"
9243
22b460a0b676 disambiguated := ; added Examples (factorial)
oheimb
parents:
diff changeset
    20
12431
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    21
lemma lemma1 [rule_format (no_asm)]: 
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    22
  "[| !x. P x \<longrightarrow> Q x; \<langle>w,s\<rangle> \<longrightarrow>\<^sub>c t |] ==>
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    23
  !c. w = While P c \<longrightarrow> \<langle>While Q c,t\<rangle> \<longrightarrow>\<^sub>c u \<longrightarrow> \<langle>While Q c,s\<rangle> \<longrightarrow>\<^sub>c u"
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    24
apply (erule evalc.induct)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    25
apply auto
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    26
done
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    27
9243
22b460a0b676 disambiguated := ; added Examples (factorial)
oheimb
parents:
diff changeset
    28
12431
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    29
lemma lemma2 [rule_format (no_asm)]: 
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    30
  "[| !x. P x \<longrightarrow> Q x; \<langle>w,s\<rangle> \<longrightarrow>\<^sub>c u |] ==>  
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    31
  !c. w = While Q c \<longrightarrow> \<langle>While P c; While Q c,s\<rangle> \<longrightarrow>\<^sub>c u"
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    32
apply (erule evalc.induct)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    33
apply (simp_all (no_asm_simp))
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    34
apply blast
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    35
apply (case_tac "P s")
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    36
apply auto
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    37
done
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    38
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    39
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    40
lemma Hoare_example: "!x. P x \<longrightarrow> Q x ==>  
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    41
  (\<langle>While P c; While Q c, s\<rangle> \<longrightarrow>\<^sub>c t) = (\<langle>While Q c, s\<rangle> \<longrightarrow>\<^sub>c t)"
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    42
  by (blast intro: lemma1 lemma2 dest: semi [THEN iffD1])
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    43
9243
22b460a0b676 disambiguated := ; added Examples (factorial)
oheimb
parents:
diff changeset
    44
12431
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    45
subsection "Factorial"
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    46
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    47
lemma factorial_3: "a~=b ==>  
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    48
  \<langle>factorial a b, Mem(a:=3)\<rangle> \<longrightarrow>\<^sub>c Mem(b:=6, a:=0)"
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    49
apply (unfold factorial_def)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    50
apply simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    51
done
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    52
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    53
text {* the same in single step mode: *}
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    54
lemmas [simp del] = evalc_cases
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    55
lemma  "a~=b \<Longrightarrow> \<langle>factorial a b, Mem(a:=3)\<rangle> \<longrightarrow>\<^sub>c Mem(b:=6, a:=0)"
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    56
apply (unfold factorial_def)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    57
apply (frule not_sym)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    58
apply (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    59
apply  (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    60
apply simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    61
apply (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    62
apply   simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    63
apply  (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    64
apply   (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    65
apply  simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    66
apply  (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    67
apply simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    68
apply (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    69
apply   simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    70
apply  (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    71
apply   (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    72
apply  simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    73
apply  (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    74
apply simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    75
apply (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    76
apply   simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    77
apply  (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    78
apply   (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    79
apply  simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    80
apply  (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    81
apply simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    82
apply (rule evalc.intros)
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    83
apply simp
07ec657249e5 converted to Isar
kleing
parents: 9243
diff changeset
    84
done
9243
22b460a0b676 disambiguated := ; added Examples (factorial)
oheimb
parents:
diff changeset
    85
  
22b460a0b676 disambiguated := ; added Examples (factorial)
oheimb
parents:
diff changeset
    86
end