author  ballarin 
Wed, 03 Dec 2008 15:27:41 +0100  
changeset 28951  e89dde5f365c 
parent 28936  f1647bf418f5 
child 28965  1de908189869 
child 28993  829e684b02ef 
permissions  rwrr 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

1 
(* Title: Pure/Isar/expression.ML 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

2 
Author: Clemens Ballarin, TU Muenchen 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

3 

28795  4 
New locale development  experimental. 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

5 
*) 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

6 

140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

7 
signature EXPRESSION = 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

8 
sig 
28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

9 
datatype 'term map = Positional of 'term option list  Named of (string * 'term) list; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

10 
type 'term expr = (string * (string * 'term map)) list; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

11 
type expression = string expr * (Name.binding * string option * mixfix) list; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

12 
type expression_i = term expr * (Name.binding * typ option * mixfix) list; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

13 

28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

14 
(* Processing of context statements *) 
28879  15 
val read_statement: Element.context list > (string * string list) list list > 
16 
Proof.context > (term * term list) list list * Proof.context; 

17 
val cert_statement: Element.context_i list > (term * term list) list list > 

18 
Proof.context > (term * term list) list list * Proof.context; 

19 

28795  20 
(* Declaring locales *) 
28902
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28898
diff
changeset

21 
val add_locale_cmd: string > bstring > expression > Element.context list > theory > 
28795  22 
string * Proof.context 
28902
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28898
diff
changeset

23 
val add_locale: string > bstring > expression_i > Element.context_i list > theory > 
28795  24 
string * Proof.context 
28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

25 

28895  26 
(* Interpretation *) 
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28936
diff
changeset

27 
val sublocale_cmd: string > expression > theory > Proof.state; 
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28936
diff
changeset

28 
val sublocale: string > expression_i > theory > Proof.state; 
28895  29 

28795  30 
(* Debugging and development *) 
31 
val parse_expression: OuterParse.token list > expression * OuterParse.token list 

28902
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28898
diff
changeset

32 
(* FIXME to spec_parse.ML *) 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

33 
end; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

34 

140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

35 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

36 
structure Expression : EXPRESSION = 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

37 
struct 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

38 

28795  39 
datatype ctxt = datatype Element.ctxt; 
40 

41 

42 
(*** Expressions ***) 

28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

43 

28872  44 
datatype 'term map = 
45 
Positional of 'term option list  

46 
Named of (string * 'term) list; 

28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

47 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

48 
type 'term expr = (string * (string * 'term map)) list; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

49 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

50 
type expression = string expr * (Name.binding * string option * mixfix) list; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

51 
type expression_i = term expr * (Name.binding * typ option * mixfix) list; 
28795  52 

28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

53 

140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

54 
(** Parsing and printing **) 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

55 

140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

56 
local 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

57 

140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

58 
structure P = OuterParse; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

59 

140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

60 
val loc_keyword = P.$$$ "fixes"  P.$$$ "constrains"  P.$$$ "assumes"  
28795  61 
P.$$$ "defines"  P.$$$ "notes"; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

62 
fun plus1_unless test scan = 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

63 
scan ::: Scan.repeat (P.$$$ "+"  Scan.unless test (P.!!! scan)); 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

64 

140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

65 
val prefix = P.name  P.$$$ ":"; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

66 
val named = P.name  (P.$$$ "="  P.term); 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

67 
val position = P.maybe P.term; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

68 
val instance = P.$$$ "where"  P.and_list1 named >> Named  
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

69 
Scan.repeat1 position >> Positional; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

70 

140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

71 
in 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

72 

28795  73 
val parse_expression = 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

74 
let 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

75 
fun expr2 x = P.xname x; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

76 
fun expr1 x = (Scan.optional prefix ""  expr2  
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

77 
Scan.optional instance (Named []) >> (fn ((p, l), i) => (l, (p, i)))) x; 
28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

78 
fun expr0 x = (plus1_unless loc_keyword expr1) x; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

79 
in expr0  P.for_fixes end; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

80 

140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

81 
end; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

82 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

83 
fun pretty_expr thy expr = 
28795  84 
let 
85 
fun pretty_pos NONE = Pretty.str "_" 

86 
 pretty_pos (SOME x) = Pretty.str x; 

87 
fun pretty_named (x, y) = [Pretty.str x, Pretty.brk 1, Pretty.str "=", 

88 
Pretty.brk 1, Pretty.str y] > Pretty.block; 

89 
fun pretty_ren (Positional ps) = take_suffix is_none ps > snd > 

90 
map pretty_pos > Pretty.breaks 

91 
 pretty_ren (Named []) = [] 

92 
 pretty_ren (Named ps) = Pretty.str "where" :: Pretty.brk 1 :: 

93 
(ps > map pretty_named > Pretty.separate "and"); 

94 
fun pretty_rename (loc, ("", ren)) = 

95 
Pretty.block (Pretty.str (NewLocale.extern thy loc) :: Pretty.brk 1 :: pretty_ren ren) 

96 
 pretty_rename (loc, (prfx, ren)) = 

97 
Pretty.block (Pretty.str prfx :: Pretty.brk 1 :: Pretty.str (NewLocale.extern thy loc) :: 

98 
Pretty.brk 1 :: pretty_ren ren); 

99 
in Pretty.separate "+" (map pretty_rename expr) > Pretty.block end; 

100 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

101 
fun err_in_expr thy msg expr = 
28795  102 
let 
103 
val err_msg = 

104 
if null expr then msg 

