author | blanchet |
Fri, 20 Dec 2013 11:34:07 +0100 | |
changeset 54835 | 431133d07192 |
parent 54834 | b125539be102 |
child 54842 | a020f7d74fed |
permissions | -rw-r--r-- |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
1 |
(* Title: HOL/BNF/Tools/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 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
6 |
Corecursor sugar. |
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 |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
11 |
datatype primcorec_option = |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
12 |
Option_Sequential | |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
13 |
Option_Exhaustive |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
14 |
val add_primcorecursive_cmd: primcorec_option list -> |
53831
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53830
diff
changeset
|
15 |
(binding * string option * mixfix) list * ((Attrib.binding * string) * string option) list -> |
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53830
diff
changeset
|
16 |
Proof.context -> Proof.state |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
17 |
val add_primcorec_cmd: primcorec_option list -> |
53831
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53830
diff
changeset
|
18 |
(binding * string option * mixfix) list * ((Attrib.binding * string) * string option) list -> |
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53830
diff
changeset
|
19 |
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
|
20 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
21 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
22 |
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
|
23 |
struct |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
24 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
25 |
open Ctr_Sugar |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
26 |
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
|
27 |
open BNF_Def |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
28 |
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
|
29 |
open BNF_FP_Def_Sugar |
54243 | 30 |
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
|
31 |
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
|
32 |
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
|
33 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
34 |
val codeN = "code" |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
35 |
val ctrN = "ctr" |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
36 |
val discN = "disc" |
54835 | 37 |
val excludeN = "exclude" |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
38 |
val selN = "sel" |
53791 | 39 |
|
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54133
diff
changeset
|
40 |
val nitpicksimp_attrs = @{attributes [nitpick_simp]}; |
53794 | 41 |
val simp_attrs = @{attributes [simp]}; |
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54133
diff
changeset
|
42 |
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
|
43 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
44 |
exception Primcorec_Error of string * 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
|
45 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
46 |
fun primcorec_error str = raise Primcorec_Error (str, []); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
47 |
fun primcorec_error_eqn str eqn = raise Primcorec_Error (str, [eqn]); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
48 |
fun primcorec_error_eqns str eqns = raise Primcorec_Error (str, eqns); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
49 |
|
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
50 |
datatype primcorec_option = |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
51 |
Option_Sequential | |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
52 |
Option_Exhaustive |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
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 |
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
|
55 |
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
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
60 |
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
|
61 |
{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
|
62 |
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
|
63 |
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
|
64 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
65 |
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
|
66 |
{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
|
67 |
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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
sel_thms: thm list, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
73 |
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
|
74 |
corec_thm: thm, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
75 |
disc_corec: thm, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
76 |
sel_corecs: thm list}; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
77 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
78 |
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
|
79 |
{corec: term, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
80 |
nested_map_idents: thm list, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
81 |
nested_map_comps: thm list, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
82 |
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
|
83 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
84 |
exception AINT_NO_MAP of term; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
85 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
95 |
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
|
96 |
val mk_disjs = try (foldr1 HOLogic.mk_disj) #> the_default @{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
|
97 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
98 |
val conjuncts_s = filter_out (curry (op =) @{const True}) o HOLogic.conjuncts; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
99 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
100 |
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
|
101 |
| 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
|
102 |
| 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
|
103 |
| 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
|
104 |
| 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
|
105 |
| 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
|
106 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
107 |
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
|
108 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
109 |
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
|
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 |
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
|
112 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
113 |
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
|
114 |
(case List.partition (can the_single) css of |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
115 |
([], _) => css |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
116 |
| ([u] :: uss, css') => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
117 |
[u] :: propagate_units (map (propagate_unit_neg (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
|
118 |
(map (propagate_unit_pos u) (uss @ css')))); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
119 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
|
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 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
|
125 |
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
|
126 |
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
|
127 |
|
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 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
|
129 |
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
|
130 |
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
|
131 |
[@{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
|
132 |
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
|
133 |
[] |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
134 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
135 |
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
|
136 |
|> 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
|
137 |
|> 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
|
138 |
|> (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
|
139 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
140 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
141 |
fun fold_rev_let_if_case ctxt f 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
|
142 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
143 |
val thy = Proof_Context.theory_of ctxt; |
53794 | 144 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
145 |
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
|
146 |
(case Term.strip_comb 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
|
147 |
(Const (@{const_name Let}, _), [_, _]) => fld conds (unfold_let t) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
148 |
| (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
|
149 |
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
|
150 |
| (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
|
151 |
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
|
152 |
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
|
153 |
(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
|
154 |
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
|
155 |
(case dest_case ctxt s 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
|
156 |
NONE => apsnd (f 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
|
157 |
| SOME (conds', branches) => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
158 |
apfst (cons s) o fold_rev (uncurry fld) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
159 |
(map (append conds o conjuncts_s) conds' ~~ branches)) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
160 |
| _ => apsnd (f 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
|
161 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
162 |
apsnd (f 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
|
163 |
end |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
164 |
| _ => apsnd (f 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
|
165 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
166 |
fld [] t o pair [] |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
167 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
168 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
169 |
fun case_of ctxt = ctr_sugar_of ctxt #> Option.map (fst o dest_Const o #casex); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
170 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
171 |
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
|
172 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
173 |
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
|
174 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
175 |
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
|
176 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
177 |
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
|
178 |
| 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
|
179 |
| 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
|
180 |
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
|
181 |
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
|
182 |
end |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
183 |
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
|
184 |
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
|
185 |
(case Term.strip_comb 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
|
186 |
(Const (@{const_name Let}, _), [_, _]) => massage_rec bound_Ts (unfold_let t) |
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}, _), 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
|
188 |
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
|
189 |
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
|
190 |
end |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
191 |
| (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
|
192 |
(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
|
193 |
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
|
194 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
195 |
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
|
196 |
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
|
197 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
198 |
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
|
199 |
(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
|
200 |
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
|
201 |
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
|
202 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
203 |
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
|
204 |
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
|
205 |
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
|
206 |
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
|
207 |
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
|
208 |
in |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
209 |
Term.list_comb (casex', |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
210 |
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
|
211 |
end |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
212 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
213 |
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
|
214 |
| _ => 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
|
215 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
216 |
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
|
217 |
end |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
218 |
| 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
|
219 |
| _ => 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
|
220 |
end |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
221 |
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 |
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
|
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 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
225 |
val massage_mutual_corec_call = massage_let_if_case; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
226 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
227 |
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
|
228 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
229 |
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
|
230 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
231 |
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
|
232 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
233 |
val build_map_Inl = build_map ctxt (uncurry Inl_const o dest_sumT o snd); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
234 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
235 |
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
|
236 |
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
|
237 |
(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
|
238 |
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
|
239 |
| 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
|
240 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
241 |
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
|
242 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
243 |
fun massage_mutual_fun 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
|
244 |
(case 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
|
245 |
Const (@{const_name comp}, _) $ t1 $ t2 => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
246 |
mk_comp bound_Ts (massage_mutual_fun bound_Ts U T t1, tap check_no_call t2) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
247 |
| _ => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
248 |
let |
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 var = Var ((Name.uu, Term.maxidx_of_term t + 1), |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
250 |
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
|
251 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
252 |
Term.lambda var (massage_mutual_call bound_Ts U T (t $ var)) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
253 |
end); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
254 |
|
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 |
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
|
256 |
(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
|
257 |
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
|
258 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
259 |
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
|
260 |
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
|
261 |
val fs' = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
262 |
map_flattened_map_args ctxt s (map3 (massage_map_or_map_arg bound_Ts) Us Ts) fs; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
263 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
264 |
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
|
265 |
end |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
266 |
| NONE => raise AINT_NO_MAP t) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
267 |
| massage_map _ _ _ t = raise AINT_NO_MAP t |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
268 |
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
|
269 |
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
|
270 |
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
|
271 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
272 |
massage_map 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
|
273 |
handle AINT_NO_MAP _ => massage_mutual_fun 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
|
274 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
275 |
fun 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
|
276 |
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
|
277 |
if has_call t then |
54277
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
278 |
(case t of |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
279 |
Const (@{const_name prod_case}, _) $ t' => |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
280 |
let |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
281 |
val U' = curried_type U; |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
282 |
val T' = curried_type T; |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
283 |
in |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
284 |
Const (@{const_name prod_case}, U' --> U) $ massage_call bound_Ts U' T' t' |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
285 |
end |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
286 |
| t1 $ t2 => |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
287 |
(if has_call t2 then |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
288 |
massage_mutual_call bound_Ts U T t |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
289 |
else |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
290 |
massage_map bound_Ts U T t1 $ t2 |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
291 |
handle AINT_NO_MAP _ => massage_mutual_call bound_Ts U T t) |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
292 |
| Abs (s, T', t') => |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
293 |
Abs (s, T', massage_call (T' :: bound_Ts) (range_type U) (range_type T) t') |
8dd0e0316881
take out possibility of moving corecursive calls past constructors -- this doesn't work in the general case
blanchet
parents:
54272
diff
changeset
|
294 |
| _ => massage_mutual_call bound_Ts U T 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
|
295 |
else |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
296 |
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
|
297 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
298 |
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
|
299 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
300 |
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
|
301 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
302 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
303 |
val fold_rev_corec_call = fold_rev_let_if_case; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
304 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
305 |
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
|
306 |
(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
|
307 |
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
|
308 |
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
|
309 |
| 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
|
310 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
311 |
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
|
312 |
(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
|
313 |
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
|
314 |
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
|
315 |
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
|
316 |
| _ => 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
|
317 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
318 |
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
|
319 |
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
|
320 |
(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
|
321 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
322 |
fun fold_rev_corec_code_rhs ctxt f = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
323 |
snd ooo fold_rev_let_if_case ctxt (fn conds => uncurry (f conds) 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
|
324 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
325 |
fun case_thms_of_term ctxt 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
|
326 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
327 |
val (caseT_names, _) = fold_rev_let_if_case ctxt (K (K I)) 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
|
328 |
val ctr_sugars = map (the o ctr_sugar_of ctxt) caseT_names; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
329 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
330 |
(maps #distincts ctr_sugars, maps #discIs ctr_sugars, maps #sel_splits ctr_sugars, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
331 |
maps #sel_split_asms ctr_sugars) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
332 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
333 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
334 |
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
|
335 |
(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
|
336 |
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
|
337 |
(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
|
338 |
NONE => not_codatatype 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
|
339 |
| SOME {ctrs, discs, selss, ...} => |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
340 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
341 |
val thy = Proof_Context.theory_of ctxt; |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
342 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
343 |
val gfpT = body_type (fastype_of (hd ctrs)); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
344 |
val As_rho = tvar_subst thy [gfpT] [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
|
345 |
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
|
346 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
347 |
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
|
348 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
349 |
map3 mk_spec ctrs discs selss |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
350 |
end) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
351 |
| _ => 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
|
352 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
353 |
fun corec_specs_of bs arg_Ts res_Ts get_indices callssss0 lthy = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
354 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
355 |
val thy = Proof_Context.theory_of lthy; |
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 |
val ((missing_res_Ts, perm0_kks, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
358 |
fp_sugars as {nested_bnfs, fp_res = {xtor_co_iterss = dtor_coiters1 :: _, ...}, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
359 |
co_inducts = coinduct_thms, ...} :: _, (_, gfp_sugar_thms)), lthy') = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
360 |
nested_to_mutual_fps Greatest_FP bs res_Ts get_indices callssss0 lthy; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
361 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
362 |
val perm_fp_sugars = sort (int_ord o pairself #index) fp_sugars; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
363 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
364 |
val indices = map #index fp_sugars; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
365 |
val perm_indices = map #index perm_fp_sugars; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
366 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
367 |
val perm_ctrss = map (#ctrs o of_fp_sugar #ctr_sugars) perm_fp_sugars; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
368 |
val perm_ctr_Tsss = map (map (binder_types o fastype_of)) perm_ctrss; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
369 |
val perm_gfpTs = map (body_type o fastype_of o hd) perm_ctrss; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
370 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
371 |
val nn0 = length res_Ts; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
372 |
val nn = length perm_gfpTs; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
373 |
val kks = 0 upto nn - 1; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
374 |
val perm_ns = map length perm_ctr_Tsss; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
375 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
376 |
val perm_Cs = map (domain_type o body_fun_type o fastype_of o co_rec_of o |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
377 |
of_fp_sugar (#xtor_co_iterss o #fp_res)) perm_fp_sugars; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
378 |
val (perm_p_Tss, (perm_q_Tssss, _, perm_f_Tssss, _)) = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
379 |
mk_coiter_fun_arg_types perm_ctr_Tsss perm_Cs perm_ns (co_rec_of dtor_coiters1); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
380 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
381 |
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
|
382 |
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
|
383 |
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
|
384 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
385 |
val fun_arg_hs = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
386 |
flat (map3 flat_corec_preds_predsss_gettersss perm_p_hss perm_q_hssss perm_f_hssss); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
387 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
388 |
fun unpermute0 perm0_xs = permute_like (op =) perm0_kks kks perm0_xs; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
389 |
fun unpermute perm_xs = permute_like (op =) perm_indices indices perm_xs; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
390 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
391 |
val coinduct_thmss = map (unpermute0 o conj_dests nn) coinduct_thms; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
392 |
|
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 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
|
394 |
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
|
395 |
val f_issss = map (map (map (map (find_index_eq fun_arg_hs)))) (unpermute perm_f_hssss); |
53358 | 396 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
397 |
val f_Tssss = unpermute perm_f_Tssss; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
398 |
val gfpTs = unpermute perm_gfpTs; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
399 |
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
|
400 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
401 |
val As_rho = tvar_subst thy (take nn0 gfpTs) res_Ts; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
402 |
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
|
403 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
408 |
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
|
409 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
410 |
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
|
411 |
(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
|
412 |
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
|
413 |
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
|
414 |
| 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
|
415 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
416 |
fun mk_ctr_spec ctr disc sels p_ho q_iss f_iss f_Tss discI sel_thms collapse corec_thm |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
417 |
disc_corec sel_corecs = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
418 |
let val nullary = not (can dest_funT (fastype_of ctr)) in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
419 |
{ctr = substA ctr, disc = substA disc, sels = map substA sels, pred = p_ho, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
420 |
calls = map3 (call_of nullary) q_iss f_iss f_Tss, discI = discI, sel_thms = sel_thms, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
421 |
collapse = collapse, corec_thm = corec_thm, disc_corec = disc_corec, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
422 |
sel_corecs = sel_corecs} |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
423 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
424 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
425 |
fun mk_ctr_specs index (ctr_sugars : ctr_sugar list) p_is q_isss f_isss f_Tsss coiter_thmsss |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
426 |
disc_coitersss sel_coiterssss = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
427 |
let |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
428 |
val ctrs = #ctrs (nth ctr_sugars index); |
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 discs = #discs (nth ctr_sugars index); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
430 |
val selss = #selss (nth ctr_sugars index); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
431 |
val p_ios = map SOME p_is @ [NONE]; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
432 |
val discIs = #discIs (nth ctr_sugars index); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
433 |
val sel_thmss = #sel_thmss (nth ctr_sugars index); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
434 |
val collapses = #collapses (nth ctr_sugars index); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
435 |
val corec_thms = co_rec_of (nth coiter_thmsss index); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
436 |
val disc_corecs = co_rec_of (nth disc_coitersss index); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
437 |
val sel_corecss = co_rec_of (nth sel_coiterssss index); |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
438 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
439 |
map13 mk_ctr_spec ctrs discs selss p_ios q_isss f_isss f_Tsss discIs sel_thmss collapses |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
440 |
corec_thms disc_corecs sel_corecss |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
441 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
442 |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
443 |
fun mk_spec ({T, index, ctr_sugars, co_iterss = coiterss, co_iter_thmsss = coiter_thmsss, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
444 |
disc_co_itersss = disc_coitersss, sel_co_iterssss = sel_coiterssss, ...} : fp_sugar) |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
445 |
p_is q_isss f_isss f_Tsss = |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
446 |
{corec = mk_co_iter thy Greatest_FP (substAT T) perm_Cs' (co_rec_of (nth coiterss index)), |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
447 |
nested_map_idents = map (unfold_thms lthy @{thms id_def} o map_id0_of_bnf) nested_bnfs, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
448 |
nested_map_comps = map map_comp_of_bnf nested_bnfs, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
449 |
ctr_specs = mk_ctr_specs index ctr_sugars p_is q_isss f_isss f_Tsss coiter_thmsss |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
450 |
disc_coitersss sel_coiterssss}; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
451 |
in |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
452 |
((is_some gfp_sugar_thms, map5 mk_spec fp_sugars p_iss q_issss f_issss f_Tssss, missing_res_Ts, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
453 |
co_induct_of coinduct_thms, strong_co_induct_of coinduct_thms, co_induct_of coinduct_thmss, |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
454 |
strong_co_induct_of coinduct_thmss), lthy') |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
455 |
end; |
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
456 |
|
53358 | 457 |
val undef_const = Const (@{const_name undefined}, dummyT); |
53357 | 458 |
|
53401 | 459 |
val abs_tuple = HOLogic.tupled_lambda o HOLogic.mk_tuple; |
53720 | 460 |
fun abstract vs = |
461 |
let fun a n (t $ u) = a n t $ a n u |
|
462 |
| a n (Abs (v, T, b)) = Abs (v, T, a (n + 1) b) |
|
463 |
| a n t = let val idx = find_index (equal t) vs in |
|
464 |
if idx < 0 then t else Bound (n + idx) end |
|
465 |
in a 0 end; |
|
54271 | 466 |
|
467 |
fun mk_prod1 bound_Ts (t, u) = |
|
468 |
HOLogic.pair_const (fastype_of1 (bound_Ts, t)) (fastype_of1 (bound_Ts, u)) $ t $ u; |
|
469 |
fun mk_tuple1 bound_Ts = the_default HOLogic.unit o try (foldr1 (mk_prod1 bound_Ts)); |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
470 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
471 |
type coeqn_data_disc = { |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
472 |
fun_name: string, |
53720 | 473 |
fun_T: typ, |
53401 | 474 |
fun_args: term list, |
53720 | 475 |
ctr: term, |
53341 | 476 |
ctr_no: int, (*###*) |
53720 | 477 |
disc: term, |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
478 |
prems: term list, |
53822 | 479 |
auto_gen: bool, |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
480 |
maybe_ctr_rhs: term option, |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
481 |
maybe_code_rhs: term option, |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
482 |
user_eqn: term |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
483 |
}; |
54001 | 484 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
485 |
type coeqn_data_sel = { |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
486 |
fun_name: string, |
53720 | 487 |
fun_T: typ, |
53401 | 488 |
fun_args: term list, |
53341 | 489 |
ctr: term, |
490 |
sel: term, |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
491 |
rhs_term: term, |
54807
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
492 |
maybe_ctr_rhs: term option, |
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
493 |
maybe_code_rhs: term option, |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
494 |
user_eqn: term |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
495 |
}; |
54001 | 496 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
497 |
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
|
498 |
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
|
499 |
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
|
500 |
|
54160 | 501 |
fun dissect_coeqn_disc seq fun_names (basic_ctr_specss : basic_corec_ctr_spec list list) |
502 |
maybe_ctr_rhs maybe_code_rhs prems' concl matchedsss = |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
503 |
let |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
504 |
fun find_subterm p = |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
505 |
let (* FIXME \<exists>? *) |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
506 |
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
|
507 |
| 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
|
508 |
in find end; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
509 |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
510 |
val applied_fun = concl |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
511 |
|> find_subterm (member ((op =) o apsnd SOME) fun_names o try (fst o dest_Free o head_of)) |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
512 |
|> 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
|
513 |
handle Option.Option => primcorec_error_eqn "malformed discriminator formula" concl; |
53720 | 514 |
val ((fun_name, fun_T), fun_args) = strip_comb applied_fun |>> dest_Free; |
54209 | 515 |
val SOME basic_ctr_specs = AList.lookup (op =) (fun_names ~~ 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
|
516 |
|
54160 | 517 |
val discs = map #disc basic_ctr_specs; |
518 |
val ctrs = map #ctr basic_ctr_specs; |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
519 |
val not_disc = head_of concl = @{term Not}; |
53401 | 520 |
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
|
521 |
primcorec_error_eqn "negated discriminator for a type with \<noteq> 2 constructors" concl; |
54160 | 522 |
val disc' = find_subterm (member (op =) discs o head_of) concl; |
54209 | 523 |
val eq_ctr0 = concl |> perhaps (try HOLogic.dest_not) |> try (HOLogic.dest_eq #> snd) |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
524 |
|> (fn SOME t => let val n = find_index (equal t) ctrs in |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
525 |
if n >= 0 then SOME n else NONE end | _ => NONE); |
54160 | 526 |
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
|
527 |
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
|
528 |
val ctr_no' = |
54160 | 529 |
if is_none disc' then the eq_ctr0 else find_index (equal (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
|
530 |
val ctr_no = if not_disc then 1 - ctr_no' else ctr_no'; |
54160 | 531 |
val {ctr, disc, ...} = nth basic_ctr_specs ctr_no; |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
532 |
|
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
533 |
val catch_all = try (fst o dest_Free o the_single) prems' = SOME Name.uu_; |
53720 | 534 |
val matchedss = AList.lookup (op =) matchedsss fun_name |> the_default []; |
535 |
val prems = map (abstract (List.rev fun_args)) prems'; |
|
536 |
val real_prems = |
|
54067 | 537 |
(if catch_all orelse seq then maps s_not_conj matchedss else []) @ |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
538 |
(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
|
539 |
|
53720 | 540 |
val matchedsss' = AList.delete (op =) fun_name matchedsss |
54065 | 541 |
|> cons (fun_name, if seq then matchedss @ [prems] else matchedss @ [real_prems]); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
542 |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
543 |
val user_eqn = |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
544 |
(real_prems, concl) |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
545 |
|>> 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
|
546 |
|> curry Logic.list_all (map dest_Free fun_args) o Logic.list_implies; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
547 |
in |
53341 | 548 |
(Disc { |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
549 |
fun_name = fun_name, |
53720 | 550 |
fun_T = fun_T, |
53401 | 551 |
fun_args = fun_args, |
53720 | 552 |
ctr = ctr, |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
553 |
ctr_no = ctr_no, |
54160 | 554 |
disc = disc, |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
555 |
prems = real_prems, |
53822 | 556 |
auto_gen = catch_all, |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
557 |
maybe_ctr_rhs = maybe_ctr_rhs, |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
558 |
maybe_code_rhs = maybe_code_rhs, |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
559 |
user_eqn = user_eqn |
53720 | 560 |
}, matchedsss') |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
561 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
562 |
|
54807
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
563 |
fun dissect_coeqn_sel fun_names (basic_ctr_specss : basic_corec_ctr_spec list list) maybe_ctr_rhs |
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
564 |
maybe_code_rhs eqn' maybe_of_spec eqn = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
565 |
let |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
566 |
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
|
567 |
handle 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
|
568 |
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
|
569 |
val sel = head_of lhs; |
53720 | 570 |
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
|
571 |
handle 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
|
572 |
primcorec_error_eqn "malformed selector argument in left-hand side" eqn; |
54160 | 573 |
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
|
574 |
handle Option.Option => |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
575 |
primcorec_error_eqn "malformed selector argument in left-hand side" eqn; |
54160 | 576 |
val {ctr, ...} = |
54209 | 577 |
(case maybe_of_spec of |
578 |
SOME of_spec => the (find_first (equal of_spec o #ctr) basic_ctr_specs) |
|
579 |
| NONE => filter (exists (equal 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
|
580 |
handle List.Empty => primcorec_error_eqn "ambiguous selector - use \"of\"" eqn); |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
581 |
val user_eqn = drop_All eqn'; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
582 |
in |
53341 | 583 |
Sel { |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
584 |
fun_name = fun_name, |
53720 | 585 |
fun_T = fun_T, |
53401 | 586 |
fun_args = fun_args, |
54160 | 587 |
ctr = ctr, |
53341 | 588 |
sel = sel, |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
589 |
rhs_term = rhs, |
54807
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
590 |
maybe_ctr_rhs = maybe_ctr_rhs, |
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
591 |
maybe_code_rhs = maybe_code_rhs, |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
592 |
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
|
593 |
} |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
594 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
595 |
|
54160 | 596 |
fun dissect_coeqn_ctr seq fun_names (basic_ctr_specss : basic_corec_ctr_spec list list) eqn' |
597 |
maybe_code_rhs prems concl matchedsss = |
|
53910 | 598 |
let |
54065 | 599 |
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
|
600 |
val (fun_name, fun_args) = strip_comb lhs |>> fst o dest_Free; |
54209 | 601 |
val SOME basic_ctr_specs = AList.lookup (op =) (fun_names ~~ basic_ctr_specss) fun_name; |
54074
43cdae9524bf
allow 'let's around constructors in constructor view
blanchet
parents:
54068
diff
changeset
|
602 |
val (ctr, ctr_args) = strip_comb (unfold_let rhs); |
54160 | 603 |
val {disc, sels, ...} = the (find_first (equal 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
|
604 |
handle Option.Option => primcorec_error_eqn "not a constructor" ctr; |
53341 | 605 |
|
54065 | 606 |
val disc_concl = betapply (disc, lhs); |
54160 | 607 |
val (maybe_eqn_data_disc, matchedsss') = if length basic_ctr_specs = 1 |
53720 | 608 |
then (NONE, matchedsss) |
54160 | 609 |
else apfst SOME (dissect_coeqn_disc seq fun_names basic_ctr_specss |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
610 |
(SOME (abstract (List.rev fun_args) rhs)) maybe_code_rhs 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
|
611 |
|
54159
eb5d58c99049
set stage for more flexible 'primrec' syntax for recursion through functions
blanchet
parents:
54157
diff
changeset
|
612 |
val sel_concls = sels ~~ ctr_args |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
613 |
|> map (fn (sel, ctr_arg) => HOLogic.mk_eq (betapply (sel, lhs), ctr_arg)); |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
614 |
|
53856 | 615 |
(* |
54065 | 616 |
val _ = tracing ("reduced\n " ^ Syntax.string_of_term @{context} concl ^ "\nto\n \<cdot> " ^ |
617 |
(is_some maybe_eqn_data_disc ? K (Syntax.string_of_term @{context} disc_concl ^ "\n \<cdot> ")) "" ^ |
|
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
618 |
space_implode "\n \<cdot> " (map (Syntax.string_of_term @{context}) sel_concls) ^ |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
619 |
"\nfor premise(s)\n \<cdot> " ^ |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
620 |
space_implode "\n \<cdot> " (map (Syntax.string_of_term @{context}) prems)); |
53856 | 621 |
*) |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
622 |
|
54160 | 623 |
val eqns_data_sel = |
54807
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
624 |
map (dissect_coeqn_sel fun_names basic_ctr_specss |
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
625 |
(SOME (abstract (List.rev fun_args) rhs)) maybe_code_rhs eqn' (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
|
626 |
in |
53720 | 627 |
(the_list maybe_eqn_data_disc @ 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
|
628 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
629 |
|
54160 | 630 |
fun dissect_coeqn_code lthy has_call fun_names basic_ctr_specss eqn' concl matchedsss = |
54065 | 631 |
let |
632 |
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
|
633 |
val (fun_name, fun_args) = strip_comb lhs |>> fst o dest_Free; |
54209 | 634 |
val SOME basic_ctr_specs = AList.lookup (op =) (fun_names ~~ basic_ctr_specss) fun_name; |
54065 | 635 |
|
636 |
val cond_ctrs = fold_rev_corec_code_rhs lthy (fn cs => fn ctr => fn _ => |
|
54160 | 637 |
if member ((op =) o apsnd #ctr) basic_ctr_specs ctr |
54065 | 638 |
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
|
639 |
else primcorec_error_eqn "not a constructor" ctr) [] rhs' [] |
54065 | 640 |
|> AList.group (op =); |
641 |
||
54068 | 642 |
val ctr_premss = (case cond_ctrs of [_] => [[]] | _ => map (s_dnf o snd) cond_ctrs); |
54065 | 643 |
val ctr_concls = cond_ctrs |> map (fn (ctr, _) => |
644 |
binder_types (fastype_of ctr) |
|
645 |
|> map_index (fn (n, T) => massage_corec_code_rhs lthy (fn _ => fn ctr' => fn args => |
|
646 |
if ctr' = ctr then nth args n else Const (@{const_name undefined}, T)) [] rhs') |
|
647 |
|> curry list_comb ctr |
|
648 |
|> curry HOLogic.mk_eq lhs); |
|
649 |
in |
|
54160 | 650 |
fold_map2 (dissect_coeqn_ctr false fun_names basic_ctr_specss eqn' |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
651 |
(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
|
652 |
ctr_premss ctr_concls matchedsss |
54065 | 653 |
end; |
654 |
||
54160 | 655 |
fun dissect_coeqn lthy seq has_call fun_names (basic_ctr_specss : basic_corec_ctr_spec list list) |
54209 | 656 |
eqn' maybe_of_spec matchedsss = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
657 |
let |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
658 |
val eqn = drop_All eqn' |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
659 |
handle TERM _ => primcorec_error_eqn "malformed function equation" eqn'; |
54065 | 660 |
val (prems, concl) = Logic.strip_horn eqn |
53341 | 661 |
|> apfst (map HOLogic.dest_Trueprop) o apsnd HOLogic.dest_Trueprop; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
662 |
|
54065 | 663 |
val head = concl |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
664 |
|> 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
|
665 |
|> head_of; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
666 |
|
54209 | 667 |
val maybe_rhs = 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
|
668 |
|
54160 | 669 |
val discs = maps (map #disc) basic_ctr_specss; |
670 |
val sels = maps (maps #sels) basic_ctr_specss; |
|
671 |
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
|
672 |
in |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
673 |
if member (op =) discs head orelse |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
674 |
is_some maybe_rhs andalso |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
675 |
member (op =) (filter (null o binder_types o fastype_of) ctrs) (the maybe_rhs) then |
54160 | 676 |
dissect_coeqn_disc seq fun_names basic_ctr_specss NONE NONE prems concl matchedsss |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
677 |
|>> single |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
678 |
else if member (op =) sels head then |
54807
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
679 |
([dissect_coeqn_sel fun_names basic_ctr_specss NONE NONE eqn' maybe_of_spec concl], matchedsss) |
54065 | 680 |
else if is_Free head andalso member (op =) fun_names (fst (dest_Free head)) andalso |
54074
43cdae9524bf
allow 'let's around constructors in constructor view
blanchet
parents:
54068
diff
changeset
|
681 |
member (op =) ctrs (head_of (unfold_let (the maybe_rhs))) then |
54160 | 682 |
dissect_coeqn_ctr seq fun_names basic_ctr_specss eqn' NONE prems concl matchedsss |
54065 | 683 |
else if is_Free head andalso member (op =) fun_names (fst (dest_Free head)) andalso |
684 |
null prems then |
|
54160 | 685 |
dissect_coeqn_code lthy has_call fun_names basic_ctr_specss eqn' concl matchedsss |
54065 | 686 |
|>> flat |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
687 |
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
|
688 |
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
|
689 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
690 |
|
54002 | 691 |
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
|
692 |
({fun_args, ctr_no, prems, ...} : coeqn_data_disc) = |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
693 |
if is_none (#pred (nth ctr_specs ctr_no)) then I else |
54068 | 694 |
s_conjs prems |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
695 |
|> curry subst_bounds (List.rev fun_args) |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
696 |
|> HOLogic.tupled_lambda (HOLogic.mk_tuple fun_args) |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
697 |
|> 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
|
698 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
699 |
fun build_corec_arg_no_call (sel_eqns : coeqn_data_sel list) sel = |
53720 | 700 |
find_first (equal sel o #sel) sel_eqns |
701 |
|> try (fn SOME {fun_args, rhs_term, ...} => abs_tuple fun_args rhs_term) |
|
702 |
|> the_default undef_const |
|
53411 | 703 |
|> K; |
53360 | 704 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
705 |
fun build_corec_args_mutual_call lthy has_call (sel_eqns : coeqn_data_sel list) sel = |
54208 | 706 |
(case find_first (equal sel o #sel) sel_eqns of |
707 |
NONE => (I, I, I) |
|
708 |
| SOME {fun_args, rhs_term, ... } => |
|
53876 | 709 |
let |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
710 |
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
|
711 |
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
|
712 |
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
|
713 |
fun rewrite_cont bound_Ts t = |
53899 | 714 |
if has_call t then mk_tuple1 bound_Ts (snd (strip_comb t)) else undef_const; |
54102 | 715 |
fun massage f _ = massage_mutual_corec_call lthy has_call f bound_Ts rhs_term |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
716 |
|> abs_tuple fun_args; |
53876 | 717 |
in |
54207
9296ebf40db0
tuned names (to make them independent from temporary naming convention used in characteristic theorems)
blanchet
parents:
54206
diff
changeset
|
718 |
(massage rewrite_stop, massage rewrite_end, massage rewrite_cont) |
54208 | 719 |
end); |
53360 | 720 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
721 |
fun build_corec_arg_nested_call lthy has_call (sel_eqns : coeqn_data_sel list) sel = |
54208 | 722 |
(case find_first (equal sel o #sel) sel_eqns of |
723 |
NONE => I |
|
724 |
| SOME {fun_args, rhs_term, ...} => |
|
53899 | 725 |
let |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
726 |
val bound_Ts = List.rev (map fastype_of fun_args); |
53899 | 727 |
fun rewrite bound_Ts U T (Abs (v, V, b)) = Abs (v, V, rewrite (V :: bound_Ts) U T b) |
728 |
| rewrite bound_Ts U T (t as _ $ _) = |
|
729 |
let val (u, vs) = strip_comb t in |
|
730 |
if is_Free u andalso has_call u then |
|
731 |
Inr_const U T $ mk_tuple1 bound_Ts vs |
|
54271 | 732 |
else if try (fst o dest_Const) u = SOME @{const_name prod_case} then |
53899 | 733 |
map (rewrite bound_Ts U T) vs |> chop 1 |>> HOLogic.mk_split o the_single |> list_comb |
734 |
else |
|
735 |
list_comb (rewrite bound_Ts U T u, map (rewrite bound_Ts U T) vs) |
|
736 |
end |
|
737 |
| rewrite _ U T t = |
|
738 |
if is_Free t andalso has_call t then Inr_const U T $ HOLogic.unit else t; |
|
739 |
fun massage t = |
|
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
740 |
rhs_term |
54102 | 741 |
|> massage_nested_corec_call lthy has_call rewrite bound_Ts (range_type (fastype_of t)) |
53735 | 742 |
|> abs_tuple fun_args; |
53899 | 743 |
in |
744 |
massage |
|
54208 | 745 |
end); |
53360 | 746 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
747 |
fun build_corec_args_sel lthy has_call (all_sel_eqns : coeqn_data_sel list) |
54002 | 748 |
(ctr_spec : corec_ctr_spec) = |
54208 | 749 |
(case filter (equal (#ctr ctr_spec) o #ctr) all_sel_eqns of |
750 |
[] => I |
|
751 |
| sel_eqns => |
|
752 |
let |
|
753 |
val sel_call_list = #sels ctr_spec ~~ #calls ctr_spec; |
|
754 |
val no_calls' = map_filter (try (apsnd (fn No_Corec n => n))) sel_call_list; |
|
755 |
val mutual_calls' = map_filter (try (apsnd (fn Mutual_Corec n => n))) sel_call_list; |
|
756 |
val nested_calls' = map_filter (try (apsnd (fn Nested_Corec n => n))) sel_call_list; |
|
757 |
in |
|
758 |
I |
|
759 |
#> fold (fn (sel, n) => nth_map n (build_corec_arg_no_call sel_eqns sel)) no_calls' |
|
760 |
#> fold (fn (sel, (q, g, h)) => |
|
761 |
let val (fq, fg, fh) = build_corec_args_mutual_call lthy has_call sel_eqns sel in |
|
762 |
nth_map q fq o nth_map g fg o nth_map h fh end) mutual_calls' |
|
763 |
#> fold (fn (sel, n) => nth_map n |
|
764 |
(build_corec_arg_nested_call lthy has_call sel_eqns sel)) nested_calls' |
|
765 |
end); |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
766 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
767 |
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
|
768 |
(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
|
769 |
let |
54160 | 770 |
val corecs = map #corec corec_specs; |
771 |
val ctr_specss = map #ctr_specs corec_specs; |
|
53360 | 772 |
val corec_args = hd corecs |
773 |
|> fst o split_last o binder_types o fastype_of |
|
54806
a0f024caa04c
pass auto-proved exhaustiveness properties to tactic;
panny
parents:
54628
diff
changeset
|
774 |
|> map (fn T => if range_type T = @{typ bool} |
a0f024caa04c
pass auto-proved exhaustiveness properties to tactic;
panny
parents:
54628
diff
changeset
|
775 |
then Abs (Name.uu_, domain_type T, @{term False}) |
a0f024caa04c
pass auto-proved exhaustiveness properties to tactic;
panny
parents:
54628
diff
changeset
|
776 |
else Const (@{const_name undefined}, T)) |
53720 | 777 |
|> fold2 (fold o build_corec_arg_disc) ctr_specss disc_eqnss |
53360 | 778 |
|> fold2 (fold o build_corec_args_sel lthy has_call) sel_eqnss ctr_specss; |
53735 | 779 |
fun currys [] t = t |
780 |
| currys Ts t = t $ mk_tuple1 (List.rev Ts) (map Bound (length Ts - 1 downto 0)) |
|
781 |
|> fold_rev (Term.abs o pair Name.uu) Ts; |
|
53401 | 782 |
|
53856 | 783 |
(* |
53360 | 784 |
val _ = tracing ("corecursor arguments:\n \<cdot> " ^ |
53411 | 785 |
space_implode "\n \<cdot> " (map (Syntax.string_of_term lthy) corec_args)); |
53856 | 786 |
*) |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
787 |
|
54835 | 788 |
val excludess' = |
53720 | 789 |
disc_eqnss |
53822 | 790 |
|> 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
|
791 |
#> 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
|
792 |
#> maps (uncurry (map o pair) |
53822 | 793 |
#> map (fn ((fun_args, c, x, a), (_, c', y, a')) => |
54068 | 794 |
((c, c', a orelse a'), (x, s_not (s_conjs y))) |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
795 |
||> apfst (map HOLogic.mk_Trueprop) o apsnd HOLogic.mk_Trueprop |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
796 |
||> Logic.list_implies |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
797 |
||> 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
|
798 |
in |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
799 |
map (list_comb o rpair corec_args) corecs |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
800 |
|> 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
|
801 |
|> map2 currys arg_Tss |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
802 |
|> Syntax.check_terms lthy |
54155 | 803 |
|> map3 (fn b => fn mx => fn t => ((b, mx), ((Binding.conceal (Thm.def_binding b), []), t))) |
804 |
bs mxs |
|
54835 | 805 |
|> rpair excludess' |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
806 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
807 |
|
54002 | 808 |
fun mk_real_disc_eqns fun_binding arg_Ts ({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
|
809 |
(sel_eqns : coeqn_data_sel list) (disc_eqns : coeqn_data_disc list) = |
53720 | 810 |
if length disc_eqns <> length ctr_specs - 1 then disc_eqns else |
811 |
let |
|
812 |
val n = 0 upto length ctr_specs |
|
813 |
|> the o find_first (fn idx => not (exists (equal idx o #ctr_no) disc_eqns)); |
|
53722
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
814 |
val fun_args = (try (#fun_args o hd) disc_eqns, try (#fun_args o hd) sel_eqns) |
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
815 |
|> the_default (map (curry Free Name.uu) arg_Ts) o merge_options; |
54807
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
816 |
val maybe_sel_eqn = find_first (equal (Binding.name_of fun_binding) o #fun_name) sel_eqns; |
53720 | 817 |
val extra_disc_eqn = { |
818 |
fun_name = Binding.name_of fun_binding, |
|
819 |
fun_T = arg_Ts ---> body_type (fastype_of (#ctr (hd ctr_specs))), |
|
53722
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
820 |
fun_args = fun_args, |
53720 | 821 |
ctr = #ctr (nth ctr_specs n), |
822 |
ctr_no = n, |
|
823 |
disc = #disc (nth ctr_specs n), |
|
54067 | 824 |
prems = maps (s_not_conj o #prems) disc_eqns, |
53822 | 825 |
auto_gen = true, |
54807
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
826 |
maybe_ctr_rhs = Option.map #maybe_ctr_rhs maybe_sel_eqn |> the_default NONE, |
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
827 |
maybe_code_rhs = Option.map #maybe_ctr_rhs maybe_sel_eqn |> the_default NONE, |
53720 | 828 |
user_eqn = undef_const}; |
829 |
in |
|
830 |
chop n disc_eqns ||> cons extra_disc_eqn |> (op @) |
|
831 |
end; |
|
832 |
||
54243 | 833 |
fun find_corec_calls ctxt has_call basic_ctr_specs ({ctr, sel, rhs_term, ...} : coeqn_data_sel) = |
54160 | 834 |
let |
835 |
val sel_no = find_first (equal ctr o #ctr) basic_ctr_specs |
|
836 |
|> find_index (equal sel) o #sels o the; |
|
54243 | 837 |
fun find t = if has_call t then snd (fold_rev_corec_call ctxt (K cons) [] t []) else []; |
54160 | 838 |
in |
839 |
find rhs_term |
|
840 |
|> K |> nth_map sel_no |> AList.map_entry (op =) ctr |
|
841 |
end; |
|
842 |
||
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
843 |
fun add_primcorec_ursive maybe_tac opts fixes specs maybe_of_specs lthy = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
844 |
let |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
845 |
val thy = Proof_Context.theory_of lthy; |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
846 |
|
53352 | 847 |
val (bs, mxs) = map_split (apfst fst) fixes; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
848 |
val (arg_Ts, res_Ts) = map (strip_type o snd o fst #>> HOLogic.mk_tupleT) fixes |> split_list; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
849 |
|
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
850 |
val _ = (case filter_out (fn (_, T) => Sign.of_sort thy (T, HOLogic.typeS)) (bs ~~ arg_Ts) of |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
851 |
[] => () |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
852 |
| (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
|
853 |
|
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
854 |
val seq = member (op =) opts Option_Sequential; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
855 |
val exhaustive = member (op =) opts Option_Exhaustive; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
856 |
|
54160 | 857 |
val fun_names = map Binding.name_of bs; |
858 |
val basic_ctr_specss = map (basic_corec_specs_of lthy) res_Ts; |
|
859 |
val has_call = exists_subterm (map (fst #>> Binding.name_of #> Free) fixes |> member (op =)); |
|
860 |
val eqns_data = |
|
861 |
fold_map2 (dissect_coeqn lthy seq has_call fun_names basic_ctr_specss) (map snd specs) |
|
54209 | 862 |
maybe_of_specs [] |
54160 | 863 |
|> flat o fst; |
864 |
||
865 |
val callssss = |
|
866 |
map_filter (try (fn Sel x => x)) eqns_data |
|
867 |
|> partition_eq ((op =) o pairself #fun_name) |
|
868 |
|> fst o finds (fn (x, ({fun_name, ...} :: _)) => x = fun_name) fun_names |
|
54161 | 869 |
|> map (flat o snd) |
54243 | 870 |
|> map2 (fold o find_corec_calls lthy has_call) basic_ctr_specss |
54160 | 871 |
|> map2 (curry (op |>)) (map (map (fn {ctr, sels, ...} => |
872 |
(ctr, map (K []) sels))) basic_ctr_specss); |
|
873 |
||
874 |
(* |
|
875 |
val _ = tracing ("callssss = " ^ @{make_string} callssss); |
|
876 |
*) |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
877 |
|
53830
ed2eb7df2aac
don't note more induction principles than there are functions + tuning
blanchet
parents:
53822
diff
changeset
|
878 |
val ((n2m, corec_specs', _, coinduct_thm, strong_coinduct_thm, coinduct_thms, |
53797 | 879 |
strong_coinduct_thms), lthy') = |
53794 | 880 |
corec_specs_of bs arg_Ts res_Ts (get_indices fixes) callssss lthy; |
53830
ed2eb7df2aac
don't note more induction principles than there are functions + tuning
blanchet
parents:
53822
diff
changeset
|
881 |
val actual_nn = length bs; |
ed2eb7df2aac
don't note more induction principles than there are functions + tuning
blanchet
parents:
53822
diff
changeset
|
882 |
val corec_specs = take actual_nn corec_specs'; (*###*) |
54178 | 883 |
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
|
884 |
|
53720 | 885 |
val disc_eqnss' = map_filter (try (fn Disc x => x)) eqns_data |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
886 |
|> partition_eq ((op =) o pairself #fun_name) |
53720 | 887 |
|> 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
|
888 |
|> map (sort ((op <) o pairself #ctr_no |> make_ord) o flat o snd); |
53720 | 889 |
val _ = disc_eqnss' |> map (fn x => |
890 |
let val d = duplicates ((op =) o pairself #ctr_no) x in null d 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
|
891 |
primcorec_error_eqns "excess discriminator formula in definition" |
53720 | 892 |
(maps (fn t => filter (equal (#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
|
893 |
|
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
894 |
val sel_eqnss = map_filter (try (fn Sel x => x)) eqns_data |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
895 |
|> partition_eq ((op =) o pairself #fun_name) |
53720 | 896 |
|> 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
|
897 |
|> map (flat o snd); |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
898 |
|
53360 | 899 |
val arg_Tss = map (binder_types o snd o fst) fixes; |
53722
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
900 |
val disc_eqnss = map5 mk_real_disc_eqns bs arg_Tss corec_specs sel_eqnss disc_eqnss'; |
54835 | 901 |
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
|
902 |
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
|
903 |
|
54835 | 904 |
fun exclude_tac (c, c', a) = |
54177 | 905 |
if a orelse c = c' orelse seq then SOME (K (HEADGOAL (mk_primcorec_assumption_tac lthy []))) |
906 |
else maybe_tac; |
|
53822 | 907 |
|
53856 | 908 |
(* |
53822 | 909 |
val _ = tracing ("exclusiveness properties:\n \<cdot> " ^ |
54835 | 910 |
space_implode "\n \<cdot> " (maps (map (Syntax.string_of_term lthy o snd)) excludess')); |
53856 | 911 |
*) |
53822 | 912 |
|
54835 | 913 |
val excludess'' = excludess' |> map (map (fn (idx, t) => |
914 |
(idx, (Option.map (Goal.prove lthy [] [] t #> Thm.close_derivation) (exclude_tac idx), t)))); |
|
915 |
val taut_thmss = map (map (apsnd (the o fst)) o filter (is_some o fst o snd)) excludess''; |
|
916 |
val (goal_idxss, goalss') = excludess'' |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
917 |
|> 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
|
918 |
|> split_list o map split_list; |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
919 |
|
54832 | 920 |
val exhaust_props = if not exhaustive then [] else |
54613 | 921 |
map (HOLogic.mk_Trueprop o mk_disjs o map (mk_conjs o #prems)) disc_eqnss |
922 |
|> map2 ((fn {fun_args, ...} => |
|
923 |
curry Logic.list_all (map dest_Free fun_args)) o hd) disc_eqnss; |
|
54832 | 924 |
val exhaust_taut_thms = if exhaustive andalso is_some maybe_tac then |
925 |
map (fn t => Goal.prove lthy [] [] t (the maybe_tac) |> Thm.close_derivation) exhaust_props |
|
54613 | 926 |
else []; |
927 |
val goalss = if exhaustive andalso is_none maybe_tac then |
|
54832 | 928 |
map (rpair []) exhaust_props :: goalss' else goalss'; |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
929 |
|
54613 | 930 |
fun prove thmss'' def_thms' lthy = |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
931 |
let |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
932 |
val def_thms = map (snd o snd) def_thms'; |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
933 |
|
54832 | 934 |
val maybe_exhaust_thms = if not exhaustive then map (K NONE) def_thms else |
935 |
map SOME (if is_none maybe_tac then hd thmss'' else exhaust_taut_thms); |
|
54835 | 936 |
val exclude_thmss = if exhaustive andalso is_none maybe_tac then tl thmss'' else thmss''; |
54613 | 937 |
|
54835 | 938 |
val excludess' = map (op ~~) (goal_idxss ~~ exclude_thmss); |
939 |
fun mk_excludesss excludes n = |
|
940 |
(excludes, map (fn k => replicate k [TrueI] @ replicate (n - k) []) (0 upto n - 1)) |
|
53822 | 941 |
|-> fold (fn ((c, c', _), thm) => nth_map c (nth_map c' (K [thm]))); |
54835 | 942 |
val excludessss = (excludess' ~~ taut_thmss |> map (op @), fun_names ~~ corec_specs) |
943 |
|-> map2 (fn excludes => fn (_, {ctr_specs, ...}) => |
|
944 |
mk_excludesss excludes (length ctr_specs)); |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
945 |
|
54835 | 946 |
fun prove_disc ({ctr_specs, ...} : corec_spec) excludesss |
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
947 |
({fun_name, fun_T, fun_args, ctr_no, prems, ...} : coeqn_data_disc) = |
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
948 |
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
|
949 |
[] |
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54271
diff
changeset
|
950 |
else |
53720 | 951 |
let |
53722
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
952 |
val {disc_corec, ...} = nth ctr_specs ctr_no; |
53720 | 953 |
val k = 1 + ctr_no; |
954 |
val m = length prems; |
|
955 |
val t = |
|
956 |
list_comb (Free (fun_name, fun_T), map Bound (length fun_args - 1 downto 0)) |
|
957 |
|> curry betapply (#disc (nth ctr_specs ctr_no)) (*###*) |
|
958 |
|> HOLogic.mk_Trueprop |
|
959 |
|> curry Logic.list_implies (map HOLogic.mk_Trueprop prems) |
|
960 |
|> curry Logic.list_all (map dest_Free fun_args); |
|
961 |
in |
|
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
962 |
if prems = [@{term False}] then [] else |
54835 | 963 |
mk_primcorec_disc_tac lthy def_thms disc_corec k m excludesss |
53720 | 964 |
|> K |> Goal.prove lthy [] [] t |
54176 | 965 |
|> Thm.close_derivation |
53720 | 966 |
|> pair (#disc (nth ctr_specs ctr_no)) |
967 |
|> single |
|
968 |
end; |
|
969 |
||
54279 | 970 |
fun prove_sel ({nested_map_idents, nested_map_comps, ctr_specs, ...} : corec_spec) |
54835 | 971 |
(disc_eqns : coeqn_data_disc list) excludesss |
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
972 |
({fun_name, fun_T, fun_args, ctr, sel, rhs_term, ...} : coeqn_data_sel) = |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
973 |
let |
53909 | 974 |
val SOME ctr_spec = find_first (equal ctr o #ctr) ctr_specs; |
53720 | 975 |
val ctr_no = find_index (equal ctr o #ctr) ctr_specs; |
54067 | 976 |
val prems = the_default (maps (s_not_conj o #prems) disc_eqns) |
53720 | 977 |
(find_first (equal ctr_no o #ctr_no) disc_eqns |> Option.map #prems); |
978 |
val sel_corec = find_index (equal sel) (#sels ctr_spec) |
|
979 |
|> nth (#sel_corecs ctr_spec); |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
980 |
val k = 1 + ctr_no; |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
981 |
val m = length prems; |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
982 |
val t = |
53720 | 983 |
list_comb (Free (fun_name, fun_T), map Bound (length fun_args - 1 downto 0)) |
984 |
|> curry betapply sel |
|
985 |
|> rpair (abstract (List.rev fun_args) rhs_term) |
|
986 |
|> HOLogic.mk_Trueprop o HOLogic.mk_eq |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
987 |
|> curry Logic.list_implies (map HOLogic.mk_Trueprop prems) |
53720 | 988 |
|> curry Logic.list_all (map dest_Free fun_args); |
53925 | 989 |
val (distincts, _, sel_splits, sel_split_asms) = case_thms_of_term lthy [] rhs_term; |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
990 |
in |
54279 | 991 |
mk_primcorec_sel_tac lthy def_thms distincts sel_splits sel_split_asms nested_map_idents |
54835 | 992 |
nested_map_comps sel_corec k m excludesss |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
993 |
|> K |> Goal.prove lthy [] [] t |
54176 | 994 |
|> Thm.close_derivation |
53720 | 995 |
|> pair sel |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
996 |
end; |
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
997 |
|
54153
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
998 |
fun prove_ctr disc_alist sel_alist (disc_eqns : coeqn_data_disc list) |
67487a607ce2
avoid 'co_' prefix with underscore meaning 'co', since it is our only possible identifier representation of '(co)'
blanchet
parents:
54145
diff
changeset
|
999 |
(sel_eqns : coeqn_data_sel list) ({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
|
1000 |
(* don't try to prove theorems when some sel_eqns are missing *) |
53720 | 1001 |
if not (exists (equal ctr o #ctr) disc_eqns) |
53722
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
1002 |
andalso not (exists (equal 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
|
1003 |
orelse |
53720 | 1004 |
filter (equal ctr o #ctr) sel_eqns |
1005 |
|> fst o finds ((op =) o apsnd #sel) sels |
|
1006 |
|> exists (null o snd) |
|
1007 |
then [] else |
|
1008 |
let |
|
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1009 |
val (fun_name, fun_T, fun_args, prems, maybe_rhs) = |
53722
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
1010 |
(find_first (equal ctr o #ctr) disc_eqns, find_first (equal 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
|
1011 |
|>> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, #prems x, |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1012 |
#maybe_ctr_rhs x)) |
54807
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
1013 |
||> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, [], #maybe_ctr_rhs x)) |
53722
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
1014 |
|> the o merge_options; |
53720 | 1015 |
val m = length prems; |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1016 |
val t = (if is_some maybe_rhs then the maybe_rhs else |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1017 |
filter (equal ctr o #ctr) sel_eqns |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1018 |
|> fst o finds ((op =) o apsnd #sel) sels |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1019 |
|> map (snd #> (fn [x] => (List.rev (#fun_args x), #rhs_term x)) #-> abstract) |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1020 |
|> curry list_comb ctr) |
53720 | 1021 |
|> curry HOLogic.mk_eq (list_comb (Free (fun_name, fun_T), |
1022 |
map Bound (length fun_args - 1 downto 0))) |
|
1023 |
|> HOLogic.mk_Trueprop |
|
1024 |
|> curry Logic.list_implies (map HOLogic.mk_Trueprop prems) |
|
1025 |
|> curry Logic.list_all (map dest_Free fun_args); |
|
53791 | 1026 |
val maybe_disc_thm = AList.lookup (op =) disc_alist disc; |
1027 |
val sel_thms = map snd (filter (member (op =) sels o fst) sel_alist); |
|
53720 | 1028 |
in |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1029 |
if prems = [@{term False}] then [] else |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1030 |
mk_primcorec_ctr_of_dtr_tac lthy m collapse maybe_disc_thm sel_thms |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1031 |
|> K |> Goal.prove lthy [] [] t |
54176 | 1032 |
|> 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
|
1033 |
|> pair ctr |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1034 |
|> single |
53876 | 1035 |
end; |
53720 | 1036 |
|
54832 | 1037 |
fun prove_code disc_eqns sel_eqns maybe_exhaust ctr_alist ctr_specs = |
54098 | 1038 |
let |
54628
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1039 |
val maybe_fun_data = |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1040 |
(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
|
1041 |
find_first (member (op =) (map #ctr ctr_specs) o #ctr) sel_eqns) |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1042 |
|>> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, #maybe_code_rhs x)) |
54807
df6350c8f61a
pass down user input in more cases in order to preserve "let"s etc.
panny
parents:
54806
diff
changeset
|
1043 |
||> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, #maybe_code_rhs x)) |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1044 |
|> merge_options; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1045 |
in |
54628
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1046 |
(case maybe_fun_data of |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1047 |
NONE => [] |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1048 |
| SOME (fun_name, fun_T, fun_args, maybe_rhs) => |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1049 |
let |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1050 |
val bound_Ts = List.rev (map fastype_of fun_args); |
54173 | 1051 |
|
54628
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1052 |
val lhs = |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1053 |
list_comb (Free (fun_name, fun_T), map Bound (length fun_args - 1 downto 0)); |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1054 |
val maybe_rhs_info = |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1055 |
(case maybe_rhs of |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1056 |
SOME rhs => |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1057 |
let |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1058 |
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
|
1059 |
val cond_ctrs = |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1060 |
fold_rev_corec_code_rhs lthy (K oo (cons oo pair)) bound_Ts raw_rhs []; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1061 |
val ctr_thms = map (the o AList.lookup (op =) ctr_alist o snd) cond_ctrs; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1062 |
in SOME (rhs, raw_rhs, ctr_thms) end |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1063 |
| NONE => |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1064 |
let |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1065 |
fun prove_code_ctr {ctr, sels, ...} = |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1066 |
if not (exists (equal ctr o fst) ctr_alist) then NONE else |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1067 |
let |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1068 |
val prems = find_first (equal ctr o #ctr) disc_eqns |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1069 |
|> Option.map #prems |> the_default []; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1070 |
val t = |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1071 |
filter (equal ctr o #ctr) sel_eqns |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1072 |
|> fst o finds ((op =) o apsnd #sel) sels |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1073 |
|> 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
|
1074 |
#-> abstract) |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1075 |
|> curry list_comb ctr; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1076 |
in |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1077 |
SOME (prems, t) |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1078 |
end; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1079 |
val maybe_ctr_conds_argss = map prove_code_ctr ctr_specs; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1080 |
in |
54628
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1081 |
let |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1082 |
val rhs = (if exhaustive |
54806
a0f024caa04c
pass auto-proved exhaustiveness properties to tactic;
panny
parents:
54628
diff
changeset
|
1083 |
orelse map_filter (try (fst o the)) maybe_ctr_conds_argss |
a0f024caa04c
pass auto-proved exhaustiveness properties to tactic;
panny
parents:
54628
diff
changeset
|
1084 |
|> forall (equal []) |
54628
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1085 |
orelse forall is_some maybe_ctr_conds_argss |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1086 |
andalso exists #auto_gen disc_eqns then |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1087 |
split_last (map_filter I maybe_ctr_conds_argss) ||> snd |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1088 |
else |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1089 |
Const (@{const_name Code.abort}, @{typ String.literal} --> |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1090 |
(@{typ unit} --> body_type fun_T) --> body_type fun_T) $ |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1091 |
HOLogic.mk_literal fun_name $ |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1092 |
absdummy @{typ unit} (incr_boundvars 1 lhs) |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1093 |
|> pair (map_filter I maybe_ctr_conds_argss)) |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1094 |
|-> fold_rev (fn (prems, u) => mk_If (s_conjs prems) u) |
ce80d7cd7277
generate "code" theorems for incomplete definitions
panny
parents:
54613
diff
changeset
|
1095 |
in SOME (rhs, rhs, map snd ctr_alist) end |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1096 |
end); |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1097 |
in |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1098 |
(case maybe_rhs_info of |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1099 |
NONE => [] |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1100 |
| SOME (rhs, raw_rhs, ctr_thms) => |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1101 |
let |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1102 |
val ms = map (Logic.count_prems o prop_of) ctr_thms; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1103 |
val (raw_t, t) = (raw_rhs, rhs) |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1104 |
|> pairself |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1105 |
(curry HOLogic.mk_eq (list_comb (Free (fun_name, fun_T), |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1106 |
map Bound (length fun_args - 1 downto 0))) |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1107 |
#> HOLogic.mk_Trueprop |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1108 |
#> curry Logic.list_all (map dest_Free fun_args)); |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1109 |
val (distincts, discIs, sel_splits, sel_split_asms) = |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1110 |
case_thms_of_term lthy bound_Ts raw_rhs; |
54098 | 1111 |
|
54613 | 1112 |
val raw_code_thm = mk_primcorec_raw_code_of_ctr_tac lthy distincts discIs |
54832 | 1113 |
sel_splits sel_split_asms ms ctr_thms maybe_exhaust |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1114 |
|> K |> Goal.prove lthy [] [] raw_t |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1115 |
|> Thm.close_derivation; |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1116 |
in |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1117 |
mk_primcorec_code_of_raw_code_tac lthy distincts sel_splits raw_code_thm |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1118 |
|> K |> Goal.prove lthy [] [] t |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1119 |
|> Thm.close_derivation |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1120 |
|> single |
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1121 |
end) |
54173 | 1122 |
end) |
1123 |
end; |
|
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1124 |
|
54835 | 1125 |
val disc_alists = map3 (maps oo prove_disc) corec_specs excludessss disc_eqnss; |
1126 |
val sel_alists = map4 (map ooo prove_sel) corec_specs disc_eqnss excludessss sel_eqnss; |
|
53791 | 1127 |
val disc_thmss = map (map snd) disc_alists; |
1128 |
val sel_thmss = map (map snd) sel_alists; |
|
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1129 |
|
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1130 |
val ctr_alists = map5 (maps oooo prove_ctr) disc_alists sel_alists disc_eqnss sel_eqnss |
54178 | 1131 |
ctr_specss; |
54097
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1132 |
val ctr_thmss = map (map snd) ctr_alists; |
92c5bd3b342d
prove user-supplied equations for ctr and code reductions, preserving "let"s, "case"s etc.;
panny
parents:
54074
diff
changeset
|
1133 |
|
54832 | 1134 |
val code_thmss = map5 prove_code disc_eqnss sel_eqnss maybe_exhaust_thms ctr_alists |
1135 |
ctr_specss; |
|
53791 | 1136 |
|
54030
732b53d9b720
don't register equations of the form 'f x = ...' as simp rules, even if they are safe (noncorecursive), because they unfold too aggresively concepts users are likely to want to stay folded
blanchet
parents:
54028
diff
changeset
|
1137 |
val simp_thmss = map2 append disc_thmss sel_thmss |
53795 | 1138 |
|
53797 | 1139 |
val common_name = mk_common_name fun_names; |
1140 |
||
53791 | 1141 |
val notes = |
53830
ed2eb7df2aac
don't note more induction principles than there are functions + tuning
blanchet
parents:
53822
diff
changeset
|
1142 |
[(coinductN, map (if n2m then single else K []) coinduct_thms, []), |
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54133
diff
changeset
|
1143 |
(codeN, code_thmss, code_nitpicksimp_attrs), |
53797 | 1144 |
(ctrN, ctr_thmss, []), |
53791 | 1145 |
(discN, disc_thmss, simp_attrs), |
54835 | 1146 |
(excludeN, exclude_thmss, []), |
54834 | 1147 |
(exhaustN, map the_list maybe_exhaust_thms, []), |
53795 | 1148 |
(selN, sel_thmss, simp_attrs), |
53797 | 1149 |
(simpsN, simp_thmss, []), |
53830
ed2eb7df2aac
don't note more induction principles than there are functions + tuning
blanchet
parents:
53822
diff
changeset
|
1150 |
(strong_coinductN, map (if n2m then single else K []) strong_coinduct_thms, [])] |
53791 | 1151 |
|> maps (fn (thmN, thmss, attrs) => |
1152 |
map2 (fn fun_name => fn thms => |
|
1153 |
((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
|
1154 |
fun_names (take actual_nn thmss)) |
53791 | 1155 |
|> filter_out (null o fst o hd o snd); |
53797 | 1156 |
|
1157 |
val common_notes = |
|
53830
ed2eb7df2aac
don't note more induction principles than there are functions + tuning
blanchet
parents:
53822
diff
changeset
|
1158 |
[(coinductN, if n2m then [coinduct_thm] else [], []), |
ed2eb7df2aac
don't note more induction principles than there are functions + tuning
blanchet
parents:
53822
diff
changeset
|
1159 |
(strong_coinductN, if n2m then [strong_coinduct_thm] else [], [])] |
53797 | 1160 |
|> filter_out (null o #2) |
1161 |
|> map (fn (thmN, thms, attrs) => |
|
1162 |
((Binding.qualify true common_name (Binding.name thmN), attrs), [(thms, [])])); |
|
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1163 |
in |
54030
732b53d9b720
don't register equations of the form 'f x = ...' as simp rules, even if they are safe (noncorecursive), because they unfold too aggresively concepts users are likely to want to stay folded
blanchet
parents:
54028
diff
changeset
|
1164 |
lthy |> Local_Theory.notes (notes @ common_notes) |> snd |
53654
8b9ea4420f81
prove simp theorems for newly generated definitions
panny
parents:
53411
diff
changeset
|
1165 |
end; |
53822 | 1166 |
|
1167 |
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
|
1168 |
in |
54177 | 1169 |
(goalss, after_qed, lthy') |
53822 | 1170 |
end; |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1171 |
|
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1172 |
fun add_primcorec_ursive_cmd maybe_tac opts (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
|
1173 |
let |
54209 | 1174 |
val (raw_specs, maybe_of_specs) = |
1175 |
split_list raw_specs' ||> map (Option.map (Syntax.read_term lthy)); |
|
53831
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53830
diff
changeset
|
1176 |
val ((fixes, specs), _) = 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
|
1177 |
in |
54591
c822230fd22b
prevent exception when equations for a function are missing;
panny
parents:
54279
diff
changeset
|
1178 |
add_primcorec_ursive maybe_tac opts fixes specs maybe_of_specs 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
|
1179 |
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
|
1180 |
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
|
1181 |
handle Primcorec_Error (str, eqns) => |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1182 |
if null eqns |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1183 |
then error ("primcorec error:\n " ^ str) |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1184 |
else error ("primcorec error:\n " ^ str ^ "\nin\n " ^ |
53822 | 1185 |
space_implode "\n " (map (quote o Syntax.string_of_term lthy) eqns)); |
1186 |
||
54177 | 1187 |
val add_primcorecursive_cmd = (fn (goalss, after_qed, lthy) => |
1188 |
lthy |
|
1189 |
|> Proof.theorem NONE after_qed goalss |
|
1190 |
|> Proof.refine (Method.primitive_text I) |
|
1191 |
|> Seq.hd) ooo add_primcorec_ursive_cmd NONE; |
|
1192 |
||
1193 |
val add_primcorec_cmd = (fn (goalss, after_qed, lthy) => |
|
1194 |
lthy |
|
1195 |
|> after_qed (map (fn [] => [] |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54243
diff
changeset
|
1196 |
| _ => primcorec_error "need exclusiveness proofs - use primcorecursive instead of primcorec") |
54177 | 1197 |
goalss)) ooo add_primcorec_ursive_cmd (SOME (fn {context = ctxt, ...} => auto_tac ctxt)); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1198 |
|
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
1199 |
end; |