author | clasohm |
Mon, 11 Jul 1994 13:15:05 +0200 | |
changeset 454 | 0d19ab250cc9 |
parent 448 | d7ff85d292c7 |
child 466 | 08d1cce222e1 |
permissions | -rw-r--r-- |
0 | 1 |
(* Title: ZF/constructor.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1993 University of Cambridge |
|
5 |
||
6 |
Constructor function module -- for Datatype Definitions |
|
7 |
||
8 |
Defines constructors and a case-style eliminator (no primitive recursion) |
|
9 |
||
10 |
Features: |
|
11 |
* least or greatest fixedpoints |
|
12 |
* user-specified product and sum constructions |
|
13 |
* mutually recursive datatypes |
|
14 |
* recursion over arbitrary monotone operators |
|
15 |
* flexible: can derive any reasonable set of introduction rules |
|
16 |
* automatically constructs a case analysis operator (but no recursion op) |
|
17 |
* efficient treatment of large declarations (e.g. 60 constructors) |
|
18 |
*) |
|
19 |
||
20 |
(** STILL NEEDS: some treatment of recursion **) |
|
21 |
||
22 |
signature CONSTRUCTOR = |
|
23 |
sig |
|
24 |
val thy : theory (*parent theory*) |
|
444 | 25 |
val rec_specs : (string * string * (string list * string * mixfix)list) list |
0 | 26 |
(*recursion ops, types, domains, constructors*) |
27 |
val rec_styp : string (*common type of all recursion ops*) |
|
28 |
val sintrs : string list (*desired introduction rules*) |
|
29 |
val monos : thm list (*monotonicity of each M operator*) |
|
30 |
val type_intrs : thm list (*type-checking intro rules*) |
|
31 |
val type_elims : thm list (*type-checking elim rules*) |
|
32 |
end; |
|
33 |
||
34 |
signature CONSTRUCTOR_RESULT = |
|
35 |
sig |
|
36 |
val con_thy : theory (*theory defining the constructors*) |
|
37 |
val con_defs : thm list (*definitions made in con_thy*) |
|
38 |
val case_eqns : thm list (*equations for case operator*) |
|
39 |
val free_iffs : thm list (*freeness rewrite rules*) |
|
40 |
val free_SEs : thm list (*freeness destruct rules*) |
|
41 |
val mk_free : string -> thm (*makes freeness theorems*) |
|
42 |
end; |
|
43 |
||
44 |
||
45 |
functor Constructor_Fun (structure Const: CONSTRUCTOR and |
|
46 |
Pr : PR and Su : SU) : CONSTRUCTOR_RESULT = |
|
47 |
struct |
|
48 |
open Logic Const; |
|
49 |
||
50 |
val dummy = writeln"Defining the constructor functions..."; |
|
51 |
||
52 |
val case_name = "f"; (*name for case variables*) |
|
53 |
||
54 |
(** Extract basic information from arguments **) |
|
55 |
||
56 |
val sign = sign_of thy; |
|
231 | 57 |
val rdty = typ_of o read_ctyp sign; |
0 | 58 |
|
59 |
val rec_names = map #1 rec_specs; |
|
60 |
||
61 |
val dummy = assert_all Syntax.is_identifier rec_names |
|
62 |
(fn a => "Name of recursive set not an identifier: " ^ a); |
|
63 |
||
64 |
(*Expands multiple constant declarations*) |
|
444 | 65 |
fun flatten_consts ((names, typ, mfix) :: cs) = |
66 |
let fun mk_const name = (name, typ, mfix) |
|
67 |
in (map mk_const names) @ (flatten_consts cs) end |
|
68 |
| flatten_consts [] = []; |
|
0 | 69 |
|
70 |
(*Constructors with types and arguments*) |
|
71 |
fun mk_con_ty_list cons_pairs = |
|
448 | 72 |
let fun mk_con_ty (id, st, syn) = |
444 | 73 |
let val T = rdty st; |
74 |
val args = mk_frees "xa" (binder_types T); |
|
448 | 75 |
val name = const_name id syn; |
76 |
(* because of mixfix annotations the internal name |
|
77 |
can be different from 'id' *) |
|
444 | 78 |
in (name, T, args) end; |
79 |
||
80 |
fun pairtypes c = flatten_consts [c]; |
|
81 |
in map mk_con_ty (flat (map pairtypes cons_pairs)) end; |
|
0 | 82 |
|
83 |
val con_ty_lists = map (mk_con_ty_list o #3) rec_specs; |
|
84 |
||
85 |
(** Define the constructors **) |
|
86 |
||
87 |
(*We identify 0 (the empty set) with the empty tuple*) |
|
88 |
fun mk_tuple [] = Const("0",iT) |
|
89 |
| mk_tuple args = foldr1 (app Pr.pair) args; |
|
90 |
||
91 |
fun mk_inject n k u = access_bal(ap Su.inl, ap Su.inr, u) n k; |
|
92 |
||
93 |
val npart = length rec_names; (*number of mutually recursive parts*) |
|
94 |
||
95 |
(*Make constructor definition*) |
|
96 |
fun mk_con_defs (kpart, con_ty_list) = |
|
444 | 97 |
let val ncon = length con_ty_list (*number of constructors*) |
98 |
fun mk_def ((a,T,args), kcon) = (*kcon = index of this constructor*) |
|
0 | 99 |
mk_defpair sign |
100 |
(list_comb (Const(a,T), args), |
|
101 |
mk_inject npart kpart (mk_inject ncon kcon (mk_tuple args))) |
|
102 |
in map mk_def (con_ty_list ~~ (1 upto ncon)) end; |
|
103 |
||
104 |
(** Define the case operator **) |
|
105 |
||
106 |
(*Combine split terms using case; yields the case operator for one part*) |
|
107 |
fun call_case case_list = |
|
108 |
let fun call_f (free,args) = |
|
109 |
ap_split Pr.split_const free (map (#2 o dest_Free) args) |
|
110 |
in fold_bal (app Su.elim) (map call_f case_list) end; |
|
111 |
||
112 |
(** Generating function variables for the case definition |
|
113 |
Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **) |
|
114 |
||
115 |
(*Treatment of a single constructor*) |
|
116 |
fun add_case ((a,T,args), (opno,cases)) = |
|
444 | 117 |
if Syntax.is_identifier a |
0 | 118 |
then (opno, |
119 |
(Free(case_name ^ "_" ^ a, T), args) :: cases) |
|
120 |
else (opno+1, |
|
121 |
(Free(case_name ^ "_op_" ^ string_of_int opno, T), args) :: cases); |
|
122 |
||
123 |
(*Treatment of a list of constructors, for one part*) |
|
124 |
fun add_case_list (con_ty_list, (opno,case_lists)) = |
|
125 |
let val (opno',case_list) = foldr add_case (con_ty_list, (opno,[])) |
|
126 |
in (opno', case_list :: case_lists) end; |
|
127 |
||
128 |
(*Treatment of all parts*) |
|
129 |
val (_, case_lists) = foldr add_case_list (con_ty_lists, (1,[])); |
|
130 |
||
131 |
val big_case_typ = flat (map (map #2) con_ty_lists) ---> (iT-->iT); |
|
132 |
||
133 |
val big_rec_name = space_implode "_" rec_names; |
|
134 |
||
135 |
val big_case_name = big_rec_name ^ "_case"; |
|
136 |
||
137 |
(*The list of all the function variables*) |
|
138 |
val big_case_args = flat (map (map #1) case_lists); |
|
139 |
||
140 |
val big_case_tm = |
|
141 |
list_comb (Const(big_case_name, big_case_typ), big_case_args); |
|
142 |
||
143 |
val big_case_def = |
|
144 |
mk_defpair sign |
|
145 |
(big_case_tm, fold_bal (app Su.elim) (map call_case case_lists)); |
|
146 |
||
147 |
(** Build the new theory **) |
|
148 |
||
149 |
val axpairs = |
|
150 |
big_case_def :: flat (map mk_con_defs ((1 upto npart) ~~ con_ty_lists)); |
|
151 |
||
444 | 152 |
val const_decs = flatten_consts |
153 |
(([big_case_name], flatten_typ sign big_case_typ, NoSyn) :: |
|
154 |
(big_rec_name ins rec_names, rec_styp, NoSyn) :: |
|
0 | 155 |
flat (map #3 rec_specs)); |
156 |
||
444 | 157 |
val con_thy = thy |
158 |
|> add_consts const_decs |
|
454
0d19ab250cc9
removed flatten_term and replaced add_axioms by add_axioms_i
clasohm
parents:
448
diff
changeset
|
159 |
|> add_axioms_i axpairs |
444 | 160 |
|> add_thyname (big_rec_name ^ "_Constructors"); |
0 | 161 |
|
162 |
(*1st element is the case definition; others are the constructors*) |
|
163 |
val con_defs = map (get_axiom con_thy o #1) axpairs; |
|
164 |
||
165 |
(** Prove the case theorem **) |
|
166 |
||
167 |
(*Each equation has the form |
|
168 |
rec_case(f_con1,...,f_conn)(coni(args)) = f_coni(args) *) |
|
169 |
fun mk_case_equation ((a,T,args), case_free) = |
|
170 |
mk_tprop |
|
171 |
(eq_const $ (big_case_tm $ (list_comb (Const(a,T), args))) |
|
172 |
$ (list_comb (case_free, args))); |
|
173 |
||
174 |
val case_trans = hd con_defs RS def_trans; |
|
175 |
||
176 |
(*proves a single case equation*) |
|
177 |
fun case_tacsf con_def _ = |
|
178 |
[rewtac con_def, |
|
179 |
rtac case_trans 1, |
|
180 |
REPEAT (resolve_tac [refl, Pr.split_eq RS trans, |
|
181 |
Su.case_inl RS trans, |
|
182 |
Su.case_inr RS trans] 1)]; |
|
183 |
||
184 |
fun prove_case_equation (arg,con_def) = |
|
185 |
prove_term (sign_of con_thy) [] |
|
186 |
(mk_case_equation arg, case_tacsf con_def); |
|
187 |
||
188 |
val free_iffs = |
|
189 |
map standard (con_defs RL [def_swap_iff]) @ |
|
190 |
[Su.distinct, Su.distinct', Su.inl_iff, Su.inr_iff, Pr.pair_iff]; |
|
191 |
||
192 |
val free_SEs = map (gen_make_elim [conjE,FalseE]) (free_iffs RL [iffD1]); |
|
193 |
||
194 |
val free_cs = ZF_cs addSEs free_SEs; |
|
195 |
||
196 |
(*Typical theorems have the form ~con1=con2, con1=con2==>False, |
|
197 |
con1(x)=con1(y) ==> x=y, con1(x)=con1(y) <-> x=y, etc. *) |
|
198 |
fun mk_free s = |
|
199 |
prove_goalw con_thy con_defs s |
|
200 |
(fn prems => [cut_facts_tac prems 1, fast_tac free_cs 1]); |
|
201 |
||
202 |
val case_eqns = map prove_case_equation |
|
203 |
(flat con_ty_lists ~~ big_case_args ~~ tl con_defs); |
|
204 |
||
205 |
end; |
|
206 |
||
207 |