src/HOL/Lifting.thy
author kuncar
Tue Apr 03 16:26:48 2012 +0200 (2012-04-03)
changeset 47308 9caab698dbe4
child 47325 ec6187036495
permissions -rw-r--r--
new package Lifting - initial commit
kuncar@47308
     1
(*  Title:      HOL/Lifting.thy
kuncar@47308
     2
    Author:     Brian Huffman and Ondrej Kuncar
kuncar@47308
     3
    Author:     Cezary Kaliszyk and Christian Urban
kuncar@47308
     4
*)
kuncar@47308
     5
kuncar@47308
     6
header {* Lifting package *}
kuncar@47308
     7
kuncar@47308
     8
theory Lifting
kuncar@47308
     9
imports Plain Equiv_Relations
kuncar@47308
    10
keywords
kuncar@47308
    11
  "print_quotmaps" "print_quotients" :: diag and
kuncar@47308
    12
  "lift_definition" :: thy_goal and
kuncar@47308
    13
  "setup_lifting" :: thy_decl
kuncar@47308
    14
uses
kuncar@47308
    15
  ("Tools/Lifting/lifting_info.ML")
kuncar@47308
    16
  ("Tools/Lifting/lifting_term.ML")
kuncar@47308
    17
  ("Tools/Lifting/lifting_def.ML")
kuncar@47308
    18
  ("Tools/Lifting/lifting_setup.ML")
kuncar@47308
    19
begin
kuncar@47308
    20
kuncar@47308
    21
subsection {* Function map and function relation *}
kuncar@47308
    22
kuncar@47308
    23
notation map_fun (infixr "--->" 55)
kuncar@47308
    24
kuncar@47308
    25
lemma map_fun_id:
kuncar@47308
    26
  "(id ---> id) = id"
kuncar@47308
    27
  by (simp add: fun_eq_iff)
kuncar@47308
    28
kuncar@47308
    29
definition
kuncar@47308
    30
  fun_rel :: "('a \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('c \<Rightarrow> 'd) \<Rightarrow> bool" (infixr "===>" 55)
kuncar@47308
    31
where
kuncar@47308
    32
  "fun_rel R1 R2 = (\<lambda>f g. \<forall>x y. R1 x y \<longrightarrow> R2 (f x) (g y))"
kuncar@47308
    33
kuncar@47308
    34
lemma fun_relI [intro]:
kuncar@47308
    35
  assumes "\<And>x y. R1 x y \<Longrightarrow> R2 (f x) (g y)"
kuncar@47308
    36
  shows "(R1 ===> R2) f g"
kuncar@47308
    37
  using assms by (simp add: fun_rel_def)
kuncar@47308
    38
kuncar@47308
    39
lemma fun_relE:
kuncar@47308
    40
  assumes "(R1 ===> R2) f g" and "R1 x y"
kuncar@47308
    41
  obtains "R2 (f x) (g y)"
kuncar@47308
    42
  using assms by (simp add: fun_rel_def)
kuncar@47308
    43
kuncar@47308
    44
lemma fun_rel_eq:
kuncar@47308
    45
  shows "((op =) ===> (op =)) = (op =)"
kuncar@47308
    46
  by (auto simp add: fun_eq_iff elim: fun_relE)
kuncar@47308
    47
kuncar@47308
    48
lemma fun_rel_eq_rel:
kuncar@47308
    49
  shows "((op =) ===> R) = (\<lambda>f g. \<forall>x. R (f x) (g x))"
kuncar@47308
    50
  by (simp add: fun_rel_def)
kuncar@47308
    51
kuncar@47308
    52
subsection {* Quotient Predicate *}
kuncar@47308
    53
kuncar@47308
    54
definition
kuncar@47308
    55
  "Quotient R Abs Rep T \<longleftrightarrow>
kuncar@47308
    56
     (\<forall>a. Abs (Rep a) = a) \<and> 
kuncar@47308
    57
     (\<forall>a. R (Rep a) (Rep a)) \<and>
kuncar@47308
    58
     (\<forall>r s. R r s \<longleftrightarrow> R r r \<and> R s s \<and> Abs r = Abs s) \<and>
kuncar@47308
    59
     T = (\<lambda>x y. R x x \<and> Abs x = y)"
