author | haftmann |
Fri, 27 Aug 2010 19:34:23 +0200 | |
changeset 38857 | 97775f3e8722 |
parent 38538 | c87b69396a37 |
child 38864 | 4abe644fcea5 |
permissions | -rw-r--r-- |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
1 |
(* Title: HOL/Tools/Datatype/datatype_codegen.ML |
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
28965
diff
changeset
|
2 |
Author: Stefan Berghofer and Florian Haftmann, TU Muenchen |
12445 | 3 |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
4 |
Code generator facilities for inductive datatypes. |
12445 | 5 |
*) |
6 |
||
7 |
signature DATATYPE_CODEGEN = |
|
8 |
sig |
|
18708 | 9 |
val setup: theory -> theory |
12445 | 10 |
end; |
11 |
||
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
12 |
structure Datatype_Codegen : DATATYPE_CODEGEN = |
12445 | 13 |
struct |
14 |
||
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
15 |
(** generic code generator **) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
16 |
|
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
17 |
(* liberal addition of code data for datatypes *) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
18 |
|
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
19 |
fun mk_constr_consts thy vs tyco cos = |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
20 |
let |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
21 |
val cs = map (fn (c, tys) => (c, tys ---> Type (tyco, map TFree vs))) cos; |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
22 |
val cs' = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) cs; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
23 |
in if is_some (try (Code.constrset_of_consts thy) cs') |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
24 |
then SOME cs |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
25 |
else NONE |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
26 |
end; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
27 |
|
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
28 |
|
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
29 |
(* case certificates *) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
30 |
|
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
31 |
fun mk_case_cert thy tyco = |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
32 |
let |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
33 |
val raw_thms = |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
34 |
(#case_rewrites o Datatype_Data.the_info thy) tyco; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
35 |
val thms as hd_thm :: _ = raw_thms |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
36 |
|> Conjunction.intr_balanced |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
37 |
|> Thm.unvarify_global |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
38 |
|> Conjunction.elim_balanced (length raw_thms) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
39 |
|> map Simpdata.mk_meta_eq |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
40 |
|> map Drule.zero_var_indexes |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
41 |
val params = fold_aterms (fn (Free (v, _)) => insert (op =) v |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
42 |
| _ => I) (Thm.prop_of hd_thm) []; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
43 |
val rhs = hd_thm |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
44 |
|> Thm.prop_of |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
45 |
|> Logic.dest_equals |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
46 |
|> fst |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
47 |
|> Term.strip_comb |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
48 |
|> apsnd (fst o split_last) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
49 |
|> list_comb; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
50 |
val lhs = Free (Name.variant params "case", Term.fastype_of rhs); |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
51 |
val asm = (Thm.cterm_of thy o Logic.mk_equals) (lhs, rhs); |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
52 |
in |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
53 |
thms |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
54 |
|> Conjunction.intr_balanced |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
55 |
|> MetaSimplifier.rewrite_rule [(Thm.symmetric o Thm.assume) asm] |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
56 |
|> Thm.implies_intr asm |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
57 |
|> Thm.generalize ([], params) 0 |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
58 |
|> AxClass.unoverload thy |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
59 |
|> Thm.varifyT_global |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
60 |
end; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
61 |
|
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
62 |
|
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
63 |
(* equality *) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
64 |
|
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
65 |
fun mk_eq_eqns thy tyco = |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
66 |
let |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
67 |
val (vs, cos) = Datatype_Data.the_spec thy tyco; |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
68 |
val { descr, index, inject = inject_thms, distinct = distinct_thms, ... } = |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
69 |
Datatype_Data.the_info thy tyco; |
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
70 |
val ty = Type (tyco, map TFree vs); |
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38538
diff
changeset
|
71 |
fun mk_eq (t1, t2) = Const (@{const_name HOL.equal}, ty --> ty --> HOLogic.boolT) |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
72 |
$ t1 $ t2; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
73 |
fun true_eq t12 = HOLogic.mk_eq (mk_eq t12, HOLogic.true_const); |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
74 |
fun false_eq t12 = HOLogic.mk_eq (mk_eq t12, HOLogic.false_const); |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
75 |
val triv_injects = map_filter |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
76 |
(fn (c, []) => SOME (HOLogic.mk_Trueprop (true_eq (Const (c, ty), Const (c, ty)))) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
77 |
| _ => NONE) cos; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
78 |
fun prep_inject (trueprop $ (equiv $ (_ $ t1 $ t2) $ rhs)) = |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
79 |
trueprop $ (equiv $ mk_eq (t1, t2) $ rhs); |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
80 |
val injects = map prep_inject (nth (Datatype_Prop.make_injs [descr] vs) index); |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
81 |
fun prep_distinct (trueprop $ (not $ (_ $ t1 $ t2))) = |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
82 |
[trueprop $ false_eq (t1, t2), trueprop $ false_eq (t2, t1)]; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
83 |
val distincts = maps prep_distinct (snd (nth (Datatype_Prop.make_distincts [descr] vs) index)); |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
84 |
val refl = HOLogic.mk_Trueprop (true_eq (Free ("x", ty), Free ("x", ty))); |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
85 |
val simpset = Simplifier.global_context thy (HOL_basic_ss addsimps |
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38538
diff
changeset
|
86 |
(map Simpdata.mk_eq (@{thm equal} :: @{thm eq_True} :: inject_thms @ distinct_thms))); |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
87 |
fun prove prop = Skip_Proof.prove_global thy [] [] prop (K (ALLGOALS (simp_tac simpset))) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
88 |
|> Simpdata.mk_eq; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
89 |
in (map prove (triv_injects @ injects @ distincts), prove refl) end; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
90 |
|
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
91 |
fun add_equality vs tycos thy = |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
92 |
let |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
93 |
fun add_def tyco lthy = |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
94 |
let |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
95 |
val ty = Type (tyco, map TFree vs); |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
96 |
fun mk_side const_name = Const (const_name, ty --> ty --> HOLogic.boolT) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
97 |
$ Free ("x", ty) $ Free ("y", ty); |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
98 |
val def = HOLogic.mk_Trueprop (HOLogic.mk_eq |
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38538
diff
changeset
|
99 |
(mk_side @{const_name HOL.equal}, mk_side @{const_name "op ="})); |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
100 |
val def' = Syntax.check_term lthy def; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
101 |
val ((_, (_, thm)), lthy') = Specification.definition |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
102 |
(NONE, (Attrib.empty_binding, def')) lthy; |
36610
bafd82950e24
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents:
36298
diff
changeset
|
103 |
val ctxt_thy = ProofContext.init_global (ProofContext.theory_of lthy); |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
104 |
val thm' = singleton (ProofContext.export lthy' ctxt_thy) thm; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
105 |
in (thm', lthy') end; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
106 |
fun tac thms = Class.intro_classes_tac [] |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
107 |
THEN ALLGOALS (ProofContext.fact_tac thms); |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
108 |
fun prefix tyco = Binding.qualify true (Long_Name.base_name tyco) o Binding.qualify true "eq" o Binding.name; |
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
109 |
fun add_eq_thms tyco = |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
110 |
Theory.checkpoint |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
111 |
#> `(fn thy => mk_eq_eqns thy tyco) |
37425 | 112 |
#-> (fn (thms, thm) => PureThy.note_thmss Thm.lemmaK |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
113 |
[((prefix tyco "refl", [Code.add_nbe_default_eqn_attribute]), [([thm], [])]), |
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
114 |
((prefix tyco "simps", [Code.add_default_eqn_attribute]), [(rev thms, [])])]) |
37425 | 115 |
#> snd |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
116 |
in |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
117 |
thy |
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38538
diff
changeset
|
118 |
|> Class.instantiation (tycos, vs, [HOLogic.class_equal]) |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
119 |
|> fold_map add_def tycos |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
120 |
|-> (fn def_thms => Class.prove_instantiation_exit_result (map o Morphism.thm) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
121 |
(fn _ => fn def_thms => tac def_thms) def_thms) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
122 |
|-> (fn def_thms => fold Code.del_eqn def_thms) |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
123 |
|> fold add_eq_thms tycos |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
124 |
end; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
125 |
|
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
126 |
|
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
127 |
(* register a datatype etc. *) |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
128 |
|
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
129 |
fun add_all_code config tycos thy = |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
130 |
let |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
131 |
val (vs :: _, coss) = (split_list o map (Datatype_Data.the_spec thy)) tycos; |
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
132 |
val any_css = map2 (mk_constr_consts thy vs) tycos coss; |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
133 |
val css = if exists is_none any_css then [] |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
134 |
else map_filter I any_css; |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
135 |
val case_rewrites = maps (#case_rewrites o Datatype_Data.the_info thy) tycos; |
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
136 |
val certs = map (mk_case_cert thy) tycos; |
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
137 |
val tycos_eq = filter_out |
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38538
diff
changeset
|
138 |
(fn tyco => can (Sorts.mg_domain (Sign.classes_of thy) tyco) [HOLogic.class_equal]) tycos; |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
139 |
in |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
140 |
if null css then thy |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
141 |
else thy |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
142 |
|> tap (fn _ => Datatype_Aux.message config "Registering datatype for code generator ...") |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
143 |
|> fold Code.add_datatype css |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
144 |
|> fold_rev Code.add_default_eqn case_rewrites |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
145 |
|> fold Code.add_case certs |
38538
c87b69396a37
liberal instantiation of class eq; tuned naming scheme
haftmann
parents:
38348
diff
changeset
|
146 |
|> not (null tycos_eq) ? add_equality vs tycos_eq |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
147 |
end; |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
148 |
|
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
149 |
|
31625 | 150 |
(** SML code generator **) |
151 |
||
152 |
open Codegen; |
|
153 |
||
154 |
(* datatype definition *) |
|
155 |
||
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
156 |
fun add_dt_defs thy defs dep module descr sorts gr = |
12445 | 157 |
let |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
158 |
val descr' = filter (can (map Datatype_Aux.dest_DtTFree o #2 o snd)) descr; |
33317 | 159 |
val rtnames = map (#1 o snd) (filter (fn (_, (_, _, cs)) => |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
160 |
exists (exists Datatype_Aux.is_rec_type o snd) cs) descr'); |
12445 | 161 |
|
17144 | 162 |
val (_, (tname, _, _)) :: _ = descr'; |
163 |
val node_id = tname ^ " (type)"; |
|
27398
768da1da59d6
simplified retrieval of theory names of consts and types
haftmann
parents:
27096
diff
changeset
|
164 |
val module' = if_library (thyname_of_type thy tname) module; |
12445 | 165 |
|
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
166 |
fun mk_dtdef prfx [] gr = ([], gr) |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
167 |
| mk_dtdef prfx ((_, (tname, dts, cs))::xs) gr = |
12445 | 168 |
let |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
169 |
val tvs = map Datatype_Aux.dest_DtTFree dts; |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
170 |
val cs' = map (apsnd (map (Datatype_Aux.typ_of_dtyp descr sorts))) cs; |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
171 |
val ((_, type_id), gr') = mk_type_id module' tname gr; |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
172 |
val (ps, gr'') = gr' |> |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
173 |
fold_map (fn (cname, cargs) => |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
174 |
fold_map (invoke_tycodegen thy defs node_id module' false) |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
175 |
cargs ##>> |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
176 |
mk_const_id module' cname) cs'; |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
177 |
val (rest, gr''') = mk_dtdef "and " xs gr'' |
12445 | 178 |
in |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
179 |
(Pretty.block (str prfx :: |
12445 | 180 |
(if null tvs then [] else |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
181 |
[mk_tuple (map str tvs), str " "]) @ |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
182 |
[str (type_id ^ " ="), Pretty.brk 1] @ |
32952 | 183 |
flat (separate [Pretty.brk 1, str "| "] |
17144 | 184 |
(map (fn (ps', (_, cname)) => [Pretty.block |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
185 |
(str cname :: |
12445 | 186 |
(if null ps' then [] else |
32952 | 187 |
flat ([str " of", Pretty.brk 1] :: |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
188 |
separate [str " *", Pretty.brk 1] |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
189 |
(map single ps'))))]) ps))) :: rest, gr''') |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
190 |
end; |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
191 |
|
25889
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
192 |
fun mk_constr_term cname Ts T ps = |
32952 | 193 |
flat (separate [str " $", Pretty.brk 1] |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
194 |
([str ("Const (\"" ^ cname ^ "\","), Pretty.brk 1, |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
195 |
mk_type false (Ts ---> T), str ")"] :: ps)); |
25889
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
196 |
|
17144 | 197 |
fun mk_term_of_def gr prfx [] = [] |
198 |
| mk_term_of_def gr prfx ((_, (tname, dts, cs)) :: xs) = |
|
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
199 |
let |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
200 |
val cs' = map (apsnd (map (Datatype_Aux.typ_of_dtyp descr sorts))) cs; |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
201 |
val dts' = map (Datatype_Aux.typ_of_dtyp descr sorts) dts; |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
202 |
val T = Type (tname, dts'); |
17144 | 203 |
val rest = mk_term_of_def gr "and " xs; |
31458 | 204 |
val (eqs, _) = fold_map (fn (cname, Ts) => fn prfx => |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
205 |
let val args = map (fn i => |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
206 |
str ("x" ^ string_of_int i)) (1 upto length Ts) |
31458 | 207 |
in (Pretty.blk (4, |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
208 |
[str prfx, mk_term_of gr module' false T, Pretty.brk 1, |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
209 |
if null Ts then str (snd (get_const_id gr cname)) |
16645 | 210 |
else parens (Pretty.block |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
211 |
[str (snd (get_const_id gr cname)), |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
212 |
Pretty.brk 1, mk_tuple args]), |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
213 |
str " =", Pretty.brk 1] @ |
25889
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
214 |
mk_constr_term cname Ts T |
31458 | 215 |
(map2 (fn x => fn U => [Pretty.block [mk_term_of gr module' false U, |
216 |
Pretty.brk 1, x]]) args Ts)), " | ") |
|
217 |
end) cs' prfx |
|
14104 | 218 |
in eqs @ rest end; |
219 |
||
17144 | 220 |
fun mk_gen_of_def gr prfx [] = [] |
221 |
| mk_gen_of_def gr prfx ((i, (tname, dts, cs)) :: xs) = |
|
14104 | 222 |
let |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
223 |
val tvs = map Datatype_Aux.dest_DtTFree dts; |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
224 |
val Us = map (Datatype_Aux.typ_of_dtyp descr sorts) dts; |
25889
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
225 |
val T = Type (tname, Us); |
14104 | 226 |
val (cs1, cs2) = |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
227 |
List.partition (exists Datatype_Aux.is_rec_type o snd) cs; |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
228 |
val SOME (cname, _) = Datatype_Aux.find_shortest_path descr i; |
14104 | 229 |
|
230 |
fun mk_delay p = Pretty.block |
|
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
231 |
[str "fn () =>", Pretty.brk 1, p]; |
14104 | 232 |
|
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
233 |
fun mk_force p = Pretty.block [p, Pretty.brk 1, str "()"]; |
25889
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
234 |
|
15397
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
235 |
fun mk_constr s b (cname, dts) = |
14104 | 236 |
let |
17144 | 237 |
val gs = map (fn dt => mk_app false (mk_gen gr module' false rtnames s |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
238 |
(Datatype_Aux.typ_of_dtyp descr sorts dt)) |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
239 |
[str (if b andalso Datatype_Aux.is_rec_type dt then "0" |
15397
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
240 |
else "j")]) dts; |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
241 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr sorts) dts; |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
242 |
val xs = map str |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
243 |
(Datatype_Prop.indexify_names (replicate (length dts) "x")); |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
244 |
val ts = map str |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33964
diff
changeset
|
245 |
(Datatype_Prop.indexify_names (replicate (length dts) "t")); |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
246 |
val (_, id) = get_const_id gr cname |
25889
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
247 |
in |
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
248 |
mk_let |
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
249 |
(map2 (fn p => fn q => mk_tuple [p, q]) xs ts ~~ gs) |
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
250 |
(mk_tuple |
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
251 |
[case xs of |
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
252 |
_ :: _ :: _ => Pretty.block |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
253 |
[str id, Pretty.brk 1, mk_tuple xs] |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
254 |
| _ => mk_app false (str id) xs, |
25889
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
255 |
mk_delay (Pretty.block (mk_constr_term cname Ts T |
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
256 |
(map (single o mk_force) ts)))]) |
14104 | 257 |
end; |
258 |
||
15397
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
259 |
fun mk_choice [c] = mk_constr "(i-1)" false c |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
260 |
| mk_choice cs = Pretty.block [str "one_of", |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
261 |
Pretty.brk 1, Pretty.blk (1, str "[" :: |
32952 | 262 |
flat (separate [str ",", Pretty.fbrk] |
15397
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
263 |
(map (single o mk_delay o mk_constr "(i-1)" false) cs)) @ |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
264 |
[str "]"]), Pretty.brk 1, str "()"]; |
14104 | 265 |
|
25889
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
266 |
val gs = maps (fn s => |
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
267 |
let val s' = strip_tname s |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
268 |
in [str (s' ^ "G"), str (s' ^ "T")] end) tvs; |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
269 |
val gen_name = "gen_" ^ snd (get_type_id gr tname) |
14104 | 270 |
|
271 |
in |
|
272 |
Pretty.blk (4, separate (Pretty.brk 1) |
|
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
273 |
(str (prfx ^ gen_name ^ |
14104 | 274 |
(if null cs1 then "" else "'")) :: gs @ |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
275 |
(if null cs1 then [] else [str "i"]) @ |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
276 |
[str "j"]) @ |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
277 |
[str " =", Pretty.brk 1] @ |
14104 | 278 |
(if not (null cs1) andalso not (null cs2) |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
279 |
then [str "frequency", Pretty.brk 1, |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
280 |
Pretty.blk (1, [str "[", |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
281 |
mk_tuple [str "i", mk_delay (mk_choice cs1)], |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
282 |
str ",", Pretty.fbrk, |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
283 |
mk_tuple [str "1", mk_delay (mk_choice cs2)], |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
284 |
str "]"]), Pretty.brk 1, str "()"] |
14104 | 285 |
else if null cs2 then |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
286 |
[Pretty.block [str "(case", Pretty.brk 1, |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
287 |
str "i", Pretty.brk 1, str "of", |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
288 |
Pretty.brk 1, str "0 =>", Pretty.brk 1, |
33035 | 289 |
mk_constr "0" true (cname, the (AList.lookup (op =) cs cname)), |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
290 |
Pretty.brk 1, str "| _ =>", Pretty.brk 1, |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
291 |
mk_choice cs1, str ")"]] |
14104 | 292 |
else [mk_choice cs2])) :: |
293 |
(if null cs1 then [] |
|
294 |
else [Pretty.blk (4, separate (Pretty.brk 1) |
|
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
295 |
(str ("and " ^ gen_name) :: gs @ [str "i"]) @ |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
296 |
[str " =", Pretty.brk 1] @ |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
297 |
separate (Pretty.brk 1) (str (gen_name ^ "'") :: gs @ |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
298 |
[str "i", str "i"]))]) @ |
17144 | 299 |
mk_gen_of_def gr "and " xs |
14104 | 300 |
end |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
301 |
|
12445 | 302 |
in |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
303 |
(module', (add_edge_acyclic (node_id, dep) gr |
12445 | 304 |
handle Graph.CYCLES _ => gr) handle Graph.UNDEF _ => |
305 |
let |
|
17144 | 306 |
val gr1 = add_edge (node_id, dep) |
307 |
(new_node (node_id, (NONE, "", "")) gr); |
|
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
308 |
val (dtdef, gr2) = mk_dtdef "datatype " descr' gr1 ; |
12445 | 309 |
in |
17144 | 310 |
map_node node_id (K (NONE, module', |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
311 |
string_of (Pretty.blk (0, separate Pretty.fbrk dtdef @ |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
312 |
[str ";"])) ^ "\n\n" ^ |
36692
54b64d4ad524
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
haftmann
parents:
36610
diff
changeset
|
313 |
(if member (op =) (!mode) "term_of" then |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
314 |
string_of (Pretty.blk (0, separate Pretty.fbrk |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
315 |
(mk_term_of_def gr2 "fun " descr') @ [str ";"])) ^ "\n\n" |
14104 | 316 |
else "") ^ |
36692
54b64d4ad524
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
haftmann
parents:
36610
diff
changeset
|
317 |
(if member (op =) (!mode) "test" then |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
318 |
string_of (Pretty.blk (0, separate Pretty.fbrk |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
319 |
(mk_gen_of_def gr2 "fun " descr') @ [str ";"])) ^ "\n\n" |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
320 |
else ""))) gr2 |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
321 |
end) |
12445 | 322 |
end; |
323 |
||
324 |
||
31625 | 325 |
(* case expressions *) |
12445 | 326 |
|
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
327 |
fun pretty_case thy defs dep module brack constrs (c as Const (_, T)) ts gr = |
12445 | 328 |
let val i = length constrs |
329 |
in if length ts <= i then |
|
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
330 |
invoke_codegen thy defs dep module brack (eta_expand c ts (i+1)) gr |
12445 | 331 |
else |
332 |
let |
|
33957 | 333 |
val ts1 = take i ts; |
334 |
val t :: ts2 = drop i ts; |
|
30190 | 335 |
val names = List.foldr OldTerm.add_term_names |
336 |
(map (fst o fst o dest_Var) (List.foldr OldTerm.add_term_vars [] ts1)) ts1; |
|
33957 | 337 |
val (Ts, dT) = split_last (take (i+1) (fst (strip_type T))); |
12445 | 338 |
|
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
339 |
fun pcase [] [] [] gr = ([], gr) |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
340 |
| pcase ((cname, cargs)::cs) (t::ts) (U::Us) gr = |
12445 | 341 |
let |
342 |
val j = length cargs; |
|
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19886
diff
changeset
|
343 |
val xs = Name.variant_list names (replicate j "x"); |
33957 | 344 |
val Us' = take j (fst (strip_type U)); |
345 |
val frees = map2 (curry Free) xs Us'; |
|
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
346 |
val (cp, gr0) = invoke_codegen thy defs dep module false |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
347 |
(list_comb (Const (cname, Us' ---> dT), frees)) gr; |
12445 | 348 |
val t' = Envir.beta_norm (list_comb (t, frees)); |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
349 |
val (p, gr1) = invoke_codegen thy defs dep module false t' gr0; |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
350 |
val (ps, gr2) = pcase cs ts Us gr1; |
12445 | 351 |
in |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
352 |
([Pretty.block [cp, str " =>", Pretty.brk 1, p]] :: ps, gr2) |
12445 | 353 |
end; |
354 |
||
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
355 |
val (ps1, gr1) = pcase constrs ts1 Ts gr ; |
32952 | 356 |
val ps = flat (separate [Pretty.brk 1, str "| "] ps1); |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
357 |
val (p, gr2) = invoke_codegen thy defs dep module false t gr1; |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
358 |
val (ps2, gr3) = fold_map (invoke_codegen thy defs dep module true) ts2 gr2; |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
359 |
in ((if not (null ts2) andalso brack then parens else I) |
12445 | 360 |
(Pretty.block (separate (Pretty.brk 1) |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
361 |
(Pretty.block ([str "(case ", p, str " of", |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
362 |
Pretty.brk 1] @ ps @ [str ")"]) :: ps2))), gr3) |
12445 | 363 |
end |
364 |
end; |
|
365 |
||
366 |
||
31625 | 367 |
(* constructors *) |
12445 | 368 |
|
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
369 |
fun pretty_constr thy defs dep module brack args (c as Const (s, T)) ts gr = |
12445 | 370 |
let val i = length args |
371 |
in if i > 1 andalso length ts < i then |
|
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
372 |
invoke_codegen thy defs dep module brack (eta_expand c ts i) gr |
12445 | 373 |
else |
374 |
let |
|
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
375 |
val id = mk_qual_id module (get_const_id gr s); |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
376 |
val (ps, gr') = fold_map |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
377 |
(invoke_codegen thy defs dep module (i = 1)) ts gr; |
12445 | 378 |
in (case args of |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
379 |
_ :: _ :: _ => (if brack then parens else I) |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
380 |
(Pretty.block [str id, Pretty.brk 1, mk_tuple ps]) |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
381 |
| _ => (mk_app brack (str id) ps), gr') |
12445 | 382 |
end |
383 |
end; |
|
384 |
||
385 |
||
31625 | 386 |
(* code generators for terms and types *) |
12445 | 387 |
|
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
388 |
fun datatype_codegen thy defs dep module brack t gr = (case strip_comb t of |
12445 | 389 |
(c as Const (s, T), ts) => |
33964 | 390 |
(case Datatype_Data.info_of_case thy s of |
22778
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
391 |
SOME {index, descr, ...} => |
22921
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
22778
diff
changeset
|
392 |
if is_some (get_assoc_code thy (s, T)) then NONE else |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
393 |
SOME (pretty_case thy defs dep module brack |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
394 |
(#3 (the (AList.lookup op = descr index))) c ts gr ) |
33964 | 395 |
| NONE => case (Datatype_Data.info_of_constr thy (s, T), strip_type T) of |
28639
9788fb18a142
datatype_codegen now checks name of result type of constructor
berghofe
parents:
28537
diff
changeset
|
396 |
(SOME {index, descr, ...}, (_, U as Type (tyname, _))) => |
22921
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
22778
diff
changeset
|
397 |
if is_some (get_assoc_code thy (s, T)) then NONE else |
28639
9788fb18a142
datatype_codegen now checks name of result type of constructor
berghofe
parents:
28537
diff
changeset
|
398 |
let |
9788fb18a142
datatype_codegen now checks name of result type of constructor
berghofe
parents:
28537
diff
changeset
|
399 |
val SOME (tyname', _, constrs) = AList.lookup op = descr index; |
9788fb18a142
datatype_codegen now checks name of result type of constructor
berghofe
parents:
28537
diff
changeset
|
400 |
val SOME args = AList.lookup op = constrs s |
22778
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
401 |
in |
28639
9788fb18a142
datatype_codegen now checks name of result type of constructor
berghofe
parents:
28537
diff
changeset
|
402 |
if tyname <> tyname' then NONE |
9788fb18a142
datatype_codegen now checks name of result type of constructor
berghofe
parents:
28537
diff
changeset
|
403 |
else SOME (pretty_constr thy defs |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
404 |
dep module brack args c ts (snd (invoke_tycodegen thy defs dep module false U gr))) |
22778
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
405 |
end |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
406 |
| _ => NONE) |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
407 |
| _ => NONE); |
12445 | 408 |
|
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
409 |
fun datatype_tycodegen thy defs dep module brack (Type (s, Ts)) gr = |
33964 | 410 |
(case Datatype_Data.get_info thy s of |
15531 | 411 |
NONE => NONE |
25889
c93803252748
Test data generation and conversion to terms is now more closely
berghofe
parents:
25864
diff
changeset
|
412 |
| SOME {descr, sorts, ...} => |
22921
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
22778
diff
changeset
|
413 |
if is_some (get_assoc_type thy s) then NONE else |
16645 | 414 |
let |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
415 |
val (ps, gr') = fold_map |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
416 |
(invoke_tycodegen thy defs dep module false) Ts gr; |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
417 |
val (module', gr'') = add_dt_defs thy defs dep module descr sorts gr' ; |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
418 |
val (tyid, gr''') = mk_type_id module' s gr'' |
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
419 |
in SOME (Pretty.block ((if null Ts then [] else |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26732
diff
changeset
|
420 |
[mk_tuple ps, str " "]) @ |
28537
1e84256d1a8a
established canonical argument order in SML code generators
haftmann
parents:
28423
diff
changeset
|
421 |
[str (mk_qual_id module tyid)]), gr''') |
12445 | 422 |
end) |
16645 | 423 |
| datatype_tycodegen _ _ _ _ _ _ _ = NONE; |
12445 | 424 |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18220
diff
changeset
|
425 |
|
20426 | 426 |
(** theory setup **) |
427 |
||
19008 | 428 |
val setup = |
36298
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
429 |
Datatype_Data.interpretation add_all_code |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
430 |
#> add_codegen "datatype" datatype_codegen |
2d55c4aba1dc
swapped generic code generator and SML code generator
haftmann
parents:
35899
diff
changeset
|
431 |
#> add_tycodegen "datatype" datatype_tycodegen; |
20597 | 432 |
|
12445 | 433 |
end; |