48 LogicalConst of int list list | |
48 LogicalConst of int list list | |
49 Abbreviation of term; |
49 Abbreviation of term; |
50 |
50 |
51 type decl = |
51 type decl = |
52 (typ * kind) * |
52 (typ * kind) * |
53 bool; (*early externing*) |
53 bool; (*authentic syntax*) |
54 |
54 |
55 datatype T = Consts of |
55 datatype T = Consts of |
56 {decls: (decl * stamp) NameSpace.table, |
56 {decls: (decl * stamp) NameSpace.table, |
57 constraints: typ Symtab.table, |
57 constraints: typ Symtab.table, |
58 rev_abbrevs: (term * term) list Symtab.table, |
58 rev_abbrevs: (term * term) list Symtab.table, |
59 expand_abbrevs: bool} * stamp; |
59 expand_abbrevs: bool} * stamp; |
60 |
60 |
61 fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2; |
61 fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2; |
62 |
62 |
63 fun make_consts (decls, constraints, rev_abbrevs, expand_abbrevs) = |
63 fun make_consts (decls, constraints, rev_abbrevs, expand_abbrevs) = |
64 Consts ({decls = decls, constraints = constraints, |
64 Consts ({decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs, |
65 expand_abbrevs = expand_abbrevs, rev_abbrevs = rev_abbrevs}, stamp ()); |
65 expand_abbrevs = expand_abbrevs}, stamp ()); |
66 |
66 |
67 fun map_consts f (Consts ({decls, constraints, rev_abbrevs, expand_abbrevs}, _)) = |
67 fun map_consts f (Consts ({decls, constraints, rev_abbrevs, expand_abbrevs}, _)) = |
68 make_consts (f (decls, constraints, rev_abbrevs, expand_abbrevs)); |
68 make_consts (f (decls, constraints, rev_abbrevs, expand_abbrevs)); |
69 |
69 |
70 fun abbrevs_of (Consts ({rev_abbrevs, ...}, _)) modes = |
70 fun abbrevs_of (Consts ({rev_abbrevs, ...}, _)) modes = |
112 val intern = NameSpace.intern o space_of; |
112 val intern = NameSpace.intern o space_of; |
113 val extern = NameSpace.extern o space_of; |
113 val extern = NameSpace.extern o space_of; |
114 |
114 |
115 fun extern_early consts c = |
115 fun extern_early consts c = |
116 (case try (the_const consts) c of |
116 (case try (the_const consts) c of |
117 SOME (_, false) => Syntax.constN ^ c |
117 SOME (_, true) => Syntax.constN ^ c |
118 | _ => extern consts c); |
118 | _ => extern consts c); |
119 |
119 |
120 fun syntax consts (c, mx) = |
120 fun syntax consts (c, mx) = |
121 let |
121 let |
122 val ((T, _), early) = the_const consts c handle TYPE (msg, _, _) => error msg; |
122 val ((T, _), authentic) = the_const consts c handle TYPE (msg, _, _) => error msg; |
123 val c' = if early then NameSpace.base c else Syntax.constN ^ c; |
123 val c' = if authentic then Syntax.constN ^ c else NameSpace.base c; |
124 in (c', T, mx) end; |
124 in (c', T, mx) end; |
125 |
125 |
126 |
126 |
127 (* read_const *) |
127 (* read_const *) |
128 |
128 |
199 (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs, expand_abbrevs)); |
199 (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs, expand_abbrevs)); |
200 |
200 |
201 |
201 |
202 (* declarations *) |
202 (* declarations *) |
203 |
203 |
204 fun declare naming ((c, declT), early) = |
204 fun declare naming ((c, declT), authentic) = |
205 map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) => |
205 map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) => |
206 let |
206 let |
207 fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos |
207 fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos |
208 | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos) |
208 | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos) |
209 | args_of (TFree _) _ = I |
209 | args_of (TFree _) _ = I |
210 and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is |
210 and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is |
211 | args_of_list [] _ _ = I; |
211 | args_of_list [] _ _ = I; |
212 val decl = (((declT, LogicalConst (map #2 (rev (args_of declT [] [])))), early), stamp ()); |
212 val decl = (((declT, LogicalConst (map #2 (rev (args_of declT [] [])))), authentic), stamp ()); |
213 in (extend_decls naming (c, decl) decls, constraints, rev_abbrevs, expand_abbrevs) end); |
213 in (extend_decls naming (c, decl) decls, constraints, rev_abbrevs, expand_abbrevs) end); |
214 |
214 |
215 |
215 |
216 (* constraints *) |
216 (* constraints *) |
217 |
217 |
245 in (Term.subst_bounds (rev vars, body), Term.list_comb (Const const, vars)) end; |
245 in (Term.subst_bounds (rev vars, body), Term.list_comb (Const const, vars)) end; |
246 in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end; |
246 in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end; |
247 |
247 |
248 in |
248 in |
249 |
249 |
250 fun abbreviate pp tsig naming mode ((c, raw_rhs), early) consts = |
250 fun abbreviate pp tsig naming mode ((c, raw_rhs), authentic) consts = |
251 let |
251 let |
252 val rhs = raw_rhs |
252 val rhs = raw_rhs |
253 |> Term.map_term_types (Type.cert_typ tsig) |
253 |> Term.map_term_types (Type.cert_typ tsig) |
254 |> certify pp tsig (consts |> expand_abbrevs false); |
254 |> certify pp tsig (consts |> expand_abbrevs false); |
255 val rhs' = rhs |
255 val rhs' = rhs |
257 val T = Term.fastype_of rhs; |
257 val T = Term.fastype_of rhs; |
258 in |
258 in |
259 consts |> map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) => |
259 consts |> map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) => |
260 let |
260 let |
261 val decls' = decls |
261 val decls' = decls |
262 |> extend_decls naming (c, (((T, Abbreviation rhs'), early), stamp ())); |
262 |> extend_decls naming (c, (((T, Abbreviation rhs'), authentic), stamp ())); |
263 val rev_abbrevs' = rev_abbrevs |
263 val rev_abbrevs' = rev_abbrevs |
264 |> fold (curry Symtab.update_list mode) (rev_abbrev (NameSpace.full naming c, T) rhs); |
264 |> fold (curry Symtab.update_list mode) (rev_abbrev (NameSpace.full naming c, T) rhs); |
265 in (decls', constraints, rev_abbrevs', expand_abbrevs) end) |
265 in (decls', constraints, rev_abbrevs', expand_abbrevs) end) |
266 end; |
266 end; |
267 |
267 |