Tutorial/Tutorial2.thy
author Christian Urban <urbanc@in.tum.de>
Tue, 31 May 2011 12:22:28 +0100
changeset 2796 3e341af86bbd
parent 2695 e8736c1cdd7f
child 3132 87eca760dcba
permissions -rw-r--r--
fixed the problem with cps-like functions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
     1
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
theory Tutorial2
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
     3
imports Lambda
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
begin
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
     6
section {* Height of a Lambda-Term *}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
     7
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
     8
text {*
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
     9
  The theory Lambda defines the notions of capture-avoiding
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    10
  substitution and the height of lambda terms. 
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    11
*}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    12
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    13
thm height.simps
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    14
thm subst.simps
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    15
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    16
subsection {* EXERCISE 7 *}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    17
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    18
text {* 
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    19
  Lets prove a property about the height of substitutions.
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    20
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    21
  Assume that the height of a lambda-term is always greater 
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    22
  or equal to 1.
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    23
*}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    24
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    25
lemma height_ge_one: 
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    26
  shows "1 \<le> height t"
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    27
by (induct t rule: lam.induct) (auto)
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    28
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    29
text {* Remove the sorries *}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    30
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    31
theorem height_subst:
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    32
  shows "height (t[x ::= t']) \<le> height t - 1 + height t'"
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    33
proof (induct t rule: lam.induct)
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    34
  case (Var y)
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    35
  show "height (Var y[x ::= t']) \<le> height (Var y) - 1 + height t'" sorry
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    36
next    
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    37
  case (App t1 t2)
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    38
  have ih1: "height (t1[x::=t']) \<le> (height t1) - 1 + height t'" by fact
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    39
  have ih2: "height (t2[x::=t']) \<le> (height t2) - 1 + height t'" by fact
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    40
  
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    41
  show "height ((App t1 t2)[x ::= t']) \<le> height (App t1 t2) - 1 + height t'" sorry  
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    42
next
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    43
  case (Lam y t1)
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    44
  have ih: "height (t1[x::=t']) \<le> height t1 - 1 + height t'" by fact
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    45
  -- {* the definition of capture-avoiding substitution *}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    46
  thm subst.simps
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    47
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    48
  show "height ((Lam [y].t1)[x ::= t']) \<le> height (Lam [y].t1) - 1 + height t'" sorry
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    49
qed
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    52
text {*
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    53
  The point is that substitutions can only be moved under a binder
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    54
  provided certain freshness conditions are satisfied. The structural
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    55
  induction above does not say anything about such freshness conditions.
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    56
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    57
  Fortunately, Nominal derives automatically some stronger induction
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    58
  principle for lambda terms which has the usual variable convention
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    59
  build in.
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    60
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    61
  In the proof below, we use this stronger induction principle. The
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    62
  variable and application case are as before.
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    63
*}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    64
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    65
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    66
theorem
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    67
  shows "height (t[x ::= t']) \<le> height t - 1 + height t'"
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    68
proof (nominal_induct t avoiding: x t' rule: lam.strong_induct)
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    69
  case (Var y)
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    70
  have "1 \<le> height t'" using height_ge_one by simp
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    71
  then show "height (Var y[x ::= t']) \<le> height (Var y) - 1 + height t'" by simp
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    72
next    
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    73
  case (App t1 t2)
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    74
  have ih1: "height (t1[x::=t']) \<le> (height t1) - 1 + height t'" 
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    75
  and  ih2: "height (t2[x::=t']) \<le> (height t2) - 1 + height t'" by fact+
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    76
  then show "height ((App t1 t2)[x ::= t']) \<le> height (App t1 t2) - 1 + height t'" by simp  
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    77
next
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    78
  case (Lam y t1)
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    79
  have ih: "height (t1[x::=t']) \<le> height t1 - 1 + height t'" by fact
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    80
  have vc: "atom y \<sharp> x" "atom y \<sharp> t'" by fact+ -- {* usual variable convention *}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    81
  
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    82
  show "height ((Lam [y].t1)[x ::= t']) \<le> height (Lam [y].t1) - 1 + height t'" sorry
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    83
qed
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    84
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    85
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    86
section {* Types and the Weakening Lemma *}
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
nominal_datatype ty =
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  tVar "string"
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
    90
| tArr "ty" "ty" (infixr "\<rightarrow>" 100)
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
text {* 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  Having defined them as nominal datatypes gives us additional
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  definitions and theorems that come with types (see below).
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  We next define the type of typing contexts, a predicate that
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  defines valid contexts (i.e. lists that contain only unique
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  variables) and the typing judgement.
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
*}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
type_synonym ty_ctx = "(name \<times> ty) list"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
inductive
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
  valid :: "ty_ctx \<Rightarrow> bool"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
where
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
  v1[intro]: "valid []"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
| v2[intro]: "\<lbrakk>valid \<Gamma>; atom x \<sharp> \<Gamma>\<rbrakk>\<Longrightarrow> valid ((x, T) # \<Gamma>)"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
inductive
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  typing :: "ty_ctx \<Rightarrow> lam \<Rightarrow> ty \<Rightarrow> bool" ("_ \<turnstile> _ : _" [60, 60, 60] 60) 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
where
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  t_Var[intro]:  "\<lbrakk>valid \<Gamma>; (x, T) \<in> set \<Gamma>\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> Var x : T"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
| t_App[intro]:  "\<lbrakk>\<Gamma> \<turnstile> t1 : T1 \<rightarrow> T2; \<Gamma> \<turnstile> t2 : T1\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> App t1 t2 : T2"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
| t_Lam[intro]:  "\<lbrakk>atom x \<sharp> \<Gamma>; (x, T1) # \<Gamma> \<turnstile> t : T2\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> Lam [x].t : T1 \<rightarrow> T2"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
text {*
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  The predicate atom x \<sharp> \<Gamma>, read as x fresh for \<Gamma>, is defined by 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  Nominal Isabelle. It is defined for lambda-terms, products, lists etc. 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  For example we have:
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
*}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
lemma
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
  fixes x::"name"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
  shows "atom x \<sharp> Lam [x].t"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
  and   "atom x \<sharp> (t1, t2) \<Longrightarrow> atom x \<sharp> App t1 t2"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
  and   "atom x \<sharp> Var y \<Longrightarrow> atom x \<sharp> y" 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
  and   "\<lbrakk>atom x \<sharp> t1; atom x \<sharp> t2\<rbrakk> \<Longrightarrow> atom x \<sharp> (t1, t2)"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
  and   "\<lbrakk>atom x \<sharp> l1; atom x \<sharp> l2\<rbrakk> \<Longrightarrow> atom x \<sharp> (l1 @ l2)"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
  and   "atom x \<sharp> y \<Longrightarrow> x \<noteq> y"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
  by (simp_all add: lam.fresh fresh_append fresh_at_base) 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
text {* 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
  We can also prove that every variable is fresh for a type. 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
*}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
lemma ty_fresh:
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
  fixes x::"name"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
  and   T::"ty"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  shows "atom x \<sharp> T"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
by (induct T rule: ty.induct)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
   (simp_all add: ty.fresh pure_fresh)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
text {* 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
  In order to state the weakening lemma in a convenient form, we 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
  using the following abbreviation. Abbreviations behave like 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
  definitions, except that they are always automatically folded 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
  and unfolded.
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
*}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
abbreviation
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
  "sub_ty_ctx" :: "ty_ctx \<Rightarrow> ty_ctx \<Rightarrow> bool" ("_ \<sqsubseteq> _" [60, 60] 60) 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
where
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
  "\<Gamma>1 \<sqsubseteq> \<Gamma>2 \<equiv> \<forall>x. x \<in> set \<Gamma>1 \<longrightarrow> x \<in> set \<Gamma>2"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   159
subsection {* EXERCISE 8 *}
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
text {*
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
  Fill in the details and give a proof of the weakening lemma. 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
*}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
lemma 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  assumes a: "\<Gamma>1 \<turnstile> t : T"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
  and     b: "valid \<Gamma>2" 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
  and     c: "\<Gamma>1 \<sqsubseteq> \<Gamma>2"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
  shows "\<Gamma>2 \<turnstile> t : T"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
using a b c
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
proof (induct arbitrary: \<Gamma>2)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
  case (t_Var \<Gamma>1 x T)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
  have a1: "valid \<Gamma>1" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  have a2: "(x, T) \<in> set \<Gamma>1" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
  have a3: "valid \<Gamma>2" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
  have a4: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
  show "\<Gamma>2 \<turnstile> Var x : T" sorry
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
next
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
  case (t_Lam x \<Gamma>1 T1 t T2) 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
  have ih: "\<And>\<Gamma>3. \<lbrakk>valid \<Gamma>3; (x, T1) # \<Gamma>1 \<sqsubseteq> \<Gamma>3\<rbrakk> \<Longrightarrow> \<Gamma>3 \<turnstile> t : T2" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
  have a0: "atom x \<sharp> \<Gamma>1" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
  have a1: "valid \<Gamma>2" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
  have a2: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
  show "\<Gamma>2 \<turnstile> Lam [x].t : T1 \<rightarrow> T2" sorry
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   187
qed (auto) -- {* the application case is automatic*}
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
text {* 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
  Despite the frequent claim that the weakening lemma is trivial,
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
  routine or obvious, the proof in the lambda-case does not go 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
  through smoothly. Painful variable renamings seem to be necessary. 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
  But the proof using renamings is horribly complicated (see below). 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
*}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
equivariance valid
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
equivariance typing
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
lemma weakening_NOT_TO_BE_TRIED_AT_HOME: 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
  assumes a: "\<Gamma>1 \<turnstile> t : T"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
  and     b: "valid \<Gamma>2" 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
  and     c: "\<Gamma>1 \<sqsubseteq> \<Gamma>2"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
  shows "\<Gamma>2 \<turnstile> t : T"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
using a b c
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
proof (induct arbitrary: \<Gamma>2)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
  -- {* lambda case *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
  case (t_Lam x \<Gamma>1 T1 t T2) 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
  have fc0: "atom x \<sharp> \<Gamma>1" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
  have ih: "\<And>\<Gamma>3. \<lbrakk>valid \<Gamma>3; (x, T1) # \<Gamma>1 \<sqsubseteq> \<Gamma>3\<rbrakk> \<Longrightarrow> \<Gamma>3 \<turnstile> t : T2" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
  -- {* we choose a fresh variable *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
  obtain c::"name" where fc1: "atom c \<sharp> (x, t, \<Gamma>1, \<Gamma>2)" by (rule obtain_fresh)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
  -- {* we alpha-rename the abstraction *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
  have "Lam [c].((c \<leftrightarrow> x) \<bullet> t) = Lam [x].t" using fc1
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
    by (auto simp add: lam.eq_iff Abs1_eq_iff flip_def)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
  moreover
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
  -- {* we can then alpha rename the goal *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
  have "\<Gamma>2 \<turnstile> Lam [c].((c \<leftrightarrow> x) \<bullet> t) : T1 \<rightarrow> T2" 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
  proof - 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
    -- {* we need to establish *} 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
    -- {* (1)   (x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2) *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
    -- {* (2)   valid ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2)) *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
    have "(1)": "(x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2)" 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
    proof -
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
      have "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
      then have "(c \<leftrightarrow> x) \<bullet> ((x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2))" using fc0 fc1
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
        by (perm_simp) (simp add: flip_fresh_fresh)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
      then show "(x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2)" by (rule permute_boolE)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
    qed
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
    moreover 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
    have "(2)": "valid ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2))" 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
    proof -
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
      have "valid \<Gamma>2" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
      then show "valid ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2))" using fc1
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
        by (auto simp add: fresh_permute_left atom_eqvt valid.eqvt)	
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
    qed
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
    -- {* these two facts give us by induction hypothesis the following *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
    ultimately have "(x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2) \<turnstile> t : T2" using ih by simp 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
    -- {* we now apply renamings to get to our goal *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
    then have "(c \<leftrightarrow> x) \<bullet> ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2) \<turnstile> t : T2)" by (rule permute_boolI)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
    then have "(c, T1) # \<Gamma>2 \<turnstile> ((c \<leftrightarrow> x) \<bullet> t) : T2" using fc1
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
      by (perm_simp) (simp add: flip_fresh_fresh ty_fresh)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
    then show "\<Gamma>2 \<turnstile> Lam [c].((c \<leftrightarrow> x) \<bullet> t) : T1 \<rightarrow> T2" using fc1 by auto
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
  qed
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
  ultimately show "\<Gamma>2 \<turnstile> Lam [x].t : T1 \<rightarrow> T2" by simp
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
qed (auto) -- {* var and app cases, luckily, are automatic *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
text {* 
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   250
  The remedy is to use again a stronger induction principle that
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
  has the usual "variable convention" already build in. The
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   252
  following command derives this induction principle for the typing
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   253
  relation. (We shall explain what happens here later.)
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
*}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
nominal_inductive typing
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
  avoids t_Lam: "x"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
  unfolding fresh_star_def
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
  by (simp_all add: fresh_Pair lam.fresh ty_fresh)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
text {* Compare the two induction principles *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
thm typing.induct
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   264
thm typing.strong_induct -- {* note the additional assumption {atom x} \<sharp> c *}
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
text {* 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
  We can use the stronger induction principle by replacing
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
  the line
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
      proof (induct arbitrary: \<Gamma>2)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
  with 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
  
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
      proof (nominal_induct avoiding: \<Gamma>2 rule: typing.strong_induct)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
  Try now the proof.
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
*}
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   278
 
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   279
subsection {* EXERCISE 9 *} 
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
lemma weakening: 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
  assumes a: "\<Gamma>1 \<turnstile> t : T"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
  and     b: "valid \<Gamma>2" 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
  and     c: "\<Gamma>1 \<sqsubseteq> \<Gamma>2"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
  shows "\<Gamma>2 \<turnstile> t : T"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
using a b c
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
proof (nominal_induct avoiding: \<Gamma>2 rule: typing.strong_induct)
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   288
  case (t_Var \<Gamma>1 x T)  -- {* again the variable case is as before *}
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
  have "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
  moreover  
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
  have "valid \<Gamma>2" by fact 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
  moreover 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
  have "(x, T)\<in> set \<Gamma>1" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
  ultimately show "\<Gamma>2 \<turnstile> Var x : T" by auto
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
next
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
  case (t_Lam x \<Gamma>1 T1 t T2) 
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
  have vc: "atom x \<sharp> \<Gamma>2" by fact  -- {* additional fact afforded by the strong induction *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
  have ih: "\<And>\<Gamma>3. \<lbrakk>valid \<Gamma>3; (x, T1) # \<Gamma>1 \<sqsubseteq> \<Gamma>3\<rbrakk> \<Longrightarrow> \<Gamma>3 \<turnstile> t : T2" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
  have a0: "atom x \<sharp> \<Gamma>1" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
  have a1: "valid \<Gamma>2" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
  have a2: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   302
  
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   303
  show "\<Gamma>2 \<turnstile> Lam [x].t : T1 \<rightarrow> T2" sorry
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
qed (auto) -- {* app case *}
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   307
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   308
section {* Unbind and an Inconsistency Example *}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   309
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   310
text {*
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   311
  You might wonder why we had to discharge some proof
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   312
  obligations in order to obtain the stronger induction
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   313
  principle for the typing relation. (Remember for
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   314
  lambda terms this stronger induction principle is
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   315
  derived automatically.)
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   316
  
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   317
  This proof obligation is really needed, because if we 
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   318
  assume universally a stronger induction principle, then 
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   319
  in some cases we can derive false. This is "shown" below.
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   320
*}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   321
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
inductive
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   324
  unbind :: "lam \<Rightarrow> lam \<Rightarrow> bool" (infixr "\<mapsto>" 60)
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
where
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
  u_Var[intro]: "Var x \<mapsto> Var x"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
| u_App[intro]: "App t1 t2 \<mapsto> App t1 t2"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
| u_Lam[intro]: "t \<mapsto> t' \<Longrightarrow> Lam [x].t \<mapsto> t'"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   330
text {* It is clear that the following judgement holds. *}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   331
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
lemma unbind_simple:
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
  shows "Lam [x].Var x \<mapsto> Var x"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
  by auto
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   336
text {*
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   337
  Now lets derive the strong induction principle for unbind.
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   338
  The point is that we cannot establish the proof obligations,
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   339
  therefore we force Isabelle to accept them by using "sorry".
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   340
*}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   341
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
equivariance unbind
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
nominal_inductive unbind
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
  avoids u_Lam: "x"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
  sorry
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   347
text {*
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   348
  Using the stronger induction principle, we can establish
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   349
  th follwoing bogus property.
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   350
*}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   351
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
lemma unbind_fake:
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
  fixes y::"name"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
  assumes a: "t \<mapsto> t'"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
  and     b: "atom y \<sharp> t"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
  shows "atom y \<sharp> t'"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
using a b
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
proof (nominal_induct avoiding: y rule: unbind.strong_induct)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
  case (u_Lam t t' x y)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
  have ih: "atom y \<sharp> t \<Longrightarrow> atom y \<sharp> t'" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
  have asm: "atom y \<sharp> Lam [x]. t" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
  have fc: "atom x \<sharp> y" by fact
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
  then have in_eq: "x \<noteq> y" by (simp add: fresh_at_base)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
  then have "atom y \<sharp> t" using asm by (simp add: lam.fresh)
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
  then show "atom y \<sharp> t'" using ih by simp
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
qed
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   368
text {*
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   369
  And from this follows the inconsitency:
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   370
*}
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   371
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
lemma
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
  shows "False"
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
proof -
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
  have "atom x \<sharp> Lam [x]. Var x" by (simp add: lam.fresh)
2695
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   376
  moreover 
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   377
  have "Lam [x]. Var x \<mapsto> Var x" using unbind_simple by auto
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   378
  ultimately 
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   379
  have "atom x \<sharp> Var x" using unbind_fake by auto
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   380
  then have "x \<noteq> x" by (simp add: lam.fresh fresh_at_base)
e8736c1cdd7f cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents: 2692
diff changeset
   381
  then show "False" by simp
2689
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
qed
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
ddc05a611005 added unbind example
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
end