src/HOL/Quotient_Examples/Lift_FSet.thy
author wenzelm
Wed Jun 22 10:09:20 2016 +0200 (2016-06-22)
changeset 63343 fb5d8a50c641
parent 63167 0909deb8059b
child 67399 eab6ce8368fa
permissions -rw-r--r--
bundle lifting_syntax;
huffman@47660
     1
(*  Title:      HOL/Quotient_Examples/Lift_FSet.thy
huffman@47660
     2
    Author:     Brian Huffman, TU Munich
huffman@47660
     3
*)
huffman@47660
     4
wenzelm@63167
     5
section \<open>Lifting and transfer with a finite set type\<close>
huffman@47660
     6
huffman@47660
     7
theory Lift_FSet
kuncar@53013
     8
imports Main
huffman@47660
     9
begin
huffman@47660
    10
wenzelm@63167
    11
subsection \<open>Equivalence relation and quotient type definition\<close>
huffman@47660
    12
huffman@47660
    13
definition list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
huffman@47660
    14
  where [simp]: "list_eq xs ys \<longleftrightarrow> set xs = set ys"
huffman@47660
    15
huffman@47660
    16
lemma reflp_list_eq: "reflp list_eq"
huffman@47660
    17
  unfolding reflp_def by simp
huffman@47660
    18
huffman@47660
    19
lemma symp_list_eq: "symp list_eq"
huffman@47660
    20
  unfolding symp_def by simp
huffman@47660
    21
huffman@47660
    22
lemma transp_list_eq: "transp list_eq"
huffman@47660
    23
  unfolding transp_def by simp
huffman@47660
    24
huffman@47660
    25
lemma equivp_list_eq: "equivp list_eq"
huffman@47660
    26
  by (intro equivpI reflp_list_eq symp_list_eq transp_list_eq)
huffman@47660
    27
wenzelm@63343
    28
context includes lifting_syntax
kuncar@53013
    29
begin
kuncar@53013
    30
kuncar@51376
    31
lemma list_eq_transfer [transfer_rule]:
kuncar@51376
    32
  assumes [transfer_rule]: "bi_unique A"
kuncar@51376
    33
  shows "(list_all2 A ===> list_all2 A ===> op =) list_eq list_eq"
kuncar@51376
    34
  unfolding list_eq_def [abs_def] by transfer_prover
kuncar@51376
    35
kuncar@51376
    36
quotient_type 'a fset = "'a list" / "list_eq" parametric list_eq_transfer
huffman@47660
    37
  by (rule equivp_list_eq)
huffman@47660
    38
wenzelm@63167
    39
subsection \<open>Lifted constant definitions\<close>
huffman@47660
    40
traytel@58961
    41
lift_definition fnil :: "'a fset" ("{||}") is "[]" parametric list.ctr_transfer(1) .
huffman@47660
    42
traytel@58961
    43
lift_definition fcons :: "'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is Cons parametric list.ctr_transfer(2)
huffman@47660
    44
  by simp
huffman@47660
    45
kuncar@51376
    46
lift_definition fappend :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is append parametric append_transfer
huffman@47660
    47
  by simp
huffman@47660
    48
traytel@58961
    49
lift_definition fmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset" is map parametric list.map_transfer
huffman@47660
    50
  by simp
huffman@47660
    51
kuncar@51376
    52
lift_definition ffilter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is filter parametric filter_transfer
huffman@47660
    53
  by simp
huffman@47660
    54
traytel@58961
    55
lift_definition fset :: "'a fset \<Rightarrow> 'a set" is set parametric list.set_transfer
huffman@47660
    56
  by simp
huffman@47660
    57
wenzelm@63167
    58
text \<open>Constants with nested types (like concat) yield a more
wenzelm@63167
    59
  complicated proof obligation.\<close>
huffman@47660
    60
huffman@47660
    61
lemma list_all2_cr_fset:
huffman@47660
    62
  "list_all2 cr_fset xs ys \<longleftrightarrow> map abs_fset xs = ys"
huffman@47660
    63
  unfolding cr_fset_def
