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