author | blanchet |
Mon, 24 Nov 2014 12:35:13 +0100 | |
changeset 59041 | 2a23235632b2 |
parent 58634 | 9f10d82e8188 |
child 59042 | ef0074e812cd |
permissions | -rw-r--r-- |
55061 | 1 |
(* Title: HOL/Tools/BNF/bnf_gfp_rec_sugar.ML |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
2 |
Author: Lorenz Panny, TU Muenchen |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
3 |
Author: Jasmin Blanchette, TU Muenchen |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
4 |
Copyright 2013 |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
5 |
|
55538 | 6 |
Corecursor sugar ("primcorec" and "primcorecursive"). |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
7 |
*) |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
8 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
9 |
signature BNF_GFP_REC_SUGAR = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
10 |
sig |
54899
7a01387c47d5
added tactic to prove 'disc_iff' properties in 'primcorec'
blanchet
parents:
54883
diff
changeset
|
11 |
datatype primcorec_option = Sequential_Option | Exhaustive_Option |
7a01387c47d5
added tactic to prove 'disc_iff' properties in 'primcorec'
blanchet
parents:
54883
diff
changeset
|
12 |
|
58223
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
13 |
datatype corec_call = |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
14 |
Dummy_No_Corec of int | |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
15 |
No_Corec of int | |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
16 |
Mutual_Corec of int * int * int | |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
17 |
Nested_Corec of int |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
18 |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
19 |
type corec_ctr_spec = |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
20 |
{ctr: term, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
21 |
disc: term, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
22 |
sels: term list, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
23 |
pred: int option, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
24 |
calls: corec_call list, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
25 |
discI: thm, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
26 |
sel_thms: thm list, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
27 |
distinct_discss: thm list list, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
28 |
collapse: thm, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
29 |
corec_thm: thm, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
30 |
corec_disc: thm, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
31 |
corec_sels: thm list} |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
32 |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
33 |
type corec_spec = |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
34 |
{corec: term, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
35 |
exhaust_discs: thm list, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
36 |
sel_defs: thm list, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
37 |
fp_nesting_maps: thm list, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
38 |
fp_nesting_map_ident0s: thm list, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
39 |
fp_nesting_map_comps: thm list, |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
40 |
ctr_specs: corec_ctr_spec list} |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
41 |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
42 |
val corec_specs_of: binding list -> typ list -> typ list -> term list -> |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
43 |
(term * term list list) list list -> local_theory -> |
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
44 |
corec_spec list * typ list * thm * thm * thm list * thm list * (Token.src list * Token.src list) |
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
45 |
* bool * local_theory |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
46 |
val add_primcorecursive_cmd: primcorec_option list -> |
53831
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53830
diff
changeset
|
47 |
(binding * string option * mixfix) list * ((Attrib.binding * string) * string option) list -> |
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53830
diff
changeset
|
48 |
Proof.context -> Proof.state |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
49 |
val add_primcorec_cmd: primcorec_option list -> |
53831
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53830
diff
changeset
|
50 |
(binding * string option * mixfix) list * ((Attrib.binding * string) * string option) list -> |
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53830
diff
changeset
|
51 |
local_theory -> local_theory |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
52 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
53 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
54 |
structure BNF_GFP_Rec_Sugar : BNF_GFP_REC_SUGAR = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
55 |
struct |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
56 |
|
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
57 |
open Ctr_Sugar_General_Tactics |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
58 |
open Ctr_Sugar |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
59 |
open BNF_Util |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
60 |
open BNF_Def |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
61 |
open BNF_FP_Util |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
62 |
open BNF_FP_Def_Sugar |
54243 | 63 |
open BNF_FP_N2M_Sugar |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
64 |
open BNF_FP_Rec_Sugar_Util |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
65 |
open BNF_GFP_Rec_Sugar_Tactics |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
66 |
|
58387 | 67 |
val codeN = "code"; |
68 |
val ctrN = "ctr"; |
|
69 |
val discN = "disc"; |
|
70 |
val disc_iffN = "disc_iff"; |
|
71 |
val excludeN = "exclude"; |
|
72 |
val selN = "sel"; |
|
53791 | 73 |
|
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54133
diff
changeset
|
74 |
val nitpicksimp_attrs = @{attributes [nitpick_simp]}; |
53794 | 75 |
val simp_attrs = @{attributes [simp]}; |
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54133
diff
changeset
|
76 |
val code_nitpicksimp_attrs = Code.add_default_eqn_attrib :: nitpicksimp_attrs; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
77 |
|
55527 | 78 |
exception PRIMCOREC of string * term list; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
79 |
|
55527 | 80 |
fun primcorec_error str = raise PRIMCOREC (str, []); |
81 |
fun primcorec_error_eqn str eqn = raise PRIMCOREC (str, [eqn]); |
|
82 |
fun primcorec_error_eqns str eqns = raise PRIMCOREC (str, eqns); |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
83 |
|
54899
7a01387c47d5
added tactic to prove 'disc_iff' properties in 'primcorec'
blanchet
parents:
54883
diff
changeset
|
84 |
datatype primcorec_option = Sequential_Option | Exhaustive_Option; |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
85 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
86 |
datatype corec_call = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
87 |
Dummy_No_Corec of int | |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
88 |
No_Corec of int | |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
89 |
Mutual_Corec of int * int * int | |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
90 |
Nested_Corec of int; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
91 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
92 |
type basic_corec_ctr_spec = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
93 |
{ctr: term, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
94 |
disc: term, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
95 |
sels: term list}; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
96 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
97 |
type corec_ctr_spec = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
98 |
{ctr: term, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
99 |
disc: term, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
100 |
sels: term list, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
101 |
pred: int option, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
102 |
calls: corec_call list, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
103 |
discI: thm, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
104 |
sel_thms: thm list, |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
105 |
distinct_discss: thm list list, |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
106 |
collapse: thm, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
107 |
corec_thm: thm, |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
108 |
corec_disc: thm, |
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
109 |
corec_sels: thm list}; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
110 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
111 |
type corec_spec = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
112 |
{corec: term, |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
113 |
exhaust_discs: thm list, |
56858 | 114 |
sel_defs: thm list, |
57397 | 115 |
fp_nesting_maps: thm list, |
57399 | 116 |
fp_nesting_map_ident0s: thm list, |
57397 | 117 |
fp_nesting_map_comps: thm list, |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
118 |
ctr_specs: corec_ctr_spec list}; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
119 |
|
58223
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
120 |
exception NO_MAP of term; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
121 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
122 |
fun not_codatatype ctxt T = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
123 |
error ("Not a codatatype: " ^ Syntax.string_of_typ ctxt T); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
124 |
fun ill_formed_corec_call ctxt t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
125 |
error ("Ill-formed corecursive call: " ^ quote (Syntax.string_of_term ctxt t)); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
126 |
fun invalid_map ctxt t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
127 |
error ("Invalid map function in " ^ quote (Syntax.string_of_term ctxt t)); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
128 |
fun unexpected_corec_call ctxt t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
129 |
error ("Unexpected corecursive call: " ^ quote (Syntax.string_of_term ctxt t)); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
130 |
|
55870 | 131 |
fun sort_list_duplicates xs = map snd (sort (int_ord o pairself fst) xs); |
54948 | 132 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
133 |
val mk_conjs = try (foldr1 HOLogic.mk_conj) #> the_default @{const True}; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
134 |
val mk_disjs = try (foldr1 HOLogic.mk_disj) #> the_default @{const False}; |
54900 | 135 |
val mk_dnf = mk_disjs o map mk_conjs; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
136 |
|
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
137 |
val conjuncts_s = filter_out (curry (op aconv) @{const True}) o HOLogic.conjuncts; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
138 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
139 |
fun s_not @{const True} = @{const False} |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
140 |
| s_not @{const False} = @{const True} |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
141 |
| s_not (@{const Not} $ t) = t |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
142 |
| s_not (@{const conj} $ t $ u) = @{const disj} $ s_not t $ s_not u |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
143 |
| s_not (@{const disj} $ t $ u) = @{const conj} $ s_not t $ s_not u |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
144 |
| s_not t = @{const Not} $ t; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
145 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
146 |
val s_not_conj = conjuncts_s o s_not o mk_conjs; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
147 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
148 |
fun propagate_unit_pos u cs = if member (op aconv) cs u then [@{const False}] else cs; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
149 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
150 |
fun propagate_unit_neg not_u cs = remove (op aconv) not_u cs; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
151 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
152 |
fun propagate_units css = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
153 |
(case List.partition (can the_single) css of |
58393 | 154 |
([], _) => css |
155 |
| ([u] :: uss, css') => |
|
156 |
[u] :: propagate_units (map (propagate_unit_neg (s_not u)) |
|
157 |
(map (propagate_unit_pos u) (uss @ css')))); |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
158 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
159 |
fun s_conjs cs = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
160 |
if member (op aconv) cs @{const False} then @{const False} |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
161 |
else mk_conjs (remove (op aconv) @{const True} cs); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
162 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
163 |
fun s_disjs ds = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
164 |
if member (op aconv) ds @{const True} then @{const True} |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
165 |
else mk_disjs (remove (op aconv) @{const False} ds); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
166 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
167 |
fun s_dnf css0 = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
168 |
let val css = propagate_units css0 in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
169 |
if null css then |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
170 |
[@{const False}] |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
171 |
else if exists null css then |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
172 |
[] |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
173 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
174 |
map (fn c :: cs => (c, cs)) css |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
175 |
|> AList.coalesce (op =) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
176 |
|> map (fn (c, css) => c :: s_dnf css) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
177 |
|> (fn [cs] => cs | css => [s_disjs (map s_conjs css)]) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
178 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
179 |
|
55341
3d2c97392e25
adapted tactic to correctly handle 'if ... then ...' and 'case ...' under lambdas
blanchet
parents:
55339
diff
changeset
|
180 |
fun fold_rev_let_if_case ctxt f bound_Ts = |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
181 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
182 |
val thy = Proof_Context.theory_of ctxt; |
53794 | 183 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
184 |
fun fld conds t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
185 |
(case Term.strip_comb t of |
55343 | 186 |
(Const (@{const_name Let}, _), [_, _]) => fld conds (unfold_lets_splits t) |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
187 |
| (Const (@{const_name If}, _), [cond, then_branch, else_branch]) => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
188 |
fld (conds @ conjuncts_s cond) then_branch o fld (conds @ s_not_conj [cond]) else_branch |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
189 |
| (Const (c, _), args as _ :: _ :: _) => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
190 |
let val n = num_binder_types (Sign.the_const_type thy c) - 1 in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
191 |
if n >= 0 andalso n < length args then |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
192 |
(case fastype_of1 (bound_Ts, nth args n) of |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
193 |
Type (s, Ts) => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
194 |
(case dest_case ctxt s Ts t of |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
195 |
SOME ({split_sels = _ :: _, ...}, conds', branches) => |
55341
3d2c97392e25
adapted tactic to correctly handle 'if ... then ...' and 'case ...' under lambdas
blanchet
parents:
55339
diff
changeset
|
196 |
fold_rev (uncurry fld) (map (append conds o conjuncts_s) conds' ~~ branches) |
3d2c97392e25
adapted tactic to correctly handle 'if ... then ...' and 'case ...' under lambdas
blanchet
parents:
55339
diff
changeset
|
197 |
| _ => f conds t) |
3d2c97392e25
adapted tactic to correctly handle 'if ... then ...' and 'case ...' under lambdas
blanchet
parents:
55339
diff
changeset
|
198 |
| _ => f conds t) |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
199 |
else |
55341
3d2c97392e25
adapted tactic to correctly handle 'if ... then ...' and 'case ...' under lambdas
blanchet
parents:
55339
diff
changeset
|
200 |
f conds t |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
201 |
end |
55342 | 202 |
| _ => f conds t); |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
203 |
in |
55341
3d2c97392e25
adapted tactic to correctly handle 'if ... then ...' and 'case ...' under lambdas
blanchet
parents:
55339
diff
changeset
|
204 |
fld [] |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
205 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
206 |
|
54970
891141de5672
only destruct cases equipped with the right stuff (in particular, 'sel_split')
blanchet
parents:
54969
diff
changeset
|
207 |
fun case_of ctxt s = |
891141de5672
only destruct cases equipped with the right stuff (in particular, 'sel_split')
blanchet
parents:
54969
diff
changeset
|
208 |
(case ctr_sugar_of ctxt s of |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
209 |
SOME {casex = Const (s', _), split_sels = _ :: _, ...} => SOME s' |
54970
891141de5672
only destruct cases equipped with the right stuff (in particular, 'sel_split')
blanchet
parents:
54969
diff
changeset
|
210 |
| _ => NONE); |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
211 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
212 |
fun massage_let_if_case ctxt has_call massage_leaf = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
213 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
214 |
val thy = Proof_Context.theory_of ctxt; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
215 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
216 |
fun check_no_call t = if has_call t then unexpected_corec_call ctxt t else (); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
217 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
218 |
fun massage_abs bound_Ts 0 t = massage_rec bound_Ts t |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
219 |
| massage_abs bound_Ts m (Abs (s, T, t)) = Abs (s, T, massage_abs (T :: bound_Ts) (m - 1) t) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
220 |
| massage_abs bound_Ts m t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
221 |
let val T = domain_type (fastype_of1 (bound_Ts, t)) in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
222 |
Abs (Name.uu, T, massage_abs (T :: bound_Ts) (m - 1) (incr_boundvars 1 t $ Bound 0)) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
223 |
end |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
224 |
and massage_rec bound_Ts t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
225 |
let val typof = curry fastype_of1 bound_Ts in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
226 |
(case Term.strip_comb t of |
55343 | 227 |
(Const (@{const_name Let}, _), [_, _]) => massage_rec bound_Ts (unfold_lets_splits t) |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
228 |
| (Const (@{const_name If}, _), obj :: (branches as [_, _])) => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
229 |
let val branches' = map (massage_rec bound_Ts) branches in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
230 |
Term.list_comb (If_const (typof (hd branches')) $ tap check_no_call obj, branches') |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
231 |
end |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
55400
diff
changeset
|
232 |
| (c as Const (@{const_name case_prod}, _), arg :: args) => |
55343 | 233 |
massage_rec bound_Ts |
57895 | 234 |
(unfold_splits_lets (Term.list_comb (c $ Envir.eta_long bound_Ts arg, args))) |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
235 |
| (Const (c, _), args as _ :: _ :: _) => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
236 |
(case try strip_fun_type (Sign.the_const_type thy c) of |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
237 |
SOME (gen_branch_Ts, gen_body_fun_T) => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
238 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
239 |
val gen_branch_ms = map num_binder_types gen_branch_Ts; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
240 |
val n = length gen_branch_ms; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
241 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
242 |
if n < length args then |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
243 |
(case gen_body_fun_T of |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
244 |
Type (_, [Type (T_name, _), _]) => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
245 |
if case_of ctxt T_name = SOME c then |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
246 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
247 |
val (branches, obj_leftovers) = chop n args; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
248 |
val branches' = map2 (massage_abs bound_Ts) gen_branch_ms branches; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
249 |
val branch_Ts' = map typof branches'; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
250 |
val body_T' = snd (strip_typeN (hd gen_branch_ms) (hd branch_Ts')); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
251 |
val casex' = Const (c, branch_Ts' ---> map typof obj_leftovers ---> body_T'); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
252 |
in |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
253 |
Term.list_comb (casex', |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
254 |
branches' @ tap (List.app check_no_call) obj_leftovers) |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
255 |
end |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
256 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
257 |
massage_leaf bound_Ts t |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
258 |
| _ => massage_leaf bound_Ts t) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
259 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
260 |
massage_leaf bound_Ts t |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
261 |
end |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
262 |
| NONE => massage_leaf bound_Ts t) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
263 |
| _ => massage_leaf bound_Ts t) |
55342 | 264 |
end; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
265 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
266 |
massage_rec |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
267 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
268 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
269 |
fun curried_type (Type (@{type_name fun}, [Type (@{type_name prod}, Ts), T])) = Ts ---> T; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
270 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
271 |
fun massage_nested_corec_call ctxt has_call raw_massage_call bound_Ts U t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
272 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
273 |
fun check_no_call t = if has_call t then unexpected_corec_call ctxt t else (); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
274 |
|
57303 | 275 |
val build_map_Inl = build_map ctxt [] (uncurry Inl_const o dest_sumT o snd); |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
276 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
277 |
fun massage_mutual_call bound_Ts U T t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
278 |
if has_call t then |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
279 |
(case try dest_sumT U of |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
280 |
SOME (U1, U2) => if U1 = T then raw_massage_call bound_Ts T U2 t else invalid_map ctxt t |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
281 |
| NONE => invalid_map ctxt t) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
282 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
283 |
build_map_Inl (T, U) $ t; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
284 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
285 |
fun massage_map bound_Ts (Type (_, Us)) (Type (s, Ts)) t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
286 |
(case try (dest_map ctxt s) t of |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
287 |
SOME (map0, fs) => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
288 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
289 |
val Type (_, dom_Ts) = domain_type (fastype_of1 (bound_Ts, t)); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
290 |
val map' = mk_map (length fs) dom_Ts Us map0; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
291 |
val fs' = |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
292 |
map_flattened_map_args ctxt s (@{map 3} (massage_map_or_map_arg bound_Ts) Us Ts) fs; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
293 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
294 |
Term.list_comb (map', fs') |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
295 |
end |
58223
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
296 |
| NONE => raise NO_MAP t) |
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
297 |
| massage_map _ _ _ t = raise NO_MAP t |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
298 |
and massage_map_or_map_arg bound_Ts U T t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
299 |
if T = U then |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
300 |
tap check_no_call t |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
301 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
302 |
massage_map bound_Ts U T t |
58223
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
303 |
handle NO_MAP _ => massage_mutual_fun bound_Ts U T t |
55339
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
304 |
and massage_mutual_fun bound_Ts U T t = |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
305 |
(case t of |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
306 |
Const (@{const_name comp}, _) $ t1 $ t2 => |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
307 |
mk_comp bound_Ts (massage_mutual_fun bound_Ts U T t1, tap check_no_call t2) |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
308 |
| _ => |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
309 |
let |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
310 |
val var = Var ((Name.uu, Term.maxidx_of_term t + 1), |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
311 |
domain_type (fastype_of1 (bound_Ts, t))); |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
312 |
in |
59041 | 313 |
Term.lambda var (Term.incr_boundvars 1 (massage_call bound_Ts U T (betapply (t, var)))) |
55339
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
314 |
end) |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
315 |
and massage_call bound_Ts U T = |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
316 |
massage_let_if_case ctxt has_call (fn bound_Ts => fn t => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
317 |
if has_call t then |
54955
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
318 |
(case U of |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
319 |
Type (s, Us) => |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
320 |
(case try (dest_ctr ctxt s) t of |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
321 |
SOME (f, args) => |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
322 |
let |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
323 |
val typof = curry fastype_of1 bound_Ts; |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
324 |
val f' = mk_ctr Us f |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
325 |
val f'_T = typof f'; |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
326 |
val arg_Ts = map typof args; |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
327 |
in |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
328 |
Term.list_comb (f', @{map 3} (massage_call bound_Ts) (binder_types f'_T) arg_Ts args) |
54955
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
329 |
end |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
330 |
| NONE => |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
331 |
(case t of |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
55400
diff
changeset
|
332 |
Const (@{const_name case_prod}, _) $ t' => |
54955
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
333 |
let |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
334 |
val U' = curried_type U; |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
335 |
val T' = curried_type T; |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
336 |
in |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
55400
diff
changeset
|
337 |
Const (@{const_name case_prod}, U' --> U) $ massage_call bound_Ts U' T' t' |
54955
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
338 |
end |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
339 |
| t1 $ t2 => |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
340 |
(if has_call t2 then |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
341 |
massage_mutual_call bound_Ts U T t |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
342 |
else |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
343 |
massage_map bound_Ts U T t1 $ t2 |
58223
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58211
diff
changeset
|
344 |
handle NO_MAP _ => massage_mutual_call bound_Ts U T t) |
54955
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
345 |
| Abs (s, T', t') => |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
346 |
Abs (s, T', massage_call (T' :: bound_Ts) (range_type U) (range_type T) t') |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
347 |
| _ => massage_mutual_call bound_Ts U T t)) |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
348 |
| _ => ill_formed_corec_call ctxt t) |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
349 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
350 |
build_map_Inl (T, U) $ t) bound_Ts; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
351 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
352 |
val T = fastype_of1 (bound_Ts, t); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
353 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
354 |
if has_call t then massage_call bound_Ts U T t else build_map_Inl (T, U) $ t |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
355 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
356 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
357 |
fun expand_to_ctr_term ctxt s Ts t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
358 |
(case ctr_sugar_of ctxt s of |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
359 |
SOME {ctrs, casex, ...} => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
360 |
Term.list_comb (mk_case Ts (Type (s, Ts)) casex, map (mk_ctr Ts) ctrs) $ t |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
361 |
| NONE => raise Fail "expand_to_ctr_term"); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
362 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
363 |
fun expand_corec_code_rhs ctxt has_call bound_Ts t = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
364 |
(case fastype_of1 (bound_Ts, t) of |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
365 |
Type (s, Ts) => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
366 |
massage_let_if_case ctxt has_call (fn _ => fn t => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
367 |
if can (dest_ctr ctxt s) t then t else expand_to_ctr_term ctxt s Ts t) bound_Ts t |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
368 |
| _ => raise Fail "expand_corec_code_rhs"); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
369 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
370 |
fun massage_corec_code_rhs ctxt massage_ctr = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
371 |
massage_let_if_case ctxt (K false) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
372 |
(fn bound_Ts => uncurry (massage_ctr bound_Ts) o Term.strip_comb); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
373 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
374 |
fun fold_rev_corec_code_rhs ctxt f = |
55341
3d2c97392e25
adapted tactic to correctly handle 'if ... then ...' and 'case ...' under lambdas
blanchet
parents:
55339
diff
changeset
|
375 |
fold_rev_let_if_case ctxt (fn conds => uncurry (f conds) o Term.strip_comb); |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
376 |
|
55400 | 377 |
fun case_thms_of_term ctxt t = |
378 |
let val ctr_sugars = map_filter (Ctr_Sugar.ctr_sugar_of_case ctxt o fst) (Term.add_consts t []) in |
|
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
379 |
(maps #distincts ctr_sugars, maps #discIs ctr_sugars, maps #exhaust_discs ctr_sugars, |
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
380 |
maps #split_sels ctr_sugars, maps #split_sel_asms ctr_sugars) |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
381 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
382 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
383 |
fun basic_corec_specs_of ctxt res_T = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
384 |
(case res_T of |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
385 |
Type (T_name, _) => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
386 |
(case Ctr_Sugar.ctr_sugar_of ctxt T_name of |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
387 |
NONE => not_codatatype ctxt res_T |
58187
d2ddd401d74d
fixed infinite loops in 'register' functions + more uniform API
blanchet
parents:
58131
diff
changeset
|
388 |
| SOME {T = fpT, ctrs, discs, selss, ...} => |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
389 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
390 |
val thy = Proof_Context.theory_of ctxt; |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
391 |
|
58187
d2ddd401d74d
fixed infinite loops in 'register' functions + more uniform API
blanchet
parents:
58131
diff
changeset
|
392 |
val As_rho = tvar_subst thy [fpT] [res_T]; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
393 |
val substA = Term.subst_TVars As_rho; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
394 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
395 |
fun mk_spec ctr disc sels = {ctr = substA ctr, disc = substA disc, sels = map substA sels}; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
396 |
in |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
397 |
@{map 3} mk_spec ctrs discs selss |
54911 | 398 |
handle ListPair.UnequalLengths => not_codatatype ctxt res_T |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
399 |
end) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
400 |
| _ => not_codatatype ctxt res_T); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
401 |
|
54955
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
402 |
fun map_thms_of_typ ctxt (Type (s, _)) = |
58462 | 403 |
(case fp_sugar_of ctxt s of SOME {fp_bnf_sugar = {map_thms, ...}, ...} => map_thms | NONE => []) |
54955
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
404 |
| map_thms_of_typ _ _ = []; |
cf8d429dc24e
reintroduce recursive calls under constructors, taken out in 8dd0e0316881 mainly and in subsequent changes
blanchet
parents:
54954
diff
changeset
|
405 |
|
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
406 |
fun corec_specs_of bs arg_Ts res_Ts callers callssss0 lthy0 = |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
407 |
let |
55005 | 408 |
val thy = Proof_Context.theory_of lthy0; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
409 |
|
57397 | 410 |
val ((missing_res_Ts, perm0_kks, fp_sugars as {fp_nesting_bnfs, |
58461 | 411 |
fp_co_induct_sugar = {common_co_inducts = common_coinduct_thms, ...}, ...} :: _, |
412 |
(_, gfp_sugar_thms)), lthy) = |
|
58335
a5a3b576fcfb
generate 'code' attribute only if 'code' plugin is enabled
blanchet
parents:
58286
diff
changeset
|
413 |
nested_to_mutual_fps (K true) Greatest_FP bs res_Ts callers callssss0 lthy0; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
414 |
|
58286 | 415 |
val coinduct_attrs_pair = |
416 |
(case gfp_sugar_thms of SOME ((_, attrs_pair), _, _, _, _) => attrs_pair | NONE => ([], [])); |
|
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
417 |
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
418 |
val perm_fp_sugars = sort (int_ord o pairself #fp_res_index) fp_sugars; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
419 |
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
420 |
val indices = map #fp_res_index fp_sugars; |
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
421 |
val perm_indices = map #fp_res_index perm_fp_sugars; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
422 |
|
58187
d2ddd401d74d
fixed infinite loops in 'register' functions + more uniform API
blanchet
parents:
58131
diff
changeset
|
423 |
val perm_fpTs = map #T perm_fp_sugars; |
58460 | 424 |
val perm_ctrXs_Tsss' = |
425 |
map (repair_nullary_single_ctr o #ctrXs_Tss o #fp_ctr_sugar) perm_fp_sugars; |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
426 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
427 |
val nn0 = length res_Ts; |
58187
d2ddd401d74d
fixed infinite loops in 'register' functions + more uniform API
blanchet
parents:
58131
diff
changeset
|
428 |
val nn = length perm_fpTs; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
429 |
val kks = 0 upto nn - 1; |
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
430 |
val perm_ns' = map length perm_ctrXs_Tsss'; |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
431 |
|
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
432 |
val perm_Ts = map #T perm_fp_sugars; |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
433 |
val perm_Xs = map #X perm_fp_sugars; |
58461 | 434 |
val perm_Cs = |
435 |
map (domain_type o body_fun_type o fastype_of o #co_rec o #fp_co_induct_sugar) perm_fp_sugars; |
|
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
436 |
val Xs_TCs = perm_Xs ~~ (perm_Ts ~~ perm_Cs); |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
437 |
|
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
438 |
fun zip_corecT (Type (s, Us)) = [Type (s, map (mk_sumTN o zip_corecT) Us)] |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
439 |
| zip_corecT U = |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
440 |
(case AList.lookup (op =) Xs_TCs U of |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
441 |
SOME (T, C) => [T, C] |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
442 |
| NONE => [U]); |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
443 |
|
55869 | 444 |
val perm_p_Tss = mk_corec_p_pred_types perm_Cs perm_ns'; |
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
445 |
val perm_f_Tssss = |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
446 |
map2 (fn C => map (map (map (curry (op -->) C) o zip_corecT))) perm_Cs perm_ctrXs_Tsss'; |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
447 |
val perm_q_Tssss = |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
448 |
map (map (map (fn [_] => [] | [_, T] => [mk_pred1T (domain_type T)]))) perm_f_Tssss; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
449 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
450 |
val (perm_p_hss, h) = indexedd perm_p_Tss 0; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
451 |
val (perm_q_hssss, h') = indexedddd perm_q_Tssss h; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
452 |
val (perm_f_hssss, _) = indexedddd perm_f_Tssss h'; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
453 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
454 |
val fun_arg_hs = |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
455 |
flat (@{map 3} flat_corec_preds_predsss_gettersss perm_p_hss perm_q_hssss perm_f_hssss); |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
456 |
|
55480
59cc4a8bc28a
allow different functions to recurse on the same type, like in the old package
blanchet
parents:
55462
diff
changeset
|
457 |
fun unpermute0 perm0_xs = permute_like_unique (op =) perm0_kks kks perm0_xs; |
59cc4a8bc28a
allow different functions to recurse on the same type, like in the old package
blanchet
parents:
55462
diff
changeset
|
458 |
fun unpermute perm_xs = permute_like_unique (op =) perm_indices indices perm_xs; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
459 |
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
460 |
val coinduct_thmss = map (unpermute0 o conj_dests nn) common_coinduct_thms; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
461 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
462 |
val p_iss = map (map (find_index_eq fun_arg_hs)) (unpermute perm_p_hss); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
463 |
val q_issss = map (map (map (map (find_index_eq fun_arg_hs)))) (unpermute perm_q_hssss); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
464 |
val f_issss = map (map (map (map (find_index_eq fun_arg_hs)))) (unpermute perm_f_hssss); |
53358 | 465 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
466 |
val f_Tssss = unpermute perm_f_Tssss; |
58187
d2ddd401d74d
fixed infinite loops in 'register' functions + more uniform API
blanchet
parents:
58131
diff
changeset
|
467 |
val fpTs = unpermute perm_fpTs; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
468 |
val Cs = unpermute perm_Cs; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
469 |
|
58187
d2ddd401d74d
fixed infinite loops in 'register' functions + more uniform API
blanchet
parents:
58131
diff
changeset
|
470 |
val As_rho = tvar_subst thy (take nn0 fpTs) res_Ts; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
471 |
val Cs_rho = map (fst o dest_TVar) Cs ~~ pad_list HOLogic.unitT nn arg_Ts; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
472 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
473 |
val substA = Term.subst_TVars As_rho; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
474 |
val substAT = Term.typ_subst_TVars As_rho; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
475 |
val substCT = Term.typ_subst_TVars Cs_rho; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
476 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
477 |
val perm_Cs' = map substCT perm_Cs; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
478 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
479 |
fun call_of nullary [] [g_i] [Type (@{type_name fun}, [_, T])] = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
480 |
(if exists_subtype_in Cs T then Nested_Corec |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
481 |
else if nullary then Dummy_No_Corec |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
482 |
else No_Corec) g_i |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
483 |
| call_of _ [q_i] [g_i, g_i'] _ = Mutual_Corec (q_i, g_i, g_i'); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
484 |
|
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
485 |
fun mk_ctr_spec ctr disc sels p_io q_iss f_iss f_Tss discI sel_thms distinct_discss collapse |
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
486 |
corec_thm corec_disc corec_sels = |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
487 |
let val nullary = not (can dest_funT (fastype_of ctr)) in |
54900 | 488 |
{ctr = substA ctr, disc = substA disc, sels = map substA sels, pred = p_io, |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
489 |
calls = @{map 3} (call_of nullary) q_iss f_iss f_Tss, discI = discI, sel_thms = sel_thms, |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
490 |
distinct_discss = distinct_discss, collapse = collapse, corec_thm = corec_thm, |
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
491 |
corec_disc = corec_disc, corec_sels = corec_sels} |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
492 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
493 |
|
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
494 |
fun mk_ctr_specs ({ctrs, discs, selss, discIs, sel_thmss, distinct_discsss, collapses, ...} |
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
495 |
: ctr_sugar) p_is q_isss f_isss f_Tsss corec_thms corec_discs corec_selss = |
55863 | 496 |
let val p_ios = map SOME p_is @ [NONE] in |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
497 |
@{map 14} mk_ctr_spec ctrs discs selss p_ios q_isss f_isss f_Tsss discIs sel_thmss |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
498 |
distinct_discsss collapses corec_thms corec_discs corec_selss |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
499 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
500 |
|
58461 | 501 |
fun mk_spec ({T, fp_ctr_sugar = {ctr_sugar as {exhaust_discs, sel_defs, ...}, ...}, |
502 |
fp_co_induct_sugar = {co_rec = corec, co_rec_thms = corec_thms, co_rec_discs = corec_discs, |
|
58459 | 503 |
co_rec_selss = corec_selss, ...}, ...} : fp_sugar) p_is q_isss f_isss f_Tsss = |
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58187
diff
changeset
|
504 |
{corec = mk_co_rec thy Greatest_FP perm_Cs' (substAT T) corec, exhaust_discs = exhaust_discs, |
57397 | 505 |
sel_defs = sel_defs, |
506 |
fp_nesting_maps = maps (map_thms_of_typ lthy o T_of_bnf) fp_nesting_bnfs, |
|
57399 | 507 |
fp_nesting_map_ident0s = map map_ident0_of_bnf fp_nesting_bnfs, |
57397 | 508 |
fp_nesting_map_comps = map map_comp_of_bnf fp_nesting_bnfs, |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
509 |
ctr_specs = mk_ctr_specs ctr_sugar p_is q_isss f_isss f_Tsss corec_thms corec_discs |
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
510 |
corec_selss}; |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
511 |
in |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
512 |
(@{map 5} mk_spec fp_sugars p_iss q_issss f_issss f_Tssss, missing_res_Ts, |
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
513 |
co_induct_of common_coinduct_thms, strong_co_induct_of common_coinduct_thms, |
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
514 |
co_induct_of coinduct_thmss, strong_co_induct_of coinduct_thmss, coinduct_attrs_pair, |
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
515 |
is_some gfp_sugar_thms, lthy) |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
516 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
517 |
|
53358 | 518 |
val undef_const = Const (@{const_name undefined}, dummyT); |
53357 | 519 |
|
55969 | 520 |
val abs_tuple_balanced = HOLogic.tupled_lambda o mk_tuple_balanced; |
55005 | 521 |
|
53720 | 522 |
fun abstract vs = |
55342 | 523 |
let |
524 |
fun abs n (t $ u) = abs n t $ abs n u |
|
525 |
| abs n (Abs (v, T, b)) = Abs (v, T, abs (n + 1) b) |
|
526 |
| abs n t = |
|
527 |
let val j = find_index (curry (op =) t) vs in |
|
528 |
if j < 0 then t else Bound (n + j) |
|
529 |
end; |
|
530 |
in abs 0 end; |
|
54271 | 531 |
|
58393 | 532 |
type coeqn_data_disc = |
533 |
{fun_name: string, |
|
534 |
fun_T: typ, |
|
535 |
fun_args: term list, |
|
536 |
ctr: term, |
|
537 |
ctr_no: int, |
|
538 |
disc: term, |
|
539 |
prems: term list, |
|
540 |
auto_gen: bool, |
|
541 |
ctr_rhs_opt: term option, |
|
542 |
code_rhs_opt: term option, |
|
543 |
eqn_pos: int, |
|
544 |
user_eqn: term}; |
|
54001 | 545 |
|
58393 | 546 |
type coeqn_data_sel = |
547 |
{fun_name: string, |
|
548 |
fun_T: typ, |
|
549 |
fun_args: term list, |
|
550 |
ctr: term, |
|
551 |
sel: term, |
|
552 |
rhs_term: term, |
|
553 |
ctr_rhs_opt: term option, |
|
554 |
code_rhs_opt: term option, |
|
555 |
eqn_pos: int, |
|
556 |
user_eqn: term}; |
|
54001 | 557 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
558 |
datatype coeqn_data = |
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
559 |
Disc of coeqn_data_disc | |
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
560 |
Sel of coeqn_data_sel; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
561 |
|
56152 | 562 |
fun check_extra_variables lthy vars names eqn = |
563 |
let val b = fold_aterms (fn x as Free (v, _) => |
|
564 |
if (not (member (op =) vars x) andalso |
|
565 |
not (member (op =) names v) andalso |
|
566 |
v <> Name.uu_ andalso |
|
567 |
not (Variable.is_fixed lthy v)) then cons x else I | _ => I) eqn [] |
|
568 |
in |
|
569 |
null b orelse |
|
570 |
primcorec_error_eqn ("extra variable(s) in equation: " ^ |
|
571 |
commas (map (Syntax.string_of_term lthy) b)) eqn |
|
572 |
end; |
|
573 |
||
574 |
fun dissect_coeqn_disc lthy fun_names sequentials (basic_ctr_specss : basic_corec_ctr_spec list list) |
|
54948 | 575 |
eqn_pos ctr_rhs_opt code_rhs_opt prems' concl matchedsss = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
576 |
let |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
577 |
fun find_subterm p = |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
578 |
let (* FIXME \<exists>? *) |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
579 |
fun find (t as u $ v) = if p t then SOME t else merge_options (find u, find v) |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
580 |
| find t = if p t then SOME t else NONE; |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
581 |
in find end; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
582 |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
583 |
val applied_fun = concl |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
584 |
|> find_subterm (member (op = o apsnd SOME) fun_names o try (fst o dest_Free o head_of)) |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
585 |
|> the |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
586 |
handle Option.Option => primcorec_error_eqn "malformed discriminator formula" concl; |
53720 | 587 |
val ((fun_name, fun_T), fun_args) = strip_comb applied_fun |>> dest_Free; |
56152 | 588 |
|
58393 | 589 |
val fixeds = filter (Variable.is_fixed lthy o fst o dest_Free) fun_args; |
590 |
val _ = null fixeds orelse |
|
591 |
primcorec_error_eqns "function argument(s) are fixed in context" fixeds; |
|
56152 | 592 |
|
58393 | 593 |
val bad = |
594 |
filter (exists_subterm (fn Free (v, _) => member (op =) fun_names v | _ => false)) prems'; |
|
595 |
val _ = null bad orelse primcorec_error_eqns "corecursive call(s) in condition(s)" bad; |
|
56152 | 596 |
|
597 |
val _ = forall is_Free fun_args orelse |
|
598 |
primcorec_error_eqn ("non-variable function argument \"" ^ |
|
599 |
Syntax.string_of_term lthy (find_first (not o is_Free) fun_args |> the) ^ |
|
600 |
"\" (pattern matching is not supported by primcorec(ursive))") applied_fun |
|
601 |
||
602 |
val _ = let val d = duplicates (op =) fun_args in null d orelse |
|
603 |
primcorec_error_eqn ("duplicate variable \"" ^ Syntax.string_of_term lthy (hd d) ^ "\"") |
|
604 |
applied_fun end; |
|
605 |
||
58393 | 606 |
val (sequential, basic_ctr_specs) = |
607 |
the (AList.lookup (op =) (fun_names ~~ (sequentials ~~ basic_ctr_specss)) fun_name); |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
608 |
|
54160 | 609 |
val discs = map #disc basic_ctr_specs; |
610 |
val ctrs = map #ctr basic_ctr_specs; |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
611 |
val not_disc = head_of concl = @{term Not}; |
53401 | 612 |
val _ = not_disc andalso length ctrs <> 2 andalso |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
613 |
primcorec_error_eqn "negated discriminator for a type with \<noteq> 2 constructors" concl; |
54160 | 614 |
val disc' = find_subterm (member (op =) discs o head_of) concl; |
54209 | 615 |
val eq_ctr0 = concl |> perhaps (try HOLogic.dest_not) |> try (HOLogic.dest_eq #> snd) |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
616 |
|> (fn SOME t => let val n = find_index (curry (op =) t) ctrs in |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
617 |
if n >= 0 then SOME n else NONE end | _ => NONE); |
56152 | 618 |
|
619 |
val _ = if is_some disc' andalso perhaps (try HOLogic.dest_not) concl <> the disc' |
|
620 |
then primcorec_error_eqn "malformed discriminator formula" concl else (); |
|
621 |
||
54160 | 622 |
val _ = is_some disc' orelse is_some eq_ctr0 orelse |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
623 |
primcorec_error_eqn "no discriminator in equation" concl; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
624 |
val ctr_no' = |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
625 |
if is_none disc' then the eq_ctr0 else find_index (curry (op =) (head_of (the disc'))) discs; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
626 |
val ctr_no = if not_disc then 1 - ctr_no' else ctr_no'; |
54160 | 627 |
val {ctr, disc, ...} = nth basic_ctr_specs ctr_no; |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
628 |
|
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
629 |
val catch_all = try (fst o dest_Free o the_single) prems' = SOME Name.uu_; |
53720 | 630 |
val matchedss = AList.lookup (op =) matchedsss fun_name |> the_default []; |
631 |
val prems = map (abstract (List.rev fun_args)) prems'; |
|
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
632 |
val actual_prems = |
54901
0b8871677e0b
use same name for feature internally as in user interface, to facilitate grepping
blanchet
parents:
54900
diff
changeset
|
633 |
(if catch_all orelse sequential then maps s_not_conj matchedss else []) @ |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
634 |
(if catch_all then [] else prems); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
635 |
|
53720 | 636 |
val matchedsss' = AList.delete (op =) fun_name matchedsss |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
637 |
|> cons (fun_name, if sequential then matchedss @ [prems] else matchedss @ [actual_prems]); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
638 |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
639 |
val user_eqn = |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
640 |
(actual_prems, concl) |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
641 |
|>> map HOLogic.mk_Trueprop ||> HOLogic.mk_Trueprop o abstract (List.rev fun_args) |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
642 |
|> curry Logic.list_all (map dest_Free fun_args) o Logic.list_implies; |
56152 | 643 |
|
644 |
val _ = check_extra_variables lthy fun_args fun_names user_eqn; |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
645 |
in |
58393 | 646 |
(Disc {fun_name = fun_name, fun_T = fun_T, fun_args = fun_args, ctr = ctr, ctr_no = ctr_no, |
647 |
disc = disc, prems = actual_prems, auto_gen = catch_all, ctr_rhs_opt = ctr_rhs_opt, |
|
648 |
code_rhs_opt = code_rhs_opt, eqn_pos = eqn_pos, user_eqn = user_eqn}, |
|
649 |
matchedsss') |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
650 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
651 |
|
56152 | 652 |
fun dissect_coeqn_sel lthy fun_names (basic_ctr_specss : basic_corec_ctr_spec list list) eqn_pos |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
653 |
ctr_rhs_opt code_rhs_opt eqn0 of_spec_opt eqn = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
654 |
let |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
655 |
val (lhs, rhs) = HOLogic.dest_eq eqn |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
656 |
handle TERM _ => |
55527 | 657 |
primcorec_error_eqn "malformed function equation (expected \"lhs = rhs\")" eqn; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
658 |
val sel = head_of lhs; |
53720 | 659 |
val ((fun_name, fun_T), fun_args) = dest_comb lhs |> snd |> strip_comb |> apfst dest_Free |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
660 |
handle TERM _ => |
55527 | 661 |
primcorec_error_eqn "malformed selector argument in left-hand side" eqn; |
56152 | 662 |
|
663 |
val _ = let val fixeds = filter (Variable.is_fixed lthy o fst o dest_Free) fun_args in |
|
664 |
null fixeds orelse primcorec_error_eqns "function argument(s) are fixed in context" fixeds |
|
665 |
end; |
|
666 |
||
54160 | 667 |
val basic_ctr_specs = the (AList.lookup (op =) (fun_names ~~ basic_ctr_specss) fun_name) |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
668 |
handle Option.Option => |
55527 | 669 |
primcorec_error_eqn "malformed selector argument in left-hand side" eqn; |
54160 | 670 |
val {ctr, ...} = |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
671 |
(case of_spec_opt of |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
672 |
SOME of_spec => the (find_first (curry (op =) of_spec o #ctr) basic_ctr_specs) |
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
673 |
| NONE => filter (exists (curry (op =) sel) o #sels) basic_ctr_specs |> the_single |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
674 |
handle List.Empty => primcorec_error_eqn "ambiguous selector - use \"of\"" eqn); |
54979
d7593bfccf25
correctly extract code RHS, with loose bound variables
blanchet
parents:
54978
diff
changeset
|
675 |
val user_eqn = drop_all eqn0; |
56152 | 676 |
|
677 |
val _ = check_extra_variables lthy fun_args fun_names user_eqn; |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
678 |
in |
58393 | 679 |
Sel {fun_name = fun_name, fun_T = fun_T, fun_args = fun_args, ctr = ctr, sel = sel, |
680 |
rhs_term = rhs, ctr_rhs_opt = ctr_rhs_opt, code_rhs_opt = code_rhs_opt, eqn_pos = eqn_pos, |
|
681 |
user_eqn = user_eqn} |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
682 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
683 |
|
56152 | 684 |
fun dissect_coeqn_ctr lthy fun_names sequentials (basic_ctr_specss : basic_corec_ctr_spec list list) |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
685 |
eqn_pos eqn0 code_rhs_opt prems concl matchedsss = |
53910 | 686 |
let |
54065 | 687 |
val (lhs, rhs) = HOLogic.dest_eq concl; |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
688 |
val (fun_name, fun_args) = strip_comb lhs |>> fst o dest_Free; |
56152 | 689 |
|
690 |
val _ = check_extra_variables lthy fun_args fun_names (drop_all eqn0); |
|
691 |
||
58393 | 692 |
val basic_ctr_specs = the (AList.lookup (op =) (fun_names ~~ basic_ctr_specss) fun_name); |
55343 | 693 |
val (ctr, ctr_args) = strip_comb (unfold_lets_splits rhs); |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
694 |
val {disc, sels, ...} = the (find_first (curry (op =) ctr o #ctr) basic_ctr_specs) |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
695 |
handle Option.Option => primcorec_error_eqn "not a constructor" ctr; |
53341 | 696 |
|
54065 | 697 |
val disc_concl = betapply (disc, lhs); |
54976
b502f04c0442
repair 'exhaustive' feature for one-constructor types
blanchet
parents:
54975
diff
changeset
|
698 |
val (eqn_data_disc_opt, matchedsss') = |
b502f04c0442
repair 'exhaustive' feature for one-constructor types
blanchet
parents:
54975
diff
changeset
|
699 |
if null (tl basic_ctr_specs) then |
b502f04c0442
repair 'exhaustive' feature for one-constructor types
blanchet
parents:
54975
diff
changeset
|
700 |
(NONE, matchedsss) |
b502f04c0442
repair 'exhaustive' feature for one-constructor types
blanchet
parents:
54975
diff
changeset
|
701 |
else |
56152 | 702 |
apfst SOME (dissect_coeqn_disc lthy fun_names sequentials basic_ctr_specss eqn_pos |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
703 |
(SOME (abstract (List.rev fun_args) rhs)) code_rhs_opt prems disc_concl matchedsss); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
704 |
|
54159
eb5d58c99049
set stage for more flexible 'primrec' syntax for recursion through functions
blanchet
parents:
54157
diff
changeset
|
705 |
val sel_concls = sels ~~ ctr_args |
56152 | 706 |
|> map (fn (sel, ctr_arg) => HOLogic.mk_eq (betapply (sel, lhs), ctr_arg)) |
56806
f7d0520e7be2
use qualified name (was interpreted as a catch-all variable name)
panny
parents:
56805
diff
changeset
|
707 |
handle ListPair.UnequalLengths => |
56152 | 708 |
primcorec_error_eqn "partially applied constructor in right-hand side" rhs; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
709 |
|
54160 | 710 |
val eqns_data_sel = |
56152 | 711 |
map (dissect_coeqn_sel lthy fun_names basic_ctr_specss eqn_pos |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
712 |
(SOME (abstract (List.rev fun_args) rhs)) code_rhs_opt eqn0 (SOME ctr)) sel_concls; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
713 |
in |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
714 |
(the_list eqn_data_disc_opt @ eqns_data_sel, matchedsss') |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
715 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
716 |
|
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
717 |
fun dissect_coeqn_code lthy has_call fun_names basic_ctr_specss eqn_pos eqn0 concl matchedsss = |
54065 | 718 |
let |
719 |
val (lhs, (rhs', rhs)) = HOLogic.dest_eq concl ||> `(expand_corec_code_rhs lthy has_call []); |
|
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
720 |
val (fun_name, fun_args) = strip_comb lhs |>> fst o dest_Free; |
56152 | 721 |
|
722 |
val _ = check_extra_variables lthy fun_args fun_names concl; |
|
723 |
||
58393 | 724 |
val basic_ctr_specs = the (AList.lookup (op =) (fun_names ~~ basic_ctr_specss) fun_name); |
54065 | 725 |
|
726 |
val cond_ctrs = fold_rev_corec_code_rhs lthy (fn cs => fn ctr => fn _ => |
|
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
727 |
if member (op = o apsnd #ctr) basic_ctr_specs ctr then cons (ctr, cs) |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
728 |
else primcorec_error_eqn "not a constructor" ctr) [] rhs' [] |
54065 | 729 |
|> AList.group (op =); |
730 |
||
54068 | 731 |
val ctr_premss = (case cond_ctrs of [_] => [[]] | _ => map (s_dnf o snd) cond_ctrs); |
54065 | 732 |
val ctr_concls = cond_ctrs |> map (fn (ctr, _) => |
58393 | 733 |
binder_types (fastype_of ctr) |
734 |
|> map_index (fn (n, T) => massage_corec_code_rhs lthy (fn _ => fn ctr' => fn args => |
|
735 |
if ctr' = ctr then nth args n else Const (@{const_name undefined}, T)) [] rhs') |
|
736 |
|> curry Term.list_comb ctr |
|
737 |
|> curry HOLogic.mk_eq lhs); |
|
54902
a9291e4d2366
internally allow different values for 'sequential' for different constructors
blanchet
parents:
54901
diff
changeset
|
738 |
|
a9291e4d2366
internally allow different values for 'sequential' for different constructors
blanchet
parents:
54901
diff
changeset
|
739 |
val sequentials = replicate (length fun_names) false; |
54065 | 740 |
in |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
741 |
@{fold_map 2} (dissect_coeqn_ctr lthy fun_names sequentials basic_ctr_specss eqn_pos eqn0 |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
742 |
(SOME (abstract (List.rev fun_args) rhs))) |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
743 |
ctr_premss ctr_concls matchedsss |
54065 | 744 |
end; |
745 |
||
54902
a9291e4d2366
internally allow different values for 'sequential' for different constructors
blanchet
parents:
54901
diff
changeset
|
746 |
fun dissect_coeqn lthy has_call fun_names sequentials |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
747 |
(basic_ctr_specss : basic_corec_ctr_spec list list) (eqn_pos, eqn0) of_spec_opt matchedsss = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
748 |
let |
54979
d7593bfccf25
correctly extract code RHS, with loose bound variables
blanchet
parents:
54978
diff
changeset
|
749 |
val eqn = drop_all eqn0 |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
750 |
handle TERM _ => primcorec_error_eqn "malformed function equation" eqn0; |
54065 | 751 |
val (prems, concl) = Logic.strip_horn eqn |
57527 | 752 |
|> map_prod (map HOLogic.dest_Trueprop) HOLogic.dest_Trueprop |
56152 | 753 |
handle TERM _ => primcorec_error_eqn "malformed function equation" eqn; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
754 |
|
54065 | 755 |
val head = concl |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
756 |
|> perhaps (try HOLogic.dest_not) |> perhaps (try (fst o HOLogic.dest_eq)) |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
757 |
|> head_of; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
758 |
|
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
759 |
val rhs_opt = concl |> perhaps (try HOLogic.dest_not) |> try (snd o HOLogic.dest_eq); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
760 |
|
54160 | 761 |
val discs = maps (map #disc) basic_ctr_specss; |
762 |
val sels = maps (maps #sels) basic_ctr_specss; |
|
763 |
val ctrs = maps (map #ctr) basic_ctr_specss; |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
764 |
in |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
765 |
if member (op =) discs head orelse |
58393 | 766 |
(is_some rhs_opt andalso |
767 |
member (op =) (map SOME fun_names) (try (fst o dest_Free) head) andalso |
|
768 |
member (op =) (filter (null o binder_types o fastype_of) ctrs) (the rhs_opt)) then |
|
56152 | 769 |
dissect_coeqn_disc lthy fun_names sequentials basic_ctr_specss eqn_pos NONE NONE prems concl |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
770 |
matchedsss |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
771 |
|>> single |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
772 |
else if member (op =) sels head then |
58393 | 773 |
(null prems orelse primcorec_error_eqn "premise(s) in selector formula" eqn; |
57550 | 774 |
([dissect_coeqn_sel lthy fun_names basic_ctr_specss eqn_pos NONE NONE eqn0 of_spec_opt |
775 |
concl], matchedsss)) |
|
56152 | 776 |
else if is_some rhs_opt andalso |
777 |
is_Free head andalso member (op =) fun_names (fst (dest_Free head)) then |
|
55343 | 778 |
if member (op =) ctrs (head_of (unfold_lets_splits (the rhs_opt))) then |
56152 | 779 |
dissect_coeqn_ctr lthy fun_names sequentials basic_ctr_specss eqn_pos eqn0 |
54979
d7593bfccf25
correctly extract code RHS, with loose bound variables
blanchet
parents:
54978
diff
changeset
|
780 |
(if null prems then |
d7593bfccf25
correctly extract code RHS, with loose bound variables
blanchet
parents:
54978
diff
changeset
|
781 |
SOME (snd (HOLogic.dest_eq (HOLogic.dest_Trueprop (Logic.strip_assums_concl eqn0)))) |
d7593bfccf25
correctly extract code RHS, with loose bound variables
blanchet
parents:
54978
diff
changeset
|
782 |
else |
d7593bfccf25
correctly extract code RHS, with loose bound variables
blanchet
parents:
54978
diff
changeset
|
783 |
NONE) |
54975 | 784 |
prems concl matchedsss |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
785 |
else if null prems then |
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
786 |
dissect_coeqn_code lthy has_call fun_names basic_ctr_specss eqn_pos eqn0 concl matchedsss |
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
787 |
|>> flat |
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
788 |
else |
54956 | 789 |
primcorec_error_eqn "cannot mix constructor and code views (see manual for details)" eqn |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
790 |
else |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
791 |
primcorec_error_eqn "malformed function equation" eqn |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
792 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
793 |
|
54002 | 794 |
fun build_corec_arg_disc (ctr_specs : corec_ctr_spec list) |
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
795 |
({fun_args, ctr_no, prems, ...} : coeqn_data_disc) = |
56858 | 796 |
if is_none (#pred (nth ctr_specs ctr_no)) then |
797 |
I |
|
798 |
else |
|
54068 | 799 |
s_conjs prems |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
800 |
|> curry subst_bounds (List.rev fun_args) |
55969 | 801 |
|> abs_tuple_balanced fun_args |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
802 |
|> K |> nth_map (the (#pred (nth ctr_specs ctr_no))); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
803 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
804 |
fun build_corec_arg_no_call (sel_eqns : coeqn_data_sel list) sel = |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
805 |
find_first (curry (op =) sel o #sel) sel_eqns |
55969 | 806 |
|> try (fn SOME {fun_args, rhs_term, ...} => abs_tuple_balanced fun_args rhs_term) |
53720 | 807 |
|> the_default undef_const |
53411 | 808 |
|> K; |
53360 | 809 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
810 |
fun build_corec_args_mutual_call lthy has_call (sel_eqns : coeqn_data_sel list) sel = |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
811 |
(case find_first (curry (op =) sel o #sel) sel_eqns of |
54208 | 812 |
NONE => (I, I, I) |
813 |
| SOME {fun_args, rhs_term, ... } => |
|
53876 | 814 |
let |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
815 |
val bound_Ts = List.rev (map fastype_of fun_args); |
54207
9296ebf40db0
tuned names (to make them independent from temporary naming convention used in characteristic theorems)
blanchet
parents:
54206
diff
changeset
|
816 |
fun rewrite_stop _ t = if has_call t then @{term False} else @{term True}; |
9296ebf40db0
tuned names (to make them independent from temporary naming convention used in characteristic theorems)
blanchet
parents:
54206
diff
changeset
|
817 |
fun rewrite_end _ t = if has_call t then undef_const else t; |
9296ebf40db0
tuned names (to make them independent from temporary naming convention used in characteristic theorems)
blanchet
parents:
54206
diff
changeset
|
818 |
fun rewrite_cont bound_Ts t = |
55969 | 819 |
if has_call t then mk_tuple1_balanced bound_Ts (snd (strip_comb t)) else undef_const; |
55339
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
820 |
fun massage f _ = massage_let_if_case lthy has_call f bound_Ts rhs_term |
55969 | 821 |
|> abs_tuple_balanced fun_args; |
53876 | 822 |
in |
54207
9296ebf40db0
tuned names (to make them independent from temporary naming convention used in characteristic theorems)
blanchet
parents:
54206
diff
changeset
|
823 |
(massage rewrite_stop, massage rewrite_end, massage rewrite_cont) |
54208 | 824 |
end); |
53360 | 825 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
826 |
fun build_corec_arg_nested_call lthy has_call (sel_eqns : coeqn_data_sel list) sel = |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
827 |
(case find_first (curry (op =) sel o #sel) sel_eqns of |
54208 | 828 |
NONE => I |
829 |
| SOME {fun_args, rhs_term, ...} => |
|
53899 | 830 |
let |
55339
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
831 |
fun massage bound_Ts U T = |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
832 |
let |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
833 |
fun rewrite bound_Ts (Abs (v, V, b)) = Abs (v, V, rewrite (V :: bound_Ts) b) |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
834 |
| rewrite bound_Ts (t as _ $ _) = |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
835 |
let val (u, vs) = strip_comb t in |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
836 |
if is_Free u andalso has_call u then |
55969 | 837 |
Inr_const U T $ mk_tuple1_balanced bound_Ts vs |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
55400
diff
changeset
|
838 |
else if try (fst o dest_Const) u = SOME @{const_name case_prod} then |
55343 | 839 |
map (rewrite bound_Ts) vs |> chop 1 |
840 |
|>> HOLogic.mk_split o the_single |
|
841 |
|> Term.list_comb |
|
55339
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
842 |
else |
55343 | 843 |
Term.list_comb (rewrite bound_Ts u, map (rewrite bound_Ts) vs) |
55339
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
844 |
end |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
845 |
| rewrite _ t = |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
846 |
if is_Free t andalso has_call t then Inr_const U T $ HOLogic.unit else t; |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
847 |
in |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
848 |
rewrite bound_Ts |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
849 |
end; |
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
850 |
|
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
851 |
val bound_Ts = List.rev (map fastype_of fun_args); |
55339
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
852 |
|
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
853 |
fun build t = |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
854 |
rhs_term |
55339
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
855 |
|> massage_nested_corec_call lthy has_call massage bound_Ts (range_type (fastype_of t)) |
55969 | 856 |
|> abs_tuple_balanced fun_args; |
53899 | 857 |
in |
55339
f09037306f25
properly massage 'if's / 'case's etc. under lambdas
blanchet
parents:
55100
diff
changeset
|
858 |
build |
54208 | 859 |
end); |
53360 | 860 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
861 |
fun build_corec_args_sel lthy has_call (all_sel_eqns : coeqn_data_sel list) |
54002 | 862 |
(ctr_spec : corec_ctr_spec) = |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
863 |
(case filter (curry (op =) (#ctr ctr_spec) o #ctr) all_sel_eqns of |
54208 | 864 |
[] => I |
865 |
| sel_eqns => |
|
866 |
let |
|
867 |
val sel_call_list = #sels ctr_spec ~~ #calls ctr_spec; |
|
868 |
val no_calls' = map_filter (try (apsnd (fn No_Corec n => n))) sel_call_list; |
|
869 |
val mutual_calls' = map_filter (try (apsnd (fn Mutual_Corec n => n))) sel_call_list; |
|
870 |
val nested_calls' = map_filter (try (apsnd (fn Nested_Corec n => n))) sel_call_list; |
|
871 |
in |
|
872 |
I |
|
873 |
#> fold (fn (sel, n) => nth_map n (build_corec_arg_no_call sel_eqns sel)) no_calls' |
|
874 |
#> fold (fn (sel, (q, g, h)) => |
|
875 |
let val (fq, fg, fh) = build_corec_args_mutual_call lthy has_call sel_eqns sel in |
|
876 |
nth_map q fq o nth_map g fg o nth_map h fh end) mutual_calls' |
|
877 |
#> fold (fn (sel, n) => nth_map n |
|
878 |
(build_corec_arg_nested_call lthy has_call sel_eqns sel)) nested_calls' |
|
879 |
end); |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
880 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
881 |
fun build_codefs lthy bs mxs has_call arg_Tss (corec_specs : corec_spec list) |
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
882 |
(disc_eqnss : coeqn_data_disc list list) (sel_eqnss : coeqn_data_sel list list) = |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
883 |
let |
54160 | 884 |
val corecs = map #corec corec_specs; |
885 |
val ctr_specss = map #ctr_specs corec_specs; |
|
53360 | 886 |
val corec_args = hd corecs |
887 |
|> fst o split_last o binder_types o fastype_of |
|
55966 | 888 |
|> map (fn T => if range_type T = HOLogic.boolT |
54806
a0f024caa04c
pass auto-proved exhaustiveness properties to tactic;
panny
parents:
54628
diff
changeset
|
889 |
then Abs (Name.uu_, domain_type T, @{term False}) |
a0f024caa04c
pass auto-proved exhaustiveness properties to tactic;
panny
parents:
54628
diff
changeset
|
890 |
else Const (@{const_name undefined}, T)) |
53720 | 891 |
|> fold2 (fold o build_corec_arg_disc) ctr_specss disc_eqnss |
53360 | 892 |
|> fold2 (fold o build_corec_args_sel lthy has_call) sel_eqnss ctr_specss; |
59041 | 893 |
|
53735 | 894 |
fun currys [] t = t |
55969 | 895 |
| currys Ts t = t $ mk_tuple1_balanced (List.rev Ts) (map Bound (length Ts - 1 downto 0)) |
53735 | 896 |
|> fold_rev (Term.abs o pair Name.uu) Ts; |
53401 | 897 |
|
54835 | 898 |
val excludess' = |
53720 | 899 |
disc_eqnss |
53822 | 900 |
|> map (map (fn x => (#fun_args x, #ctr_no x, #prems x, #auto_gen x)) |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
901 |
#> fst o (fn xs => fold_map (fn x => fn ys => ((x, ys), ys @ [x])) xs []) |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
902 |
#> maps (uncurry (map o pair) |
53822 | 903 |
#> map (fn ((fun_args, c, x, a), (_, c', y, a')) => |
54068 | 904 |
((c, c', a orelse a'), (x, s_not (s_conjs y))) |
57527 | 905 |
||> map_prod (map HOLogic.mk_Trueprop) HOLogic.mk_Trueprop |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
906 |
||> Logic.list_implies |
55342 | 907 |
||> curry Logic.list_all (map dest_Free fun_args)))); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
908 |
in |
55343 | 909 |
map (Term.list_comb o rpair corec_args) corecs |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
910 |
|> map2 (fn Ts => fn t => if length Ts = 0 then t $ HOLogic.unit else t) arg_Tss |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
911 |
|> map2 currys arg_Tss |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
912 |
|> Syntax.check_terms lthy |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
913 |
|> @{map 3} (fn b => fn mx => fn t => ((b, mx), ((Binding.conceal (Thm.def_binding b), []), t))) |
54155 | 914 |
bs mxs |
54835 | 915 |
|> rpair excludess' |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
916 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
917 |
|
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
918 |
fun mk_actual_disc_eqns fun_binding arg_Ts exhaustive ({ctr_specs, ...} : corec_spec) |
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
919 |
(sel_eqns : coeqn_data_sel list) (disc_eqns : coeqn_data_disc list) = |
54910 | 920 |
let val num_disc_eqns = length disc_eqns in |
54912 | 921 |
if (exhaustive andalso num_disc_eqns <> 0) orelse num_disc_eqns <> length ctr_specs - 1 then |
54910 | 922 |
disc_eqns |
923 |
else |
|
924 |
let |
|
925 |
val n = 0 upto length ctr_specs |
|
55342 | 926 |
|> the o find_first (fn j => not (exists (curry (op =) j o #ctr_no) disc_eqns)); |
54958
4933165fd112
do not use wrong constructor in auto-generated proof goal
panny
parents:
54956
diff
changeset
|
927 |
val {ctr, disc, ...} = nth ctr_specs n; |
58393 | 928 |
val sel_eqn_opt = find_first (equal ctr o #ctr) sel_eqns; |
929 |
||
930 |
val fun_name = Binding.name_of fun_binding; |
|
931 |
val fun_T = arg_Ts ---> body_type (fastype_of (#ctr (hd ctr_specs))); |
|
54910 | 932 |
val fun_args = (try (#fun_args o hd) disc_eqns, try (#fun_args o hd) sel_eqns) |
933 |
|> the_default (map (curry Free Name.uu) arg_Ts) o merge_options; |
|
58393 | 934 |
val prems = maps (s_not_conj o #prems) disc_eqns; |
935 |
val ctr_rhs_opt = Option.map #ctr_rhs_opt sel_eqn_opt |> the_default NONE; |
|
936 |
val code_rhs_opt = Option.map #code_rhs_opt sel_eqn_opt |> the_default NONE; |
|
937 |
val eqn_pos = Option.map (curry (op +) 1 o #eqn_pos) sel_eqn_opt |> the_default 100000 (* FIXME *); |
|
938 |
||
939 |
val extra_disc_eqn = |
|
940 |
{fun_name = fun_name, fun_T = fun_T, fun_args = fun_args, ctr = ctr, ctr_no = n, |
|
941 |
disc = disc, prems = prems, auto_gen = true, ctr_rhs_opt = ctr_rhs_opt, |
|
942 |
code_rhs_opt = code_rhs_opt, eqn_pos = eqn_pos, user_eqn = undef_const}; |
|
54910 | 943 |
in |
944 |
chop n disc_eqns ||> cons extra_disc_eqn |> (op @) |
|
945 |
end |
|
946 |
end; |
|
53720 | 947 |
|
55100 | 948 |
fun find_corec_calls ctxt has_call (basic_ctr_specs : basic_corec_ctr_spec list) |
949 |
({ctr, sel, rhs_term, ...} : coeqn_data_sel) = |
|
54160 | 950 |
let |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
951 |
val sel_no = find_first (curry (op =) ctr o #ctr) basic_ctr_specs |
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
952 |
|> find_index (curry (op =) sel) o #sels o the; |
54160 | 953 |
in |
55341
3d2c97392e25
adapted tactic to correctly handle 'if ... then ...' and 'case ...' under lambdas
blanchet
parents:
55339
diff
changeset
|
954 |
K (if has_call rhs_term then fold_rev_let_if_case ctxt (K cons) [] rhs_term [] else []) |
3d2c97392e25
adapted tactic to correctly handle 'if ... then ...' and 'case ...' under lambdas
blanchet
parents:
55339
diff
changeset
|
955 |
|> nth_map sel_no |> AList.map_entry (op =) ctr |
54160 | 956 |
end; |
957 |
||
54900 | 958 |
fun applied_fun_of fun_name fun_T fun_args = |
55343 | 959 |
Term.list_comb (Free (fun_name, fun_T), map Bound (length fun_args - 1 downto 0)); |
54900 | 960 |
|
54921
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
961 |
fun is_trivial_implies thm = |
54967
78de75e3e52a
exhaustive rules like '(False ==> P) ==> P ==> P' are now filtered out as trivial
blanchet
parents:
54959
diff
changeset
|
962 |
uncurry (member (op aconv)) (Logic.strip_horn (Thm.prop_of thm)); |
54921
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
963 |
|
55009
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
964 |
fun add_primcorec_ursive auto opts fixes specs of_specs_opt lthy = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
965 |
let |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
966 |
val thy = Proof_Context.theory_of lthy; |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
967 |
|
53352 | 968 |
val (bs, mxs) = map_split (apfst fst) fixes; |
55969 | 969 |
val (arg_Ts, res_Ts) = map (strip_type o snd o fst #>> mk_tupleT_balanced) fixes |> split_list; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
970 |
|
56254 | 971 |
val _ = (case filter_out (fn (_, T) => Sign.of_sort thy (T, @{sort type})) (bs ~~ arg_Ts) of |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
972 |
[] => () |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
973 |
| (b, _) :: _ => primcorec_error ("type of " ^ Binding.print b ^ " contains top sort")); |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
974 |
|
54902
a9291e4d2366
internally allow different values for 'sequential' for different constructors
blanchet
parents:
54901
diff
changeset
|
975 |
val actual_nn = length bs; |
a9291e4d2366
internally allow different values for 'sequential' for different constructors
blanchet
parents:
54901
diff
changeset
|
976 |
|
a9291e4d2366
internally allow different values for 'sequential' for different constructors
blanchet
parents:
54901
diff
changeset
|
977 |
val sequentials = replicate actual_nn (member (op =) opts Sequential_Option); |
a9291e4d2366
internally allow different values for 'sequential' for different constructors
blanchet
parents:
54901
diff
changeset
|
978 |
val exhaustives = replicate actual_nn (member (op =) opts Exhaustive_Option); |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
979 |
|
54160 | 980 |
val fun_names = map Binding.name_of bs; |
981 |
val basic_ctr_specss = map (basic_corec_specs_of lthy) res_Ts; |
|
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
982 |
val frees = map (fst #>> Binding.name_of #> Free) fixes; |
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
983 |
val has_call = exists_subterm (member (op =) frees); |
54160 | 984 |
val eqns_data = |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
985 |
@{fold_map 2} (dissect_coeqn lthy has_call fun_names sequentials basic_ctr_specss) |
55871 | 986 |
(tag_list 0 (map snd specs)) of_specs_opt [] |
54160 | 987 |
|> flat o fst; |
988 |
||
56152 | 989 |
val _ = |
990 |
let |
|
991 |
val missing = fun_names |
|
992 |
|> filter (map (fn Disc x => #fun_name x | Sel x => #fun_name x) eqns_data |
|
993 |
|> not oo member (op =)) |
|
994 |
in |
|
995 |
null missing |
|
996 |
orelse primcorec_error_eqns ("missing equations for function(s): " ^ commas missing) [] |
|
997 |
end; |
|
998 |
||
54160 | 999 |
val callssss = |
1000 |
map_filter (try (fn Sel x => x)) eqns_data |
|
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1001 |
|> partition_eq (op = o pairself #fun_name) |
54160 | 1002 |
|> fst o finds (fn (x, ({fun_name, ...} :: _)) => x = fun_name) fun_names |
54161 | 1003 |
|> map (flat o snd) |
54243 | 1004 |
|> map2 (fold o find_corec_calls lthy has_call) basic_ctr_specss |
54160 | 1005 |
|> map2 (curry (op |>)) (map (map (fn {ctr, sels, ...} => |
1006 |
(ctr, map (K []) sels))) basic_ctr_specss); |
|
1007 |
||
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1008 |
val (corec_specs', _, coinduct_thm, coinduct_strong_thm, coinduct_thms, coinduct_strong_thms, |
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1009 |
(coinduct_attrs, common_coinduct_attrs), n2m, lthy') = |
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55571
diff
changeset
|
1010 |
corec_specs_of bs arg_Ts res_Ts frees callssss lthy; |
54948 | 1011 |
val corec_specs = take actual_nn corec_specs'; |
54178 | 1012 |
val ctr_specss = map #ctr_specs corec_specs; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1013 |
|
53720 | 1014 |
val disc_eqnss' = map_filter (try (fn Disc x => x)) eqns_data |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1015 |
|> partition_eq (op = o pairself #fun_name) |
53720 | 1016 |
|> fst o finds (fn (x, ({fun_name, ...} :: _)) => x = fun_name) fun_names |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1017 |
|> map (sort (op < o pairself #ctr_no |> make_ord) o flat o snd); |
56152 | 1018 |
|
53720 | 1019 |
val _ = disc_eqnss' |> map (fn x => |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1020 |
let val d = duplicates (op = o pairself #ctr_no) x in null d orelse |
56152 | 1021 |
(if forall (is_some o #ctr_rhs_opt) x then |
1022 |
primcorec_error_eqns "multiple equations for constructor(s)" |
|
1023 |
(maps (fn t => filter (curry (op =) (#ctr_no t) o #ctr_no) x) d |
|
1024 |
|> map (the o #ctr_rhs_opt)) else |
|
1025 |
primcorec_error_eqns "excess discriminator formula in definition" |
|
1026 |
(maps (fn t => filter (curry (op =) (#ctr_no t) o #ctr_no) x) d |> map #user_eqn)) end); |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1027 |
|
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1028 |
val sel_eqnss = map_filter (try (fn Sel x => x)) eqns_data |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1029 |
|> partition_eq (op = o pairself #fun_name) |
53720 | 1030 |
|> fst o finds (fn (x, ({fun_name, ...} :: _)) => x = fun_name) fun_names |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1031 |
|> map (flat o snd); |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1032 |
|
53360 | 1033 |
val arg_Tss = map (binder_types o snd o fst) fixes; |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
1034 |
val disc_eqnss = @{map 6} mk_actual_disc_eqns bs arg_Tss exhaustives corec_specs sel_eqnss |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1035 |
disc_eqnss'; |
54835 | 1036 |
val (defs, excludess') = |
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
1037 |
build_codefs lthy' bs mxs has_call arg_Tss corec_specs disc_eqnss sel_eqnss; |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1038 |
|
55009
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1039 |
val tac_opts = |
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1040 |
map (fn {code_rhs_opt, ...} :: _ => |
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1041 |
if auto orelse is_some code_rhs_opt then SOME (auto_tac o #context) else NONE) disc_eqnss; |
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1042 |
|
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1043 |
fun exclude_tac tac_opt sequential (c, c', a) = |
54901
0b8871677e0b
use same name for feature internally as in user interface, to facilitate grepping
blanchet
parents:
54900
diff
changeset
|
1044 |
if a orelse c = c' orelse sequential then |
0b8871677e0b
use same name for feature internally as in user interface, to facilitate grepping
blanchet
parents:
54900
diff
changeset
|
1045 |
SOME (K (HEADGOAL (mk_primcorec_assumption_tac lthy []))) |
0b8871677e0b
use same name for feature internally as in user interface, to facilitate grepping
blanchet
parents:
54900
diff
changeset
|
1046 |
else |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1047 |
tac_opt; |
53822 | 1048 |
|
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
1049 |
val excludess'' = @{map 3} (fn tac_opt => fn sequential => map (fn (j, goal) => |
55342 | 1050 |
(j, (Option.map (Goal.prove_sorry lthy [] [] goal #> Thm.close_derivation) |
1051 |
(exclude_tac tac_opt sequential j), goal)))) |
|
55009
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1052 |
tac_opts sequentials excludess'; |
56152 | 1053 |
|
54835 | 1054 |
val taut_thmss = map (map (apsnd (the o fst)) o filter (is_some o fst o snd)) excludess''; |
55009
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1055 |
val (goal_idxss, exclude_goalss) = excludess'' |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1056 |
|> map (map (apsnd (rpair [] o snd)) o filter (is_none o fst o snd)) |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1057 |
|> split_list o map split_list; |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1058 |
|
54921
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
1059 |
fun list_all_fun_args extras = |
54910 | 1060 |
map2 (fn [] => I |
54921
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
1061 |
| {fun_args, ...} :: _ => map (curry Logic.list_all (extras @ map dest_Free fun_args))) |
54903
c664bd02bf94
internally allow different values for 'exhaustive' for different constructors
blanchet
parents:
54902
diff
changeset
|
1062 |
disc_eqnss; |
54844 | 1063 |
|
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1064 |
val syntactic_exhaustives = |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1065 |
map (fn disc_eqns => forall (null o #prems orf is_some o #code_rhs_opt) disc_eqns |
54913 | 1066 |
orelse exists #auto_gen disc_eqns) |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1067 |
disc_eqnss; |
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1068 |
val de_facto_exhaustives = |
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1069 |
map2 (fn b => fn b' => b orelse b') exhaustives syntactic_exhaustives; |
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1070 |
|
54903
c664bd02bf94
internally allow different values for 'exhaustive' for different constructors
blanchet
parents:
54902
diff
changeset
|
1071 |
val nchotomy_goalss = |
54904 | 1072 |
map2 (fn false => K [] | true => single o HOLogic.mk_Trueprop o mk_dnf o map #prems) |
1073 |
de_facto_exhaustives disc_eqnss |
|
54921
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
1074 |
|> list_all_fun_args [] |
54903
c664bd02bf94
internally allow different values for 'exhaustive' for different constructors
blanchet
parents:
54902
diff
changeset
|
1075 |
val nchotomy_taut_thmss = |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
1076 |
@{map 5} (fn tac_opt => fn {exhaust_discs = res_exhaust_discs, ...} => |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1077 |
fn {code_rhs_opt, ...} :: _ => fn [] => K [] |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1078 |
| [goal] => fn true => |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1079 |
let |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1080 |
val (_, _, arg_exhaust_discs, _, _) = |
55400 | 1081 |
case_thms_of_term lthy (the_default Term.dummy code_rhs_opt); |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1082 |
in |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1083 |
[Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} => |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1084 |
mk_primcorec_nchotomy_tac ctxt (res_exhaust_discs @ arg_exhaust_discs)) |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1085 |
|> Thm.close_derivation] |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1086 |
end |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1087 |
| false => |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1088 |
(case tac_opt of |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1089 |
SOME tac => [Goal.prove_sorry lthy [] [] goal tac |> Thm.close_derivation] |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1090 |
| NONE => [])) |
55400 | 1091 |
tac_opts corec_specs disc_eqnss nchotomy_goalss syntactic_exhaustives; |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1092 |
|
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1093 |
val syntactic_exhaustives = |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1094 |
map (fn disc_eqns => forall (null o #prems orf is_some o #code_rhs_opt) disc_eqns |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1095 |
orelse exists #auto_gen disc_eqns) |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1096 |
disc_eqnss; |
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1097 |
|
55009
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1098 |
val nchotomy_goalss = |
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1099 |
map2 (fn (NONE, false) => map (rpair []) | _ => K []) (tac_opts ~~ syntactic_exhaustives) |
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1100 |
nchotomy_goalss; |
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1101 |
|
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1102 |
val goalss = nchotomy_goalss @ exclude_goalss; |
54844 | 1103 |
|
55462 | 1104 |
fun prove thmss'' def_infos lthy = |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1105 |
let |
55462 | 1106 |
val def_thms = map (snd o snd) def_infos; |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1107 |
|
54972
5747fdd4ad3b
fix 'primcorec' (as opposed to 'primcorecursive') with 'exhaustive')
blanchet
parents:
54970
diff
changeset
|
1108 |
val (nchotomy_thmss, exclude_thmss) = |
55009
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1109 |
(map2 append (take actual_nn thmss'') nchotomy_taut_thmss, drop actual_nn thmss''); |
54613 | 1110 |
|
54927
a5a2598f0651
proper name generation to avoid clash with 'P' in user specification
blanchet
parents:
54926
diff
changeset
|
1111 |
val ps = |
a5a2598f0651
proper name generation to avoid clash with 'P' in user specification
blanchet
parents:
54926
diff
changeset
|
1112 |
Variable.variant_frees lthy (maps (maps #fun_args) disc_eqnss) [("P", HOLogic.boolT)]; |
a5a2598f0651
proper name generation to avoid clash with 'P' in user specification
blanchet
parents:
54926
diff
changeset
|
1113 |
|
54903
c664bd02bf94
internally allow different values for 'exhaustive' for different constructors
blanchet
parents:
54902
diff
changeset
|
1114 |
val exhaust_thmss = |
c664bd02bf94
internally allow different values for 'exhaustive' for different constructors
blanchet
parents:
54902
diff
changeset
|
1115 |
map2 (fn false => K [] |
54921
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
1116 |
| true => fn disc_eqns as {fun_args, ...} :: _ => |
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
1117 |
let |
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
1118 |
val p = Bound (length fun_args); |
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
1119 |
fun mk_imp_p Qs = Logic.list_implies (Qs, HOLogic.mk_Trueprop p); |
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
1120 |
in |
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
1121 |
[mk_imp_p (map (mk_imp_p o map HOLogic.mk_Trueprop o #prems) disc_eqns)] |
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
1122 |
end) |
54904 | 1123 |
de_facto_exhaustives disc_eqnss |
54927
a5a2598f0651
proper name generation to avoid clash with 'P' in user specification
blanchet
parents:
54926
diff
changeset
|
1124 |
|> list_all_fun_args ps |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
1125 |
|> @{map 3} (fn disc_eqns as {fun_args, ...} :: _ => fn [] => K [] |
54903
c664bd02bf94
internally allow different values for 'exhaustive' for different constructors
blanchet
parents:
54902
diff
changeset
|
1126 |
| [nchotomy_thm] => fn [goal] => |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
1127 |
[mk_primcorec_exhaust_tac lthy ("" (* for "P" *) :: map (fst o dest_Free) fun_args) |
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
1128 |
(length disc_eqns) nchotomy_thm |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
1129 |
|> K |> Goal.prove_sorry lthy [] [] goal |
54903
c664bd02bf94
internally allow different values for 'exhaustive' for different constructors
blanchet
parents:
54902
diff
changeset
|
1130 |
|> Thm.close_derivation]) |
c664bd02bf94
internally allow different values for 'exhaustive' for different constructors
blanchet
parents:
54902
diff
changeset
|
1131 |
disc_eqnss nchotomy_thmss; |
54921
862c36b6e57c
avoid schematic variable in goal, which sometimes gets instantiated by tactic
blanchet
parents:
54917
diff
changeset
|
1132 |
val nontriv_exhaust_thmss = map (filter_out is_trivial_implies) exhaust_thmss; |
54844 | 1133 |
|
54835 | 1134 |
val excludess' = map (op ~~) (goal_idxss ~~ exclude_thmss); |
1135 |
fun mk_excludesss excludes n = |
|
54973 | 1136 |
fold (fn ((c, c', _), thm) => nth_map c (nth_map c' (K [thm]))) |
54974
d1c76303244e
use correct default for exclude rules to avoid weird tactic failures
blanchet
parents:
54973
diff
changeset
|
1137 |
excludes (map (fn k => replicate k [asm_rl] @ replicate (n - k) []) (0 upto n - 1)); |
54973 | 1138 |
val excludessss = |
1139 |
map2 (fn excludes => mk_excludesss excludes o length o #ctr_specs) |
|
1140 |
(map2 append excludess' taut_thmss) corec_specs; |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1141 |
|
54835 | 1142 |
fun prove_disc ({ctr_specs, ...} : corec_spec) excludesss |
54948 | 1143 |
({fun_name, fun_T, fun_args, ctr_no, prems, eqn_pos, ...} : coeqn_data_disc) = |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
1144 |
if Term.aconv_untyped (#disc (nth ctr_specs ctr_no), @{term "\<lambda>x. x = x"}) then |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
1145 |
[] |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
1146 |
else |
53720 | 1147 |
let |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1148 |
val {disc, corec_disc, ...} = nth ctr_specs ctr_no; |
53720 | 1149 |
val k = 1 + ctr_no; |
1150 |
val m = length prems; |
|
54900 | 1151 |
val goal = |
1152 |
applied_fun_of fun_name fun_T fun_args |
|
1153 |
|> curry betapply disc |
|
53720 | 1154 |
|> HOLogic.mk_Trueprop |
1155 |
|> curry Logic.list_implies (map HOLogic.mk_Trueprop prems) |
|
1156 |
|> curry Logic.list_all (map dest_Free fun_args); |
|
1157 |
in |
|
54900 | 1158 |
if prems = [@{term False}] then |
1159 |
[] |
|
1160 |
else |
|
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1161 |
mk_primcorec_disc_tac lthy def_thms corec_disc k m excludesss |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
1162 |
|> K |> Goal.prove_sorry lthy [] [] goal |
54900 | 1163 |
|> Thm.close_derivation |
1164 |
|> pair (#disc (nth ctr_specs ctr_no)) |
|
54948 | 1165 |
|> pair eqn_pos |
54900 | 1166 |
|> single |
53720 | 1167 |
end; |
1168 |
||
57399 | 1169 |
fun prove_sel ({sel_defs, fp_nesting_maps, fp_nesting_map_ident0s, fp_nesting_map_comps, |
57397 | 1170 |
ctr_specs, ...} : corec_spec) (disc_eqns : coeqn_data_disc list) excludesss |
56858 | 1171 |
({fun_name, fun_T, fun_args, ctr, sel, rhs_term, code_rhs_opt, eqn_pos, ...} |
1172 |
: coeqn_data_sel) = |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1173 |
let |
58393 | 1174 |
val ctr_spec = the (find_first (curry (op =) ctr o #ctr) ctr_specs); |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
1175 |
val ctr_no = find_index (curry (op =) ctr o #ctr) ctr_specs; |
54067 | 1176 |
val prems = the_default (maps (s_not_conj o #prems) disc_eqns) |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
1177 |
(find_first (curry (op =) ctr_no o #ctr_no) disc_eqns |> Option.map #prems); |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1178 |
val corec_sel = find_index (curry (op =) sel) (#sels ctr_spec) |
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1179 |
|> nth (#corec_sels ctr_spec); |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1180 |
val k = 1 + ctr_no; |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1181 |
val m = length prems; |
54900 | 1182 |
val goal = |
1183 |
applied_fun_of fun_name fun_T fun_args |
|
53720 | 1184 |
|> curry betapply sel |
1185 |
|> rpair (abstract (List.rev fun_args) rhs_term) |
|
1186 |
|> HOLogic.mk_Trueprop o HOLogic.mk_eq |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1187 |
|> curry Logic.list_implies (map HOLogic.mk_Trueprop prems) |
53720 | 1188 |
|> curry Logic.list_all (map dest_Free fun_args); |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1189 |
val (distincts, _, _, split_sels, split_sel_asms) = case_thms_of_term lthy rhs_term; |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1190 |
in |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1191 |
mk_primcorec_sel_tac lthy def_thms distincts split_sels split_sel_asms fp_nesting_maps |
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1192 |
fp_nesting_map_ident0s fp_nesting_map_comps corec_sel k m excludesss |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
1193 |
|> K |> Goal.prove_sorry lthy [] [] goal |
54176 | 1194 |
|> Thm.close_derivation |
56858 | 1195 |
|> `(is_some code_rhs_opt ? fold_thms lthy sel_defs) (*mildly too aggressive*) |
53720 | 1196 |
|> pair sel |
54948 | 1197 |
|> pair eqn_pos |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1198 |
end; |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1199 |
|
56858 | 1200 |
fun prove_ctr disc_alist sel_alist ({sel_defs, ...} : corec_spec) |
1201 |
(disc_eqns : coeqn_data_disc list) (sel_eqns : coeqn_data_sel list) |
|
1202 |
({ctr, disc, sels, collapse, ...} : corec_ctr_spec) = |
|
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1203 |
(* don't try to prove theorems when some sel_eqns are missing *) |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
1204 |
if not (exists (curry (op =) ctr o #ctr) disc_eqns) |
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
1205 |
andalso not (exists (curry (op =) ctr o #ctr) sel_eqns) |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1206 |
orelse |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
1207 |
filter (curry (op =) ctr o #ctr) sel_eqns |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1208 |
|> fst o finds (op = o apsnd #sel) sels |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
1209 |
|> exists (null o snd) then |
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
1210 |
[] |
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
1211 |
else |
53720 | 1212 |
let |
56858 | 1213 |
val (fun_name, fun_T, fun_args, prems, ctr_rhs_opt, code_rhs_opt, eqn_pos) = |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
1214 |
(find_first (curry (op =) ctr o #ctr) disc_eqns, |
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
1215 |
find_first (curry (op =) ctr o #ctr) sel_eqns) |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1216 |
|>> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, #prems x, |
56858 | 1217 |
#ctr_rhs_opt x, #code_rhs_opt x, #eqn_pos x)) |
1218 |
||> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, [], |
|
1219 |
#ctr_rhs_opt x, #code_rhs_opt x, #eqn_pos x)) |
|
53722
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
1220 |
|> the o merge_options; |
53720 | 1221 |
val m = length prems; |
54900 | 1222 |
val goal = |
56858 | 1223 |
(case ctr_rhs_opt of |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1224 |
SOME rhs => rhs |
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1225 |
| NONE => |
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1226 |
filter (curry (op =) ctr o #ctr) sel_eqns |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1227 |
|> fst o finds (op = o apsnd #sel) sels |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1228 |
|> map (snd #> (fn [x] => (List.rev (#fun_args x), #rhs_term x)) #-> abstract) |
55343 | 1229 |
|> curry Term.list_comb ctr) |
54900 | 1230 |
|> curry mk_Trueprop_eq (applied_fun_of fun_name fun_T fun_args) |
53720 | 1231 |
|> curry Logic.list_implies (map HOLogic.mk_Trueprop prems) |
1232 |
|> curry Logic.list_all (map dest_Free fun_args); |
|
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1233 |
val disc_thm_opt = AList.lookup (op =) disc_alist disc; |
56858 | 1234 |
val sel_thms = map (snd o snd) (filter (member (op =) sels o fst) sel_alist); |
53720 | 1235 |
in |
56858 | 1236 |
if prems = [@{term False}] then |
1237 |
[] |
|
1238 |
else |
|
54954 | 1239 |
mk_primcorec_ctr_tac lthy m collapse disc_thm_opt sel_thms |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
1240 |
|> K |> Goal.prove_sorry lthy [] [] goal |
56858 | 1241 |
|> is_some code_rhs_opt ? fold_thms lthy sel_defs (*mildly too aggressive*) |
54176 | 1242 |
|> Thm.close_derivation |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1243 |
|> pair ctr |
54948 | 1244 |
|> pair eqn_pos |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1245 |
|> single |
53876 | 1246 |
end; |
53720 | 1247 |
|
55100 | 1248 |
fun prove_code exhaustive (disc_eqns : coeqn_data_disc list) |
1249 |
(sel_eqns : coeqn_data_sel list) nchotomys ctr_alist ctr_specs = |
|
54098 | 1250 |
let |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1251 |
val fun_data_opt = |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1252 |
(find_first (member (op =) (map #ctr ctr_specs) o #ctr) disc_eqns, |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1253 |
find_first (member (op =) (map #ctr ctr_specs) o #ctr) sel_eqns) |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1254 |
|>> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, #code_rhs_opt x)) |
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1255 |
||> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, #code_rhs_opt x)) |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1256 |
|> merge_options; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1257 |
in |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1258 |
(case fun_data_opt of |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1259 |
NONE => [] |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1260 |
| SOME (fun_name, fun_T, fun_args, rhs_opt) => |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1261 |
let |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1262 |
val bound_Ts = List.rev (map fastype_of fun_args); |
54173 | 1263 |
|
54900 | 1264 |
val lhs = applied_fun_of fun_name fun_T fun_args; |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1265 |
val rhs_info_opt = |
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1266 |
(case rhs_opt of |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1267 |
SOME rhs => |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1268 |
let |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1269 |
val raw_rhs = expand_corec_code_rhs lthy has_call bound_Ts rhs; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1270 |
val cond_ctrs = |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1271 |
fold_rev_corec_code_rhs lthy (K oo (cons oo pair)) bound_Ts raw_rhs []; |
54978
afc156c7e4f7
cope gracefully with missing ctr equations by plugging in 'False ==> ...'
blanchet
parents:
54976
diff
changeset
|
1272 |
val ctr_thms = |
afc156c7e4f7
cope gracefully with missing ctr equations by plugging in 'False ==> ...'
blanchet
parents:
54976
diff
changeset
|
1273 |
map (the_default FalseE o AList.lookup (op =) ctr_alist o snd) cond_ctrs; |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1274 |
in SOME (false, rhs, raw_rhs, ctr_thms) end |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1275 |
| NONE => |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1276 |
let |
55100 | 1277 |
fun prove_code_ctr ({ctr, sels, ...} : corec_ctr_spec) = |
56858 | 1278 |
if not (exists (curry (op =) ctr o fst) ctr_alist) then |
1279 |
NONE |
|
1280 |
else |
|
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1281 |
let |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
1282 |
val prems = find_first (curry (op =) ctr o #ctr) disc_eqns |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1283 |
|> Option.map #prems |> the_default []; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1284 |
val t = |
54923
ffed2452f5f6
instantiate schematics as projections to avoid HOU trouble
blanchet
parents:
54921
diff
changeset
|
1285 |
filter (curry (op =) ctr o #ctr) sel_eqns |
55008
b5b2e193ca33
use 'disc_exhausts' property both from types on which 'case's take place and on return type
blanchet
parents:
55005
diff
changeset
|
1286 |
|> fst o finds (op = o apsnd #sel) sels |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1287 |
|> map (snd #> (fn [x] => (List.rev (#fun_args x), #rhs_term x)) |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1288 |
#-> abstract) |
55343 | 1289 |
|> curry Term.list_comb ctr; |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1290 |
in |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1291 |
SOME (prems, t) |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1292 |
end; |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1293 |
val ctr_conds_argss_opt = map prove_code_ctr ctr_specs; |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1294 |
val exhaustive_code = |
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1295 |
exhaustive |
54948 | 1296 |
orelse exists (is_some andf (null o fst o the)) ctr_conds_argss_opt |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1297 |
orelse forall is_some ctr_conds_argss_opt |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1298 |
andalso exists #auto_gen disc_eqns; |
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1299 |
val rhs = |
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1300 |
(if exhaustive_code then |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1301 |
split_last (map_filter I ctr_conds_argss_opt) ||> snd |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1302 |
else |
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1303 |
Const (@{const_name Code.abort}, @{typ String.literal} --> |
55966 | 1304 |
(HOLogic.unitT --> body_type fun_T) --> body_type fun_T) $ |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1305 |
HOLogic.mk_literal fun_name $ |
55966 | 1306 |
absdummy HOLogic.unitT (incr_boundvars 1 lhs) |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1307 |
|> pair (map_filter I ctr_conds_argss_opt)) |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1308 |
|-> fold_rev (fn (prems, u) => mk_If (s_conjs prems) u) |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1309 |
in |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1310 |
SOME (exhaustive_code, rhs, rhs, map snd ctr_alist) |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1311 |
end); |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1312 |
in |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1313 |
(case rhs_info_opt of |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1314 |
NONE => [] |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1315 |
| SOME (exhaustive_code, rhs, raw_rhs, ctr_thms) => |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1316 |
let |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1317 |
val ms = map (Logic.count_prems o prop_of) ctr_thms; |
54900 | 1318 |
val (raw_goal, goal) = (raw_rhs, rhs) |
1319 |
|> pairself (curry mk_Trueprop_eq (applied_fun_of fun_name fun_T fun_args) |
|
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1320 |
#> curry Logic.list_all (map dest_Free fun_args)); |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1321 |
val (distincts, discIs, _, split_sels, split_sel_asms) = |
55400 | 1322 |
case_thms_of_term lthy raw_rhs; |
54098 | 1323 |
|
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1324 |
val raw_code_thm = mk_primcorec_raw_code_tac lthy distincts discIs split_sels |
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1325 |
split_sel_asms ms ctr_thms |
54905
2fdec6c29eb7
don't generate any proof obligation for implicit (de facto) exclusiveness
blanchet
parents:
54904
diff
changeset
|
1326 |
(if exhaustive_code then try the_single nchotomys else NONE) |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
1327 |
|> K |> Goal.prove_sorry lthy [] [] raw_goal |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1328 |
|> Thm.close_derivation; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1329 |
in |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1330 |
mk_primcorec_code_tac lthy distincts split_sels raw_code_thm |
54951
e25b4d22082b
for code equations that coincide with ctr equations, make sure the usr's input is preserved for both
blanchet
parents:
54948
diff
changeset
|
1331 |
|> K |> Goal.prove_sorry lthy [] [] goal |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1332 |
|> Thm.close_derivation |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1333 |
|> single |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1334 |
end) |
54173 | 1335 |
end) |
1336 |
end; |
|
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1337 |
|
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
1338 |
val disc_alistss = @{map 3} (map oo prove_disc) corec_specs excludessss disc_eqnss; |
54948 | 1339 |
val disc_alists = map (map snd o flat) disc_alistss; |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
1340 |
val sel_alists = @{map 4} (map ooo prove_sel) corec_specs disc_eqnss excludessss sel_eqnss; |
55870 | 1341 |
val disc_thmss = map (map snd o sort_list_duplicates o flat) disc_alistss; |
54948 | 1342 |
val disc_thmsss' = map (map (map (snd o snd))) disc_alistss; |
56858 | 1343 |
val sel_thmss = map (map (fst o snd) o sort_list_duplicates) sel_alists; |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1344 |
|
54959
30ded82ff806
fixed 'disc_iff' tactic in the case where different equations use different variable names for the function arguments
blanchet
parents:
54958
diff
changeset
|
1345 |
fun prove_disc_iff ({ctr_specs, ...} : corec_spec) exhaust_thms disc_thmss' |
30ded82ff806
fixed 'disc_iff' tactic in the case where different equations use different variable names for the function arguments
blanchet
parents:
54958
diff
changeset
|
1346 |
(({fun_args = exhaust_fun_args, ...} : coeqn_data_disc) :: _) disc_thms |
54948 | 1347 |
({fun_name, fun_T, fun_args, ctr_no, prems, eqn_pos, ...} : coeqn_data_disc) = |
55870 | 1348 |
if null exhaust_thms orelse null disc_thms then |
54900 | 1349 |
[] |
1350 |
else |
|
1351 |
let |
|
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1352 |
val {disc, distinct_discss, ...} = nth ctr_specs ctr_no; |
54900 | 1353 |
val goal = |
1354 |
mk_Trueprop_eq (applied_fun_of fun_name fun_T fun_args |> curry betapply disc, |
|
1355 |
mk_conjs prems) |
|
1356 |
|> curry Logic.list_all (map dest_Free fun_args); |
|
1357 |
in |
|
54969 | 1358 |
mk_primcorec_disc_iff_tac lthy (map (fst o dest_Free) exhaust_fun_args) |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57895
diff
changeset
|
1359 |
(the_single exhaust_thms) disc_thms disc_thmss' (flat distinct_discss) |
54969 | 1360 |
|> K |> Goal.prove_sorry lthy [] [] goal |
1361 |
|> Thm.close_derivation |
|
55859 | 1362 |
|> fold (fn rule => perhaps (try (fn thm => Meson.first_order_resolve thm rule))) |
54969 | 1363 |
@{thms eqTrueE eq_False[THEN iffD1] notnotD} |
1364 |
|> pair eqn_pos |
|
1365 |
|> single |
|
54900 | 1366 |
end; |
1367 |
||
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
1368 |
val disc_iff_thmss = @{map 6} (flat ooo map2 oooo prove_disc_iff) corec_specs exhaust_thmss |
54959
30ded82ff806
fixed 'disc_iff' tactic in the case where different equations use different variable names for the function arguments
blanchet
parents:
54958
diff
changeset
|
1369 |
disc_thmsss' disc_eqnss disc_thmsss' disc_eqnss |
55870 | 1370 |
|> map sort_list_duplicates; |
54900 | 1371 |
|
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
1372 |
val ctr_alists = @{map 6} (fn disc_alist => maps oooo prove_ctr disc_alist) disc_alists |
56858 | 1373 |
(map (map snd) sel_alists) corec_specs disc_eqnss sel_eqnss ctr_specss; |
54948 | 1374 |
val ctr_thmss' = map (map snd) ctr_alists; |
1375 |
val ctr_thmss = map (map snd o order_list) ctr_alists; |
|
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1376 |
|
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
1377 |
val code_thmss = |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
1378 |
@{map 6} prove_code exhaustives disc_eqnss sel_eqnss nchotomy_thmss ctr_thmss' |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58462
diff
changeset
|
1379 |
ctr_specss; |
53791 | 1380 |
|
54968
baa2baf29eff
use 'disc_iff' as simp rules whenever possible + clean up '= True', '= False', etc.
blanchet
parents:
54967
diff
changeset
|
1381 |
val disc_iff_or_disc_thmss = |
baa2baf29eff
use 'disc_iff' as simp rules whenever possible + clean up '= True', '= False', etc.
blanchet
parents:
54967
diff
changeset
|
1382 |
map2 (fn [] => I | disc_iffs => K disc_iffs) disc_iff_thmss disc_thmss; |
baa2baf29eff
use 'disc_iff' as simp rules whenever possible + clean up '= True', '= False', etc.
blanchet
parents:
54967
diff
changeset
|
1383 |
val simp_thmss = map2 append disc_iff_or_disc_thmss sel_thmss; |
53795 | 1384 |
|
53797 | 1385 |
val common_name = mk_common_name fun_names; |
1386 |
||
55860 | 1387 |
val anonymous_notes = |
1388 |
[(flat disc_iff_or_disc_thmss, simp_attrs)] |
|
1389 |
|> map (fn (thms, attrs) => ((Binding.empty, attrs), [(thms, [])])); |
|
1390 |
||
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1391 |
val common_notes = |
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1392 |
[(coinductN, if n2m then [coinduct_thm] else [], common_coinduct_attrs), |
58286 | 1393 |
(coinduct_strongN, if n2m then [coinduct_strong_thm] else [], common_coinduct_attrs)] |
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1394 |
|> filter_out (null o #2) |
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1395 |
|> map (fn (thmN, thms, attrs) => |
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1396 |
((Binding.qualify true common_name (Binding.name thmN), attrs), [(thms, [])])); |
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1397 |
|
53791 | 1398 |
val notes = |
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1399 |
[(coinductN, map (if n2m then single else K []) coinduct_thms, coinduct_attrs), |
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1400 |
(coinduct_strongN, map (if n2m then single else K []) coinduct_strong_thms, |
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1401 |
coinduct_attrs), |
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54133
diff
changeset
|
1402 |
(codeN, code_thmss, code_nitpicksimp_attrs), |
53797 | 1403 |
(ctrN, ctr_thmss, []), |
55860 | 1404 |
(discN, disc_thmss, []), |
54900 | 1405 |
(disc_iffN, disc_iff_thmss, []), |
54835 | 1406 |
(excludeN, exclude_thmss, []), |
54909 | 1407 |
(exhaustN, nontriv_exhaust_thmss, []), |
53795 | 1408 |
(selN, sel_thmss, simp_attrs), |
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1409 |
(simpsN, simp_thmss, [])] |
53791 | 1410 |
|> maps (fn (thmN, thmss, attrs) => |
1411 |
map2 (fn fun_name => fn thms => |
|
1412 |
((Binding.qualify true fun_name (Binding.name thmN), attrs), [(thms, [])])) |
|
53830
ed2eb7df2aac
don't note more induction principles than there are functions + tuning
blanchet
parents:
53822
diff
changeset
|
1413 |
fun_names (take actual_nn thmss)) |
53791 | 1414 |
|> filter_out (null o fst o hd o snd); |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1415 |
in |
55462 | 1416 |
lthy |
1417 |
|> Spec_Rules.add Spec_Rules.Equational (map fst def_infos, flat sel_thmss) |
|
1418 |
|> Spec_Rules.add Spec_Rules.Equational (map fst def_infos, flat ctr_thmss) |
|
1419 |
|> Spec_Rules.add Spec_Rules.Equational (map fst def_infos, flat code_thmss) |
|
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58223
diff
changeset
|
1420 |
|> Local_Theory.notes (anonymous_notes @ common_notes @ notes) |
55462 | 1421 |
|> snd |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1422 |
end; |
53822 | 1423 |
|
1424 |
fun after_qed thmss' = fold_map Local_Theory.define defs #-> prove thmss'; |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1425 |
in |
54177 | 1426 |
(goalss, after_qed, lthy') |
53822 | 1427 |
end; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1428 |
|
56945 | 1429 |
fun add_primcorec_ursive_cmd auto opts (raw_fixes, raw_specs_of) lthy = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1430 |
let |
54926
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54925
diff
changeset
|
1431 |
val (raw_specs, of_specs_opt) = |
56945 | 1432 |
split_list raw_specs_of ||> map (Option.map (Syntax.read_term lthy)); |
1433 |
val (fixes, specs) = fst (Specification.read_spec raw_fixes raw_specs lthy); |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1434 |
in |
55009
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1435 |
add_primcorec_ursive auto opts fixes specs of_specs_opt lthy |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
1436 |
handle ERROR str => primcorec_error str |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1437 |
end |
55527 | 1438 |
handle PRIMCOREC (str, eqns) => |
1439 |
if null eqns then |
|
1440 |
error ("primcorec error:\n " ^ str) |
|
1441 |
else |
|
1442 |
error ("primcorec error:\n " ^ str ^ "\nin\n " ^ |
|
1443 |
space_implode "\n " (map (quote o Syntax.string_of_term lthy) eqns)); |
|
53822 | 1444 |
|
54177 | 1445 |
val add_primcorecursive_cmd = (fn (goalss, after_qed, lthy) => |
1446 |
lthy |
|
1447 |
|> Proof.theorem NONE after_qed goalss |
|
54883
dd04a8b654fc
proper context for norm_hhf and derived operations;
wenzelm
parents:
54844
diff
changeset
|
1448 |
|> Proof.refine (Method.primitive_text (K I)) |
55009
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1449 |
|> Seq.hd) ooo add_primcorec_ursive_cmd false; |
54177 | 1450 |
|
1451 |
val add_primcorec_cmd = (fn (goalss, after_qed, lthy) => |
|
1452 |
lthy |
|
1453 |
|> after_qed (map (fn [] => [] |
|
54972
5747fdd4ad3b
fix 'primcorec' (as opposed to 'primcorecursive') with 'exhaustive')
blanchet
parents:
54970
diff
changeset
|
1454 |
| _ => error "\"auto\" failed -- use \"primcorecursive\" instead of \"primcorec\"") |
55009
d4b69107a86a
automatically solve proof obligations produced for code equations
blanchet
parents:
55008
diff
changeset
|
1455 |
goalss)) ooo add_primcorec_ursive_cmd true; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1456 |
|
55529 | 1457 |
val primcorec_option_parser = Parse.group (fn () => "option") |
1458 |
(Parse.reserved "sequential" >> K Sequential_Option |
|
1459 |
|| Parse.reserved "exhaustive" >> K Exhaustive_Option) |
|
1460 |
||
58394
f0c51576964a
more honest 'primcorec' -- don't parse a theorem name that is then ignored
blanchet
parents:
58393
diff
changeset
|
1461 |
(* FIXME: should use "Parse_Spec.spec" instead of "Parse.prop" and honor binding *) |
55529 | 1462 |
val where_alt_specs_of_parser = Parse.where_ |-- Parse.!!! (Parse.enum1 "|" |
58394
f0c51576964a
more honest 'primcorec' -- don't parse a theorem name that is then ignored
blanchet
parents:
58393
diff
changeset
|
1463 |
((Parse.prop >> pair Attrib.empty_binding) -- Scan.option (Parse.reserved "of" |-- Parse.const))); |
55529 | 1464 |
|
1465 |
val _ = Outer_Syntax.local_theory_to_proof @{command_spec "primcorecursive"} |
|
1466 |
"define primitive corecursive functions" |
|
1467 |
((Scan.optional (@{keyword "("} |-- |
|
1468 |
Parse.!!! (Parse.list1 primcorec_option_parser) --| @{keyword ")"}) []) -- |
|
1469 |
(Parse.fixes -- where_alt_specs_of_parser) >> uncurry add_primcorecursive_cmd); |
|
1470 |
||
1471 |
val _ = Outer_Syntax.local_theory @{command_spec "primcorec"} |
|
1472 |
"define primitive corecursive functions" |
|
1473 |
((Scan.optional (@{keyword "("} |-- |
|
1474 |
Parse.!!! (Parse.list1 primcorec_option_parser) --| @{keyword ")"}) []) -- |
|
1475 |
(Parse.fixes -- where_alt_specs_of_parser) >> uncurry add_primcorec_cmd); |
|
1476 |
||
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1477 |
end; |