huffman@47660
    64
  apply safe
huffman@47660
    65
  apply (erule list_all2_induct, simp, simp)
huffman@47660
    66
  apply (simp add: list_all2_map2 List.list_all2_refl)
huffman@47660
    67
  done
huffman@47660
    68
huffman@47660
    69
lemma abs_fset_eq_iff: "abs_fset xs = abs_fset ys \<longleftrightarrow> list_eq xs ys"
huffman@47660
    70
  using Quotient_rel [OF Quotient_fset] by simp
huffman@47660
    71
kuncar@51376
    72
lift_definition fconcat :: "'a fset fset \<Rightarrow> 'a fset" is concat parametric concat_transfer
kuncar@55732
    73
proof (simp only: fset.pcr_cr_eq)
huffman@47660
    74
  fix xss yss :: "'a list list"
huffman@47660
    75
  assume "(list_all2 cr_fset OO list_eq OO (list_all2 cr_fset)\<inverse>\<inverse>) xss yss"
huffman@47660
    76
  then obtain uss vss where
huffman@47660
    77
    "list_all2 cr_fset xss uss" and "list_eq uss vss" and
huffman@47660
    78
    "list_all2 cr_fset yss vss" by clarsimp
huffman@47660
    79
  hence "list_eq (map abs_fset xss) (map abs_fset yss)"
huffman@47660
    80
    unfolding list_all2_cr_fset by simp
huffman@47660
    81
  thus "list_eq (concat xss) (concat yss)"
huffman@47660
    82
    apply (simp add: set_eq_iff image_def)
huffman@47660
    83
    apply safe
huffman@47660
    84
    apply (rename_tac xs, drule_tac x="abs_fset xs" in spec)
huffman@47660
    85
    apply (drule iffD1, fast, clarsimp simp add: abs_fset_eq_iff, fast)
huffman@47660
    86
    apply (rename_tac xs, drule_tac x="abs_fset xs" in spec)
huffman@47660
    87
    apply (drule iffD2, fast, clarsimp simp add: abs_fset_eq_iff, fast)
huffman@47660
    88
    done
huffman@47660
    89
qed
huffman@47660
    90
kuncar@53013
    91
lemma member_transfer:
kuncar@53013
    92
  assumes [transfer_rule]: "bi_unique A"
kuncar@53013
    93
  shows "(A ===> list_all2 A ===> op=) (\<lambda>x xs. x \<in> set xs) (\<lambda>x xs. x \<in> set xs)"
kuncar@53013
    94
by transfer_prover
kuncar@53013
    95
kuncar@53013
    96
end
kuncar@53013
    97
traytel@51410
    98
syntax
traytel@51410
    99
  "_insert_fset"     :: "args => 'a fset"  ("{|(_)|}")
traytel@51410
   100
traytel@51410
   101
translations
traytel@51410
   102
  "{|x, xs|}" == "CONST fcons x {|xs|}"
traytel@51410
   103
  "{|x|}"     == "CONST fcons x {||}"
traytel@51410
   104
kuncar@51411
   105
lift_definition fmember :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<in>|" 50) is "\<lambda>x xs. x \<in> set xs"
kuncar@51411
   106
   parametric member_transfer by simp
traytel@51410
   107
traytel@51410
   108
abbreviation notin_fset :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<notin>|" 50) where
traytel@51410
   109
  "x |\<notin>| S \<equiv> \<not> (x |\<in>| S)"
traytel@51410
   110
kuncar@51411
   111
lemma fmember_fmap[simp]: "a |\<in>| fmap f X = (\<exists>b. b |\<in>| X \<and> a = f b)"
traytel@51410
   112
  by transfer auto
traytel@51410
   113
wenzelm@63167
   114
text \<open>We can export code:\<close>
kuncar@47937
   115
kuncar@51411
   116
export_code fnil fcons fappend fmap ffilter fset fmember in SML
kuncar@47937
   117
wenzelm@63167
   118
subsection \<open>Using transfer with type \<open>fset\<close>\<close>
huffman@47660
   119
wenzelm@63167
   120
