src/HOL/Library/Quotient_List.thy
author huffman
Tue May 15 12:07:16 2012 +0200 (2012-05-15)
changeset 47929 3465c09222e0
parent 47923 ba9df9685e7c
child 47936 756f30eac792
permissions -rw-r--r--
transfer rules for many more list constants
wenzelm@47455
     1
(*  Title:      HOL/Library/Quotient_List.thy
huffman@47641
     2
    Author:     Cezary Kaliszyk, Christian Urban and Brian Huffman
kaliszyk@35222
     3
*)
wenzelm@35788
     4
wenzelm@35788
     5
header {* Quotient infrastructure for the list type *}
wenzelm@35788
     6
kaliszyk@35222
     7
theory Quotient_List
huffman@47929
     8
imports Main Quotient_Set Quotient_Product Quotient_Option
kaliszyk@35222
     9
begin
kaliszyk@35222
    10
huffman@47641
    11
subsection {* Relator for list type *}
huffman@47641
    12
haftmann@40820
    13
lemma map_id [id_simps]:
haftmann@40820
    14
  "map id = id"
haftmann@46663
    15
  by (fact List.map.id)
kaliszyk@35222
    16
huffman@47641
    17
lemma list_all2_eq [id_simps, relator_eq]:
haftmann@40820
    18
  "list_all2 (op =) = (op =)"
haftmann@40820
    19
proof (rule ext)+
haftmann@40820
    20
  fix xs ys
haftmann@40820
    21
  show "list_all2 (op =) xs ys \<longleftrightarrow> xs = ys"
