src/HOL/Induct/QuoNestedDataType.thy
author wenzelm
Sat, 07 Apr 2012 16:41:59 +0200
changeset 47389 e8552cba702d
parent 45694 4a8743618257
child 49834 b27bbb021df1
permissions -rw-r--r--
explicit checks stable_finished_theory/stable_command allow parallel asynchronous command transactions; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 40822
diff changeset
     1
(*  Title:      HOL/Induct/QuoNestedDataType.thy
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
     3
    Copyright   2004  University of Cambridge
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
     4
*)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
     5
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
     6
header{*Quotienting a Free Algebra Involving Nested Recursion*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
     7
16417
9bc16273c2d4 migrated theory headers to new format
haftmann
parents: 15172
diff changeset
     8
theory QuoNestedDataType imports Main begin
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
     9
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    10
subsection{*Defining the Free Algebra*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    11
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    12
text{*Messages with encryption and decryption as free constructors.*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    13
datatype
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    14
     freeExp = VAR  nat
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 30198
diff changeset
    15
             | PLUS  freeExp freeExp
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 30198
diff changeset
    16
             | FNCALL  nat "freeExp list"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    17
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    18
text{*The equivalence relation, which makes PLUS associative.*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    19
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    20
text{*The first rule is the desired equation. The next three rules
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    21
make the equations applicable to subterms. The last two rules are symmetry
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    22
and transitivity.*}
23746
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    23
inductive_set
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    24
  exprel :: "(freeExp * freeExp) set"
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    25
  and exp_rel :: "[freeExp, freeExp] => bool"  (infixl "\<sim>" 50)
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    26
  where
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    27
    "X \<sim> Y == (X,Y) \<in> exprel"
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    28
  | ASSOC: "PLUS X (PLUS Y Z) \<sim> PLUS (PLUS X Y) Z"
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    29
  | VAR: "VAR N \<sim> VAR N"
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    30
  | PLUS: "\<lbrakk>X \<sim> X'; Y \<sim> Y'\<rbrakk> \<Longrightarrow> PLUS X Y \<sim> PLUS X' Y'"
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    31
  | FNCALL: "(Xs,Xs') \<in> listrel exprel \<Longrightarrow> FNCALL F Xs \<sim> FNCALL F Xs'"
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    32
  | SYM:   "X \<sim> Y \<Longrightarrow> Y \<sim> X"
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    33
  | TRANS: "\<lbrakk>X \<sim> Y; Y \<sim> Z\<rbrakk> \<Longrightarrow> X \<sim> Z"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    34
  monos listrel_mono
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    35
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    36
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    37
text{*Proving that it is an equivalence relation*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    38
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
    39
lemma exprel_refl: "X \<sim> X"
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
    40
  and list_exprel_refl: "(Xs,Xs) \<in> listrel(exprel)"
23746
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    41
  by (induct X and Xs) (blast intro: exprel.intros listrel.intros)+
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    42
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    43
theorem equiv_exprel: "equiv UNIV exprel"
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
    44
proof -
30198
922f944f03b2 name changes
nipkow
parents: 23746
diff changeset
    45
  have "refl exprel" by (simp add: refl_on_def exprel_refl)
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
    46
  moreover have "sym exprel" by (simp add: sym_def, blast intro: exprel.SYM)
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
    47
  moreover have "trans exprel" by (simp add: trans_def, blast intro: exprel.TRANS)
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
    48
  ultimately show ?thesis by (simp add: equiv_def)
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    49
qed
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    50
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    51
theorem equiv_list_exprel: "equiv UNIV (listrel exprel)"
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
    52
  using equiv_listrel [OF equiv_exprel] by simp
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    53
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    54
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    55
lemma FNCALL_Nil: "FNCALL F [] \<sim> FNCALL F []"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    56
apply (rule exprel.intros) 
23746
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    57
apply (rule listrel.intros) 
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    58
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    59
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    60
lemma FNCALL_Cons:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    61
     "\<lbrakk>X \<sim> X'; (Xs,Xs') \<in> listrel(exprel)\<rbrakk>
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    62
      \<Longrightarrow> FNCALL F (X#Xs) \<sim> FNCALL F (X'#Xs')"
23746
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    63
by (blast intro: exprel.intros listrel.intros) 
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    64
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    65
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    66
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    67
subsection{*Some Functions on the Free Algebra*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    68
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    69
subsubsection{*The Set of Variables*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    70
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    71
text{*A function to return the set of variables present in a message.  It will
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    72
be lifted to the initial algrebra, to serve as an example of that process.
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    73
Note that the "free" refers to the free datatype rather than to the concept
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    74
of a free variable.*}
39246
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
    75
primrec freevars :: "freeExp \<Rightarrow> nat set" 
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
    76
  and freevars_list :: "freeExp list \<Rightarrow> nat set" where
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
    77
  "freevars (VAR N) = {N}"
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
    78
| "freevars (PLUS X Y) = freevars X \<union> freevars Y"
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
    79
| "freevars (FNCALL F Xs) = freevars_list Xs"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    80
39246
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
    81
| "freevars_list [] = {}"
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
    82
| "freevars_list (X # Xs) = freevars X \<union> freevars_list Xs"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    83
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    84
text{*This theorem lets us prove that the vars function respects the
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    85
equivalence relation.  It also helps us prove that Variable
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    86
  (the abstract constructor) is injective*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    87
theorem exprel_imp_eq_freevars: "U \<sim> V \<Longrightarrow> freevars U = freevars V"
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
    88
apply (induct set: exprel) 
23746
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
    89
apply (erule_tac [4] listrel.induct) 
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    90
apply (simp_all add: Un_assoc)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    91
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    92
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    93
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    94
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    95
subsubsection{*Functions for Freeness*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    96
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
    97
text{*A discriminator function to distinguish vars, sums and function calls*}
39246
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
    98
primrec freediscrim :: "freeExp \<Rightarrow> int" where
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
    99
  "freediscrim (VAR N) = 0"
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   100
| "freediscrim (PLUS X Y) = 1"
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   101
| "freediscrim (FNCALL F Xs) = 2"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   102
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   103
theorem exprel_imp_eq_freediscrim:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   104
     "U \<sim> V \<Longrightarrow> freediscrim U = freediscrim V"
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   105
  by (induct set: exprel) auto
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   106
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   107
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   108
text{*This function, which returns the function name, is used to
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   109
prove part of the injectivity property for FnCall.*}
39246
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   110
primrec freefun :: "freeExp \<Rightarrow> nat" where
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   111
  "freefun (VAR N) = 0"
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   112
| "freefun (PLUS X Y) = 0"
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   113
| "freefun (FNCALL F Xs) = F"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   114
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   115
theorem exprel_imp_eq_freefun:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   116
     "U \<sim> V \<Longrightarrow> freefun U = freefun V"
23746
a455e69c31cc Adapted to new inductive definition package.
berghofe
parents: 22269
diff changeset
   117
  by (induct set: exprel) (simp_all add: listrel.intros)
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   118
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   119
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   120
text{*This function, which returns the list of function arguments, is used to
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   121
prove part of the injectivity property for FnCall.*}
39246
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   122
primrec freeargs :: "freeExp \<Rightarrow> freeExp list" where
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   123
  "freeargs (VAR N) = []"
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   124
| "freeargs (PLUS X Y) = []"
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   125
| "freeargs (FNCALL F Xs) = Xs"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   126
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   127
theorem exprel_imp_eqv_freeargs:
40822
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   128
  assumes "U \<sim> V"
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   129
  shows "(freeargs U, freeargs V) \<in> listrel exprel"
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   130
proof -
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   131
  from equiv_list_exprel have sym: "sym (listrel exprel)" by (rule equivE)
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   132
  from equiv_list_exprel have trans: "trans (listrel exprel)" by (rule equivE)
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   133
  from assms show ?thesis
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   134
    apply induct
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   135
    apply (erule_tac [4] listrel.induct) 
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   136
    apply (simp_all add: listrel.intros)
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   137
    apply (blast intro: symD [OF sym])
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   138
    apply (blast intro: transD [OF trans])
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   139
    done
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   140
qed
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   141
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   142
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   143
subsection{*The Initial Algebra: A Quotiented Message Type*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   144
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 41959
diff changeset
   145
definition "Exp = UNIV//exprel"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   146
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 41959
diff changeset
   147
typedef (open) exp = Exp
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 41959
diff changeset
   148
  morphisms Rep_Exp Abs_Exp
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 41959
diff changeset
   149
  unfolding Exp_def by (auto simp add: quotient_def)
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   150
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   151
text{*The abstract message constructors*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   152
19736
wenzelm
parents: 18460
diff changeset
   153
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21210
diff changeset
   154
  Var :: "nat \<Rightarrow> exp" where
19736
wenzelm
parents: 18460
diff changeset
   155
  "Var N = Abs_Exp(exprel``{VAR N})"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   156
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21210
diff changeset
   157
definition
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21210
diff changeset
   158
  Plus :: "[exp,exp] \<Rightarrow> exp" where
19736
wenzelm
parents: 18460
diff changeset
   159
   "Plus X Y =
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   160
       Abs_Exp (\<Union>U \<in> Rep_Exp X. \<Union>V \<in> Rep_Exp Y. exprel``{PLUS U V})"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   161
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21210
diff changeset
   162
definition
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21210
diff changeset
   163
  FnCall :: "[nat, exp list] \<Rightarrow> exp" where
19736
wenzelm
parents: 18460
diff changeset
   164
   "FnCall F Xs =
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   165
       Abs_Exp (\<Union>Us \<in> listset (map Rep_Exp Xs). exprel `` {FNCALL F Us})"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   166
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   167
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   168
text{*Reduces equality of equivalence classes to the @{term exprel} relation:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   169
  @{term "(exprel `` {x} = exprel `` {y}) = ((x,y) \<in> exprel)"} *}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   170
lemmas equiv_exprel_iff = eq_equiv_class_iff [OF equiv_exprel UNIV_I UNIV_I]
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   171
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   172
declare equiv_exprel_iff [simp]
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   173
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   174
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   175
text{*All equivalence classes belong to set of representatives*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   176
lemma [simp]: "exprel``{U} \<in> Exp"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   177
by (auto simp add: Exp_def quotient_def intro: exprel_refl)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   178
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   179
lemma inj_on_Abs_Exp: "inj_on Abs_Exp Exp"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   180
apply (rule inj_on_inverseI)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   181
apply (erule Abs_Exp_inverse)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   182
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   183
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   184
text{*Reduces equality on abstractions to equality on representatives*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   185
declare inj_on_Abs_Exp [THEN inj_on_iff, simp]
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   186
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   187
declare Abs_Exp_inverse [simp]
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   188
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   189
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   190
text{*Case analysis on the representation of a exp as an equivalence class.*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   191
lemma eq_Abs_Exp [case_names Abs_Exp, cases type: exp]:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   192
     "(!!U. z = Abs_Exp(exprel``{U}) ==> P) ==> P"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   193
apply (rule Rep_Exp [of z, unfolded Exp_def, THEN quotientE])
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   194
apply (drule arg_cong [where f=Abs_Exp])
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   195
apply (auto simp add: Rep_Exp_inverse intro: exprel_refl)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   196
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   197
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   198
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   199
subsection{*Every list of abstract expressions can be expressed in terms of a
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   200
  list of concrete expressions*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   201
19736
wenzelm
parents: 18460
diff changeset
   202
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21210
diff changeset
   203
  Abs_ExpList :: "freeExp list => exp list" where
19736
wenzelm
parents: 18460
diff changeset
   204
  "Abs_ExpList Xs = map (%U. Abs_Exp(exprel``{U})) Xs"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   205
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   206
lemma Abs_ExpList_Nil [simp]: "Abs_ExpList [] == []"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   207
by (simp add: Abs_ExpList_def)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   208
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   209
lemma Abs_ExpList_Cons [simp]:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   210
     "Abs_ExpList (X#Xs) == Abs_Exp (exprel``{X}) # Abs_ExpList Xs"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   211
by (simp add: Abs_ExpList_def)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   212
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   213
lemma ExpList_rep: "\<exists>Us. z = Abs_ExpList Us"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   214
apply (induct z)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   215
apply (rule_tac [2] z=a in eq_Abs_Exp)
18447
da548623916a removed or modified some instances of [iff]
paulson
parents: 18242
diff changeset
   216
apply (auto simp add: Abs_ExpList_def Cons_eq_map_conv intro: exprel_refl)
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   217
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   218
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   219
lemma eq_Abs_ExpList [case_names Abs_ExpList]:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   220
     "(!!Us. z = Abs_ExpList Us ==> P) ==> P"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   221
by (rule exE [OF ExpList_rep], blast) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   222
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   223
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   224
subsubsection{*Characteristic Equations for the Abstract Constructors*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   225
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   226
lemma Plus: "Plus (Abs_Exp(exprel``{U})) (Abs_Exp(exprel``{V})) = 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   227
             Abs_Exp (exprel``{PLUS U V})"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   228
proof -
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   229
  have "(\<lambda>U V. exprel `` {PLUS U V}) respects2 exprel"
40822
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   230
    by (auto simp add: congruent2_def exprel.PLUS)
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   231
  thus ?thesis
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   232
    by (simp add: Plus_def UN_equiv_class2 [OF equiv_exprel equiv_exprel])
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   233
qed
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   234
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   235
text{*It is not clear what to do with FnCall: it's argument is an abstraction
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   236
of an @{typ "exp list"}. Is it just Nil or Cons? What seems to work best is to
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   237
regard an @{typ "exp list"} as a @{term "listrel exprel"} equivalence class*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   238
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   239
text{*This theorem is easily proved but never used. There's no obvious way
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   240
even to state the analogous result, @{text FnCall_Cons}.*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   241
lemma FnCall_Nil: "FnCall F [] = Abs_Exp (exprel``{FNCALL F []})"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   242
  by (simp add: FnCall_def)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   243
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   244
lemma FnCall_respects: 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   245
     "(\<lambda>Us. exprel `` {FNCALL F Us}) respects (listrel exprel)"
40822
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   246
  by (auto simp add: congruent_def exprel.FNCALL)
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   247
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   248
lemma FnCall_sing:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   249
     "FnCall F [Abs_Exp(exprel``{U})] = Abs_Exp (exprel``{FNCALL F [U]})"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   250
proof -
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   251
  have "(\<lambda>U. exprel `` {FNCALL F [U]}) respects exprel"
40822
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   252
    by (auto simp add: congruent_def FNCALL_Cons listrel.intros)
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   253
  thus ?thesis
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   254
    by (simp add: FnCall_def UN_equiv_class [OF equiv_exprel])
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   255
qed
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   256
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   257
lemma listset_Rep_Exp_Abs_Exp:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   258
     "listset (map Rep_Exp (Abs_ExpList Us)) = listrel exprel `` {Us}";
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   259
  by (induct Us) (simp_all add: listrel_Cons Abs_ExpList_def)
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   260
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   261
lemma FnCall:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   262
     "FnCall F (Abs_ExpList Us) = Abs_Exp (exprel``{FNCALL F Us})"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   263
proof -
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   264
  have "(\<lambda>Us. exprel `` {FNCALL F Us}) respects (listrel exprel)"
40822
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   265
    by (auto simp add: congruent_def exprel.FNCALL)
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   266
  thus ?thesis
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   267
    by (simp add: FnCall_def UN_equiv_class [OF equiv_list_exprel]
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   268
                  listset_Rep_Exp_Abs_Exp)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   269
qed
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   270
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   271
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   272
text{*Establishing this equation is the point of the whole exercise*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   273
theorem Plus_assoc: "Plus X (Plus Y Z) = Plus (Plus X Y) Z"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   274
by (cases X, cases Y, cases Z, simp add: Plus exprel.ASSOC)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   275
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   276
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   277
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   278
subsection{*The Abstract Function to Return the Set of Variables*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   279
19736
wenzelm
parents: 18460
diff changeset
   280
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21210
diff changeset
   281
  vars :: "exp \<Rightarrow> nat set" where
19736
wenzelm
parents: 18460
diff changeset
   282
  "vars X = (\<Union>U \<in> Rep_Exp X. freevars U)"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   283
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   284
lemma vars_respects: "freevars respects exprel"
40822
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   285
by (auto simp add: congruent_def exprel_imp_eq_freevars) 
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   286
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   287
text{*The extension of the function @{term vars} to lists*}
39246
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   288
primrec vars_list :: "exp list \<Rightarrow> nat set" where
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   289
  "vars_list []    = {}"
9e58f0499f57 modernized primrec
haftmann
parents: 32960
diff changeset
   290
| "vars_list(E#Es) = vars E \<union> vars_list Es"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   291
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   292
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   293
text{*Now prove the three equations for @{term vars}*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   294
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   295
lemma vars_Variable [simp]: "vars (Var N) = {N}"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   296
by (simp add: vars_def Var_def 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   297
              UN_equiv_class [OF equiv_exprel vars_respects]) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   298
 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   299
lemma vars_Plus [simp]: "vars (Plus X Y) = vars X \<union> vars Y"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   300
apply (cases X, cases Y) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   301
apply (simp add: vars_def Plus 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   302
                 UN_equiv_class [OF equiv_exprel vars_respects]) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   303
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   304
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   305
lemma vars_FnCall [simp]: "vars (FnCall F Xs) = vars_list Xs"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   306
apply (cases Xs rule: eq_Abs_ExpList) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   307
apply (simp add: FnCall)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   308
apply (induct_tac Us) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   309
apply (simp_all add: vars_def UN_equiv_class [OF equiv_exprel vars_respects])
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   310
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   311
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   312
lemma vars_FnCall_Nil: "vars (FnCall F Nil) = {}" 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   313
by simp
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   314
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   315
lemma vars_FnCall_Cons: "vars (FnCall F (X#Xs)) = vars X \<union> vars_list Xs"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   316
by simp
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   317
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   318
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   319
subsection{*Injectivity Properties of Some Constructors*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   320
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   321
lemma VAR_imp_eq: "VAR m \<sim> VAR n \<Longrightarrow> m = n"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   322
by (drule exprel_imp_eq_freevars, simp)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   323
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   324
text{*Can also be proved using the function @{term vars}*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   325
lemma Var_Var_eq [iff]: "(Var m = Var n) = (m = n)"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   326
by (auto simp add: Var_def exprel_refl dest: VAR_imp_eq)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   327
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   328
lemma VAR_neqv_PLUS: "VAR m \<sim> PLUS X Y \<Longrightarrow> False"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   329
by (drule exprel_imp_eq_freediscrim, simp)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   330
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   331
theorem Var_neq_Plus [iff]: "Var N \<noteq> Plus X Y"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   332
apply (cases X, cases Y) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   333
apply (simp add: Var_def Plus) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   334
apply (blast dest: VAR_neqv_PLUS) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   335
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   336
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   337
theorem Var_neq_FnCall [iff]: "Var N \<noteq> FnCall F Xs"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   338
apply (cases Xs rule: eq_Abs_ExpList) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   339
apply (auto simp add: FnCall Var_def)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   340
apply (drule exprel_imp_eq_freediscrim, simp)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   341
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   342
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   343
subsection{*Injectivity of @{term FnCall}*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   344
19736
wenzelm
parents: 18460
diff changeset
   345
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21210
diff changeset
   346
  "fun" :: "exp \<Rightarrow> nat" where
39910
10097e0a9dbd constant `contents` renamed to `the_elem`
haftmann
parents: 39246
diff changeset
   347
  "fun X = the_elem (\<Union>U \<in> Rep_Exp X. {freefun U})"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   348
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   349
lemma fun_respects: "(%U. {freefun U}) respects exprel"
40822
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   350
by (auto simp add: congruent_def exprel_imp_eq_freefun) 
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   351
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   352
lemma fun_FnCall [simp]: "fun (FnCall F Xs) = F"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   353
apply (cases Xs rule: eq_Abs_ExpList) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   354
apply (simp add: FnCall fun_def UN_equiv_class [OF equiv_exprel fun_respects])
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   355
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   356
19736
wenzelm
parents: 18460
diff changeset
   357
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21210
diff changeset
   358
  args :: "exp \<Rightarrow> exp list" where
39910
10097e0a9dbd constant `contents` renamed to `the_elem`
haftmann
parents: 39246
diff changeset
   359
  "args X = the_elem (\<Union>U \<in> Rep_Exp X. {Abs_ExpList (freeargs U)})"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   360
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   361
text{*This result can probably be generalized to arbitrary equivalence
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   362
relations, but with little benefit here.*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   363
lemma Abs_ExpList_eq:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   364
     "(y, z) \<in> listrel exprel \<Longrightarrow> Abs_ExpList (y) = Abs_ExpList (z)"
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   365
  by (induct set: listrel) simp_all
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   366
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   367
lemma args_respects: "(%U. {Abs_ExpList (freeargs U)}) respects exprel"
40822
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   368
by (auto simp add: congruent_def Abs_ExpList_eq exprel_imp_eqv_freeargs) 
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   369
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   370
lemma args_FnCall [simp]: "args (FnCall F Xs) = Xs"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   371
apply (cases Xs rule: eq_Abs_ExpList) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   372
apply (simp add: FnCall args_def UN_equiv_class [OF equiv_exprel args_respects])
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   373
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   374
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   375
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   376
lemma FnCall_FnCall_eq [iff]:
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   377
     "(FnCall F Xs = FnCall F' Xs') = (F=F' & Xs=Xs')" 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   378
proof
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   379
  assume "FnCall F Xs = FnCall F' Xs'"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   380
  hence "fun (FnCall F Xs) = fun (FnCall F' Xs')" 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   381
    and "args (FnCall F Xs) = args (FnCall F' Xs')" by auto
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   382
  thus "F=F' & Xs=Xs'" by simp
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   383
next
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   384
  assume "F=F' & Xs=Xs'" thus "FnCall F Xs = FnCall F' Xs'" by simp
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   385
qed
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   386
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   387
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   388
subsection{*The Abstract Discriminator*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   389
text{*However, as @{text FnCall_Var_neq_Var} illustrates, we don't need this
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   390
function in order to prove discrimination theorems.*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   391
19736
wenzelm
parents: 18460
diff changeset
   392
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21210
diff changeset
   393
  discrim :: "exp \<Rightarrow> int" where
39910
10097e0a9dbd constant `contents` renamed to `the_elem`
haftmann
parents: 39246
diff changeset
   394
  "discrim X = the_elem (\<Union>U \<in> Rep_Exp X. {freediscrim U})"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   395
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   396
lemma discrim_respects: "(\<lambda>U. {freediscrim U}) respects exprel"
40822
98a5faa5aec0 adaptions to changes in Equiv_Relation.thy
haftmann
parents: 39910
diff changeset
   397
by (auto simp add: congruent_def exprel_imp_eq_freediscrim) 
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   398
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   399
text{*Now prove the four equations for @{term discrim}*}
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   400
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   401
lemma discrim_Var [simp]: "discrim (Var N) = 0"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   402
by (simp add: discrim_def Var_def 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   403
              UN_equiv_class [OF equiv_exprel discrim_respects]) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   404
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   405
lemma discrim_Plus [simp]: "discrim (Plus X Y) = 1"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   406
apply (cases X, cases Y) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   407
apply (simp add: discrim_def Plus 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   408
                 UN_equiv_class [OF equiv_exprel discrim_respects]) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   409
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   410
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   411
lemma discrim_FnCall [simp]: "discrim (FnCall F Xs) = 2"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   412
apply (rule_tac z=Xs in eq_Abs_ExpList) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   413
apply (simp add: discrim_def FnCall
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   414
                 UN_equiv_class [OF equiv_exprel discrim_respects]) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   415
done
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   416
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   417
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   418
text{*The structural induction rule for the abstract type*}
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   419
theorem exp_inducts:
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   420
  assumes V:    "\<And>nat. P1 (Var nat)"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   421
      and P:    "\<And>exp1 exp2. \<lbrakk>P1 exp1; P1 exp2\<rbrakk> \<Longrightarrow> P1 (Plus exp1 exp2)"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   422
      and F:    "\<And>nat list. P2 list \<Longrightarrow> P1 (FnCall nat list)"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   423
      and Nil:  "P2 []"
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   424
      and Cons: "\<And>exp list. \<lbrakk>P1 exp; P2 list\<rbrakk> \<Longrightarrow> P2 (exp # list)"
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   425
  shows "P1 exp" and "P2 list"
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   426
proof -
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   427
  obtain U where exp: "exp = (Abs_Exp (exprel `` {U}))" by (cases exp)
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   428
  obtain Us where list: "list = Abs_ExpList Us" by (rule eq_Abs_ExpList)
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   429
  have "P1 (Abs_Exp (exprel `` {U}))" and "P2 (Abs_ExpList Us)"
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   430
  proof (induct U and Us)
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   431
    case (VAR nat)
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   432
    with V show ?case by (simp add: Var_def) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   433
  next
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   434
    case (PLUS X Y)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   435
    with P [of "Abs_Exp (exprel `` {X})" "Abs_Exp (exprel `` {Y})"]
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   436
    show ?case by (simp add: Plus) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   437
  next
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   438
    case (FNCALL nat list)
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   439
    with F [of "Abs_ExpList list"]
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   440
    show ?case by (simp add: FnCall) 
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   441
  next
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   442
    case Nil_freeExp
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   443
    with Nil show ?case by simp
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   444
  next
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   445
    case Cons_freeExp
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   446
    with Cons show ?case by simp
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   447
  qed
18460
9a1458cb2956 tuned induct proofs;
wenzelm
parents: 18447
diff changeset
   448
  with exp and list show "P1 exp" and "P2 list" by (simp_all only:)
15172
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   449
qed
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   450
73069e033a0b new example of a quotiented nested data type
paulson
parents:
diff changeset
   451
end