text \<open>The correspondence relation \<open>cr_fset\<close> can only relate
wenzelm@63167
   121
  \<open>list\<close> and \<open>fset\<close> types with the same element type.
wenzelm@63167
   122
  To relate nested types like \<open>'a list list\<close> and
wenzelm@63167
   123
  \<open>'a fset fset\<close>, we define a parameterized version of the
wenzelm@63167
   124
  correspondence relation, \<open>pcr_fset\<close>.\<close>
kuncar@50227
   125
kuncar@50227
   126
thm pcr_fset_def
huffman@47660
   127
wenzelm@63167
   128
subsection \<open>Transfer examples\<close>
huffman@47660
   129
wenzelm@63167
   130
text \<open>The \<open>transfer\<close> method replaces equality on \<open>fset\<close> with the \<open>list_eq\<close> relation on lists, which is
wenzelm@63167
   131
  logically equivalent.\<close>
huffman@47660
   132
huffman@47660
   133
lemma "fmap f (fmap g xs) = fmap (f \<circ> g) xs"
huffman@47660
   134
  apply transfer
huffman@47660
   135
  apply simp
huffman@47660
   136
  done
huffman@47660
   137
wenzelm@63167
   138
text \<open>The \<open>transfer'\<close> variant can replace equality on \<open>fset\<close> with equality on \<open>list\<close>, which is logically stronger
wenzelm@63167
   139
  but sometimes more convenient.\<close>
huffman@47660
   140
huffman@47660
   141
lemma "fmap f (fmap g xs) = fmap (f \<circ> g) xs"
huffman@52354
   142
  using map_map [Transfer.transferred] .
huffman@47660
   143
huffman@47660
   144
lemma "ffilter p (fmap f xs) = fmap f (ffilter (p \<circ> f) xs)"
huffman@52354
   145
  using filter_map [Transfer.transferred] .
huffman@47660
   146
huffman@47660
   147
lemma "ffilter p (ffilter q xs) = ffilter (\<lambda>x. q x \<and> p x) xs"
huffman@52354
   148
  using filter_filter [Transfer.transferred] .
huffman@47660
   149
huffman@47660
   150
lemma "fset (fcons x xs) = insert x (fset xs)"
blanchet@57816
   151
  using list.set(2) [Transfer.transferred] .
huffman@47660
   152
huffman@47660
   153
lemma "fset (fappend xs ys) = fset xs \<union> fset ys"
huffman@52354
   154
  using set_append [Transfer.transferred] .
huffman@47660
   155
huffman@47660
   156
lemma "fset (fconcat xss) = (\<Union>xs\<in>fset xss. fset xs)"
huffman@52354
   157
  using set_concat [Transfer.transferred] .
huffman@47660
   158
huffman@47660
   159
lemma "\<forall>x\<in>fset xs. f x = g x \<Longrightarrow> fmap f xs = fmap g xs"
huffman@47660
   160
  apply transfer
huffman@47660
   161
  apply (simp cong: map_cong del: set_map)
huffman@47660
   162
  done
huffman@47660
   163
huffman@47660
   164
lemma "fnil = fconcat xss \<longleftrightarrow> (\<forall>xs\<in>fset xss. xs = fnil)"
huffman@47660
   165
  apply transfer
huffman@47660
   166
  apply simp
huffman@47660
   167
  done
huffman@47660
   168
huffman@47660
   169
lemma "fconcat (fmap (\<lambda>x. fcons x fnil) xs) = xs"
huffman@52354
   170
  apply transfer
huffman@47660
   171
  apply simp
huffman@47660
   172
  done
huffman@47660
   173
huffman@47660
   174
lemma concat_map_concat: "concat (map concat xsss) = concat (concat xsss)"
huffman@47660
   175
  by (induct xsss, simp_all)
huffman@47660
   176
huffman@47660
   177
lemma "fconcat (fmap fconcat xss) = fconcat (fconcat xss)"
huffman@52354
   178
  using concat_map_concat [Transfer.transferred] .
huffman@47660
   179
huffman@47660
   180
end