src/HOL/Datatype_Examples/Lift_BNF.thy
author haftmann
Thu, 24 Nov 2016 11:33:55 +0100
changeset 64523 49a29161d8ef
parent 63167 0909deb8059b
child 71262 a30278c8585f
permissions -rw-r--r--
clarified NEWS concerning Library/Poly_Deriv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62695
b287b56a6ce5 file header
blanchet
parents: 62137
diff changeset
     1
(*  Title:      HOL/Datatype_Examples/Lift_BNF.thy
b287b56a6ce5 file header
blanchet
parents: 62137
diff changeset
     2
    Author:     Dmitriy Traytel, ETH Zürich
b287b56a6ce5 file header
blanchet
parents: 62137
diff changeset
     3
    Copyright   2015
b287b56a6ce5 file header
blanchet
parents: 62137
diff changeset
     4
b287b56a6ce5 file header
blanchet
parents: 62137
diff changeset
     5
Demonstration of the "lift_bnf" command.
b287b56a6ce5 file header
blanchet
parents: 62137
diff changeset
     6
*)
b287b56a6ce5 file header
blanchet
parents: 62137
diff changeset
     7
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62697
diff changeset
     8
section \<open>Demonstration of the \textbf{lift_bnf} Command\<close>
62695
b287b56a6ce5 file header
blanchet
parents: 62137
diff changeset
     9
60918
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    10
theory Lift_BNF
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    11
imports Main
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    12
begin
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    13
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    14
typedef 'a nonempty_list = "{xs :: 'a list. xs \<noteq> []}"
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    15
  by blast
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    16
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    17
lift_bnf (no_warn_wits) (neset: 'a) nonempty_list
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    18
  for map: nemap rel: nerel
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    19
  by simp_all
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    20
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    21
typedef ('a :: finite, 'b) fin_nonempty_list = "{(xs :: 'a set, ys :: 'b list). ys \<noteq> []}"
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    22
  by blast
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    23
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    24
lift_bnf (dead 'a :: finite, 'b) fin_nonempty_list
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    25
  by auto
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    26
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    27
datatype 'a tree = Leaf | Node 'a "'a tree nonempty_list"
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    28
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    29
record 'a point =
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    30
  xCoord :: 'a
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    31
  yCoord :: 'a
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    32
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    33
copy_bnf ('a, 's) point_ext
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    34
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    35
typedef 'a it = "UNIV :: 'a set"
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    36
  by blast
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    37
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    38
copy_bnf (plugins del: size) 'a it
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    39
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    40
typedef ('a, 'b) T_prod = "UNIV :: ('a \<times> 'b) set"
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    41
  by blast
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    42
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    43
copy_bnf ('a, 'b) T_prod
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    44
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    45
typedef ('a, 'b, 'c) T_func = "UNIV :: ('a \<Rightarrow> 'b * 'c) set"
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    46
  by blast
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    47
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    48
copy_bnf ('a, 'b, 'c) T_func
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    49
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    50
typedef ('a, 'b) sum_copy = "UNIV :: ('a + 'b) set"
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    51
  by blast
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    52
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    53
copy_bnf ('a, 'b) sum_copy
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    54
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    55
typedef ('a, 'b) T_sum = "{Inl x | x. True} :: ('a + 'b) set"
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    56
  by blast
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    57
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    58
lift_bnf (no_warn_wits) ('a, 'b) T_sum [wits: "Inl :: 'a \<Rightarrow> 'a + 'b"]
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    59
  by (auto simp: map_sum_def sum_set_defs split: sum.splits)
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    60
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    61
typedef ('key, 'value) alist = "{xs :: ('key \<times> 'value) list. (distinct \<circ> map fst) xs}"
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    62
  morphisms impl_of Alist
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    63
proof
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    64
  show "[] \<in> {xs. (distinct o map fst) xs}"
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    65
    by simp
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    66
qed
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    67
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    68
lift_bnf (dead 'k, 'v) alist [wits: "Nil :: ('k \<times> 'v) list"]
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    69
  by simp_all
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    70
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    71
typedef 'a myopt = "{X :: 'a set. finite X \<and> card X \<le> 1}" by (rule exI[of _ "{}"]) auto
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    72
lemma myopt_type_def: "type_definition
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    73
  (\<lambda>X. if card (Rep_myopt X) = 1 then Some (the_elem (Rep_myopt X)) else None)
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    74
  (\<lambda>x. Abs_myopt (case x of Some x \<Rightarrow> {x} | _ \<Rightarrow> {}))
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    75
  (UNIV :: 'a option set)"
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    76
  apply unfold_locales
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    77
    apply (auto simp: Abs_myopt_inverse dest!: card_eq_SucD split: option.splits)
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    78
   apply (metis Rep_myopt_inverse)
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    79
  apply (metis One_nat_def Rep_myopt Rep_myopt_inverse Suc_le_mono card_0_eq le0 le_antisym mem_Collect_eq nat.exhaust)
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    80
  done
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    81
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    82
copy_bnf 'a myopt via myopt_type_def
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    83
62137
b8dc1fd7d900 more careful witness' type analysis
traytel
parents: 60918
diff changeset
    84
typedef ('k, 'v) fmap = "{M :: ('k \<rightharpoonup> 'v). finite (dom M)}"
b8dc1fd7d900 more careful witness' type analysis
traytel
parents: 60918
diff changeset
    85
  by (rule exI[of _ Map.empty]) simp_all
b8dc1fd7d900 more careful witness' type analysis
traytel
parents: 60918
diff changeset
    86
b8dc1fd7d900 more careful witness' type analysis
traytel
parents: 60918
diff changeset
    87
lift_bnf (dead 'k, 'v) fmap [wits: "Map.empty :: 'k \<Rightarrow> 'v option"]
b8dc1fd7d900 more careful witness' type analysis
traytel
parents: 60918
diff changeset
    88
  by auto
b8dc1fd7d900 more careful witness' type analysis
traytel
parents: 60918
diff changeset
    89
60918
4ceef1592e8c new command for lifting BNF structure over typedefs
traytel
parents:
diff changeset
    90
end