author | blanchet |
Mon, 15 Sep 2014 10:49:07 +0200 | |
changeset 58335 | a5a3b576fcfb |
parent 58315 | 6d8458bc6e27 |
child 58340 | 5f6f48e87de6 |
permissions | -rw-r--r-- |
55061 | 1 |
(* Title: HOL/Tools/BNF/bnf_lfp_compat.ML |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
2 |
Author: Jasmin Blanchette, TU Muenchen |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
3 |
Copyright 2013, 2014 |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
4 |
|
58147 | 5 |
Compatibility layer with the old datatype package. Partly based on |
58119 | 6 |
|
7 |
Title: HOL/Tools/Old_Datatype/old_datatype_data.ML |
|
8 |
Author: Stefan Berghofer, TU Muenchen |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
9 |
*) |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
10 |
|
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
11 |
signature BNF_LFP_COMPAT = |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
12 |
sig |
58125 | 13 |
datatype nesting_preference = Keep_Nesting | Unfold_Nesting |
58123
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
14 |
|
58125 | 15 |
val get_all: theory -> nesting_preference -> Old_Datatype_Aux.info Symtab.table |
16 |
val get_info: theory -> nesting_preference -> string -> Old_Datatype_Aux.info option |
|
17 |
val the_info: theory -> nesting_preference -> string -> Old_Datatype_Aux.info |
|
58129
3ec65a7f2b50
ported Refute to use new datatypes when possible
blanchet
parents:
58126
diff
changeset
|
18 |
val the_spec: theory -> string -> (string * sort) list * (string * typ list) list |
58125 | 19 |
val the_descr: theory -> nesting_preference -> string list -> |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
20 |
Old_Datatype_Aux.descr * (string * sort) list * string list * string |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
21 |
* (string list * string list) * (typ list * typ list) |
58129
3ec65a7f2b50
ported Refute to use new datatypes when possible
blanchet
parents:
58126
diff
changeset
|
22 |
val get_constrs: theory -> string -> (string * typ) list option |
58186 | 23 |
val interpretation: string -> nesting_preference -> |
58123
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
24 |
(Old_Datatype_Aux.config -> string list -> theory -> theory) -> theory -> theory |
58125 | 25 |
val datatype_compat: string list -> local_theory -> local_theory |
26 |
val datatype_compat_global: string list -> theory -> theory |
|
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
27 |
val datatype_compat_cmd: string list -> local_theory -> local_theory |
58125 | 28 |
val add_datatype: nesting_preference -> Old_Datatype_Aux.spec list -> theory -> |
29 |
string list * theory |
|
58300 | 30 |
val add_datatype_dead: nesting_preference -> Old_Datatype_Aux.spec list -> theory -> |
31 |
string list * theory |
|
58126 | 32 |
val add_primrec: (binding * typ option * mixfix) list -> (Attrib.binding * term) list -> |
33 |
local_theory -> (term list * thm list) * local_theory |
|
58147 | 34 |
val add_primrec_global: (binding * typ option * mixfix) list -> |
35 |
(Attrib.binding * term) list -> theory -> (term list * thm list) * theory |
|
36 |
val add_primrec_overloaded: (string * (string * typ) * bool) list -> |
|
37 |
(binding * typ option * mixfix) list -> (Attrib.binding * term) list -> theory -> |
|
38 |
(term list * thm list) * theory |
|
39 |
val add_primrec_simple: ((binding * typ) * mixfix) list -> term list -> |
|
58220 | 40 |
local_theory -> (string list * (term list * thm list)) * local_theory |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
41 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
42 |
|
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
43 |
structure BNF_LFP_Compat : BNF_LFP_COMPAT = |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
44 |
struct |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
45 |
|
54006 | 46 |
open Ctr_Sugar |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
47 |
open BNF_Util |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
48 |
open BNF_Tactics |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
49 |
open BNF_FP_Util |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
50 |
open BNF_FP_Def_Sugar |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
51 |
open BNF_FP_N2M_Sugar |
58123
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
52 |
open BNF_LFP |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
53 |
|
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
54 |
val compat_N = "compat_"; |
58213 | 55 |
val rec_split_N = "rec_split_"; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
56 |
|
58125 | 57 |
datatype nesting_preference = Keep_Nesting | Unfold_Nesting; |
58123
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
58 |
|
58213 | 59 |
fun mk_split_rec_rhs ctxt fpTs Cs (recs as rec1 :: _) = |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
60 |
let |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
61 |
fun repair_rec_arg_args [] [] = [] |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
62 |
| repair_rec_arg_args ((g_T as Type (@{type_name fun}, _)) :: g_Ts) (g :: gs) = |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
63 |
let |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
64 |
val (x_Ts, body_T) = strip_type g_T; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
65 |
in |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
66 |
(case try HOLogic.dest_prodT body_T of |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
67 |
NONE => [g] |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
68 |
| SOME (fst_T, _) => |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
69 |
if member (op =) fpTs fst_T then |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
70 |
let val (xs, _) = mk_Frees "x" x_Ts ctxt in |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
71 |
map (fn mk_proj => fold_rev Term.lambda xs (mk_proj (Term.list_comb (g, xs)))) |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
72 |
[HOLogic.mk_fst, HOLogic.mk_snd] |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
73 |
end |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
74 |
else |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
75 |
[g]) |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
76 |
:: repair_rec_arg_args g_Ts gs |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
77 |
end |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
78 |
| repair_rec_arg_args (g_T :: g_Ts) (g :: gs) = |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
79 |
if member (op =) fpTs g_T then |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
80 |
let |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
81 |
val j = find_index (member (op =) Cs) g_Ts; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
82 |
val h = nth gs j; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
83 |
val g_Ts' = nth_drop j g_Ts; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
84 |
val gs' = nth_drop j gs; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
85 |
in |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
86 |
[g, h] :: repair_rec_arg_args g_Ts' gs' |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
87 |
end |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
88 |
else |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
89 |
[g] :: repair_rec_arg_args g_Ts gs; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
90 |
|
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
91 |
fun repair_back_rec_arg f_T f' = |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
92 |
let |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
93 |
val g_Ts = Term.binder_types f_T; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
94 |
val (gs, _) = mk_Frees "g" g_Ts ctxt; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
95 |
in |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
96 |
fold_rev Term.lambda gs (Term.list_comb (f', |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
97 |
flat_rec_arg_args (repair_rec_arg_args g_Ts gs))) |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
98 |
end; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
99 |
|
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
100 |
val f_Ts = binder_fun_types (fastype_of rec1); |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
101 |
val (fs', _) = mk_Frees "f" (replicate (length f_Ts) Term.dummyT) ctxt; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
102 |
|
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
103 |
fun mk_rec' recx = |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
104 |
fold_rev Term.lambda fs' (Term.list_comb (recx, map2 repair_back_rec_arg f_Ts fs')) |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
105 |
|> Syntax.check_term ctxt; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
106 |
in |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
107 |
map mk_rec' recs |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
108 |
end; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
109 |
|
58213 | 110 |
fun define_split_recs fpTs Cs recs lthy = |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
111 |
let |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
112 |
val b_names = Name.variant_list [] (map base_name_of_typ fpTs); |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
113 |
|
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
114 |
fun mk_binding b_name = |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
115 |
Binding.qualify true (compat_N ^ b_name) |
58213 | 116 |
(Binding.prefix_name rec_split_N (Binding.name b_name)); |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
117 |
|
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
118 |
val bs = map mk_binding b_names; |
58213 | 119 |
val rhss = mk_split_rec_rhs lthy fpTs Cs recs; |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
120 |
in |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
121 |
fold_map3 (define_co_rec_as Least_FP Cs) fpTs bs rhss lthy |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
122 |
end; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
123 |
|
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
124 |
fun mk_split_rec_thmss ctxt Xs ctrXs_Tsss ctrss rec0_thmss (recs as rec1 :: _) rec_defs = |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
125 |
let |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
126 |
val f_Ts = binder_fun_types (fastype_of rec1); |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
127 |
val (fs, _) = mk_Frees "f" f_Ts ctxt; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
128 |
val frecs = map (fn recx => Term.list_comb (recx, fs)) recs; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
129 |
|
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
130 |
val Xs_frecs = Xs ~~ frecs; |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
131 |
val fss = unflat ctrss fs; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
132 |
|
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
133 |
fun mk_rec_call g n (Type (@{type_name fun}, [dom_T, ran_T])) = |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
134 |
Abs (Name.uu, dom_T, mk_rec_call g (n + 1) ran_T) |
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
135 |
| mk_rec_call g n X = |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
136 |
let |
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
137 |
val frec = the (AList.lookup (op =) Xs_frecs X); |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
138 |
val xg = Term.list_comb (g, map Bound (n - 1 downto 0)); |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
139 |
in frec $ xg end; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
140 |
|
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
141 |
fun mk_rec_arg_arg ctrXs_T g = |
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
142 |
g :: (if member (op =) Xs (body_type ctrXs_T) then [mk_rec_call g 0 ctrXs_T] else []); |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
143 |
|
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
144 |
fun mk_goal frec ctrXs_Ts ctr f = |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
145 |
let |
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
146 |
val ctr_Ts = binder_types (fastype_of ctr); |
58217 | 147 |
val (gs, _) = mk_Frees "g" ctr_Ts ctxt; |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
148 |
val gctr = Term.list_comb (ctr, gs); |
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
149 |
val fgs = flat_rec_arg_args (map2 mk_rec_arg_arg ctrXs_Ts gs); |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
150 |
in |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
151 |
fold_rev (fold_rev Logic.all) [fs, gs] |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
152 |
(mk_Trueprop_eq (frec $ gctr, Term.list_comb (f, fgs))) |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
153 |
end; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
154 |
|
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
155 |
val goalss = map4 (map3 o mk_goal) frecs ctrXs_Tsss ctrss fss; |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
156 |
|
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
157 |
fun tac ctxt = |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
158 |
unfold_thms_tac ctxt (@{thms o_apply fst_conv snd_conv} @ rec_defs @ flat rec0_thmss) THEN |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
159 |
HEADGOAL (rtac refl); |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
160 |
|
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
161 |
fun prove goal = |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
162 |
Goal.prove_sorry ctxt [] [] goal (tac o #context) |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
163 |
|> Thm.close_derivation; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
164 |
in |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
165 |
map (map prove) goalss |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
166 |
end; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
167 |
|
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
168 |
fun define_split_rec_derive_induct_rec_thms Xs fpTs ctrXs_Tsss ctrss inducts induct recs0 rec_thmss |
58217 | 169 |
lthy = |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
170 |
let |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
171 |
val thy = Proof_Context.theory_of lthy; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
172 |
|
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
173 |
(* imperfect: will not yield the expected theorem for functions taking a large number of |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
174 |
arguments *) |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
175 |
val repair_induct = unfold_thms lthy @{thms all_mem_range}; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
176 |
|
58214 | 177 |
val inducts' = map repair_induct inducts; |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
178 |
val induct' = repair_induct induct; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
179 |
|
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
180 |
val Cs = map ((fn TVar ((s, _), S) => TFree (s, S)) o body_type o fastype_of) recs0; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
181 |
val recs = map2 (mk_co_rec thy Least_FP Cs) fpTs recs0; |
58213 | 182 |
val ((recs', rec'_defs), lthy') = define_split_recs fpTs Cs recs lthy |>> split_list; |
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
183 |
val rec'_thmss = mk_split_rec_thmss lthy' Xs ctrXs_Tsss ctrss rec_thmss recs' rec'_defs; |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
184 |
in |
58214 | 185 |
((inducts', induct', recs', rec'_thmss), lthy') |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
186 |
end; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
187 |
|
58219
61b852f90161
improved 'datatype_compat' further for recursion through functions
blanchet
parents:
58218
diff
changeset
|
188 |
fun body_rec_indices (Old_Datatype_Aux.DtRec kk) = [kk] |
61b852f90161
improved 'datatype_compat' further for recursion through functions
blanchet
parents:
58218
diff
changeset
|
189 |
| body_rec_indices (Old_Datatype_Aux.DtType (@{type_name fun}, [_, D])) = body_rec_indices D |
61b852f90161
improved 'datatype_compat' further for recursion through functions
blanchet
parents:
58218
diff
changeset
|
190 |
| body_rec_indices _ = []; |
61b852f90161
improved 'datatype_compat' further for recursion through functions
blanchet
parents:
58218
diff
changeset
|
191 |
|
56453
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
192 |
fun reindex_desc desc = |
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
193 |
let |
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
194 |
val kks = map fst desc; |
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
195 |
val perm_kks = sort int_ord kks; |
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
196 |
|
58112
8081087096ad
renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents:
57983
diff
changeset
|
197 |
fun perm_dtyp (Old_Datatype_Aux.DtType (s, Ds)) = Old_Datatype_Aux.DtType (s, map perm_dtyp Ds) |
8081087096ad
renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents:
57983
diff
changeset
|
198 |
| perm_dtyp (Old_Datatype_Aux.DtRec kk) = |
8081087096ad
renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents:
57983
diff
changeset
|
199 |
Old_Datatype_Aux.DtRec (find_index (curry (op =) kk) kks) |
58113 | 200 |
| perm_dtyp D = D; |
56453
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
201 |
in |
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
202 |
if perm_kks = kks then |
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
203 |
desc |
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
204 |
else |
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
205 |
perm_kks ~~ |
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
206 |
map (fn (_, (s, Ds, sDss)) => (s, map perm_dtyp Ds, map (apsnd (map perm_dtyp)) sDss)) desc |
58113 | 207 |
end; |
56453
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
208 |
|
58131
1abeda3c3bc2
drop hopeless feature -- unfolding of BNF datatype info without a prior 'datatype_compat'
blanchet
parents:
58130
diff
changeset
|
209 |
fun mk_infos_of_mutually_recursive_new_datatypes nesting_pref check_names fpT_names0 lthy = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
210 |
let |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
211 |
val thy = Proof_Context.theory_of lthy; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
212 |
|
58315 | 213 |
fun not_datatype s = error (quote s ^ " is not a datatype"); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
214 |
fun not_mutually_recursive ss = |
58315 | 215 |
error ("{" ^ commas ss ^ "} is not a complete set of mutually recursive datatypes"); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
216 |
|
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
217 |
fun lfp_sugar_of s = |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
218 |
(case fp_sugar_of lthy s of |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
219 |
SOME (fp_sugar as {fp = Least_FP, ...}) => fp_sugar |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
220 |
| _ => not_datatype s); |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
221 |
|
58229 | 222 |
val fpTs0 as Type (_, var_As) :: _ = |
223 |
map (#T o lfp_sugar_of o fst o dest_Type) (#Ts (#fp_res (lfp_sugar_of (hd fpT_names0)))); |
|
56455
1ff66e72628b
allow arguments to 'datatype_compat' in disorder
blanchet
parents:
56453
diff
changeset
|
224 |
val fpT_names = map (fst o dest_Type) fpTs0; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
225 |
|
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
226 |
val _ = check_names (op =) (fpT_names0, fpT_names) orelse not_mutually_recursive fpT_names0; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
227 |
|
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
228 |
val (As_names, _) = Variable.variant_fixes (map (fn TVar ((s, _), _) => s) var_As) lthy; |
56452
0c98c9118407
preserve user type variable names to avoid mismatches/confusion
blanchet
parents:
56002
diff
changeset
|
229 |
val As = map2 (fn s => fn TVar (_, S) => TFree (s, S)) As_names var_As; |
56455
1ff66e72628b
allow arguments to 'datatype_compat' in disorder
blanchet
parents:
56453
diff
changeset
|
230 |
val fpTs = map (fn s => Type (s, As)) fpT_names; |
56453
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
231 |
|
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
232 |
val nn_fp = length fpTs; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
233 |
|
58112
8081087096ad
renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents:
57983
diff
changeset
|
234 |
val mk_dtyp = Old_Datatype_Aux.dtyp_of_typ (map (apsnd (map Term.dest_TFree) o dest_Type) fpTs); |
56453
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
235 |
|
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
236 |
fun mk_ctr_descr Ts = mk_ctr Ts #> dest_Const ##> (binder_types #> map mk_dtyp); |
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
237 |
fun mk_typ_descr index (Type (T_name, Ts)) ({ctrs, ...} : ctr_sugar) = |
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
238 |
(index, (T_name, map mk_dtyp Ts, map (mk_ctr_descr Ts) ctrs)); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
239 |
|
56455
1ff66e72628b
allow arguments to 'datatype_compat' in disorder
blanchet
parents:
56453
diff
changeset
|
240 |
val fp_ctr_sugars = map (#ctr_sugar o lfp_sugar_of) fpT_names; |
56453
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
241 |
val orig_descr = map3 mk_typ_descr (0 upto nn_fp - 1) fpTs fp_ctr_sugars; |
58112
8081087096ad
renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents:
57983
diff
changeset
|
242 |
val all_infos = Old_Datatype_Data.get_all thy; |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
243 |
val (orig_descr' :: nested_descrs) = |
58125 | 244 |
if nesting_pref = Keep_Nesting then [orig_descr] |
245 |
else fst (Old_Datatype_Aux.unfold_datatypes lthy orig_descr all_infos orig_descr nn_fp); |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
246 |
|
57798 | 247 |
fun cliquify_descr [] = [] |
248 |
| cliquify_descr [entry] = [[entry]] |
|
249 |
| cliquify_descr (full_descr as (_, (T_name1, _, _)) :: _) = |
|
250 |
let |
|
251 |
val nn = |
|
252 |
if member (op =) fpT_names T_name1 then |
|
253 |
nn_fp |
|
254 |
else |
|
255 |
(case Symtab.lookup all_infos T_name1 of |
|
256 |
SOME {descr, ...} => |
|
58112
8081087096ad
renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents:
57983
diff
changeset
|
257 |
length (filter_out (exists Old_Datatype_Aux.is_rec_type o #2 o snd) descr) |
57798 | 258 |
| NONE => raise Fail "unknown old-style datatype"); |
259 |
in |
|
260 |
chop nn full_descr ||> cliquify_descr |> op :: |
|
261 |
end; |
|
262 |
||
56453
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
263 |
(* put nested types before the types that nest them, as needed for N2M *) |
56484 | 264 |
val descrs = burrow reindex_desc (orig_descr' :: rev nested_descrs); |
265 |
val (cliques, descr) = |
|
57798 | 266 |
split_list (flat (map_index (fn (i, descr) => map (pair i) descr) |
267 |
(maps cliquify_descr descrs))); |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
268 |
|
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
269 |
val fpTs' = Old_Datatype_Aux.get_rec_types descr; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
270 |
val nn = length fpTs'; |
55485 | 271 |
|
58217 | 272 |
val fp_sugars = map (lfp_sugar_of o fst o dest_Type) fpTs'; |
58219
61b852f90161
improved 'datatype_compat' further for recursion through functions
blanchet
parents:
58218
diff
changeset
|
273 |
val ctr_Tsss = map (map (map (Old_Datatype_Aux.typ_of_dtyp descr) o snd) o #3 o snd) descr; |
61b852f90161
improved 'datatype_compat' further for recursion through functions
blanchet
parents:
58218
diff
changeset
|
274 |
val kkssss = map (map (map body_rec_indices o snd) o #3 o snd) descr; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
275 |
|
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
276 |
val callers = map (fn kk => Var ((Name.uu, kk), @{typ "unit => unit"})) (0 upto nn - 1); |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
277 |
|
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
278 |
fun apply_comps n kk = |
55966 | 279 |
mk_partial_compN n (replicate n HOLogic.unitT ---> HOLogic.unitT) (nth callers kk); |
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
280 |
|
58219
61b852f90161
improved 'datatype_compat' further for recursion through functions
blanchet
parents:
58218
diff
changeset
|
281 |
val callssss = map2 (map2 (map2 (map o apply_comps o num_binder_types))) ctr_Tsss kkssss; |
56453
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
282 |
|
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
283 |
val b_names = Name.variant_list [] (map base_name_of_typ fpTs'); |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
284 |
val compat_b_names = map (prefix compat_N) b_names; |
56453
00548d372f02
support deeply nested datatypes in 'datatype_compat'
blanchet
parents:
56452
diff
changeset
|
285 |
val compat_bs = map Binding.name compat_b_names; |
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
286 |
|
58217 | 287 |
val ((fp_sugars', (lfp_sugar_thms', _)), lthy') = |
54286
22616f65d4ea
properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents:
54267
diff
changeset
|
288 |
if nn > nn_fp then |
58335
a5a3b576fcfb
generate 'code' attribute only if 'code' plugin is enabled
blanchet
parents:
58315
diff
changeset
|
289 |
mutualize_fp_sugars (K true) Least_FP cliques compat_bs fpTs' callers callssss fp_sugars |
a5a3b576fcfb
generate 'code' attribute only if 'code' plugin is enabled
blanchet
parents:
58315
diff
changeset
|
290 |
lthy |
54286
22616f65d4ea
properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents:
54267
diff
changeset
|
291 |
else |
58217 | 292 |
((fp_sugars, (NONE, NONE)), lthy); |
293 |
||
58271 | 294 |
fun mk_ctr_of ({ctr_sugar = {ctrs, ...}, ...} : fp_sugar) (Type (_, Ts)) = map (mk_ctr Ts) ctrs; |
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
295 |
val substAT = Term.typ_subst_atomic (var_As ~~ As); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
296 |
|
58217 | 297 |
val Xs' = map #X fp_sugars'; |
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
298 |
val ctrXs_Tsss' = map (map (map substAT) o #ctrXs_Tss) fp_sugars'; |
58217 | 299 |
val ctrss' = map2 mk_ctr_of fp_sugars' fpTs'; |
300 |
val {common_co_inducts = [induct], ...} :: _ = fp_sugars'; |
|
301 |
val inducts = map (the_single o #co_inducts) fp_sugars'; |
|
302 |
val recs = map #co_rec fp_sugars'; |
|
303 |
val rec_thmss = map #co_rec_thms fp_sugars'; |
|
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
304 |
|
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
305 |
fun is_nested_rec_type (Type (@{type_name fun}, [_, T])) = member (op =) Xs' (body_type T) |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
306 |
| is_nested_rec_type _ = false; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
307 |
|
58217 | 308 |
val ((lfp_sugar_thms'', (inducts', induct', recs', rec'_thmss)), lthy'') = |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
309 |
if nesting_pref = Unfold_Nesting andalso |
58218
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
310 |
exists (exists (exists is_nested_rec_type)) ctrXs_Tsss' then |
a92acec845a7
no type-based lookup -- these fail in the general, ambiguous case
blanchet
parents:
58217
diff
changeset
|
311 |
define_split_rec_derive_induct_rec_thms Xs' fpTs' ctrXs_Tsss' ctrss' inducts induct recs |
58217 | 312 |
rec_thmss lthy' |
58214 | 313 |
|>> `(fn (inducts', induct', _, rec'_thmss) => |
314 |
SOME ((inducts', induct', mk_induct_attrs ctrss'), (rec'_thmss, []))) |
|
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
315 |
else |
58217 | 316 |
((lfp_sugar_thms', (inducts, induct, recs, rec_thmss)), lthy'); |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
317 |
|
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
318 |
val rec'_names = map (fst o dest_Const) recs'; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
319 |
val rec'_thms = flat rec'_thmss; |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
320 |
|
55567 | 321 |
fun mk_info (kk, {T = Type (T_name0, _), ctr_sugar = {casex, exhaust, nchotomy, injects, |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57807
diff
changeset
|
322 |
distincts, case_thms, case_cong, case_cong_weak, split, split_asm, ...}, ...} : fp_sugar) = |
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55531
diff
changeset
|
323 |
(T_name0, |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
324 |
{index = kk, descr = descr, inject = injects, distinct = distincts, induct = induct', |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
325 |
inducts = inducts', exhaust = exhaust, nchotomy = nchotomy, rec_names = rec'_names, |
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
326 |
rec_rewrites = rec'_thms, case_name = fst (dest_Const casex), case_rewrites = case_thms, |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57807
diff
changeset
|
327 |
case_cong = case_cong, case_cong_weak = case_cong_weak, split = split, |
57794 | 328 |
split_asm = split_asm}); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
329 |
|
58217 | 330 |
val infos = map_index mk_info (take nn_fp fp_sugars'); |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
331 |
in |
58217 | 332 |
(nn, b_names, compat_b_names, lfp_sugar_thms'', infos, lthy'') |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
333 |
end; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
334 |
|
58131
1abeda3c3bc2
drop hopeless feature -- unfolding of BNF datatype info without a prior 'datatype_compat'
blanchet
parents:
58130
diff
changeset
|
335 |
fun infos_of_new_datatype_mutual_cluster lthy fpT_name = |
1abeda3c3bc2
drop hopeless feature -- unfolding of BNF datatype info without a prior 'datatype_compat'
blanchet
parents:
58130
diff
changeset
|
336 |
#5 (mk_infos_of_mutually_recursive_new_datatypes Keep_Nesting subset [fpT_name] lthy) |
1abeda3c3bc2
drop hopeless feature -- unfolding of BNF datatype info without a prior 'datatype_compat'
blanchet
parents:
58130
diff
changeset
|
337 |
handle ERROR _ => []; |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
338 |
|
58125 | 339 |
fun get_all thy nesting_pref = |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
340 |
let |
58133 | 341 |
val lthy = Proof_Context.init_global thy; |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
342 |
val old_info_tab = Old_Datatype_Data.get_all thy; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
343 |
val new_T_names = BNF_FP_Def_Sugar.fp_sugars_of_global thy |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
344 |
|> map_filter (try (fn {T = Type (s, _), fp_res_index = 0, ...} => s)); |
58131
1abeda3c3bc2
drop hopeless feature -- unfolding of BNF datatype info without a prior 'datatype_compat'
blanchet
parents:
58130
diff
changeset
|
345 |
val new_infos = maps (infos_of_new_datatype_mutual_cluster lthy) new_T_names; |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
346 |
in |
58125 | 347 |
fold (if nesting_pref = Keep_Nesting then Symtab.update else Symtab.default) new_infos |
58123
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
348 |
old_info_tab |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
349 |
end; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
350 |
|
58125 | 351 |
fun get_one get_old get_new thy nesting_pref x = |
58131
1abeda3c3bc2
drop hopeless feature -- unfolding of BNF datatype info without a prior 'datatype_compat'
blanchet
parents:
58130
diff
changeset
|
352 |
let val (get_fst, get_snd) = (get_old thy, get_new thy) |> nesting_pref = Keep_Nesting ? swap in |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
353 |
(case get_fst x of NONE => get_snd x | res => res) |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
354 |
end; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
355 |
|
58131
1abeda3c3bc2
drop hopeless feature -- unfolding of BNF datatype info without a prior 'datatype_compat'
blanchet
parents:
58130
diff
changeset
|
356 |
fun get_info_of_new_datatype thy T_name = |
58130 | 357 |
let val lthy = Proof_Context.init_global thy in |
58131
1abeda3c3bc2
drop hopeless feature -- unfolding of BNF datatype info without a prior 'datatype_compat'
blanchet
parents:
58130
diff
changeset
|
358 |
AList.lookup (op =) (infos_of_new_datatype_mutual_cluster lthy T_name) T_name |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
359 |
end; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
360 |
|
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
361 |
val get_info = get_one Old_Datatype_Data.get_info get_info_of_new_datatype; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
362 |
|
58125 | 363 |
fun the_info thy nesting_pref T_name = |
364 |
(case get_info thy nesting_pref T_name of |
|
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
365 |
SOME info => info |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
366 |
| NONE => error ("Unknown datatype " ^ quote T_name)); |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
367 |
|
58129
3ec65a7f2b50
ported Refute to use new datatypes when possible
blanchet
parents:
58126
diff
changeset
|
368 |
fun the_spec thy T_name = |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
369 |
let |
58129
3ec65a7f2b50
ported Refute to use new datatypes when possible
blanchet
parents:
58126
diff
changeset
|
370 |
val {descr, index, ...} = the_info thy Keep_Nesting T_name; |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
371 |
val (_, Ds, ctrs0) = the (AList.lookup (op =) descr index); |
58129
3ec65a7f2b50
ported Refute to use new datatypes when possible
blanchet
parents:
58126
diff
changeset
|
372 |
val tfrees = map Old_Datatype_Aux.dest_DtTFree Ds; |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
373 |
val ctrs = map (apsnd (map (Old_Datatype_Aux.typ_of_dtyp descr))) ctrs0; |
58129
3ec65a7f2b50
ported Refute to use new datatypes when possible
blanchet
parents:
58126
diff
changeset
|
374 |
in (tfrees, ctrs) end; |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
375 |
|
58125 | 376 |
fun the_descr thy nesting_pref (T_names0 as T_name01 :: _) = |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
377 |
let |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
378 |
fun not_mutually_recursive ss = |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
379 |
error ("{" ^ commas ss ^ "} is not a complete set of mutually recursive datatypes"); |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
380 |
|
58125 | 381 |
val info = the_info thy nesting_pref T_name01; |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
382 |
val descr = #descr info; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
383 |
|
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
384 |
val (_, Ds, _) = the (AList.lookup (op =) descr (#index info)); |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
385 |
val vs = map Old_Datatype_Aux.dest_DtTFree Ds; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
386 |
|
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
387 |
fun is_DtTFree (Old_Datatype_Aux.DtTFree _) = true |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
388 |
| is_DtTFree _ = false; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
389 |
|
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
390 |
val k = find_index (fn (_, (_, dTs, _)) => not (forall is_DtTFree dTs)) descr; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
391 |
val protoTs as (dataTs, _) = chop k descr |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
392 |
|> (pairself o map) |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
393 |
(fn (_, (T_name, Ds, _)) => (T_name, map (Old_Datatype_Aux.typ_of_dtyp descr) Ds)); |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
394 |
|
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
395 |
val T_names = map fst dataTs; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
396 |
val _ = eq_set (op =) (T_names, T_names0) orelse not_mutually_recursive T_names0 |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
397 |
|
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
398 |
val (Ts, Us) = (pairself o map) Type protoTs; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
399 |
|
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
400 |
val names = map Long_Name.base_name T_names; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
401 |
val (auxnames, _) = Name.make_context names |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
402 |
|> fold_map (Name.variant o Old_Datatype_Aux.name_of_typ) Us; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
403 |
val prefix = space_implode "_" names; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
404 |
in |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
405 |
(descr, vs, T_names, prefix, (names, auxnames), (Ts, Us)) |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
406 |
end; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
407 |
|
58129
3ec65a7f2b50
ported Refute to use new datatypes when possible
blanchet
parents:
58126
diff
changeset
|
408 |
fun get_constrs thy T_name = |
3ec65a7f2b50
ported Refute to use new datatypes when possible
blanchet
parents:
58126
diff
changeset
|
409 |
try (the_spec thy) T_name |
58119 | 410 |
|> Option.map (fn (tfrees, ctrs) => |
411 |
let |
|
412 |
fun varify_tfree (s, S) = TVar ((s, 0), S); |
|
413 |
fun varify_typ (TFree x) = varify_tfree x |
|
414 |
| varify_typ T = T; |
|
415 |
||
416 |
val dataT = Type (T_name, map varify_tfree tfrees); |
|
417 |
||
418 |
fun mk_ctr_typ Ts = map (Term.map_atyps varify_typ) Ts ---> dataT; |
|
419 |
in |
|
420 |
map (apsnd mk_ctr_typ) ctrs |
|
421 |
end); |
|
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
422 |
|
58125 | 423 |
fun old_interpretation_of nesting_pref f config T_names thy = |
424 |
if nesting_pref = Unfold_Nesting orelse exists (is_none o fp_sugar_of_global thy) T_names then |
|
58122 | 425 |
f config T_names thy |
426 |
else |
|
427 |
thy; |
|
428 |
||
58137 | 429 |
fun new_interpretation_of nesting_pref f (fp_sugars : fp_sugar list) thy = |
58122 | 430 |
let val T_names = map (fst o dest_Type o #T) fp_sugars in |
58146 | 431 |
if forall (curry (op =) Least_FP o #fp) fp_sugars andalso |
432 |
(nesting_pref = Keep_Nesting orelse |
|
433 |
exists (is_none o Old_Datatype_Data.get_info thy) T_names) then |
|
58123
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
434 |
f Old_Datatype_Aux.default_config T_names thy |
58122 | 435 |
else |
436 |
thy |
|
437 |
end; |
|
438 |
||
58186 | 439 |
fun interpretation name nesting_pref f = |
58177
166131276380
introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
58147
diff
changeset
|
440 |
let val new_f = new_interpretation_of nesting_pref f in |
166131276380
introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
58147
diff
changeset
|
441 |
Old_Datatype_Data.interpretation (old_interpretation_of nesting_pref f) |
58186 | 442 |
#> fp_sugars_interpretation name new_f (Local_Theory.background_theory o new_f) |
58177
166131276380
introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
58147
diff
changeset
|
443 |
end; |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
444 |
|
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
445 |
val code_nitpicksimp_simp_attrs = Code.add_default_eqn_attrib :: @{attributes [nitpick_simp, simp]}; |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
446 |
|
58125 | 447 |
fun datatype_compat fpT_names lthy = |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
448 |
let |
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
449 |
val (nn, b_names, compat_b_names, lfp_sugar_thms, infos, lthy') = |
58131
1abeda3c3bc2
drop hopeless feature -- unfolding of BNF datatype info without a prior 'datatype_compat'
blanchet
parents:
58130
diff
changeset
|
450 |
mk_infos_of_mutually_recursive_new_datatypes Unfold_Nesting eq_set fpT_names lthy; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
451 |
|
53746
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
452 |
val all_notes = |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
453 |
(case lfp_sugar_thms of |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
454 |
NONE => [] |
58214 | 455 |
| SOME ((inducts, induct, induct_attrs), (rec_thmss, _)) => |
53746
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
456 |
let |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58189
diff
changeset
|
457 |
val common_name = compat_N ^ mk_common_name b_names; |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
458 |
|
53746
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
459 |
val common_notes = |
58214 | 460 |
(if nn > 1 then [(inductN, [induct], induct_attrs)] else []) |
53746
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
461 |
|> filter_out (null o #2) |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
462 |
|> map (fn (thmN, thms, attrs) => |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
463 |
((Binding.qualify true common_name (Binding.name thmN), attrs), [(thms, [])])); |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
464 |
|
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
465 |
val notes = |
58214 | 466 |
[(inductN, map single inducts, induct_attrs), |
55479
ece4910c3ea0
improved 'datatype_new_compat': generate more fixpoint equations for types like 'datatype_new x = C (x list) (x list)' (here, one equation for each x list instead of a single for both), for higher compatibility + code generation attributes on the recursor
blanchet
parents:
55409
diff
changeset
|
467 |
(recN, rec_thmss, code_nitpicksimp_simp_attrs)] |
53746
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
468 |
|> filter_out (null o #2) |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
469 |
|> maps (fn (thmN, thmss, attrs) => |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
470 |
if forall null thmss then |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
471 |
[] |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
472 |
else |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
473 |
map2 (fn b_name => fn thms => |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
474 |
((Binding.qualify true b_name (Binding.name thmN), attrs), [(thms, [])])) |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
475 |
compat_b_names thmss); |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
476 |
in |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
477 |
common_notes @ notes |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
478 |
end); |
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
479 |
|
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
480 |
val register_interpret = |
58112
8081087096ad
renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents:
57983
diff
changeset
|
481 |
Old_Datatype_Data.register infos |
58113 | 482 |
#> Old_Datatype_Data.interpretation_data (Old_Datatype_Aux.default_config, map fst infos); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
483 |
in |
58117
9608028d8f43
more compatibility between old- and new-style datatypes
blanchet
parents:
58113
diff
changeset
|
484 |
lthy' |
53746
bd038e48526d
have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents:
53309
diff
changeset
|
485 |
|> Local_Theory.raw_theory register_interpret |
57633 | 486 |
|> Local_Theory.notes all_notes |
487 |
|> snd |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
488 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
489 |
|
58179 | 490 |
val datatype_compat_global = map_local_theory o datatype_compat; |
58125 | 491 |
|
492 |
fun datatype_compat_cmd raw_fpT_names lthy = |
|
493 |
let |
|
494 |
val fpT_names = |
|
495 |
map (fst o dest_Type o Proof_Context.read_type_name {proper = true, strict = false} lthy) |
|
496 |
raw_fpT_names; |
|
497 |
in |
|
498 |
datatype_compat fpT_names lthy |
|
499 |
end; |
|
500 |
||
58300 | 501 |
fun gen_add_datatype live nesting_pref old_specs thy = |
58123
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
502 |
let |
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
503 |
val fpT_names = map (Sign.full_name thy o #1 o fst) old_specs; |
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
504 |
|
58300 | 505 |
fun new_type_args_of (s, S) = |
506 |
(if live then SOME Binding.empty else NONE, (TFree (s, @{sort type}), S)); |
|
58123
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
507 |
fun new_ctr_spec_of (b, Ts, mx) = (((Binding.empty, b), map (pair Binding.empty) Ts), mx); |
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
508 |
|
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
509 |
fun new_spec_of ((b, old_tyargs, mx), old_ctr_specs) = |
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
510 |
(((((map new_type_args_of old_tyargs, b), mx), map new_ctr_spec_of old_ctr_specs), |
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
511 |
(Binding.empty, Binding.empty)), []); |
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
512 |
|
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
513 |
val new_specs = map new_spec_of old_specs; |
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
514 |
in |
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
515 |
(fpT_names, |
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
516 |
thy |
58189
9d714be4f028
added 'plugins' option to control which hooks are enabled
blanchet
parents:
58186
diff
changeset
|
517 |
|> map_local_theory (co_datatypes Least_FP construct_lfp (default_ctr_options, new_specs)) |
58125 | 518 |
|> nesting_pref = Unfold_Nesting ? perhaps (try (datatype_compat_global fpT_names))) |
58123
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
519 |
end; |
62765d39539f
implemented compatibility definition of datatype
blanchet
parents:
58122
diff
changeset
|
520 |
|
58300 | 521 |
val add_datatype = gen_add_datatype true; |
522 |
val add_datatype_dead = gen_add_datatype false; |
|
523 |
||
58126 | 524 |
val add_primrec = apfst (apsnd flat) ooo BNF_LFP_Rec_Sugar.add_primrec; |
58147 | 525 |
val add_primrec_global = apfst (apsnd flat) ooo BNF_LFP_Rec_Sugar.add_primrec_global; |
526 |
val add_primrec_overloaded = apfst (apsnd flat) oooo BNF_LFP_Rec_Sugar.add_primrec_overloaded; |
|
58220 | 527 |
val add_primrec_simple = apfst (apsnd (apsnd (flat o snd))) ooo |
58147 | 528 |
BNF_LFP_Rec_Sugar.add_primrec_simple; |
58126 | 529 |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
530 |
val _ = |
55531
601ca8efa000
renamed 'datatype_new_compat' to 'datatype_compat'
blanchet
parents:
55486
diff
changeset
|
531 |
Outer_Syntax.local_theory @{command_spec "datatype_compat"} |
58315 | 532 |
"register datatypes as old-style datatypes and derive old-style properties" |
55531
601ca8efa000
renamed 'datatype_new_compat' to 'datatype_compat'
blanchet
parents:
55486
diff
changeset
|
533 |
(Scan.repeat1 Parse.type_const >> datatype_compat_cmd); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
534 |
|
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
535 |
end; |