haftmann@40820
    22
    by (induct xs ys rule: list_induct2') simp_all
haftmann@40820
    23
qed
kaliszyk@35222
    24
huffman@47660
    25
lemma list_all2_OO: "list_all2 (A OO B) = list_all2 A OO list_all2 B"
huffman@47660
    26
proof (intro ext iffI)
huffman@47660
    27
  fix xs ys
huffman@47660
    28
  assume "list_all2 (A OO B) xs ys"
huffman@47660
    29
  thus "(list_all2 A OO list_all2 B) xs ys"
huffman@47660
    30
    unfolding OO_def
huffman@47660
    31
    by (induct, simp, simp add: list_all2_Cons1 list_all2_Cons2, fast)
huffman@47660
    32
next
huffman@47660
    33
  fix xs ys
huffman@47660
    34
  assume "(list_all2 A OO list_all2 B) xs ys"
huffman@47660
    35
  then obtain zs where "list_all2 A xs zs" and "list_all2 B zs ys" ..
huffman@47660
    36
  thus "list_all2 (A OO B) xs ys"
huffman@47660
    37
    by (induct arbitrary: ys, simp, clarsimp simp add: list_all2_Cons1, fast)
huffman@47660
    38
qed
huffman@47660
    39
haftmann@40820
    40
lemma list_reflp:
haftmann@40820
    41
  assumes "reflp R"
haftmann@40820
    42
  shows "reflp (list_all2 R)"
haftmann@40820
    43
proof (rule reflpI)
haftmann@40820
    44
  from assms have *: "\<And>xs. R xs xs" by (rule reflpE)
haftmann@40820
    45
  fix xs
haftmann@40820
    46
  show "list_all2 R xs xs"
haftmann@40820
    47
    by (induct xs) (simp_all add: *)
haftmann@40820
    48
qed
kaliszyk@35222
    49
haftmann@40820
    50
lemma list_symp:
haftmann@40820
    51
  assumes "symp R"
haftmann@40820
    52
  shows "symp (list_all2 R)"
haftmann@40820
    53
proof (rule sympI)
haftmann@40820
    54
  from assms have *: "\<And>xs ys. R xs ys \<Longrightarrow> R ys xs" by (rule sympE)
haftmann@40820
    55
  fix xs ys
haftmann@40820
    56
  assume "list_all2 R xs ys"
haftmann@40820
    57
  then show "list_all2 R ys xs"
haftmann@40820
    58
    by (induct xs ys rule: list_induct2') (simp_all add: *)
haftmann@40820
    59
qed
kaliszyk@35222
    60
haftmann@40820
    61
lemma list_transp:
haftmann@40820
    62
  assumes "transp R"
haftmann@40820
    63
  shows "transp (list_all2 R)"
haftmann@40820
    64
proof (rule transpI)
haftmann@40820
    65
  from assms have *: "\<And>xs ys zs. R xs ys \<Longrightarrow> R ys zs \<Longrightarrow> R xs zs" by (rule transpE)
haftmann@40820
    66
  fix xs ys zs
huffman@45803
    67
  assume "list_all2 R xs ys" and "list_all2 R ys zs"
huffman@45803
    68
  then show "list_all2 R xs zs"
huffman@45803
    69
    by (induct arbitrary: zs) (auto simp: list_all2_Cons1 intro: *)
haftmann@40820
    70
qed
kaliszyk@35222
    71
haftmann@40820
    72
lemma list_equivp [quot_equiv]:
haftmann@40820
    73
  "equivp R \<Longrightarrow> equivp (list_all2 R)"
haftmann@40820
    74
  by (blast intro: equivpI list_reflp list_symp list_transp elim: equivpE)
kaliszyk@35222
    75
huffman@47641
    76
lemma right_total_list_all2 [transfer_rule]:
huffman@47641
    77
  "right_total R \<Longrightarrow> right_total (list_all2 R)"
huffman@47641
    78
  unfolding right_total_def
huffman@47641
    79
  by (rule allI, induct_tac y, simp, simp add: list_all2_Cons2)
huffman@47641
    80
huffman@47641
    81
lemma right_unique_list_all2 [transfer_rule]:
huffman@47641
    82
  "right_unique R \<Longrightarrow> right_unique (list_all2 R)"
huffman@47641
    83
  unfolding right_unique_def
huffman@47641
    84
  apply (rule allI, rename_tac xs, induct_tac xs)
huffman@47641
    85
  apply (auto simp add: list_all2_Cons1)
huffman@47641
    86
  done
huffman@47641
    87
huffman@47641
    88
lemma bi_total_list_all2 [transfer_rule]:
huffman@47641
    89
  "bi_total A \<Longrightarrow> bi_total (list_all2 A)"
huffman@47641
    90
  unfolding bi_total_def
huffman@47641
    91
  apply safe
huffman@47641
    92
  apply (rename_tac xs, induct_tac xs, simp, simp add: list_all2_Cons1)
huffman@47641
    93
  apply (rename_tac ys, induct_tac ys, simp, simp add: list_all2_Cons2)
huffman@47641
    94
  done
huffman@47641
    95
huffman@47641
    96
lemma bi_unique_list_all2 [transfer_rule]:
huffman@47641
    97
  "bi_unique A \<Longrightarrow> bi_unique (list_all2 A)"
huffman@47641
    98
  unfolding bi_unique_def
huffman@47641
    99
  apply (rule conjI)
huffman@47641
   100
  apply (rule allI, rename_tac xs, induct_tac xs)
huffman@47641
   101
  apply (simp, force simp add: list_all2_Cons1)
huffman@47641
   102
  apply (subst (2) all_comm, subst (1) all_comm)
huffman@47641
   103
  apply (rule allI, rename_tac xs, induct_tac xs)
huffman@47641
   104
  apply (simp, force simp add: list_all2_Cons2)
huffman@47641
   105
  done
huffman@47641
   106
huffman@47641
   107
subsection {* Transfer rules for transfer package *}
huffman@47641
   108
huffman@47641
   109
lemma Nil_transfer [transfer_rule]: "(list_all2 A) [] []"
huffman@47641
   110
  by simp
huffman@47641
   111
huffman@47641
   112
lemma Cons_transfer [transfer_rule]:
huffman@47641
   113
  "(A ===> list_all2 A ===> list_all2 A) Cons Cons"
huffman@47641
   114
  unfolding fun_rel_def by simp
huffman@47641
   115
huffman@47641
   116
lemma list_case_transfer [transfer_rule]:
huffman@47641
   117
  "(B ===> (A ===> list_all2 A ===> B) ===> list_all2 A ===> B)
huffman@47641
   118
    list_case list_case"
huffman@47641
   119
  unfolding fun_rel_def by (simp split: list.split)
huffman@47641
   120
huffman@47641
   121
lemma list_rec_transfer [transfer_rule]:
huffman@47641
   122
  "(B ===> (A ===> list_all2 A ===> B ===> B) ===> list_all2 A ===> B)
huffman@47641
   123
    list_rec list_rec"
huffman@47641
   124
  unfolding fun_rel_def by (clarify, erule list_all2_induct, simp_all)
huffman@47641
   125
huffman@47929
   126
lemma tl_transfer [transfer_rule]:
huffman@47929
   127
  "(list_all2 A ===> list_all2 A) tl tl"
huffman@47929
   128
  unfolding tl_def by transfer_prover
huffman@47929
   129
huffman@47929
   130
lemma butlast_transfer [transfer_rule]:
huffman@47929
   131
  "(list_all2 A ===> list_all2 A) butlast butlast"
huffman@47929
   132
  by (rule fun_relI, erule list_all2_induct, auto)
huffman@47929
   133
huffman@47929
   134
lemma set_transfer [transfer_rule]:
huffman@47929
   135
  "(list_all2 A ===> set_rel A) set set"
huffman@47929
   136
  unfolding set_def by transfer_prover
huffman@47929
   137
huffman@47641
   138
lemma map_transfer [transfer_rule]:
huffman@47641
   139
  "((A ===> B) ===> list_all2 A ===> list_all2 B) map map"
huffman@47641
   140
  unfolding List.map_def by transfer_prover
huffman@47641
   141
huffman@47641
   142
lemma append_transfer [transfer_rule]:
huffman@47641
   143
  "(list_all2 A ===> list_all2 A ===> list_all2 A) append append"
huffman@47641
   144
  unfolding List.append_def by transfer_prover
huffman@47641
   145
huffman@47929
   146
lemma rev_transfer [transfer_rule]:
huffman@47929
   147
  "(list_all2 A ===> list_all2 A) rev rev"
huffman@47929
   148
  unfolding List.rev_def by transfer_prover
huffman@47929
   149
huffman@47641
   150
lemma filter_transfer [transfer_rule]:
huffman@47641
   151
  "((A ===> op =) ===> list_all2 A ===> list_all2 A) filter filter"
huffman@47641
   152
  unfolding List.filter_def by transfer_prover
huffman@47641
   153
huffman@47929
   154
lemma fold_transfer [transfer_rule]:
huffman@47929
   155
  "((A ===> B ===> B) ===> list_all2 A ===> B ===> B) fold fold"
huffman@47929
   156
  unfolding List.fold_def by transfer_prover
huffman@47929
   157
huffman@47641
   158
lemma foldr_transfer [transfer_rule]:
huffman@47641
   159
  "((A ===> B ===> B) ===> list_all2 A ===> B ===> B) foldr foldr"
huffman@47641
   160
  unfolding List.foldr_def by transfer_prover
huffman@47641
   161
huffman@47641
   162
lemma foldl_transfer [transfer_rule]:
huffman@47641
   163
  "((B ===> A ===> B) ===> B ===> list_all2 A ===> B) foldl foldl"
huffman@47641
   164
  unfolding List.foldl_def by transfer_prover
huffman@47641
   165
huffman@47641
   166
lemma concat_transfer [transfer_rule]:
huffman@47641
   167
  "(list_all2 (list_all2 A) ===> list_all2 A) concat concat"
huffman@47641
   168
  unfolding List.concat_def by transfer_prover
huffman@47641
   169
huffman@47641
   170
lemma drop_transfer [transfer_rule]:
huffman@47641
   171
  "(op = ===> list_all2 A ===> list_all2 A) drop drop"
huffman@47641
   172
  unfolding List.drop_def by transfer_prover
huffman@47641
   173
huffman@47641
   174
lemma take_transfer [transfer_rule]:
huffman@47641
   175
  "(op = ===> list_all2 A ===> list_all2 A) take take"
huffman@47641
   176
  unfolding List.take_def by transfer_prover
huffman@47641
   177
huffman@47929
   178
lemma list_update_transfer [transfer_rule]:
huffman@47929
   179
  "(list_all2 A ===> op = ===> A ===> list_all2 A) list_update list_update"
huffman@47929
   180
  unfolding list_update_def by transfer_prover
huffman@47929
   181
huffman@47929
   182
lemma takeWhile_transfer [transfer_rule]:
huffman@47929
   183
  "((A ===> op =) ===> list_all2 A ===> list_all2 A) takeWhile takeWhile"
huffman@47929
   184
  unfolding takeWhile_def by transfer_prover
huffman@47929
   185
huffman@47929
   186
lemma dropWhile_transfer [transfer_rule]:
huffman@47929
   187
  "((A ===> op =) ===> list_all2 A ===> list_all2 A) dropWhile dropWhile"
huffman@47929
   188
  unfolding dropWhile_def by transfer_prover
huffman@47929
   189
huffman@47929
   190
lemma zip_transfer [transfer_rule]:
huffman@47929
   191
  "(list_all2 A ===> list_all2 B ===> list_all2 (prod_rel A B)) zip zip"
huffman@47929
   192
  unfolding zip_def by transfer_prover
huffman@47929
   193
huffman@47929
   194
lemma insert_transfer [transfer_rule]:
huffman@47929
   195
  assumes [transfer_rule]: "bi_unique A"
huffman@47929
   196
  shows "(A ===> list_all2 A ===> list_all2 A) List.insert List.insert"
huffman@47929
   197
  unfolding List.insert_def [abs_def] by transfer_prover
huffman@47929
   198
huffman@47929
   199
lemma find_transfer [transfer_rule]:
huffman@47929
   200
  "((A ===> op =) ===> list_all2 A ===> option_rel A) List.find List.find"
huffman@47929
   201
  unfolding List.find_def by transfer_prover
huffman@47929
   202
huffman@47929
   203
lemma remove1_transfer [transfer_rule]:
huffman@47929
   204
  assumes [transfer_rule]: "bi_unique A"
huffman@47929
   205
  shows "(A ===> list_all2 A ===> list_all2 A) remove1 remove1"
huffman@47929
   206
  unfolding remove1_def by transfer_prover
huffman@47929
   207
huffman@47929
   208
lemma removeAll_transfer [transfer_rule]:
huffman@47929
   209
  assumes [transfer_rule]: "bi_unique A"
huffman@47929
   210
  shows "(A ===> list_all2 A ===> list_all2 A) removeAll removeAll"
huffman@47929
   211
  unfolding removeAll_def by transfer_prover
huffman@47929
   212
huffman@47929
   213
lemma distinct_transfer [transfer_rule]:
huffman@47929
   214
  assumes [transfer_rule]: "bi_unique A"
huffman@47929
   215
  shows "(list_all2 A ===> op =) distinct distinct"
huffman@47929
   216
  unfolding distinct_def by transfer_prover
huffman@47929
   217
huffman@47929
   218
lemma remdups_transfer [transfer_rule]:
huffman@47929
   219
  assumes [transfer_rule]: "bi_unique A"
huffman@47929
   220
  shows "(list_all2 A ===> list_all2 A) remdups remdups"
huffman@47929
   221
  unfolding remdups_def by transfer_prover
huffman@47929
   222
huffman@47929
   223
lemma replicate_transfer [transfer_rule]:
huffman@47929
   224
  "(op = ===> A ===> list_all2 A) replicate replicate"
huffman@47929
   225
  unfolding replicate_def by transfer_prover
huffman@47929
   226
huffman@47641
   227
lemma length_transfer [transfer_rule]:
huffman@47641
   228
  "(list_all2 A ===> op =) length length"
huffman@47641
   229
  unfolding list_size_overloaded_def by transfer_prover
huffman@47641
   230
huffman@47929
   231
lemma rotate1_transfer [transfer_rule]:
huffman@47929
   232
  "(list_all2 A ===> list_all2 A) rotate1 rotate1"
huffman@47929
   233
  unfolding rotate1_def by transfer_prover
huffman@47929
   234
huffman@47929
   235
lemma funpow_transfer [transfer_rule]: (* FIXME: move to Transfer.thy *)
huffman@47929
   236
  "(op = ===> (A ===> A) ===> (A ===> A)) compow compow"
huffman@47929
   237
  unfolding funpow_def by transfer_prover
huffman@47929
   238
huffman@47929
   239
lemma rotate_transfer [transfer_rule]:
huffman@47929
   240
  "(op = ===> list_all2 A ===> list_all2 A) rotate rotate"
huffman@47929
   241
  unfolding rotate_def [abs_def] by transfer_prover
huffman@47641
   242
huffman@47641
   243
lemma list_all2_transfer [transfer_rule]:
huffman@47641
   244
  "((A ===> B ===> op =) ===> list_all2 A ===> list_all2 B ===> op =)
huffman@47641
   245
    list_all2 list_all2"
huffman@47929
   246
  apply (subst (4) list_all2_def [abs_def])
huffman@47929
   247
  apply (subst (3) list_all2_def [abs_def])
huffman@47929
   248
  apply transfer_prover
huffman@47641
   249
  done
huffman@47641
   250
huffman@47929
   251
lemma sublist_transfer [transfer_rule]:
huffman@47929
   252
  "(list_all2 A ===> set_rel (op =) ===> list_all2 A) sublist sublist"
huffman@47929
   253
  unfolding sublist_def [abs_def] by transfer_prover
huffman@47929
   254
huffman@47929
   255
lemma partition_transfer [transfer_rule]:
huffman@47929
   256
  "((A ===> op =) ===> list_all2 A ===> prod_rel (list_all2 A) (list_all2 A))
huffman@47929
   257
    partition partition"
huffman@47929
   258
  unfolding partition_def by transfer_prover
huffman@47650
   259
huffman@47923
   260
lemma lists_transfer [transfer_rule]:
huffman@47923
   261
  "(set_rel A ===> set_rel (list_all2 A)) lists lists"
huffman@47923
   262
  apply (rule fun_relI, rule set_relI)
huffman@47923
   263
  apply (erule lists.induct, simp)
huffman@47923
   264
  apply (simp only: set_rel_def list_all2_Cons1, metis lists.Cons)
huffman@47923
   265
  apply (erule lists.induct, simp)
huffman@47923
   266
  apply (simp only: set_rel_def list_all2_Cons2, metis lists.Cons)
huffman@47923
   267
  done
huffman@47923
   268
huffman@47929
   269
lemma set_Cons_transfer [transfer_rule]:
huffman@47929
   270
  "(set_rel A ===> set_rel (list_all2 A) ===> set_rel (list_all2 A))
huffman@47929
   271
    set_Cons set_Cons"
huffman@47929
   272
  unfolding fun_rel_def set_rel_def set_Cons_def
huffman@47929
   273
  apply safe
huffman@47929
   274
  apply (simp add: list_all2_Cons1, fast)
huffman@47929
   275
  apply (simp add: list_all2_Cons2, fast)
huffman@47929
   276
  done
huffman@47929
   277
huffman@47929
   278
lemma listset_transfer [transfer_rule]:
huffman@47929
   279
  "(list_all2 (set_rel A) ===> set_rel (list_all2 A)) listset listset"
huffman@47929
   280
  unfolding listset_def by transfer_prover
huffman@47929
   281
huffman@47929
   282
lemma null_transfer [transfer_rule]:
huffman@47929
   283
  "(list_all2 A ===> op =) List.null List.null"
huffman@47929
   284
  unfolding fun_rel_def List.null_def by auto
huffman@47929
   285
huffman@47929
   286
lemma list_all_transfer [transfer_rule]:
huffman@47929
   287
  "((A ===> op =) ===> list_all2 A ===> op =) list_all list_all"
huffman@47929
   288
  unfolding list_all_iff [abs_def] by transfer_prover
huffman@47929
   289
huffman@47929
   290
lemma list_ex_transfer [transfer_rule]:
huffman@47929
   291
  "((A ===> op =) ===> list_all2 A ===> op =) list_ex list_ex"
huffman@47929
   292
  unfolding list_ex_iff [abs_def] by transfer_prover
huffman@47929
   293
huffman@47929
   294
lemma splice_transfer [transfer_rule]:
huffman@47929
   295
  "(list_all2 A ===> list_all2 A ===> list_all2 A) splice splice"
huffman@47929
   296
  apply (rule fun_relI, erule list_all2_induct, simp add: fun_rel_def, simp)
huffman@47929
   297
  apply (rule fun_relI)
huffman@47929
   298
  apply (erule_tac xs=x in list_all2_induct, simp, simp add: fun_rel_def)
huffman@47929
   299
  done
huffman@47929
   300
huffman@47641
   301
subsection {* Setup for lifting package *}
huffman@47641
   302
kuncar@47777
   303
lemma Quotient_list[quot_map]:
huffman@47641
   304
  assumes "Quotient R Abs Rep T"
huffman@47641
   305
  shows "Quotient (list_all2 R) (map Abs) (map Rep) (list_all2 T)"
huffman@47641
   306
proof (unfold Quotient_alt_def, intro conjI allI impI)
huffman@47641
   307
  from assms have 1: "\<And>x y. T x y \<Longrightarrow> Abs x = y"
huffman@47641
   308
    unfolding Quotient_alt_def by simp
huffman@47641
   309
  fix xs ys assume "list_all2 T xs ys" thus "map Abs xs = ys"
huffman@47641
   310
    by (induct, simp, simp add: 1)
huffman@47641
   311
next
huffman@47641
   312
  from assms have 2: "\<And>x. T (Rep x) x"
huffman@47641
   313
    unfolding Quotient_alt_def by simp
huffman@47641
   314
  fix xs show "list_all2 T (map Rep xs) xs"
huffman@47641
   315
    by (induct xs, simp, simp add: 2)
huffman@47641
   316
next
huffman@47641
   317
  from assms have 3: "\<And>x y. R x y \<longleftrightarrow> T x (Abs x) \<and> T y (Abs y) \<and> Abs x = Abs y"
huffman@47641
   318
    unfolding Quotient_alt_def by simp
huffman@47641
   319
  fix xs ys show "list_all2 R xs ys \<longleftrightarrow> list_all2 T xs (map Abs xs) \<and>
huffman@47641
   320
    list_all2 T ys (map Abs ys) \<and> map Abs xs = map Abs ys"
huffman@47641
   321
    by (induct xs ys rule: list_induct2', simp_all, metis 3)
huffman@47641
   322
qed
huffman@47641
   323
huffman@47641
   324
lemma list_invariant_commute [invariant_commute]:
huffman@47641
   325
  "list_all2 (Lifting.invariant P) = Lifting.invariant (list_all P)"
huffman@47641
   326
  apply (simp add: fun_eq_iff list_all2_def list_all_iff Lifting.invariant_def Ball_def) 
huffman@47641
   327
  apply (intro allI) 
huffman@47641
   328
  apply (induct_tac rule: list_induct2') 
huffman@47641
   329
  apply simp_all 
huffman@47641
   330
  apply metis
huffman@47641
   331
done
huffman@47641
   332
huffman@47641
   333
subsection {* Rules for quotient package *}
huffman@47641
   334
kuncar@47308
   335
lemma list_quotient3 [quot_thm]:
kuncar@47308
   336
  assumes "Quotient3 R Abs Rep"
kuncar@47308
   337
  shows "Quotient3 (list_all2 R) (map Abs) (map Rep)"
kuncar@47308
   338
proof (rule Quotient3I)
kuncar@47308
   339
  from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient3_abs_rep)
haftmann@40820
   340
  then show "\<And>xs. map Abs (map Rep xs) = xs" by (simp add: comp_def)
haftmann@40820
   341
next
kuncar@47308
   342
  from assms have "\<And>x y. R (Rep x) (Rep y) \<longleftrightarrow> x = y" by (rule Quotient3_rel_rep)
haftmann@40820
   343
  then show "\<And>xs. list_all2 R (map Rep xs) (map Rep xs)"
haftmann@40820
   344
    by (simp add: list_all2_map1 list_all2_map2 list_all2_eq)
haftmann@40820
   345
next
haftmann@40820
   346
  fix xs ys
kuncar@47308
   347
  from assms have "\<And>x y. R x x \<and> R y y \<and> Abs x = Abs y \<longleftrightarrow> R x y" by (rule Quotient3_rel)
haftmann@40820
   348
  then show "list_all2 R xs ys \<longleftrightarrow> list_all2 R xs xs \<and> list_all2 R ys ys \<and> map Abs xs = map Abs ys"
haftmann@40820
   349
    by (induct xs ys rule: list_induct2') auto
haftmann@40820
   350
qed
kaliszyk@35222
   351
kuncar@47308
   352
declare [[mapQ3 list = (list_all2, list_quotient3)]]
kuncar@47094
   353
haftmann@40820
   354
lemma cons_prs [quot_preserve]:
kuncar@47308
   355
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222
   356
  shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
kuncar@47308
   357
  by (auto simp add: fun_eq_iff comp_def Quotient3_abs_rep [OF q])
kaliszyk@35222
   358
haftmann@40820
   359
lemma cons_rsp [quot_respect]:
kuncar@47308
   360
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@37492
   361
  shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
haftmann@40463
   362
  by auto
kaliszyk@35222
   363
haftmann@40820
   364
lemma nil_prs [quot_preserve]:
kuncar@47308
   365
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222
   366
  shows "map Abs [] = []"
kaliszyk@35222
   367
  by simp
kaliszyk@35222
   368
haftmann@40820
   369
lemma nil_rsp [quot_respect]:
kuncar@47308
   370
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@37492
   371
  shows "list_all2 R [] []"
kaliszyk@35222
   372
  by simp
kaliszyk@35222
   373
kaliszyk@35222
   374
lemma map_prs_aux:
kuncar@47308
   375
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   376
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   377
  shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
kaliszyk@35222
   378
  by (induct l)
kuncar@47308
   379
     (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222
   380
haftmann@40820
   381
lemma map_prs [quot_preserve]:
kuncar@47308
   382
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   383
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   384
  shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
kaliszyk@36216
   385
  and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
haftmann@40463
   386
  by (simp_all only: fun_eq_iff map_prs_aux[OF a b] comp_def)
kuncar@47308
   387
    (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
haftmann@40463
   388
haftmann@40820
   389
lemma map_rsp [quot_respect]:
kuncar@47308
   390
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   391
  and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492
   392
  shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
kaliszyk@37492
   393
  and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
huffman@47641
   394
  unfolding list_all2_eq [symmetric] by (rule map_transfer)+
kaliszyk@35222
   395
kaliszyk@35222
   396
lemma foldr_prs_aux:
kuncar@47308
   397
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   398
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   399
  shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
kuncar@47308
   400
  by (induct l) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222
   401
haftmann@40820
   402
lemma foldr_prs [quot_preserve]:
kuncar@47308
   403
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   404
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   405
  shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
haftmann@40463
   406
  apply (simp add: fun_eq_iff)
haftmann@40463
   407
  by (simp only: fun_eq_iff foldr_prs_aux[OF a b])
kaliszyk@35222
   408
     (simp)
kaliszyk@35222
   409
kaliszyk@35222
   410
lemma foldl_prs_aux:
kuncar@47308
   411
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   412
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   413
  shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
kuncar@47308
   414
  by (induct l arbitrary:e) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222
   415
haftmann@40820
   416
lemma foldl_prs [quot_preserve]:
kuncar@47308
   417
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   418
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   419
  shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
haftmann@40463
   420
  by (simp add: fun_eq_iff foldl_prs_aux [OF a b])
kaliszyk@35222
   421
kaliszyk@35222
   422
(* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
kaliszyk@35222
   423
lemma foldl_rsp[quot_respect]:
kuncar@47308
   424
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   425
  and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492
   426
  shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_all2 R2 ===> R1) foldl foldl"
huffman@47641
   427
  by (rule foldl_transfer)
kaliszyk@35222
   428
kaliszyk@35222
   429
lemma foldr_rsp[quot_respect]:
kuncar@47308
   430
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   431
  and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492
   432
  shows "((R1 ===> R2 ===> R2) ===> list_all2 R1 ===> R2 ===> R2) foldr foldr"
huffman@47641
   433
  by (rule foldr_transfer)
kaliszyk@35222
   434
kaliszyk@37492
   435
lemma list_all2_rsp:
kaliszyk@36154
   436
  assumes r: "\<forall>x y. R x y \<longrightarrow> (\<forall>a b. R a b \<longrightarrow> S x a = T y b)"
kaliszyk@37492
   437
  and l1: "list_all2 R x y"
kaliszyk@37492
   438
  and l2: "list_all2 R a b"
kaliszyk@37492
   439
  shows "list_all2 S x a = list_all2 T y b"
huffman@45803
   440
  using l1 l2
huffman@45803
   441
  by (induct arbitrary: a b rule: list_all2_induct,
huffman@45803
   442
    auto simp: list_all2_Cons1 list_all2_Cons2 r)
kaliszyk@36154
   443
haftmann@40820
   444
lemma [quot_respect]:
kaliszyk@37492
   445
  "((R ===> R ===> op =) ===> list_all2 R ===> list_all2 R ===> op =) list_all2 list_all2"
huffman@47641
   446
  by (rule list_all2_transfer)
kaliszyk@36154
   447
haftmann@40820
   448
lemma [quot_preserve]:
kuncar@47308
   449
  assumes a: "Quotient3 R abs1 rep1"
kaliszyk@37492
   450
  shows "((abs1 ---> abs1 ---> id) ---> map rep1 ---> map rep1 ---> id) list_all2 = list_all2"
nipkow@39302
   451
  apply (simp add: fun_eq_iff)
kaliszyk@36154
   452
  apply clarify
kaliszyk@36154
   453
  apply (induct_tac xa xb rule: list_induct2')
kuncar@47308
   454
  apply (simp_all add: Quotient3_abs_rep[OF a])
kaliszyk@36154
   455
  done
kaliszyk@36154
   456
haftmann@40820
   457
lemma [quot_preserve]:
kuncar@47308
   458
  assumes a: "Quotient3 R abs1 rep1"
kaliszyk@37492
   459
  shows "(list_all2 ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
kuncar@47308
   460
  by (induct l m rule: list_induct2') (simp_all add: Quotient3_rel_rep[OF a])
kaliszyk@36154
   461
kaliszyk@37492
   462
lemma list_all2_find_element:
kaliszyk@36276
   463
  assumes a: "x \<in> set a"
kaliszyk@37492
   464
  and b: "list_all2 R a b"
kaliszyk@36276
   465
  shows "\<exists>y. (y \<in> set b \<and> R x y)"
huffman@45803
   466
  using b a by induct auto
kaliszyk@36276
   467
kaliszyk@37492
   468
lemma list_all2_refl:
kaliszyk@35222
   469
  assumes a: "\<And>x y. R x y = (R x = R y)"
kaliszyk@37492
   470
  shows "list_all2 R x x"
kaliszyk@35222
   471
  by (induct x) (auto simp add: a)
kaliszyk@35222
   472
kaliszyk@35222
   473
end