kuncar@47308
    60
kuncar@47308
    61
lemma QuotientI:
kuncar@47308
    62
  assumes "\<And>a. Abs (Rep a) = a"
kuncar@47308
    63
    and "\<And>a. R (Rep a) (Rep a)"
kuncar@47308
    64
    and "\<And>r s. R r s \<longleftrightarrow> R r r \<and> R s s \<and> Abs r = Abs s"
kuncar@47308
    65
    and "T = (\<lambda>x y. R x x \<and> Abs x = y)"
kuncar@47308
    66
  shows "Quotient R Abs Rep T"
kuncar@47308
    67
  using assms unfolding Quotient_def by blast
kuncar@47308
    68
kuncar@47308
    69
lemma Quotient_abs_rep:
kuncar@47308
    70
  assumes a: "Quotient R Abs Rep T"
kuncar@47308
    71
  shows "Abs (Rep a) = a"
kuncar@47308
    72
  using a
kuncar@47308
    73
  unfolding Quotient_def
kuncar@47308
    74
  by simp
kuncar@47308
    75
kuncar@47308
    76
lemma Quotient_rep_reflp:
kuncar@47308
    77
  assumes a: "Quotient R Abs Rep T"
kuncar@47308
    78
  shows "R (Rep a) (Rep a)"
kuncar@47308
    79
  using a
kuncar@47308
    80
  unfolding Quotient_def
kuncar@47308
    81
  by blast
kuncar@47308
    82
kuncar@47308
    83
lemma Quotient_rel:
kuncar@47308
    84
  assumes a: "Quotient R Abs Rep T"
kuncar@47308
    85
  shows "R r r \<and> R s s \<and> Abs r = Abs s \<longleftrightarrow> R r s" -- {* orientation does not loop on rewriting *}
kuncar@47308
    86
  using a
kuncar@47308
    87
  unfolding Quotient_def
kuncar@47308
    88
  by blast
kuncar@47308
    89
kuncar@47308
    90
lemma Quotient_cr_rel:
kuncar@47308
    91
  assumes a: "Quotient R Abs Rep T"
kuncar@47308
    92
  shows "T = (\<lambda>x y. R x x \<and> Abs x = y)"
kuncar@47308
    93
  using a
kuncar@47308
    94
  unfolding Quotient_def
kuncar@47308
    95
  by blast
kuncar@47308
    96
kuncar@47308
    97
lemma Quotient_refl1: 
kuncar@47308
    98
  assumes a: "Quotient R Abs Rep T" 
kuncar@47308
    99
  shows "R r s \<Longrightarrow> R r r"
kuncar@47308
   100
  using a unfolding Quotient_def 
kuncar@47308
   101
  by fast
kuncar@47308
   102
kuncar@47308
   103
lemma Quotient_refl2: 
kuncar@47308
   104
  assumes a: "Quotient R Abs Rep T" 
kuncar@47308
   105
  shows "R r s \<Longrightarrow> R s s"
kuncar@47308
   106
  using a unfolding Quotient_def 
kuncar@47308
   107
  by fast
kuncar@47308
   108
kuncar@47308
   109
lemma Quotient_rel_rep:
kuncar@47308
   110
  assumes a: "Quotient R Abs Rep T"
kuncar@47308
   111
  shows "R (Rep a) (Rep b) \<longleftrightarrow> a = b"
kuncar@47308
   112
  using a
kuncar@47308
   113
  unfolding Quotient_def
kuncar@47308
   114
  by metis
kuncar@47308
   115
kuncar@47308
   116
lemma Quotient_rep_abs:
kuncar@47308
   117
  assumes a: "Quotient R Abs Rep T"
kuncar@47308
   118
  shows "R r r \<Longrightarrow> R (Rep (Abs r)) r"
kuncar@47308
   119
  using a unfolding Quotient_def
kuncar@47308
   120
  by blast
kuncar@47308
   121
kuncar@47308
   122
lemma Quotient_rel_abs:
kuncar@47308
   123
  assumes a: "Quotient R Abs Rep T"
kuncar@47308
   124
  shows "R r s \<Longrightarrow> Abs r = Abs s"
kuncar@47308
   125
  using a unfolding Quotient_def
