src/HOL/Datatype_Examples/Compat.thy
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 58442 039b54d9b5c4
child 58889 5b7a9633cfa8
permissions -rw-r--r--
specialized specification: avoid trivial instances
blanchet@58309
     1
(*  Title:      HOL/Datatype_Examples/Compat.thy
blanchet@57634
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@57634
     3
    Copyright   2014
blanchet@57634
     4
blanchet@57634
     5
Tests for compatibility with the old datatype package.
blanchet@57634
     6
*)
blanchet@57634
     7
blanchet@58354
     8
header {* Tests for Compatibility with the Old Datatype Package *}
blanchet@57634
     9
blanchet@56454
    10
theory Compat
blanchet@58372
    11
imports "~~/src/HOL/Library/Old_Datatype"
blanchet@56454
    12
begin
blanchet@56454
    13
blanchet@58354
    14
subsection {* Viewing and Registering New-Style Datatypes as Old-Style Ones *}
blanchet@58125
    15
blanchet@58354
    16
ML {*
blanchet@58124
    17
fun check_len n xs label =
blanchet@58124
    18
  length xs = n orelse error ("Expected length " ^ string_of_int (length xs) ^ " for " ^ label);
blanchet@58124
    19
blanchet@58124
    20
fun check_lens (n1, n2, n3) (xs1, xs2, xs3) =
blanchet@58124
    21
  check_len n1 xs1 "old" andalso check_len n2 xs2 "unfold" andalso check_len n3 xs3 "keep";
blanchet@58124
    22
blanchet@58124
    23
fun get_descrs thy lens T_name =
blanchet@58124
    24
  (these (Option.map #descr (Old_Datatype_Data.get_info thy T_name)),
blanchet@58354
    25
   these (Option.map #descr (BNF_LFP_Compat.get_info thy [] T_name)),
blanchet@58354
    26
   these (Option.map #descr (BNF_LFP_Compat.get_info thy [BNF_LFP_Compat.Keep_Nesting] T_name)))
blanchet@58124
    27
  |> tap (check_lens lens);
blanchet@58354
    28
*}
blanchet@58124
    29
blanchet@58305
    30
old_datatype 'a old_lst = Old_Nl | Old_Cns 'a "'a old_lst"
blanchet@58124
    31
blanchet@58392
    32
text {*
blanchet@58392
    33
A few tests to make sure that @{text old_datatype} works as expected:
blanchet@58392
    34
*}
blanchet@58392
    35
blanchet@58392
    36
primrec old_len :: "'a old_lst \<Rightarrow> nat" where
blanchet@58392
    37
  "old_len Old_Nl = 0"
blanchet@58392
    38
| "old_len (Old_Cns _ xs) = Suc (old_len xs)"
blanchet@58392
    39
blanchet@58392
    40
export_code old_len checking SML OCaml? Haskell? Scala
blanchet@58392
    41
blanchet@58392
    42
lemma "Old_Nl = Old_Cns x xs"
blanchet@58442
    43
  nitpick (* [expect = genuine] *)
blanchet@58392
    44
  quickcheck [exhaustive, expect = counterexample]
blanchet@58392
    45
  quickcheck [random, expect = counterexample]
blanchet@58442
    46
  quickcheck [narrowing (* , expect = counterexample *)]
blanchet@58392
    47
  oops
blanchet@58392
    48
blanchet@58392
    49
lemma "old_len xs = size xs"
blanchet@58392
    50
  by (induct xs) auto
blanchet@58392
    51
blanchet@58354
    52
ML {* get_descrs @{theory} (1, 1, 1) @{type_name old_lst} *}
blanchet@58124
    53
blanchet@58310
    54
datatype 'a lst = Nl | Cns 'a "'a lst"
blanchet@58124
    55
blanchet@58354
    56
ML {* get_descrs @{theory} (0, 1, 1) @{type_name lst} *}
blanchet@58124
    57
blanchet@56454
    58
datatype_compat lst
blanchet@56454
    59
blanchet@58354
    60
ML {* get_descrs @{theory} (1, 1, 1) @{type_name lst} *}
blanchet@58124
    61
blanchet@58310
    62
datatype 'b w = W | W' "'b w \<times> 'b list"
blanchet@58124
    63
blanchet@58358
    64
ML {* get_descrs @{theory} (0, 1, 1) @{type_name w} *}
blanchet@58358
    65
blanchet@56454
    66
datatype_compat w
blanchet@56454
    67
blanchet@58358
    68
ML {* get_descrs @{theory} (2, 2, 1) @{type_name w} *}
blanchet@58124
    69
blanchet@58310
    70
datatype ('c, 'b) s = L 'c | R 'b
blanchet@58124
    71
blanchet@58354
    72
ML {* get_descrs @{theory} (0, 1, 1) @{type_name s} *}
blanchet@58124
    73
blanchet@58310
    74
datatype 'd x = X | X' "('d x lst, 'd list) s"
blanchet@58124
    75
blanchet@58354
    76
ML {* get_descrs @{theory} (0, 1, 1) @{type_name x} *}
blanchet@58124
    77
blanchet@56454
    78
datatype_compat s
blanchet@58124
    79
blanchet@58354
    80
ML {* get_descrs @{theory} (1, 1, 1) @{type_name s} *}
blanchet@58354
    81
ML {* get_descrs @{theory} (0, 1, 1) @{type_name x} *}
blanchet@58124
    82
blanchet@56454
    83
datatype_compat x
blanchet@56454
    84
blanchet@58354
    85
ML {* get_descrs @{theory} (3, 3, 1) @{type_name x} *}
blanchet@58124
    86
blanchet@58216
    87
thm x.induct x.rec
blanchet@58216
    88
thm compat_x.induct compat_x.rec
blanchet@58216
    89
blanchet@58310
    90
datatype 'a tttre = TTTre 'a "'a tttre lst lst lst"
blanchet@58124
    91
blanchet@58354
    92
ML {* get_descrs @{theory} (0, 1, 1) @{type_name tttre} *}
blanchet@58124
    93
blanchet@56454
    94
datatype_compat tttre
blanchet@56454
    95
blanchet@58354
    96
ML {* get_descrs @{theory} (4, 4, 1) @{type_name tttre} *}
blanchet@58124
    97
blanchet@58216
    98
thm tttre.induct tttre.rec
blanchet@58216
    99
thm compat_tttre.induct compat_tttre.rec
blanchet@58216
   100
blanchet@58310
   101
datatype 'a ftre = FEmp | FTre "'a \<Rightarrow> 'a ftre lst"
blanchet@58124
   102
blanchet@58354
   103
ML {* get_descrs @{theory} (0, 1, 1) @{type_name ftre} *}
blanchet@58124
   104
blanchet@56454
   105
datatype_compat ftre
blanchet@56454
   106
blanchet@58354
   107
ML {* get_descrs @{theory} (2, 2, 1) @{type_name ftre} *}
blanchet@58124
   108
blanchet@58216
   109
thm ftre.induct ftre.rec
blanchet@58216
   110
thm compat_ftre.induct compat_ftre.rec
blanchet@58216
   111
blanchet@58310
   112
datatype 'a btre = BTre 'a "'a btre lst" "'a btre lst"
blanchet@58124
   113
blanchet@58354
   114
ML {* get_descrs @{theory} (0, 1, 1) @{type_name btre} *}
blanchet@58124
   115
blanchet@56454
   116
datatype_compat btre
blanchet@56454
   117
blanchet@58354
   118
ML {* get_descrs @{theory} (3, 3, 1) @{type_name btre} *}
blanchet@58124
   119
blanchet@58216
   120
thm btre.induct btre.rec
blanchet@58216
   121
thm compat_btre.induct compat_btre.rec
blanchet@58216
   122
blanchet@58310
   123
datatype 'a foo = Foo | Foo' 'a "'a bar" and 'a bar = Bar | Bar' 'a "'a foo"
blanchet@58124
   124
blanchet@58354
   125
ML {* get_descrs @{theory} (0, 2, 2) @{type_name foo} *}
blanchet@58354
   126
ML {* get_descrs @{theory} (0, 2, 2) @{type_name bar} *}
blanchet@58124
   127
blanchet@56454
   128
datatype_compat foo bar
blanchet@56454
   129
blanchet@58354
   130
ML {* get_descrs @{theory} (2, 2, 2) @{type_name foo} *}
blanchet@58354
   131
ML {* get_descrs @{theory} (2, 2, 2) @{type_name bar} *}
blanchet@58124
   132
blanchet@58310
   133
datatype 'a tre = Tre 'a "'a tre lst"
blanchet@58124
   134
blanchet@58354
   135
ML {* get_descrs @{theory} (0, 1, 1) @{type_name tre} *}
blanchet@58124
   136
blanchet@56454
   137
datatype_compat tre
blanchet@56454
   138
blanchet@58354
   139
ML {* get_descrs @{theory} (2, 2, 1) @{type_name tre} *}
blanchet@58124
   140
blanchet@58216
   141
thm tre.induct tre.rec
blanchet@58216
   142
thm compat_tre.induct compat_tre.rec
blanchet@56454
   143
blanchet@58310
   144
datatype 'a f = F 'a and 'a g = G 'a
blanchet@58124
   145
blanchet@58354
   146
ML {* get_descrs @{theory} (0, 2, 2) @{type_name f} *}
blanchet@58354
   147
ML {* get_descrs @{theory} (0, 2, 2) @{type_name g} *}
blanchet@58124
   148
blanchet@58310
   149
datatype h = H "h f" | H'
blanchet@58124
   150
blanchet@58354
   151
ML {* get_descrs @{theory} (0, 1, 1) @{type_name h} *}
blanchet@58124
   152
blanchet@56454
   153
datatype_compat f g
blanchet@58124
   154
blanchet@58354
   155
ML {* get_descrs @{theory} (2, 2, 2) @{type_name f} *}
blanchet@58354
   156
ML {* get_descrs @{theory} (2, 2, 2) @{type_name g} *}
blanchet@58354
   157
ML {* get_descrs @{theory} (0, 1, 1) @{type_name h} *}
blanchet@58124
   158
blanchet@56454
   159
datatype_compat h
blanchet@56454
   160
blanchet@58354
   161
ML {* get_descrs @{theory} (3, 3, 1) @{type_name h} *}
blanchet@58124
   162
blanchet@58216
   163
thm h.induct h.rec
blanchet@58216
   164
thm compat_h.induct compat_h.rec
blanchet@58216
   165
blanchet@58310
   166
datatype myunit = MyUnity
blanchet@58124
   167
blanchet@58354
   168
ML {* get_descrs @{theory} (0, 1, 1) @{type_name myunit} *}
blanchet@58124
   169
blanchet@56454
   170
datatype_compat myunit
blanchet@56454
   171
blanchet@58354
   172
ML {* get_descrs @{theory} (1, 1, 1) @{type_name myunit} *}
blanchet@58124
   173
blanchet@58310
   174
datatype mylist = MyNil | MyCons nat mylist
blanchet@58124
   175
blanchet@58354
   176
ML {* get_descrs @{theory} (0, 1, 1) @{type_name mylist} *}
blanchet@58124
   177
blanchet@56454
   178
datatype_compat mylist
blanchet@56454
   179
blanchet@58354
   180
ML {* get_descrs @{theory} (1, 1, 1) @{type_name mylist} *}
blanchet@58124
   181
blanchet@58310
   182
datatype foo' = FooNil | FooCons bar' foo' and bar' = Bar
blanchet@58124
   183
blanchet@58354
   184
ML {* get_descrs @{theory} (0, 2, 2) @{type_name foo'} *}
blanchet@58354
   185
ML {* get_descrs @{theory} (0, 2, 2) @{type_name bar'} *}
blanchet@58124
   186
blanchet@56454
   187
datatype_compat bar' foo'
blanchet@56454
   188
blanchet@58354
   189
ML {* get_descrs @{theory} (2, 2, 2) @{type_name foo'} *}
blanchet@58354
   190
ML {* get_descrs @{theory} (2, 2, 2) @{type_name bar'} *}
blanchet@58124
   191
blanchet@58305
   192
old_datatype funky = Funky "funky tre" | Funky'
blanchet@58124
   193
blanchet@58354
   194
ML {* get_descrs @{theory} (3, 3, 3) @{type_name funky} *}
blanchet@58124
   195
blanchet@58305
   196
old_datatype fnky = Fnky "nat tre"
blanchet@56454
   197
blanchet@58354
   198
ML {* get_descrs @{theory} (1, 1, 1) @{type_name fnky} *}
blanchet@58124
   199
blanchet@58310
   200
datatype tree = Tree "tree foo"
blanchet@58124
   201
blanchet@58354
   202
ML {* get_descrs @{theory} (0, 1, 1) @{type_name tree} *}
blanchet@58124
   203
traytel@56488
   204
datatype_compat tree
blanchet@56454
   205
blanchet@58354
   206
ML {* get_descrs @{theory} (3, 3, 1) @{type_name tree} *}
blanchet@58125
   207
blanchet@58216
   208
thm tree.induct tree.rec
blanchet@58216
   209
thm compat_tree.induct compat_tree.rec
blanchet@58125
   210
blanchet@58216
   211
blanchet@58354
   212
subsection {* Creating New-Style Datatypes Using Old-Style Interfaces *}
blanchet@58125
   213
blanchet@58354
   214
ML {*
blanchet@58125
   215
val l_specs =
blanchet@58125
   216
  [((@{binding l}, [("'a", @{sort type})], NoSyn),
blanchet@58125
   217
   [(@{binding N}, [], NoSyn),
blanchet@58354
   218
    (@{binding C}, [@{typ 'a}, Type (Sign.full_name @{theory} @{binding l}, [@{typ 'a}])],
blanchet@58354
   219
     NoSyn)])];
blanchet@58354
   220
*}
blanchet@58125
   221
blanchet@58354
   222
setup {* snd o BNF_LFP_Compat.add_datatype [] l_specs *}
blanchet@58125
   223
blanchet@58354
   224
ML {* get_descrs @{theory} (1, 1, 1) @{type_name l} *}
blanchet@58125
   225
blanchet@58125
   226
thm l.exhaust l.map l.induct l.rec l.size
blanchet@58125
   227
blanchet@58354
   228
ML {*
blanchet@58125
   229
val t_specs =
blanchet@58125
   230
  [((@{binding t}, [("'b", @{sort type})], NoSyn),
blanchet@58354
   231
   [(@{binding T}, [@{typ 'b},
blanchet@58354
   232
       Type (@{type_name l}, [Type (Sign.full_name @{theory} @{binding t}, [@{typ 'b}])])],
blanchet@58354
   233
     NoSyn)])];
blanchet@58354
   234
*}
blanchet@58125
   235
blanchet@58354
   236
setup {* snd o BNF_LFP_Compat.add_datatype [] t_specs *}
blanchet@58125
   237
blanchet@58354
   238
ML {* get_descrs @{theory} (2, 2, 1) @{type_name t} *}
blanchet@58125
   239
blanchet@58125
   240
thm t.exhaust t.map t.induct t.rec t.size
blanchet@58216
   241
thm compat_t.induct compat_t.rec
blanchet@58216
   242
blanchet@58354
   243
ML {*
blanchet@58216
   244
val ft_specs =
blanchet@58216
   245
  [((@{binding ft}, [("'a", @{sort type})], NoSyn),
blanchet@58216
   246
   [(@{binding FT0}, [], NoSyn),
blanchet@58216
   247
    (@{binding FT}, [@{typ 'a} --> Type (Sign.full_name @{theory} @{binding ft}, [@{typ 'a}])],
blanchet@58216
   248
     NoSyn)])];
blanchet@58354
   249
*}
blanchet@58216
   250
blanchet@58354
   251
setup {* snd o BNF_LFP_Compat.add_datatype [] ft_specs *}
blanchet@58216
   252
blanchet@58354
   253
ML {* get_descrs @{theory} (1, 1, 1) @{type_name ft} *}
blanchet@58216
   254
blanchet@58216
   255
thm ft.exhaust ft.induct ft.rec ft.size
blanchet@58216
   256
thm compat_ft.induct compat_ft.rec
blanchet@56454
   257
blanchet@56454
   258
end