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