kuncar@47308
   126
  by blast
kuncar@47308
   127
kuncar@47308
   128
lemma Quotient_symp:
kuncar@47308
   129
  assumes a: "Quotient R Abs Rep T"
kuncar@47308
   130
  shows "symp R"
kuncar@47308
   131
  using a unfolding Quotient_def using sympI by (metis (full_types))
kuncar@47308
   132
kuncar@47308
   133
lemma Quotient_transp:
kuncar@47308
   134
  assumes a: "Quotient R Abs Rep T"
kuncar@47308
   135
  shows "transp R"
kuncar@47308
   136
  using a unfolding Quotient_def using transpI by (metis (full_types))
kuncar@47308
   137
kuncar@47308
   138
lemma Quotient_part_equivp:
kuncar@47308
   139
  assumes a: "Quotient R Abs Rep T"
kuncar@47308
   140
  shows "part_equivp R"
kuncar@47308
   141
by (metis Quotient_rep_reflp Quotient_symp Quotient_transp a part_equivpI)
kuncar@47308
   142
kuncar@47308
   143
lemma identity_quotient: "Quotient (op =) id id (op =)"
kuncar@47308
   144
unfolding Quotient_def by simp 
kuncar@47308
   145
kuncar@47308
   146
lemma Quotient_alt_def:
kuncar@47308
   147
  "Quotient R Abs Rep T \<longleftrightarrow>