105 
else msg ^ "\n" ^ Pretty.string_of (Pretty.block 

106 
[Pretty.str "The above error(s) occurred in expression:", Pretty.brk 1, 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

107 
pretty_expr thy expr]) 
28795  108 
in error err_msg end; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

109 

140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

110 

28859  111 
(** Internalise locale names in expr **) 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

112 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

113 
fun intern thy instances = map (apfst (NewLocale.intern thy)) instances; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

114 

140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

115 

28859  116 
(** Parameters of expression. 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

117 

28895  118 
Sanity check of instantiations and extraction of implicit parameters. 
119 
The latter only occurs iff strict = false. 

120 
Positional instantiations are extended to match full length of parameter list 

121 
of instantiated locale. **) 

122 

123 
fun parameters_of thy strict (expr, fixed) = 

28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

124 
let 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

125 
fun reject_dups message xs = 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

126 
let val dups = duplicates (op =) xs 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

127 
in 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

128 
if null dups then () else error (message ^ commas dups) 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

129 
end; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

130 

28795  131 
fun match_bind (n, b) = (n = Name.name_of b); 
132 
fun bind_eq (b1, b2) = (Name.name_of b1 = Name.name_of b2); 

133 
(* FIXME: cannot compare bindings for equality. *) 

134 

135 
fun params_loc loc = 

28859  136 
(NewLocale.params_of thy loc > map (fn (p, _, mx) => (p, mx)), loc); 
28795  137 
fun params_inst (expr as (loc, (prfx, Positional insts))) = 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

138 
let 
28795  139 
val (ps, loc') = params_loc loc; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

140 
val d = length ps  length insts; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

141 
val insts' = 
28879  142 
if d < 0 then error ("More arguments than parameters in instantiation of locale " ^ 
143 
quote (NewLocale.extern thy loc)) 

28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

144 
else insts @ replicate d NONE; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

145 
val ps' = (ps ~~ insts') > 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

146 
map_filter (fn (p, NONE) => SOME p  (_, SOME _) => NONE); 
28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

