src/HOL/Quotient_Examples/Lift_RBT.thy
author huffman
Wed, 23 Nov 2011 07:00:01 +0100
changeset 45615 c05e8209a3aa
parent 45577 33b964e117bd
child 45629 ef08425dd2d5
permissions -rw-r--r--
remove outdated comment
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45577
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
     1
(* Author: Lukas Bulwahn, TU Muenchen *)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
     2
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
     3
header {* Lifting operations of RBT trees *}
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
     4
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
     5
theory Lift_RBT 
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
     6
imports Main "~~/src/HOL/Library/RBT_Impl"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
     7
begin
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
     8
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
     9
subsection {* Type definition *}
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    10
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    11
typedef (open) ('a, 'b) rbt = "{t :: ('a\<Colon>linorder, 'b) RBT_Impl.rbt. is_rbt t}"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    12
  morphisms impl_of RBT
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    13
proof -
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    14
  have "RBT_Impl.Empty \<in> ?rbt" by simp
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    15
  then show ?thesis ..
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    16
qed
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    17
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    18
local_setup {* fn lthy =>
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    19
let
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    20
  val quotients = {qtyp = @{typ "('a, 'b) rbt"}, rtyp = @{typ "('a, 'b) RBT_Impl.rbt"},
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    21
    equiv_rel = @{term "dummy"}, equiv_thm = @{thm refl}}
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    22
  val qty_full_name = @{type_name "rbt"}
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    23
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    24
  fun qinfo phi = Quotient_Info.transform_quotients phi quotients
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    25
  in lthy
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    26
    |> Local_Theory.declaration {syntax = false, pervasive = true}
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    27
        (fn phi => Quotient_Info.update_quotients qty_full_name (qinfo phi)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    28
       #> Quotient_Info.update_abs_rep qty_full_name (Quotient_Info.transform_abs_rep phi
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    29
         {abs = @{term "RBT"}, rep = @{term "impl_of"}}))
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    30
  end
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    31
*}
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    32
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    33
lemma rbt_eq_iff:
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    34
  "t1 = t2 \<longleftrightarrow> impl_of t1 = impl_of t2"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    35
  by (simp add: impl_of_inject)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    36
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    37
lemma rbt_eqI:
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    38
  "impl_of t1 = impl_of t2 \<Longrightarrow> t1 = t2"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    39
  by (simp add: rbt_eq_iff)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    40
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    41
lemma is_rbt_impl_of [simp, intro]:
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    42
  "is_rbt (impl_of t)"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    43
  using impl_of [of t] by simp
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    44
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    45
lemma RBT_impl_of [simp, code abstype]:
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    46
  "RBT (impl_of t) = t"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    47
  by (simp add: impl_of_inverse)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    48
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    49
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    50
subsection {* Primitive operations *}
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    51
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    52
quotient_definition lookup where "lookup :: ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b" is "RBT_Impl.lookup"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    53
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    54
declare lookup_def[unfolded map_fun_def comp_def id_def, code]
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    55
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    56
(* FIXME: some bug in quotient?*)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    57
(*
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    58
quotient_definition empty where "empty :: ('a\<Colon>linorder, 'b) rbt"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    59
is "RBT_Impl.Empty"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    60
*)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    61
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    62
definition empty :: "('a\<Colon>linorder, 'b) rbt" where
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    63
  "empty = RBT RBT_Impl.Empty"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    64
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    65
lemma impl_of_empty [code abstract]:
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    66
  "impl_of empty = RBT_Impl.Empty"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    67
  by (simp add: empty_def RBT_inverse)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    68
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    69
quotient_definition insert where "insert :: 'a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    70
is "RBT_Impl.insert"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    71
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    72
lemma impl_of_insert [code abstract]:
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    73
  "impl_of (insert k v t) = RBT_Impl.insert k v (impl_of t)"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    74
  by (simp add: insert_def RBT_inverse)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    75
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    76
quotient_definition delete where "delete :: 'a\<Colon>linorder \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    77
is "RBT_Impl.delete"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    78
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    79
lemma impl_of_delete [code abstract]:
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    80
  "impl_of (delete k t) = RBT_Impl.delete k (impl_of t)"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    81
  by (simp add: delete_def RBT_inverse)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    82
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    83
(* FIXME: bug in quotient? *)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    84
(*
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    85
quotient_definition entries where "entries :: ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) list"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    86
is "RBT_Impl.entries"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    87
*)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    88
definition entries :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) list" where
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    89
  [code]: "entries t = RBT_Impl.entries (impl_of t)"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    90
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    91
(* FIXME: bug in quotient? *)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    92
(*
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    93
quotient_definition keys where "keys :: ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a list"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    94
is "RBT_Impl.keys"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    95
*)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    96
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    97
quotient_definition "bulkload :: ('a\<Colon>linorder \<times> 'b) list \<Rightarrow> ('a, 'b) rbt"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    98
is "RBT_Impl.bulkload"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
    99
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   100
lemma impl_of_bulkload [code abstract]:
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   101
  "impl_of (bulkload xs) = RBT_Impl.bulkload xs"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   102
  by (simp add: bulkload_def RBT_inverse)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   103
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   104
quotient_definition "map_entry :: 'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   105
is "RBT_Impl.map_entry"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   106
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   107
lemma impl_of_map_entry [code abstract]:
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   108
  "impl_of (map_entry k f t) = RBT_Impl.map_entry k f (impl_of t)"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   109
  by (simp add: map_entry_def RBT_inverse)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   110
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   111
(* Another bug: map is supposed to be a new definition, not using the old one.
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   112
quotient_definition "map :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   113
is "RBT_Impl.map"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   114
*)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   115
(* But this works, and then shows the other bug... *)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   116
(*
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   117
quotient_definition map where "map :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   118
is "RBT_Impl.map"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   119
*)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   120
definition map :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   121
  "map f t = RBT (RBT_Impl.map f (impl_of t))"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   122
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   123
lemma impl_of_map [code abstract]:
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   124
  "impl_of (map f t) = RBT_Impl.map f (impl_of t)"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   125
  by (simp add: map_def RBT_inverse)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   126
(* FIXME: bug?
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   127
quotient_definition fold where "fold :: ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" is "RBT_Impl.fold"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   128
*)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   129
(*FIXME: definition of fold should have the code attribute. *)
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   130
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   131
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" where
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   132
  [code]: "fold f t = RBT_Impl.fold f (impl_of t)"
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   133
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   134
33b964e117bd adding another example for lifting definitions
bulwahn
parents:
diff changeset
   135
end