kuncar@47308
   148
    (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and>
kuncar@47308
   149
    (\<forall>b. T (Rep b) b) \<and>
kuncar@47308
   150
    (\<forall>x y. R x y \<longleftrightarrow> T x (Abs x) \<and> T y (Abs y) \<and> Abs x = Abs y)"
kuncar@47308
   151
apply safe
kuncar@47308
   152
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   153
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   154
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   155
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   156
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   157
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   158
apply (rule QuotientI)
kuncar@47308
   159
apply simp
kuncar@47308
   160
apply metis
kuncar@47308
   161
apply simp
kuncar@47308
   162
apply (rule ext, rule ext, metis)
kuncar@47308
   163
done
kuncar@47308
   164
kuncar@47308
   165
lemma Quotient_alt_def2:
kuncar@47308
   166
  "Quotient R Abs Rep T \<longleftrightarrow>
kuncar@47308
   167
    (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and>
kuncar@47308
   168
    (\<forall>b. T (Rep b) b) \<and>
kuncar@47308
   169
    (\<forall>x y. R x y \<longleftrightarrow> T x (Abs y) \<and> T y (Abs x))"
kuncar@47308
   170
  unfolding Quotient_alt_def by (safe, metis+)
kuncar@47308
   171
kuncar@47308
   172
lemma fun_quotient:
kuncar@47308
   173
  assumes 1: "Quotient R1 abs1 rep1 T1"
kuncar@47308
   174
  assumes 2: "Quotient R2 abs2 rep2 T2"
kuncar@47308
   175
  shows "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2) (T1 ===> T2)"
kuncar@47308
   176
  using assms unfolding Quotient_alt_def2
kuncar@47308
   177
  unfolding fun_rel_def fun_eq_iff map_fun_apply
kuncar@47308
   178
  by (safe, metis+)
kuncar@47308
   179
kuncar@47308
   180
lemma apply_rsp:
kuncar@47308
   181
  fixes f g::"'a \<Rightarrow> 'c"
kuncar@47308
   182
  assumes q: "Quotient R1 Abs1 Rep1 T1"
kuncar@47308
   183
  and     a: "(R1 ===> R2) f g" "R1 x y"
kuncar@47308
   184
  shows "R2 (f x) (g y)"
kuncar@47308
   185
  using a by (auto elim: fun_relE)
kuncar@47308
   186
kuncar@47308
   187
lemma apply_rsp':
kuncar@47308
   188
  assumes a: "(R1 ===> R2) f g" "R1 x y"
kuncar@47308
   189
  shows "R2 (f x) (g y)"
kuncar@47308
   190
  using a by (auto elim: fun_relE)
kuncar@47308
   191
kuncar@47308
   192
lemma apply_rsp'':
kuncar@47308
   193
  assumes "Quotient R Abs Rep T"
kuncar@47308
   194
  and "(R ===> S) f f"
kuncar@47308
   195
  shows "S (f (Rep x)) (f (Rep x))"
kuncar@47308
   196
proof -
kuncar@47308
   197
  from assms(1) have "R (Rep x) (Rep x)" by (rule Quotient_rep_reflp)
kuncar@47308
   198
  then show ?thesis using assms(2) by (auto intro: apply_rsp')
kuncar@47308
   199
qed
kuncar@47308
   200
kuncar@47308
   201
subsection {* Quotient composition *}
kuncar@47308
   202
kuncar@47308
   203
lemma Quotient_compose:
kuncar@47308
   204
  assumes 1: "Quotient R1 Abs1 Rep1 T1"
kuncar@47308
   205
  assumes 2: "Quotient R2 Abs2 Rep2 T2"
kuncar@47308
   206
  shows "Quotient (T1 OO R2 OO conversep T1) (Abs2 \<circ> Abs1) (Rep1 \<circ> Rep2) (T1 OO T2)"
kuncar@47308
   207
proof -
kuncar@47308
   208
  from 1 have Abs1: "\<And>a b. T1 a b \<Longrightarrow> Abs1 a = b"
kuncar@47308
   209
    unfolding Quotient_alt_def by simp
kuncar@47308
   210
  from 1 have Rep1: "\<And>b. T1 (Rep1 b) b"
kuncar@47308
   211
    unfolding Quotient_alt_def by simp
kuncar@47308
   212
  from 2 have Abs2: "\<And>a b. T2 a b \<Longrightarrow> Abs2 a = b"
kuncar@47308
   213
    unfolding Quotient_alt_def by simp
kuncar@47308
   214
  from 2 have Rep2: "\<And>b. T2 (Rep2 b) b"
kuncar@47308
   215
    unfolding Quotient_alt_def by simp
kuncar@47308
   216
  from 2 have R2:
kuncar@47308
   217
    "\<And>x y. R2 x y \<longleftrightarrow> T2 x (Abs2 x) \<and> T2 y (Abs2 y) \<and> Abs2 x = Abs2 y"
kuncar@47308
   218
    unfolding Quotient_alt_def by simp
kuncar@47308
   219
  show ?thesis
kuncar@47308
   220
    unfolding Quotient_alt_def
kuncar@47308
   221
    apply simp
kuncar@47308
   222
    apply safe
kuncar@47308
   223
    apply (drule Abs1, simp)
kuncar@47308
   224
    apply (erule Abs2)
kuncar@47308
   225
    apply (rule pred_compI)
kuncar@47308
   226
    apply (rule Rep1)
kuncar@47308
   227
    apply (rule Rep2)
kuncar@47308
   228
    apply (rule pred_compI, assumption)
kuncar@47308
   229
    apply (drule Abs1, simp)
kuncar@47308
   230
    apply (clarsimp simp add: R2)
kuncar@47308
   231
    apply (rule pred_compI, assumption)
kuncar@47308
   232
    apply (drule Abs1, simp)+
kuncar@47308
   233
    apply (clarsimp simp add: R2)
kuncar@47308
   234
    apply (drule Abs1, simp)+
kuncar@47308
   235
    apply (clarsimp simp add: R2)
kuncar@47308
   236
    apply (rule pred_compI, assumption)
kuncar@47308
   237
    apply (rule pred_compI [rotated])
kuncar@47308
   238
    apply (erule conversepI)
kuncar@47308
   239
    apply (drule Abs1, simp)+
kuncar@47308
   240
    apply (simp add: R2)
kuncar@47308
   241
    done
kuncar@47308
   242
qed
kuncar@47308
   243
kuncar@47308
   244
subsection {* Invariant *}
kuncar@47308
   245
kuncar@47308
   246
definition invariant :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" 
kuncar@47308
   247
  where "invariant R = (\<lambda>x y. R x \<and> x = y)"
kuncar@47308
   248
kuncar@47308
   249
lemma invariant_to_eq:
kuncar@47308
   250
  assumes "invariant P x y"
kuncar@47308
   251
  shows "x = y"
kuncar@47308
   252
using assms by (simp add: invariant_def)
kuncar@47308
   253
kuncar@47308
   254
lemma fun_rel_eq_invariant:
kuncar@47308
   255
  shows "((invariant R) ===> S) = (\<lambda>f g. \<forall>x. R x \<longrightarrow> S (f x) (g x))"
kuncar@47308
   256
by (auto simp add: invariant_def fun_rel_def)
kuncar@47308
   257
kuncar@47308
   258
lemma invariant_same_args:
kuncar@47308
   259
  shows "invariant P x x \<equiv> P x"
kuncar@47308
   260
using assms by (auto simp add: invariant_def)
kuncar@47308
   261
kuncar@47308
   262
lemma copy_type_to_Quotient:
kuncar@47308
   263
  assumes "type_definition Rep Abs UNIV"
kuncar@47308
   264
  and T_def: "T \<equiv> (\<lambda>x y. Abs x = y)"
kuncar@47308
   265
  shows "Quotient (op =) Abs Rep T"
kuncar@47308
   266
proof -
kuncar@47308
   267
  interpret type_definition Rep Abs UNIV by fact
kuncar@47308
   268
  from Abs_inject Rep_inverse T_def show ?thesis by (auto intro!: QuotientI)
kuncar@47308
   269
qed
kuncar@47308
   270
kuncar@47308
   271
lemma copy_type_to_equivp:
kuncar@47308
   272
  fixes Abs :: "'a \<Rightarrow> 'b"
kuncar@47308
   273
  and Rep :: "'b \<Rightarrow> 'a"
kuncar@47308
   274
  assumes "type_definition Rep Abs (UNIV::'a set)"
kuncar@47308
   275
  shows "equivp (op=::'a\<Rightarrow>'a\<Rightarrow>bool)"
kuncar@47308
   276
by (rule identity_equivp)
kuncar@47308
   277
kuncar@47308
   278
lemma invariant_type_to_Quotient:
kuncar@47308
   279
  assumes "type_definition Rep Abs {x. P x}"
kuncar@47308
   280
  and T_def: "T \<equiv> (\<lambda>x y. (invariant P) x x \<and> Abs x = y)"
kuncar@47308
   281
  shows "Quotient (invariant P) Abs Rep T"
kuncar@47308
   282
proof -
kuncar@47308
   283
  interpret type_definition Rep Abs "{x. P x}" by fact
kuncar@47308
   284
  from Rep Abs_inject Rep_inverse T_def show ?thesis by (auto intro!: QuotientI simp: invariant_def)
kuncar@47308
   285
qed
kuncar@47308
   286
kuncar@47308
   287
lemma invariant_type_to_part_equivp:
kuncar@47308
   288
  assumes "type_definition Rep Abs {x. P x}"
kuncar@47308
   289
  shows "part_equivp (invariant P)"
kuncar@47308
   290
proof (intro part_equivpI)
kuncar@47308
   291
  interpret type_definition Rep Abs "{x. P x}" by fact
kuncar@47308
   292
  show "\<exists>x. invariant P x x" using Rep by (auto simp: invariant_def)
kuncar@47308
   293
next
kuncar@47308
   294
  show "symp (invariant P)" by (auto intro: sympI simp: invariant_def)
kuncar@47308
   295
next
kuncar@47308
   296
  show "transp (invariant P)" by (auto intro: transpI simp: invariant_def)
kuncar@47308
   297
qed
kuncar@47308
   298
kuncar@47308
   299
subsection {* ML setup *}
kuncar@47308
   300
kuncar@47308
   301
text {* Auxiliary data for the lifting package *}
kuncar@47308
   302
kuncar@47308
   303
use "Tools/Lifting/lifting_info.ML"
kuncar@47308
   304
setup Lifting_Info.setup
kuncar@47308
   305
kuncar@47308
   306
declare [[map "fun" = (fun_rel, fun_quotient)]]
kuncar@47308
   307
kuncar@47308
   308
use "Tools/Lifting/lifting_term.ML"
kuncar@47308
   309
kuncar@47308
   310
use "Tools/Lifting/lifting_def.ML"
kuncar@47308
   311
kuncar@47308
   312
use "Tools/Lifting/lifting_setup.ML"
kuncar@47308
   313
kuncar@47308
   314
hide_const (open) invariant
kuncar@47308
   315
kuncar@47308
   316
end