author | wenzelm |
Sat, 28 Mar 2015 17:26:42 +0100 | |
changeset 59828 | 0e9baaf0e0bb |
parent 59050 | 376446e98951 |
child 61246 | 077b88f9ec16 |
permissions | -rw-r--r-- |
17707
bc0270e9d27f
back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents:
17670
diff
changeset
|
1 |
(* Title: Pure/defs.ML |
bc0270e9d27f
back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents:
17670
diff
changeset
|
2 |
Author: Makarius |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
3 |
|
19692 | 4 |
Global well-formedness checks for constant definitions. Covers plain |
19701 | 5 |
definitions and simple sub-structural overloading. |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
6 |
*) |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
7 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
8 |
signature DEFS = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
9 |
sig |
42384 | 10 |
val pretty_const: Proof.context -> string * typ list -> Pretty.T |
19701 | 11 |
val plain_args: typ list -> bool |
17707
bc0270e9d27f
back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents:
17670
diff
changeset
|
12 |
type T |
33712 | 13 |
type spec = |
55544 | 14 |
{def: string option, |
15 |
description: string, |
|
16 |
pos: Position.T, |
|
17 |
lhs: typ list, |
|
18 |
rhs: (string * typ list) list} |
|
33712 | 19 |
val all_specifications_of: T -> (string * spec list) list |
20 |
val specifications_of: T -> string -> spec list |
|
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
21 |
val dest: T -> |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
22 |
{restricts: ((string * typ list) * string) list, |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
23 |
reducts: ((string * typ list) * (string * typ list) list) list} |
19590
12af4942923d
simple substructure test for typargs (independent type constructors);
wenzelm
parents:
19569
diff
changeset
|
24 |
val empty: T |
42389 | 25 |
val merge: Proof.context -> T * T -> T |
42384 | 26 |
val define: Proof.context -> bool -> string option -> string -> |
19727 | 27 |
string * typ list -> (string * typ list) list -> T -> T |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
28 |
end |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
29 |
|
17711 | 30 |
structure Defs: DEFS = |
17707
bc0270e9d27f
back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents:
17670
diff
changeset
|
31 |
struct |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
32 |
|
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
33 |
(* type arguments *) |
19613
9bf274ec94cf
allow dependencies of disjoint collections of instances;
wenzelm
parents:
19590
diff
changeset
|
34 |
|
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
35 |
type args = typ list; |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
36 |
|
42384 | 37 |
fun pretty_const ctxt (c, args) = |
19613
9bf274ec94cf
allow dependencies of disjoint collections of instances;
wenzelm
parents:
19590
diff
changeset
|
38 |
let |
19692 | 39 |
val prt_args = |
40 |
if null args then [] |
|
42384 | 41 |
else [Pretty.list "(" ")" (map (Syntax.pretty_typ ctxt o Logic.unvarifyT_global) args)]; |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
42 |
in Pretty.block (Pretty.str c :: prt_args) end; |
19624 | 43 |
|
19707 | 44 |
fun plain_args args = |
45 |
forall Term.is_TVar args andalso not (has_duplicates (op =) args); |
|
46 |
||
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
47 |
fun disjoint_args (Ts, Us) = |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
48 |
not (Type.could_unifys (Ts, Us)) orelse |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
49 |
((Type.raw_unifys (Ts, map (Logic.incr_tvar (maxidx_of_typs Ts + 1)) Us) Vartab.empty; false) |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
50 |
handle Type.TUNIFY => true); |
19692 | 51 |
|
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
52 |
fun match_args (Ts, Us) = |
56050
fdccbb97915a
minor performance tuning via fast matching filter;
wenzelm
parents:
55544
diff
changeset
|
53 |
if Type.could_matches (Ts, Us) then |
fdccbb97915a
minor performance tuning via fast matching filter;
wenzelm
parents:
55544
diff
changeset
|
54 |
Option.map Envir.subst_type |
fdccbb97915a
minor performance tuning via fast matching filter;
wenzelm
parents:
55544
diff
changeset
|
55 |
(SOME (Type.raw_matches (Ts, Us) Vartab.empty) handle Type.TYPE_MATCH => NONE) |
fdccbb97915a
minor performance tuning via fast matching filter;
wenzelm
parents:
55544
diff
changeset
|
56 |
else NONE; |
19692 | 57 |
|
58 |
||
59 |
(* datatype defs *) |
|
60 |
||
33701
9dd1079cec3a
primitive defs: clarified def (axiom name) vs. description;
wenzelm
parents:
32785
diff
changeset
|
61 |
type spec = |
55544 | 62 |
{def: string option, |
63 |
description: string, |
|
64 |
pos: Position.T, |
|
65 |
lhs: args, |
|
66 |
rhs: (string * args) list}; |
|
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
67 |
|
19692 | 68 |
type def = |
55544 | 69 |
{specs: spec Inttab.table, (*source specifications*) |
70 |
restricts: (args * string) list, (*global restrictions imposed by incomplete patterns*) |
|
19712
3ae3cc4b1eac
wellformed: be less ambitious about structural containment;
wenzelm
parents:
19707
diff
changeset
|
71 |
reducts: (args * (string * args) list) list}; (*specifications as reduction system*) |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
72 |
|
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
73 |
fun make_def (specs, restricts, reducts) = |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
74 |
{specs = specs, restricts = restricts, reducts = reducts}: def; |
19692 | 75 |
|
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
76 |
fun map_def c f = |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
77 |
Symtab.default (c, make_def (Inttab.empty, [], [])) #> |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
78 |
Symtab.map_entry c (fn {specs, restricts, reducts}: def => |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
79 |
make_def (f (specs, restricts, reducts))); |
19692 | 80 |
|
81 |
||
82 |
datatype T = Defs of def Symtab.table; |
|
83 |
||
19712
3ae3cc4b1eac
wellformed: be less ambitious about structural containment;
wenzelm
parents:
19707
diff
changeset
|
84 |
fun lookup_list which defs c = |
19692 | 85 |
(case Symtab.lookup defs c of |
19713 | 86 |
SOME (def: def) => which def |
19692 | 87 |
| NONE => []); |
88 |
||
32050 | 89 |
fun all_specifications_of (Defs defs) = |
90 |
(map o apsnd) (map snd o Inttab.dest o #specs) (Symtab.dest defs); |
|
91 |
||
24199 | 92 |
fun specifications_of (Defs defs) = lookup_list (map snd o Inttab.dest o #specs) defs; |
32050 | 93 |
|
19692 | 94 |
val restricts_of = lookup_list #restricts; |
95 |
val reducts_of = lookup_list #reducts; |
|
96 |
||
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
97 |
fun dest (Defs defs) = |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
98 |
let |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
99 |
val restricts = Symtab.fold (fn (c, {restricts, ...}) => |
33701
9dd1079cec3a
primitive defs: clarified def (axiom name) vs. description;
wenzelm
parents:
32785
diff
changeset
|
100 |
fold (fn (args, description) => cons ((c, args), description)) restricts) defs []; |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
101 |
val reducts = Symtab.fold (fn (c, {reducts, ...}) => |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
102 |
fold (fn (args, deps) => cons ((c, args), deps)) reducts) defs []; |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
103 |
in {restricts = restricts, reducts = reducts} end; |
19692 | 104 |
|
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
105 |
val empty = Defs Symtab.empty; |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
106 |
|
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
107 |
|
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
108 |
(* specifications *) |
19692 | 109 |
|
55544 | 110 |
fun disjoint_specs c (i, {description = a, pos = pos_a, lhs = Ts, ...}: spec) = |
111 |
Inttab.forall (fn (j, {description = b, pos = pos_b, lhs = Us, ...}: spec) => |
|
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
112 |
i = j orelse disjoint_args (Ts, Us) orelse |
55544 | 113 |
error ("Clash of specifications for constant " ^ quote c ^ ":\n" ^ |
114 |
" " ^ quote a ^ Position.here pos_a ^ "\n" ^ |
|
115 |
" " ^ quote b ^ Position.here pos_b)); |
|
19692 | 116 |
|
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
117 |
fun join_specs c ({specs = specs1, restricts, reducts}, {specs = specs2, ...}: def) = |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
118 |
let |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
119 |
val specs' = |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
120 |
Inttab.fold (fn spec2 => (disjoint_specs c spec2 specs1; Inttab.update spec2)) specs2 specs1; |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
121 |
in make_def (specs', restricts, reducts) end; |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
122 |
|
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
123 |
fun update_specs c spec = map_def c (fn (specs, restricts, reducts) => |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
124 |
(disjoint_specs c spec specs; (Inttab.update spec specs, restricts, reducts))); |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
125 |
|
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
126 |
|
19701 | 127 |
(* normalized dependencies: reduction with well-formedness check *) |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
128 |
|
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
129 |
local |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
130 |
|
19729 | 131 |
val prt = Pretty.string_of oo pretty_const; |
42384 | 132 |
fun err ctxt (c, args) (d, Us) s1 s2 = |
133 |
error (s1 ^ " dependency of constant " ^ prt ctxt (c, args) ^ " -> " ^ prt ctxt (d, Us) ^ s2); |
|
19729 | 134 |
|
42384 | 135 |
fun acyclic ctxt (c, args) (d, Us) = |
19729 | 136 |
c <> d orelse |
137 |
is_none (match_args (args, Us)) orelse |
|
42384 | 138 |
err ctxt (c, args) (d, Us) "Circular" ""; |
19729 | 139 |
|
42384 | 140 |
fun wellformed ctxt defs (c, args) (d, Us) = |
57520
3ad1b289f21b
proper plain_args to ensure that multi-argument overloading cannot escape pattern restriction (despite more liberal structural containment before 3ae3cc4b1eac);
wenzelm
parents:
56050
diff
changeset
|
141 |
plain_args Us orelse |
19729 | 142 |
(case find_first (fn (Ts, _) => not (disjoint_args (Ts, Us))) (restricts_of defs d) of |
33701
9dd1079cec3a
primitive defs: clarified def (axiom name) vs. description;
wenzelm
parents:
32785
diff
changeset
|
143 |
SOME (Ts, description) => |
42384 | 144 |
err ctxt (c, args) (d, Us) "Malformed" |
145 |
("\n(restriction " ^ prt ctxt (d, Ts) ^ " from " ^ quote description ^ ")") |
|
19729 | 146 |
| NONE => true); |
19692 | 147 |
|
42384 | 148 |
fun reduction ctxt defs const deps = |
19692 | 149 |
let |
19701 | 150 |
fun reduct Us (Ts, rhs) = |
151 |
(case match_args (Ts, Us) of |
|
152 |
NONE => NONE |
|
153 |
| SOME subst => SOME (map (apsnd (map subst)) rhs)); |
|
154 |
fun reducts (d, Us) = get_first (reduct Us) (reducts_of defs d); |
|
155 |
||
156 |
val reds = map (`reducts) deps; |
|
157 |
val deps' = |
|
158 |
if forall (is_none o #1) reds then NONE |
|
20668 | 159 |
else SOME (fold_rev |
160 |
(fn (NONE, dp) => insert (op =) dp | (SOME dps, _) => fold (insert (op =)) dps) reds []); |
|
42384 | 161 |
val _ = forall (acyclic ctxt const) (the_default deps deps'); |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
162 |
in deps' end; |
19692 | 163 |
|
19760 | 164 |
in |
165 |
||
42384 | 166 |
fun normalize ctxt = |
19692 | 167 |
let |
19701 | 168 |
fun norm_update (c, {reducts, ...}: def) (changed, defs) = |
169 |
let |
|
170 |
val reducts' = reducts |> map (fn (args, deps) => |
|
42384 | 171 |
(args, perhaps (reduction ctxt defs (c, args)) deps)); |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
172 |
in |
19701 | 173 |
if reducts = reducts' then (changed, defs) |
32785 | 174 |
else (true, defs |> map_def c (fn (specs, restricts, _) => (specs, restricts, reducts'))) |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
175 |
end; |
19701 | 176 |
fun norm_all defs = |
177 |
(case Symtab.fold norm_update defs (false, defs) of |
|
178 |
(true, defs') => norm_all defs' |
|
179 |
| (false, _) => defs); |
|
19729 | 180 |
fun check defs (c, {reducts, ...}: def) = |
42384 | 181 |
reducts |> forall (fn (args, deps) => forall (wellformed ctxt defs (c, args)) deps); |
19729 | 182 |
in norm_all #> (fn defs => tap (Symtab.forall (check defs)) defs) end; |
19701 | 183 |
|
42384 | 184 |
fun dependencies ctxt (c, args) restr deps = |
19712
3ae3cc4b1eac
wellformed: be less ambitious about structural containment;
wenzelm
parents:
19707
diff
changeset
|
185 |
map_def c (fn (specs, restricts, reducts) => |
3ae3cc4b1eac
wellformed: be less ambitious about structural containment;
wenzelm
parents:
19707
diff
changeset
|
186 |
let |
3ae3cc4b1eac
wellformed: be less ambitious about structural containment;
wenzelm
parents:
19707
diff
changeset
|
187 |
val restricts' = Library.merge (op =) (restricts, restr); |
3ae3cc4b1eac
wellformed: be less ambitious about structural containment;
wenzelm
parents:
19707
diff
changeset
|
188 |
val reducts' = insert (op =) (args, deps) reducts; |
3ae3cc4b1eac
wellformed: be less ambitious about structural containment;
wenzelm
parents:
19707
diff
changeset
|
189 |
in (specs, restricts', reducts') end) |
42384 | 190 |
#> normalize ctxt; |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
191 |
|
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
192 |
end; |
19692 | 193 |
|
194 |
||
19624 | 195 |
(* merge *) |
196 |
||
42389 | 197 |
fun merge ctxt (Defs defs1, Defs defs2) = |
19613
9bf274ec94cf
allow dependencies of disjoint collections of instances;
wenzelm
parents:
19590
diff
changeset
|
198 |
let |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
199 |
fun add_deps (c, args) restr deps defs = |
19692 | 200 |
if AList.defined (op =) (reducts_of defs c) args then defs |
42384 | 201 |
else dependencies ctxt (c, args) restr deps defs; |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
202 |
fun add_def (c, {restricts, reducts, ...}: def) = |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
203 |
fold (fn (args, deps) => add_deps (c, args) restricts deps) reducts; |
19760 | 204 |
in |
205 |
Defs (Symtab.join join_specs (defs1, defs2) |
|
42384 | 206 |
|> normalize ctxt |> Symtab.fold add_def defs2) |
19760 | 207 |
end; |
19613
9bf274ec94cf
allow dependencies of disjoint collections of instances;
wenzelm
parents:
19590
diff
changeset
|
208 |
|
9bf274ec94cf
allow dependencies of disjoint collections of instances;
wenzelm
parents:
19590
diff
changeset
|
209 |
|
9bf274ec94cf
allow dependencies of disjoint collections of instances;
wenzelm
parents:
19590
diff
changeset
|
210 |
(* define *) |
19590
12af4942923d
simple substructure test for typargs (independent type constructors);
wenzelm
parents:
19569
diff
changeset
|
211 |
|
42384 | 212 |
fun define ctxt unchecked def description (c, args) deps (Defs defs) = |
17707
bc0270e9d27f
back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents:
17670
diff
changeset
|
213 |
let |
55544 | 214 |
val pos = Position.thread_data (); |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
215 |
val restr = |
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
216 |
if plain_args args orelse |
32785 | 217 |
(case args of [Type (_, rec_args)] => plain_args rec_args | _ => false) |
33701
9dd1079cec3a
primitive defs: clarified def (axiom name) vs. description;
wenzelm
parents:
32785
diff
changeset
|
218 |
then [] else [(args, description)]; |
19692 | 219 |
val spec = |
55544 | 220 |
(serial (), {def = def, description = description, pos = pos, lhs = args, rhs = deps}); |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
221 |
val defs' = defs |> update_specs c spec; |
42384 | 222 |
in Defs (defs' |> (if unchecked then I else dependencies ctxt (c, args) restr deps)) end; |
19697
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
223 |
|
423af2e013b8
specifications_of: lhs/rhs represented as typargs;
wenzelm
parents:
19695
diff
changeset
|
224 |
end; |