| author | paulson |
| Fri, 08 Jan 1999 13:20:59 +0100 | |
| changeset 6071 | 1b2392ac5752 |
| parent 6053 | 8a1059aa01f0 |
| child 6093 | 87bf8c03b169 |
| permissions | -rw-r--r-- |
| 1461 | 1 |
(* Title: ZF/ind-syntax.ML |
| 0 | 2 |
ID: $Id$ |
| 1461 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
| 0 | 4 |
Copyright 1993 University of Cambridge |
5 |
||
6 |
Abstract Syntax functions for Inductive Definitions |
|
7 |
*) |
|
8 |
||
| 516 | 9 |
(*The structure protects these items from redeclaration (somewhat!). The |
10 |
datatype definitions in theory files refer to these items by name! |
|
11 |
*) |
|
12 |
structure Ind_Syntax = |
|
13 |
struct |
|
| 0 | 14 |
|
|
4804
02b7c759159b
Fixed bug in inductive sections to allow disjunctive premises;
paulson
parents:
4352
diff
changeset
|
15 |
(*Print tracing messages during processing of "inductive" theory sections*) |
|
02b7c759159b
Fixed bug in inductive sections to allow disjunctive premises;
paulson
parents:
4352
diff
changeset
|
16 |
val trace = ref false; |
|
02b7c759159b
Fixed bug in inductive sections to allow disjunctive premises;
paulson
parents:
4352
diff
changeset
|
17 |
|
|
6053
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
18 |
fun traceIt msg ct = |
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
19 |
if !trace then (writeln (msg ^ string_of_cterm ct); ct) |
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
20 |
else ct; |
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
21 |
|
|
4352
7ac9f3e8a97d
Moved some functions from ZF/ind_syntax.ML to FOL/fologic.ML
paulson
parents:
3925
diff
changeset
|
22 |
(** Abstract syntax definitions for ZF **) |
| 0 | 23 |
|
|
4352
7ac9f3e8a97d
Moved some functions from ZF/ind_syntax.ML to FOL/fologic.ML
paulson
parents:
3925
diff
changeset
|
24 |
val iT = Type("i",[]);
|
| 0 | 25 |
|
|
4352
7ac9f3e8a97d
Moved some functions from ZF/ind_syntax.ML to FOL/fologic.ML
paulson
parents:
3925
diff
changeset
|
26 |
val mem_const = Const("op :", [iT,iT]--->FOLogic.oT);
|
| 0 | 27 |
|
28 |
(*Creates All(%v.v:A --> P(v)) rather than Ball(A,P) *) |
|
29 |
fun mk_all_imp (A,P) = |
|
|
4352
7ac9f3e8a97d
Moved some functions from ZF/ind_syntax.ML to FOL/fologic.ML
paulson
parents:
3925
diff
changeset
|
30 |
FOLogic.all_const iT $ |
|
4972
7fe1d30c1374
mk_all_imp: no longer creates goals that have beta-redexes
paulson
parents:
4804
diff
changeset
|
31 |
Abs("v", iT, FOLogic.imp $ (mem_const $ Bound 0 $ A) $
|
|
7fe1d30c1374
mk_all_imp: no longer creates goals that have beta-redexes
paulson
parents:
4804
diff
changeset
|
32 |
betapply(P, Bound 0)); |
| 0 | 33 |
|
34 |
val Part_const = Const("Part", [iT,iT-->iT]--->iT);
|
|
35 |
||
|
6053
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
36 |
val apply_const = Const("op `", [iT,iT]--->iT);
|
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
37 |
|
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
38 |
val Vrecursor_const = Const("Vrecursor", [[iT,iT]--->iT, iT]--->iT);
|
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
39 |
|
|
4352
7ac9f3e8a97d
Moved some functions from ZF/ind_syntax.ML to FOL/fologic.ML
paulson
parents:
3925
diff
changeset
|
40 |
val Collect_const = Const("Collect", [iT, iT-->FOLogic.oT] ---> iT);
|
| 0 | 41 |
fun mk_Collect (a,D,t) = Collect_const $ D $ absfree(a, iT, t); |
42 |
||
| 516 | 43 |
(*simple error-checking in the premises of an inductive definition*) |
44 |
fun chk_prem rec_hd (Const("op &",_) $ _ $ _) =
|
|
| 1461 | 45 |
error"Premises may not be conjuctive" |
| 516 | 46 |
| chk_prem rec_hd (Const("op :",_) $ t $ X) =
|
| 1461 | 47 |
deny (Logic.occs(rec_hd,t)) "Recursion term on left of member symbol" |
| 516 | 48 |
| chk_prem rec_hd t = |
| 1461 | 49 |
deny (Logic.occs(rec_hd,t)) "Recursion term in side formula"; |
| 516 | 50 |
|
|
14
1c0926788772
ex/{bin.ML,comb.ML,prop.ML}: replaced NewSext by Syntax.simple_sext
lcp
parents:
6
diff
changeset
|
51 |
(*Return the conclusion of a rule, of the form t:X*) |
| 0 | 52 |
fun rule_concl rl = |
| 435 | 53 |
let val Const("Trueprop",_) $ (Const("op :",_) $ t $ X) =
|
| 1461 | 54 |
Logic.strip_imp_concl rl |
| 435 | 55 |
in (t,X) end; |
56 |
||
57 |
(*As above, but return error message if bad*) |
|
58 |
fun rule_concl_msg sign rl = rule_concl rl |
|
59 |
handle Bind => error ("Ill-formed conclusion of introduction rule: " ^
|
|
| 1461 | 60 |
Sign.string_of_term sign rl); |
| 0 | 61 |
|
62 |
(*For deriving cases rules. CollectD2 discards the domain, which is redundant; |
|
63 |
read_instantiate replaces a propositional variable by a formula variable*) |
|
64 |
val equals_CollectD = |
|
65 |
read_instantiate [("W","?Q")]
|
|
66 |
(make_elim (equalityD1 RS subsetD RS CollectD2)); |
|
67 |
||
68 |
||
| 516 | 69 |
(** For datatype definitions **) |
70 |
||
|
6053
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
71 |
(*Constructor name, type, mixfix info; |
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
72 |
internal name from mixfix, datatype sets, full premises*) |
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
73 |
type constructor_spec = |
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
74 |
((string * typ * mixfix) * string * term list * term list); |
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
75 |
|
| 516 | 76 |
fun dest_mem (Const("op :",_) $ x $ A) = (x,A)
|
77 |
| dest_mem _ = error "Constructor specifications must have the form x:A"; |
|
78 |
||
79 |
(*read a constructor specification*) |
|
80 |
fun read_construct sign (id, sprems, syn) = |
|
|
4352
7ac9f3e8a97d
Moved some functions from ZF/ind_syntax.ML to FOL/fologic.ML
paulson
parents:
3925
diff
changeset
|
81 |
let val prems = map (readtm sign FOLogic.oT) sprems |
| 1461 | 82 |
val args = map (#1 o dest_mem) prems |
83 |
val T = (map (#2 o dest_Free) args) ---> iT |
|
84 |
handle TERM _ => error |
|
85 |
"Bad variable in constructor specification" |
|
| 568 | 86 |
val name = Syntax.const_name id syn (*handle infix constructors*) |
| 516 | 87 |
in ((id,T,syn), name, args, prems) end; |
88 |
||
89 |
val read_constructs = map o map o read_construct; |
|
| 0 | 90 |
|
| 516 | 91 |
(*convert constructor specifications into introduction rules*) |
|
3925
90f499226ab9
(co) inductive / datatype package adapted to qualified names;
wenzelm
parents:
2266
diff
changeset
|
92 |
fun mk_intr_tms sg (rec_tm, constructs) = |
|
90f499226ab9
(co) inductive / datatype package adapted to qualified names;
wenzelm
parents:
2266
diff
changeset
|
93 |
let |
|
90f499226ab9
(co) inductive / datatype package adapted to qualified names;
wenzelm
parents:
2266
diff
changeset
|
94 |
fun mk_intr ((id,T,syn), name, args, prems) = |
|
90f499226ab9
(co) inductive / datatype package adapted to qualified names;
wenzelm
parents:
2266
diff
changeset
|
95 |
Logic.list_implies |
|
4352
7ac9f3e8a97d
Moved some functions from ZF/ind_syntax.ML to FOL/fologic.ML
paulson
parents:
3925
diff
changeset
|
96 |
(map FOLogic.mk_Trueprop prems, |
|
7ac9f3e8a97d
Moved some functions from ZF/ind_syntax.ML to FOL/fologic.ML
paulson
parents:
3925
diff
changeset
|
97 |
FOLogic.mk_Trueprop |
|
7ac9f3e8a97d
Moved some functions from ZF/ind_syntax.ML to FOL/fologic.ML
paulson
parents:
3925
diff
changeset
|
98 |
(mem_const $ list_comb (Const (Sign.full_name sg name, T), args) |
|
7ac9f3e8a97d
Moved some functions from ZF/ind_syntax.ML to FOL/fologic.ML
paulson
parents:
3925
diff
changeset
|
99 |
$ rec_tm)) |
| 516 | 100 |
in map mk_intr constructs end; |
101 |
||
|
3925
90f499226ab9
(co) inductive / datatype package adapted to qualified names;
wenzelm
parents:
2266
diff
changeset
|
102 |
fun mk_all_intr_tms sg arg = List.concat (ListPair.map (mk_intr_tms sg) arg); |
| 0 | 103 |
|
| 1461 | 104 |
val Un = Const("op Un", [iT,iT]--->iT)
|
105 |
and empty = Const("0", iT)
|
|
106 |
and univ = Const("univ", iT-->iT)
|
|
107 |
and quniv = Const("quniv", iT-->iT);
|
|
| 0 | 108 |
|
| 516 | 109 |
(*Make a datatype's domain: form the union of its set parameters*) |
110 |
fun union_params rec_tm = |
|
111 |
let val (_,args) = strip_comb rec_tm |
|
112 |
in case (filter (fn arg => type_of arg = iT) args) of |
|
113 |
[] => empty |
|
114 |
| iargs => fold_bal (app Un) iargs |
|
115 |
end; |
|
116 |
||
|
742
faa3efc1d130
data_domain,Codata_domain: removed replicate; now return one
lcp
parents:
578
diff
changeset
|
117 |
(*Previously these both did replicate (length rec_tms); however now |
|
faa3efc1d130
data_domain,Codata_domain: removed replicate; now return one
lcp
parents:
578
diff
changeset
|
118 |
[q]univ itself constitutes the sum domain for mutual recursion!*) |
|
6053
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
119 |
fun data_domain false rec_tms = univ $ union_params (hd rec_tms) |
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
4972
diff
changeset
|
120 |
| data_domain true rec_tms = quniv $ union_params (hd rec_tms); |
| 0 | 121 |
|
122 |
(*Could go to FOL, but it's hardly general*) |
|
| 516 | 123 |
val def_swap_iff = prove_goal IFOL.thy "a==b ==> a=c <-> c=b" |
124 |
(fn [def] => [(rewtac def), (rtac iffI 1), (REPEAT (etac sym 1))]); |
|
| 0 | 125 |
|
126 |
val def_trans = prove_goal IFOL.thy "[| f==g; g(a)=b |] ==> f(a)=b" |
|
127 |
(fn [rew,prem] => [ rewtac rew, rtac prem 1 ]); |
|
128 |
||
| 55 | 129 |
(*Delete needless equality assumptions*) |
130 |
val refl_thin = prove_goal IFOL.thy "!!P. [| a=a; P |] ==> P" |
|
131 |
(fn _ => [assume_tac 1]); |
|
| 0 | 132 |
|
|
1418
f5f97ee67cbb
Improving space efficiency of inductive/datatype definitions.
paulson
parents:
742
diff
changeset
|
133 |
(*Includes rules for succ and Pair since they are common constructions*) |
|
f5f97ee67cbb
Improving space efficiency of inductive/datatype definitions.
paulson
parents:
742
diff
changeset
|
134 |
val elim_rls = [asm_rl, FalseE, succ_neq_0, sym RS succ_neq_0, |
| 1461 | 135 |
Pair_neq_0, sym RS Pair_neq_0, Pair_inject, |
136 |
make_elim succ_inject, |
|
137 |
refl_thin, conjE, exE, disjE]; |
|
|
1418
f5f97ee67cbb
Improving space efficiency of inductive/datatype definitions.
paulson
parents:
742
diff
changeset
|
138 |
|
|
f5f97ee67cbb
Improving space efficiency of inductive/datatype definitions.
paulson
parents:
742
diff
changeset
|
139 |
(*Turns iff rules into safe elimination rules*) |
|
f5f97ee67cbb
Improving space efficiency of inductive/datatype definitions.
paulson
parents:
742
diff
changeset
|
140 |
fun mk_free_SEs iffs = map (gen_make_elim [conjE,FalseE]) (iffs RL [iffD1]); |
|
f5f97ee67cbb
Improving space efficiency of inductive/datatype definitions.
paulson
parents:
742
diff
changeset
|
141 |
|
| 516 | 142 |
end; |
143 |