147 
in (ps', (loc', (prfx, Positional insts'))) end 
28795  148 
 params_inst (expr as (loc, (prfx, Named insts))) = 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

149 
let 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

150 
val _ = reject_dups "Duplicate instantiation of the following parameter(s): " 
28859  151 
(map fst insts); 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

152 

28795  153 
val (ps, loc') = params_loc loc; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

154 
val ps' = fold (fn (p, _) => fn ps => 
28795  155 
if AList.defined match_bind ps p then AList.delete match_bind p ps 
28859  156 
else error (quote p ^" not a parameter of instantiated expression.")) insts ps; 
28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

157 
in (ps', (loc', (prfx, Named insts))) end; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

158 
fun params_expr is = 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

159 
let 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

160 
val (is', ps') = fold_map (fn i => fn ps => 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

161 
let 
28795  162 
val (ps', i') = params_inst i; 
163 
val ps'' = AList.join bind_eq (fn p => fn (mx1, mx2) => 

164 
(* FIXME: should check for bindings being the same. 

165 
Instead we check for equal name and syntax. *) 

28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

166 
if mx1 = mx2 then mx1 
28862  167 
else error ("Conflicting syntax for parameter" ^ quote (Name.display p) ^ 
28859  168 
" in expression.")) (ps, ps') 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

169 
in (i', ps'') end) is [] 
28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

170 
in (ps', is') end; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

171 

28895  172 
val (implicit, expr') = params_expr expr; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

173 

28895  174 
val implicit' = map (#1 #> Name.name_of) implicit; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

175 
val fixed' = map (#1 #> Name.name_of) fixed; 
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

176 
val _ = reject_dups "Duplicate fixed parameter(s): " fixed'; 
28895  177 
val implicit'' = if strict then [] 
178 
else let val _ = reject_dups 

179 
"Parameter(s) declared simultaneously in expression and for clause: " (implicit' @ fixed') 

180 
in map (fn (b, mx) => (b, NONE, mx)) implicit end; 

28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

181 

28895  182 
in (expr', implicit'' @ fixed) end; 
28697
140bfb63f893
Newstyle locale expressions with instantiation (new file expression.ML).
ballarin
parents:
diff
changeset

183 

28795  184 

185 
(** Read instantiation **) 

186 

28872  187 
(* Parse positional or named instantiation *) 
188 

28859  189 
local 
190 

28872  191 
fun prep_inst parse_term parms (Positional insts) ctxt = 
192 
(insts ~~ parms) > map (fn 

193 
(NONE, p) => Syntax.parse_term ctxt p  

194 
(SOME t, _) => parse_term ctxt t) 

195 
 prep_inst parse_term parms (Named insts) ctxt = 

196 
parms > map (fn p => case AList.lookup (op =) insts p of 

197 
SOME t => parse_term ctxt t  

198 
NONE => Syntax.parse_term ctxt p); 

199 

200 
in 

201 

202 
fun parse_inst x = prep_inst Syntax.parse_term x; 

203 
fun make_inst x = prep_inst (K I) x; 

204 

205 
end; 

206 

207 

208 
(* Instantiation morphism *) 

209 

210 
fun inst_morph (parm_names, parm_types) (prfx, insts') ctxt = 

28795  211 
let 
212 
(* parameters *) 

213 
val type_parm_names = fold Term.add_tfreesT parm_types [] > map fst; 

214 

215 
(* type inference and contexts *) 

216 
val parm_types' = map (TypeInfer.paramify_vars o Logic.varifyT) parm_types; 

217 
val type_parms = fold Term.add_tvarsT parm_types' [] > map (Logic.mk_type o TVar); 

218 
val arg = type_parms @ map2 TypeInfer.constrain parm_types' insts'; 

219 
val res = Syntax.check_terms ctxt arg; 

220 
val ctxt' = ctxt > fold Variable.auto_fixes res; 

28872  221 

28795  222 
(* instantiation *) 
223 
val (type_parms'', res') = chop (length type_parms) res; 

224 
val insts'' = (parm_names ~~ res') > map_filter 

225 
(fn (inst as (x, Free (y, _))) => if x = y then NONE else SOME inst  

226 
inst => SOME inst); 

227 
val instT = Symtab.make (type_parm_names ~~ map Logic.dest_type type_parms''); 

228 
val inst = Symtab.make insts''; 

229 
in 

230 
(Element.inst_morphism (ProofContext.theory_of ctxt) (instT, inst) $> 

231 
Morphism.name_morphism (Name.qualified prfx), ctxt') 

232 
end; 

28859  233 

28795  234 

235 
(*** Locale processing ***) 

236 

28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

237 
(** Parsing **) 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

238 

5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

239 
fun parse_elem prep_typ prep_term ctxt elem = 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

240 
Element.map_ctxt {name = I, var = I, typ = prep_typ ctxt, 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

241 
term = prep_term ctxt, fact = I, attrib = I} elem; 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

242 

5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

243 
fun parse_concl prep_term ctxt concl = 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

244 
(map o map) (fn (t, ps) => 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

245 
(prep_term ctxt, map (prep_term ctxt) ps)) concl; 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

246 

5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

247 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

248 
(** Simultaneous type inference: instantiations + elements + conclusion **) 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

249 

6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

250 
local 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

251 

6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

252 
fun mk_type T = (Logic.mk_type T, []); 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

253 
fun mk_term t = (t, []); 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

254 
fun mk_propp (p, pats) = (Syntax.type_constraint propT p, pats); 
28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

255 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

256 
fun dest_type (T, []) = Logic.dest_type T; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

257 
fun dest_term (t, []) = t; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

258 
fun dest_propp (p, pats) = (p, pats); 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

259 

6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

260 
fun extract_inst (_, (_, ts)) = map mk_term ts; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

261 
fun restore_inst ((l, (p, _)), cs) = (l, (p, map dest_term cs)); 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

262 

6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

263 
fun extract_elem (Fixes fixes) = map (#2 #> the_list #> map mk_type) fixes 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

264 
 extract_elem (Constrains csts) = map (#2 #> single #> map mk_type) csts 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

265 
 extract_elem (Assumes asms) = map (#2 #> map mk_propp) asms 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

266 
 extract_elem (Defines defs) = map (fn (_, (t, ps)) => [mk_propp (t, ps)]) defs 
28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

267 
 extract_elem (Notes _) = []; 
28795  268 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

269 
fun restore_elem (Fixes fixes, css) = 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

270 
(fixes ~~ css) > map (fn ((x, _, mx), cs) => 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

271 
(x, cs > map dest_type > try hd, mx)) > Fixes 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

272 
 restore_elem (Constrains csts, css) = 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

273 
(csts ~~ css) > map (fn ((x, _), cs) => 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

274 
(x, cs > map dest_type > hd)) > Constrains 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

275 
 restore_elem (Assumes asms, css) = 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

276 
(asms ~~ css) > map (fn ((b, _), cs) => (b, map dest_propp cs)) > Assumes 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

277 
 restore_elem (Defines defs, css) = 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

278 
(defs ~~ css) > map (fn ((b, _), [c]) => (b, dest_propp c)) > Defines 
28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

279 
 restore_elem (Notes notes, _) = Notes notes; 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

280 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

281 
fun check cs context = 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

282 
let 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

283 
fun prep (_, pats) (ctxt, t :: ts) = 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

284 
let val ctxt' = Variable.auto_fixes t ctxt 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

285 
in 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

286 
((t, Syntax.check_props (ProofContext.set_mode ProofContext.mode_pattern ctxt') pats), 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

287 
(ctxt', ts)) 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

288 
end 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

289 
val (cs', (context', _)) = fold_map prep cs 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

290 
(context, Syntax.check_terms 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

291 
(ProofContext.set_mode ProofContext.mode_schematic context) (map fst cs)); 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

292 
in (cs', context') end; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

293 

6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

294 
in 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

295 

28872  296 
fun check_autofix insts elems concl ctxt = 
28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

297 
let 
28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

298 
val inst_cs = map extract_inst insts; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

299 
val elem_css = map extract_elem elems; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

300 
val concl_cs = (map o map) mk_propp concl; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

301 
(* Type inference *) 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

302 
val (inst_cs' :: css', ctxt') = 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

303 
(fold_burrow o fold_burrow) check (inst_cs :: elem_css @ [concl_cs]) ctxt; 
28936
f1647bf418f5
No resolution of patterns within context statements.
ballarin
parents:
28903
diff
changeset

304 
val (elem_css', [concl_cs']) = chop (length elem_css) css'; 
28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

305 
in 
28936
f1647bf418f5
No resolution of patterns within context statements.
ballarin
parents:
28903
diff
changeset

306 
(map restore_inst (insts ~~ inst_cs'), map restore_elem (elems ~~ elem_css'), 
f1647bf418f5
No resolution of patterns within context statements.
ballarin
parents:
28903
diff
changeset

307 
concl_cs', ctxt') 
28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

308 
end; 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

309 

6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

310 
end; 
28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

311 

5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

312 

5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

313 
(** Prepare locale elements **) 
28795  314 

315 
fun declare_elem prep_vars (Fixes fixes) ctxt = 

316 
let val (vars, _) = prep_vars fixes ctxt 

28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

317 
in ctxt > ProofContext.add_fixes_i vars > snd end 
28795  318 
 declare_elem prep_vars (Constrains csts) ctxt = 
28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

319 
ctxt > prep_vars (map (fn (x, T) => (Name.binding x, SOME T, NoSyn)) csts) > snd 
28872  320 
 declare_elem _ (Assumes _) ctxt = ctxt 
321 
 declare_elem _ (Defines _) ctxt = ctxt 

28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

322 
 declare_elem _ (Notes _) ctxt = ctxt; 
28795  323 

28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

324 
(** Finish locale elements, extract specification text **) 
28795  325 

326 
val norm_term = Envir.beta_norm oo Term.subst_atomic; 

327 

328 
fun abstract_thm thy eq = 

329 
Thm.assume (Thm.cterm_of thy eq) > Drule.gen_all > Drule.abs_def; 

330 

331 
fun bind_def ctxt eq (xs, env, ths) = 

332 
let 

333 
val ((y, T), b) = LocalDefs.abs_def eq; 

334 
val b' = norm_term env b; 

335 
val th = abstract_thm (ProofContext.theory_of ctxt) eq; 

336 
fun err msg = error (msg ^ ": " ^ quote y); 

337 
in 

338 
exists (fn (x, _) => x = y) xs andalso 

339 
err "Attempt to define previously specified variable"; 

340 
exists (fn (Free (y', _), _) => y = y'  _ => false) env andalso 

341 
err "Attempt to redefine variable"; 

342 
(Term.add_frees b' xs, (Free (y, T), b') :: env, th :: ths) 

343 
end; 

344 

28872  345 
fun eval_text _ _ (Fixes _) text = text 
346 
 eval_text _ _ (Constrains _) text = text 

347 
 eval_text _ is_ext (Assumes asms) 

28795  348 
(((exts, exts'), (ints, ints')), (xs, env, defs)) = 
349 
let 

350 
val ts = maps (map #1 o #2) asms; 

351 
val ts' = map (norm_term env) ts; 

28872  352 
val spec' = 
353 
if is_ext then ((exts @ ts, exts' @ ts'), (ints, ints')) 

354 
else ((exts, exts'), (ints @ ts, ints' @ ts')); 

28795  355 
in (spec', (fold Term.add_frees ts' xs, env, defs)) end 
28872  356 
 eval_text ctxt _ (Defines defs) (spec, binds) = 
28795  357 
(spec, fold (bind_def ctxt o #1 o #2) defs binds) 
28872  358 
 eval_text _ _ (Notes _) text = text; 
28795  359 

28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

360 
fun closeup _ _ false elem = elem 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

361 
 closeup ctxt parms true elem = 
28795  362 
let 
363 
fun close_frees t = 

364 
let 

365 
val rev_frees = 

366 
Term.fold_aterms (fn Free (x, T) => 

28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

367 
if AList.defined (op =) parms x then I else insert (op =) (x, T)  _ => I) t []; 
28795  368 
in Term.list_all_free (rev rev_frees, t) end; 
369 

370 
fun no_binds [] = [] 

28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

371 
 no_binds _ = error "Illegal term bindings in context element"; 
28795  372 
in 
373 
(case elem of 

374 
Assumes asms => Assumes (asms > map (fn (a, propps) => 

375 
(a, map (fn (t, ps) => (close_frees t, no_binds ps)) propps))) 

376 
 Defines defs => Defines (defs > map (fn (a, (t, ps)) => 

377 
(a, (close_frees (#2 (LocalDefs.cert_def ctxt t)), no_binds ps)))) 

378 
 e => e) 

379 
end; 

380 

28872  381 
fun finish_primitive parms _ (Fixes fixes) = Fixes (map (fn (binding, _, mx) => 
28795  382 
let val x = Name.name_of binding 
383 
in (binding, AList.lookup (op =) parms x, mx) end) fixes) 

28872  384 
 finish_primitive _ _ (Constrains _) = Constrains [] 
385 
 finish_primitive _ close (Assumes asms) = close (Assumes asms) 

386 
 finish_primitive _ close (Defines defs) = close (Defines defs) 

387 
 finish_primitive _ _ (Notes facts) = Notes facts; 

388 

389 
fun finish_inst ctxt parms do_close (loc, (prfx, inst)) text = 

390 
let 

391 
val thy = ProofContext.theory_of ctxt; 

392 
val (parm_names, parm_types) = NewLocale.params_of thy loc > 

393 
map (fn (b, SOME T, _) => (Name.name_of b, T)) > split_list; 

394 
val (asm, defs) = NewLocale.specification_of thy loc; 

395 
val (morph, _) = inst_morph (parm_names, parm_types) (prfx, inst) ctxt; 

396 
val asm' = Option.map (Morphism.term morph) asm; 

397 
val defs' = map (Morphism.term morph) defs; 

398 
val text' = text > 

399 
(if is_some asm 

400 
then eval_text ctxt false (Assumes [(Attrib.no_binding, [(the asm', [])])]) 

401 
else I) > 

402 
(if not (null defs) 

403 
then eval_text ctxt false (Defines (map (fn def => (Attrib.no_binding, (def, []))) defs')) 

404 
else I) 

405 
(* FIXME clone from new_locale.ML *) 

406 
in ((loc, morph), text') end; 

28795  407 

28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

408 
fun finish_elem ctxt parms do_close elem text = 
28795  409 
let 
28872  410 
val elem' = finish_primitive parms (closeup ctxt parms do_close) elem; 
411 
val text' = eval_text ctxt true elem' text; 

28795  412 
in (elem', text') end 
413 

28872  414 
fun finish ctxt parms do_close insts elems text = 
415 
let 

416 
val (deps, text') = fold_map (finish_inst ctxt parms do_close) insts text; 

417 
val (elems', text'') = fold_map (finish_elem ctxt parms do_close) elems text'; 

418 
in (deps, elems', text'') end; 

28795  419 

420 

28895  421 
(** Process full context statement: instantiations + elements + conclusion **) 
422 

423 
(* Interleave incremental parsing and type inference over entire parsed stretch. *) 

424 

28795  425 
local 
426 

28895  427 
fun prep_full_context_statement parse_typ parse_prop parse_inst prep_vars prep_expr 
428 
strict do_close context raw_import raw_elems raw_concl = 

28795  429 
let 
28872  430 
val thy = ProofContext.theory_of context; 
431 

28895  432 
val (raw_insts, fixed) = parameters_of thy strict (apfst (prep_expr thy) raw_import); 
433 

28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28936
diff
changeset

434 
fun prep_inst (loc, (prfx, inst)) (i, insts, ctxt) = 
28872  435 
let 
436 
val (parm_names, parm_types) = NewLocale.params_of thy loc > 

437 
map (fn (b, SOME T, _) => (Name.name_of b, T)) > split_list; 

438 
val inst' = parse_inst parm_names inst ctxt; 

28885
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

439 
val parm_types' = map (TypeInfer.paramify_vars o 
6f6bf52e75bb
Expression types cleaned up, proper treatment of term patterns.
ballarin
parents:
28879
diff
changeset

440 
Term.map_type_tvar (fn ((x, _), S) => TVar ((x, i), S)) o Logic.varifyT) parm_types; 
28872  441 
val inst'' = map2 TypeInfer.constrain parm_types' inst'; 
442 
val insts' = insts @ [(loc, (prfx, inst''))]; 

28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28936
diff
changeset

443 
val (insts'', _, _, ctxt' (* FIXME not used *) ) = check_autofix insts' [] [] ctxt; 
28872  444 
val inst''' = insts'' > List.last > snd > snd; 
445 
val (morph, _) = inst_morph (parm_names, parm_types) (prfx, inst''') ctxt; 

28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28936
diff
changeset

446 
val ctxt'' = NewLocale.activate_declarations thy (loc, morph) ctxt; 
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28936
diff
changeset

447 
in (i+1, insts', ctxt'') end; 
28872  448 

449 
fun prep_elem raw_elem (insts, elems, ctxt) = 

28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

450 
let 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

451 
val ctxt' = declare_elem prep_vars raw_elem ctxt; 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

452 
val elems' = elems @ [parse_elem parse_typ parse_prop ctxt' raw_elem]; 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

453 
(* FIXME term bindings *) 
28872  454 
val (_, _, _, ctxt'') = check_autofix insts elems' [] ctxt'; 
455 
in (insts, elems', ctxt') end; 

28795  456 

28872  457 
fun prep_concl raw_concl (insts, elems, ctxt) = 
28795  458 
let 
28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

459 
val concl = (map o map) (fn (t, ps) => 
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

460 
(parse_prop ctxt t, map (parse_prop ctxt) ps)) raw_concl; 
28872  461 
in check_autofix insts elems concl ctxt end; 
28795  462 

28872  463 
val fors = prep_vars fixed context > fst; 
464 
val ctxt = context > ProofContext.add_fixes_i fors > snd; 

28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28936
diff
changeset

465 
val (_, insts', ctxt') = fold prep_inst raw_insts (0, [], NewLocale.clear_idents ctxt); 
28872  466 
val (_, elems'', ctxt'') = fold prep_elem raw_elems (insts', [], ctxt'); 
467 
val (insts, elems, concl, ctxt) = prep_concl raw_concl (insts', elems'', ctxt''); 

28795  468 

28872  469 
(* Retrieve parameter types *) 
28795  470 
val xs = fold (fn Fixes fixes => (fn ps => ps @ map (Name.name_of o #1) fixes)  
28872  471 
_ => fn ps => ps) (Fixes fors :: elems) []; 
28859  472 
val (Ts, ctxt') = fold_map ProofContext.inferred_param xs ctxt; 
28895  473 
val parms = xs ~~ Ts; (* params from expression and elements *) 
28795  474 

28872  475 
val Fixes fors' = finish_primitive parms I (Fixes fors); 
476 
val (deps, elems', text) = finish ctxt' parms do_close insts elems ((([], []), ([], [])), ([], [], [])); 

28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

477 
(* text has the following structure: 
28795  478 
(((exts, exts'), (ints, ints')), (xs, env, defs)) 
479 
where 

28872  480 
exts: external assumptions (terms in assumes elements) 
28795  481 
exts': dito, normalised wrt. env 
28872  482 
ints: internal assumptions (terms in assumptions from insts) 
28795  483 
ints': dito, normalised wrt. env 
484 
xs: the free variables in exts' and ints' and rhss of definitions, 

485 
this includes parameters except defined parameters 

486 
env: list of term pairs encoding substitutions, where the first term 

487 
is a free variable; substitutions represent defines elements and 

488 
the rhs is normalised wrt. the previous env 

489 
defs: theorems representing the substitutions from defines elements 

490 
(thms are normalised wrt. env). 

491 
elems is an updated version of raw_elems: 

492 
 type info added to Fixes and modified in Constrains 

493 
 axiom and definition statement replaced by corresponding one 

494 
from proppss in Assumes and Defines 

495 
 Facts unchanged 

496 
*) 

28852
5ddea758679b
Type inference for elements through syntax module.
ballarin
parents:
28832
diff
changeset

497 

28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

498 
in ((fors', deps, elems', concl), (parms, text)) end 
28795  499 

500 
in 

501 

28895  502 
fun read_full_context_statement x = 
503 
prep_full_context_statement Syntax.parse_typ Syntax.parse_prop parse_inst 

504 
ProofContext.read_vars intern x; 

505 
fun cert_full_context_statement x = 

506 
prep_full_context_statement (K I) (K I) make_inst ProofContext.cert_vars (K I) x; 

28795  507 

508 
end; 

509 

510 

28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

511 
(* Context statement: elements + conclusion *) 
28795  512 

513 
local 

514 

28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

515 
fun prep_statement prep activate raw_elems raw_concl context = 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

516 
let 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

517 
val ((_, _, elems, concl), _) = prep true false context ([], []) raw_elems raw_concl; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

518 
val (_, context') = activate elems (ProofContext.set_stmt true context); 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

519 
in (concl, context') end; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

520 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

521 
in 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

522 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

523 
fun read_statement x = prep_statement read_full_context_statement Element.activate x; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

524 
fun cert_statement x = prep_statement cert_full_context_statement Element.activate_i x; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

525 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

526 
end; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

527 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

528 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

529 
(* Locale declaration: import + elements *) 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

530 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

531 
local 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

532 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

533 
fun prep_declaration prep activate raw_import raw_elems context = 
28795  534 
let 
535 
val thy = ProofContext.theory_of context; 

536 

28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

537 
val ((fixed, deps, elems, _), (parms, (spec, (_, _, defs)))) = 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

538 
prep false true context raw_import raw_elems []; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

539 
(* Declare parameters and imported facts *) 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

540 
val context' = context > 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

541 
ProofContext.add_fixes_i fixed > snd > 
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28936
diff
changeset

542 
NewLocale.clear_idents > fold (NewLocale.activate_facts thy) deps; 
28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

543 
val ((elems', _), _) = activate elems (ProofContext.set_stmt true context'); 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

544 
in ((fixed, deps, elems'), (parms, spec, defs)) end; 
28795  545 

546 
in 

547 

28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

548 
fun read_declaration x = prep_declaration read_full_context_statement Element.activate x; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

549 
fun cert_declaration x = prep_declaration cert_full_context_statement Element.activate_i x; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

550 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

551 
end; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

552 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

553 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

554 
(* Locale expression to set up a goal *) 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

555 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

556 
local 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

557 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

558 
fun props_of thy (name, morph) = 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

559 
let 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

560 
val (asm, defs) = NewLocale.specification_of thy name; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

561 
in 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

562 
(case asm of NONE => defs  SOME asm => asm :: defs) > map (Morphism.term morph) 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

563 
end; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

564 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

565 
fun prep_goal_expression prep expression context = 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

566 
let 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

567 
val thy = ProofContext.theory_of context; 
28879  568 

28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

569 
val ((fixed, deps, _, _), _) = prep true true context expression [] []; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

570 
(* proof obligations *) 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

571 
val propss = map (props_of thy) deps; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

572 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

573 
val goal_ctxt = context > 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

574 
ProofContext.add_fixes_i fixed > snd > 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

575 
(fold o fold) Variable.auto_fixes propss; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

576 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

577 
val export = Variable.export_morphism goal_ctxt context; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

578 
val exp_fact = Drule.zero_var_indexes_list o map Thm.strip_shyps o Morphism.fact export; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

579 
(* val exp_term = TermSubst.zero_var_indexes o Morphism.term export; *) 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

580 
val exp_term = Drule.term_rule thy (singleton exp_fact); 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

581 
val exp_typ = Logic.type_map exp_term; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

582 
val export' = 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

583 
Morphism.morphism {name = I, var = I, typ = exp_typ, term = exp_term, fact = exp_fact}; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

584 
in ((propss, deps, export'), goal_ctxt) end; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

585 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

586 
in 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

587 

530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

588 
fun read_goal_expression x = prep_goal_expression read_full_context_statement x; 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

589 
fun cert_goal_expression x = prep_goal_expression cert_full_context_statement x; 
28879  590 

28795  591 
end; 
592 

593 

594 
(*** Locale declarations ***) 

595 

28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

596 
(* axiomsN: name of theorem set with destruct rules for locale predicates, 
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

597 
also name suffix of delta predicates and assumptions. *) 
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

598 

b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

599 
val axiomsN = "axioms"; 
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

600 

28795  601 
local 
602 

603 
(* introN: name of theorems for introduction rules of locale and 

28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

604 
delta predicates *) 
28795  605 

606 
val introN = "intro"; 

607 

608 
fun atomize_spec thy ts = 

609 
let 

610 
val t = Logic.mk_conjunction_balanced ts; 

611 
val body = ObjectLogic.atomize_term thy t; 

612 
val bodyT = Term.fastype_of body; 

613 
in 

614 
if bodyT = propT then (t, propT, Thm.reflexive (Thm.cterm_of thy t)) 

615 
else (body, bodyT, ObjectLogic.atomize (Thm.cterm_of thy t)) 

616 
end; 

617 

618 
(* achieve plain syntax for locale predicates (without "PROP") *) 

619 

620 
fun aprop_tr' n c = (Syntax.constN ^ c, fn ctxt => fn args => 

621 
if length args = n then 

622 
Syntax.const "_aprop" $ 

623 
Term.list_comb (Syntax.free (Consts.extern (ProofContext.consts_of ctxt) c), args) 

624 
else raise Match); 

625 

28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

626 
(* define one predicate including its intro rule and axioms 
28795  627 
 bname: predicate name 
628 
 parms: locale parameters 

629 
 defs: thms representing substitutions from defines elements 

630 
 ts: terms representing locale assumptions (not normalised wrt. defs) 

631 
 norm_ts: terms representing locale assumptions (normalised wrt. defs) 

632 
 thy: the theory 

633 
*) 

634 

635 
fun def_pred bname parms defs ts norm_ts thy = 

636 
let 

637 
val name = Sign.full_name thy bname; 

638 

639 
val (body, bodyT, body_eq) = atomize_spec thy norm_ts; 

640 
val env = Term.add_term_free_names (body, []); 

641 
val xs = filter (member (op =) env o #1) parms; 

642 
val Ts = map #2 xs; 

643 
val extraTs = (Term.term_tfrees body \\ fold Term.add_tfreesT Ts []) 

644 
> Library.sort_wrt #1 > map TFree; 

645 
val predT = map Term.itselfT extraTs > Ts > bodyT; 

646 

647 
val args = map Logic.mk_type extraTs @ map Free xs; 

648 
val head = Term.list_comb (Const (name, predT), args); 

649 
val statement = ObjectLogic.ensure_propT thy head; 

650 

651 
val ([pred_def], defs_thy) = 

652 
thy 

653 
> bodyT = propT ? Sign.add_advanced_trfuns ([], [], [aprop_tr' (length args) name], []) 

654 
> Sign.declare_const [] ((Name.binding bname, predT), NoSyn) > snd 

655 
> PureThy.add_defs false 

656 
[((Thm.def_name bname, Logic.mk_equals (head, body)), [Thm.kind_internal])]; 

657 
val defs_ctxt = ProofContext.init defs_thy > Variable.declare_term head; 

658 

659 
val cert = Thm.cterm_of defs_thy; 

660 

661 
val intro = Goal.prove_global defs_thy [] norm_ts statement (fn _ => 

662 
MetaSimplifier.rewrite_goals_tac [pred_def] THEN 

663 
Tactic.compose_tac (false, body_eq RS Drule.equal_elim_rule1, 1) 1 THEN 

664 
Tactic.compose_tac (false, 

665 
Conjunction.intr_balanced (map (Thm.assume o cert) norm_ts), 0) 1); 

666 

667 
val conjuncts = 

668 
(Drule.equal_elim_rule2 OF [body_eq, 

669 
MetaSimplifier.rewrite_rule [pred_def] (Thm.assume (cert statement))]) 

670 
> Conjunction.elim_balanced (length ts); 

671 
val axioms = ts ~~ conjuncts > map (fn (t, ax) => 

672 
Element.prove_witness defs_ctxt t 

673 
(MetaSimplifier.rewrite_goals_tac defs THEN 

674 
Tactic.compose_tac (false, ax, 0) 1)); 

675 
in ((statement, intro, axioms), defs_thy) end; 

676 

677 
in 

678 

679 
(* CB: main predicate definition function *) 

680 

681 
fun define_preds pname (parms, ((exts, exts'), (ints, ints')), defs) thy = 

682 
let 

683 
val (a_pred, a_intro, a_axioms, thy'') = 

684 
if null exts then (NONE, NONE, [], thy) 

685 
else 

686 
let 

687 
val aname = if null ints then pname else pname ^ "_" ^ axiomsN; 

688 
val ((statement, intro, axioms), thy') = 

689 
thy 

690 
> def_pred aname parms defs exts exts'; 

691 
val (_, thy'') = 

692 
thy' 

693 
> Sign.add_path aname 

694 
> Sign.no_base_names 

28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

695 
> PureThy.note_thmss Thm.internalK 
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

696 
[((Name.binding introN, []), [([intro], [NewLocale.unfold_attrib])])] 
28795  697 
> Sign.restore_naming thy'; 
698 
in (SOME statement, SOME intro, axioms, thy'') end; 

699 
val (b_pred, b_intro, b_axioms, thy'''') = 

700 
if null ints then (NONE, NONE, [], thy'') 

701 
else 

702 
let 

703 
val ((statement, intro, axioms), thy''') = 

704 
thy'' 

705 
> def_pred pname parms defs (ints @ the_list a_pred) (ints' @ the_list a_pred); 

706 
val (_, thy'''') = 

707 
thy''' 

708 
> Sign.add_path pname 

709 
> Sign.no_base_names 

710 
> PureThy.note_thmss Thm.internalK 

28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

711 
[((Name.binding introN, []), [([intro], [NewLocale.intro_attrib])]), 
28795  712 
((Name.binding axiomsN, []), 
713 
[(map (Drule.standard o Element.conclude_witness) axioms, [])])] 

714 
> Sign.restore_naming thy'''; 

715 
in (SOME statement, SOME intro, axioms, thy'''') end; 

716 
in ((a_pred, a_intro, a_axioms), (b_pred, b_intro, b_axioms), thy'''') end; 

717 

718 
end; 

719 

720 

721 
local 

722 

723 
fun assumes_to_notes (Assumes asms) axms = 

724 
fold_map (fn (a, spec) => fn axs => 

725 
let val (ps, qs) = chop (length spec) axs 

726 
in ((a, [(ps, [])]), qs) end) asms axms 

727 
> apfst (curry Notes Thm.assumptionK) 

728 
 assumes_to_notes e axms = (e, axms); 

729 

730 
fun defines_to_notes thy (Defines defs) defns = 

731 
let 

732 
val defs' = map (fn (_, (def, _)) => def) defs 

733 
val notes = map (fn (a, (def, _)) => 

734 
(a, [([assume (cterm_of thy def)], [])])) defs 

735 
in 

736 
(Notes (Thm.definitionK, notes), defns @ defs') 

737 
end 

738 
 defines_to_notes _ e defns = (e, defns); 

739 

28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

740 
fun gen_add_locale prep_decl 
28795  741 
bname predicate_name raw_imprt raw_body thy = 
742 
let 

743 
val thy_ctxt = ProofContext.init thy; 

744 
val name = Sign.full_name thy bname; 

745 
val _ = NewLocale.test_locale thy name andalso 

746 
error ("Duplicate definition of locale " ^ quote name); 

747 

28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

748 
val ((fixed, deps, body_elems), text as (parms, ((_, exts'), _), defs)) = 
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

749 
prep_decl raw_imprt raw_body thy_ctxt; 
28872  750 
val ((a_statement, a_intro, a_axioms), (b_statement, b_intro, b_axioms), thy') = 
28795  751 
define_preds predicate_name text thy; 
752 

753 
val extraTs = fold Term.add_tfrees exts' [] \\ fold Term.add_tfreesT (map snd parms) []; 

754 
val _ = if null extraTs then () 

755 
else warning ("Additional type variable(s) in locale specification " ^ quote bname); 

756 

28872  757 
val satisfy = Element.satisfy_morphism b_axioms; 
28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

758 

28895  759 
val params = fixed @ 
28872  760 
(body_elems > map_filter (fn Fixes fixes => SOME fixes  _ => NONE) > flat); 
28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

761 
val asm = if is_some b_statement then b_statement else a_statement; 
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

762 
val (body_elems', defns) = fold_map (defines_to_notes thy') body_elems []; 
28795  763 
val notes = body_elems' > 
28872  764 
(fn elems => fold_map assumes_to_notes elems (map Element.conclude_witness a_axioms)) > 
765 
fst > map (Element.morph_ctxt satisfy) > 

28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

766 
map_filter (fn Notes notes => SOME notes  _ => NONE) > 
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

767 
(if is_some asm 
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

768 
then cons (Thm.internalK, [((Name.binding (bname ^ "_" ^ axiomsN), []), 
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

769 
[([assume (cterm_of thy' (the asm))], [(Attrib.internal o K) NewLocale.witness_attrib])])]) 
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

770 
else I); 
28795  771 

28872  772 
val deps' = map (fn (l, morph) => (l, morph $> satisfy)) deps; 
773 

28795  774 
val loc_ctxt = thy' > 
28872  775 
NewLocale.register_locale name (extraTs, params) 
28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

776 
(asm, map prop_of defs) ([], []) 
28872  777 
(map (fn n => (n, stamp ())) notes > rev) (map (fn d => (d, stamp ())) deps' > rev) > 
28795  778 
NewLocale.init name 
779 
in (name, loc_ctxt) end; 

780 

781 
in 

782 

28902
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28898
diff
changeset

783 
val add_locale_cmd = gen_add_locale read_declaration; 
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28898
diff
changeset

784 
val add_locale = gen_add_locale cert_declaration; 
28795  785 

786 
end; 

787 

28895  788 

789 
(*** Interpretation ***) 

790 

791 
(** Witnesses and goals **) 

792 

793 
fun prep_propp propss = propss > map (map (rpair [] o Element.mark_witness)); 

794 

795 
fun prep_result propps thmss = 

796 
ListPair.map (fn (props, thms) => map2 Element.make_witness props thms) (propps, thmss); 

797 

798 

799 
(** Interpretation between locales: declaring sublocale relationships **) 

800 

801 
local 

802 

28902
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28898
diff
changeset

803 
fun gen_sublocale prep_expr intern 
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28936
diff
changeset

804 
raw_target expression thy = 
28895  805 
let 
28902
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28898
diff
changeset

806 
val target = intern thy raw_target; 
28895  807 
val target_ctxt = NewLocale.init target thy; 
808 

28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

809 
val ((propss, deps, export), goal_ctxt) = prep_expr expression target_ctxt; 
28898
530c7d28a962
Proper treatment of expressions with free arguments.
ballarin
parents:
28895
diff
changeset

810 

28902
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28898
diff
changeset

811 
fun store_dep ((name, morph), thms) = 
28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

812 
NewLocale.add_dependency target (name, morph $> Element.satisfy_morphism thms $> export); 
28895  813 

28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28936
diff
changeset

814 
fun after_qed results = fold store_dep (deps ~~ prep_result propss results); 
28895  815 
in 
816 
goal_ctxt > 

28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28936
diff
changeset

817 
Proof.theorem_i NONE after_qed (prep_propp propss) > 
28895  818 
Element.refine_witness > Seq.hd 
819 
end; 

820 

821 
in 

822 

28902
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28898
diff
changeset

823 
fun sublocale_cmd x = gen_sublocale read_goal_expression NewLocale.intern x; 
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28898
diff
changeset

824 
fun sublocale x = gen_sublocale cert_goal_expression (K I) x; 
28895  825 

28795  826 
end; 
28895  827 

828 

829 
end; 

28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset

830 