author  wenzelm 
Wed, 29 Nov 2006 15:45:02 +0100  
changeset 21594  2859c94d67d4 
parent 21585  2444f1d1127b 
child 21611  fc95ff1fe738 
permissions  rwrr 
20894  1 
(* Title: Pure/Isar/theory_target.ML 
2 
ID: $Id$ 

3 
Author: Makarius 

4 

21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

5 
Common theory/locale targets. 
20894  6 
*) 
7 

8 
signature THEORY_TARGET = 

9 
sig 

21006  10 
val peek: local_theory > string option 
20962  11 
val begin: bstring > Proof.context > local_theory 
20894  12 
val init: xstring option > theory > local_theory 
13 
val init_i: string option > theory > local_theory 

14 
end; 

15 

16 
structure TheoryTarget: THEORY_TARGET = 

17 
struct 

18 

21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

19 

20894  20 
(** locale targets **) 
21 

21006  22 
(* context data *) 
23 

24 
structure Data = ProofDataFun 

25 
( 

26 
val name = "Isar/theory_target"; 

27 
type T = string option; 

28 
fun init _ = NONE; 

29 
fun print _ _ = (); 

30 
); 

31 

32 
val _ = Context.add_setup Data.init; 

33 
val peek = Data.get; 

34 

35 

20894  36 
(* pretty *) 
37 

38 
fun pretty loc ctxt = 

39 
let 

40 
val thy = ProofContext.theory_of ctxt; 

41 
val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt)); 

21585  42 
val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt)); 
20894  43 
val elems = 
44 
(if null fixes then [] else [Element.Fixes fixes]) @ 

45 
(if null assumes then [] else [Element.Assumes assumes]); 

46 
in 

20984  47 
if loc = "" then 
48 
[Pretty.block 

49 
[Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy), 

50 
Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]] 

51 
else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)] 

52 
else 

53 
[Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =") 

54 
(map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)] 

20894  55 
end; 
56 

57 

58 
(* consts *) 

59 

21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

60 
fun consts is_loc depends decls lthy = 
20894  61 
let 
21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

62 
val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy))); 
20894  63 

64 
fun const ((c, T), mx) thy = 

65 
let 

66 
val U = map #2 xs > T; 

21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

67 
val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs); 
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

68 
val thy' = Sign.add_consts_authentic [(c, U, Syntax.unlocalize_mixfix is_loc mx)] thy; 
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

69 
in (((c, mx), t), thy') end; 
20894  70 

21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

71 
val (abbrs, lthy') = lthy > LocalTheory.theory_result (fold_map const decls); 
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

72 
val defs = abbrs > map (fn (x, t) => (x, (("", []), t))); 
20894  73 
in 
74 
lthy' 

21570  75 
> is_loc ? LocalTheory.abbrevs Syntax.default_mode abbrs 
21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

76 
> LocalDefs.add_defs defs 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

77 
>> map (apsnd snd) 
20894  78 
end; 
79 

80 

81 
(* axioms *) 

82 

83 
local 

84 

85 
fun add_axiom hyps (name, prop) thy = 

86 
let 

87 
val name' = if name = "" then "axiom_" ^ serial_string () else name; 

88 
val prop' = Logic.list_implies (hyps, prop); 

89 
val thy' = thy > Theory.add_axioms_i [(name', prop')]; 

90 
val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name')); 

91 
val prems = map (Thm.assume o Thm.cterm_of thy') hyps; 

92 
in (Drule.implies_elim_list axm prems, thy') end; 

93 

94 
in 

95 

21433  96 
fun axioms kind specs lthy = 
20915  97 
let 
21585  98 
val hyps = map Thm.term_of (Assumption.assms_of lthy); 
20915  99 
fun axiom ((name, atts), props) thy = thy 
100 
> fold_map (add_axiom hyps) (PureThy.name_multi name props) 

101 
> (fn ths => pair ((name, atts), [(ths, [])])); 

102 
in 

103 
lthy 

20984  104 
> fold (fold Variable.declare_term o snd) specs 
20915  105 
> LocalTheory.theory_result (fold_map axiom specs) 
21433  106 
> LocalTheory.notes kind 
20915  107 
end; 
20894  108 

109 
end; 

110 

111 

112 
(* defs *) 

113 

21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

114 
local 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

115 

21570  116 
infix also; 
117 
fun eq1 also eq2 = 

118 
eq2 COMP (eq1 COMP (Drule.incr_indexes2 eq1 eq2 transitive_thm)); 

119 

21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

120 
fun expand_term ctxt t = 
21570  121 
let 
122 
val thy = ProofContext.theory_of ctxt; 

123 
val thy_ctxt = ProofContext.init thy; 

124 
val ct = Thm.cterm_of thy t; 

21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

125 
val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) > Drule.dest_term; 
21570  126 
in (Thm.term_of ct', Tactic.rewrite true defs ct) end; 
127 

20894  128 
fun add_def (name, prop) = 
129 
Theory.add_defs_i false false [(name, prop)] #> 

130 
(fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy)); 

131 

132 
in 

133 

21570  134 
fun defs kind args lthy0 = 
20894  135 
let 
21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

136 
fun def ((c, mx), ((name, atts), rhs)) lthy1 = 
20894  137 
let 
21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

138 
val (rhs', rhs_conv) = expand_term lthy0 rhs; 
21570  139 
val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' []; 
20894  140 

21570  141 
val ([(lhs, lhs_def)], lthy2) = lthy1 
142 
> LocalTheory.consts (member (op =) xs) [((c, Term.fastype_of rhs), mx)]; 

143 
val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def)); 

21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

144 

21570  145 
val name' = Thm.def_name_optional c name; 
146 
val (def, lthy3) = lthy2 

20894  147 
> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs'))); 
148 

21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

149 
val eq = 
21570  150 
(*c == loc.c xs*) lhs_def 
151 
(*lhs' == rhs'*) also def 

152 
(*rhs' == rhs*) also Thm.symmetric rhs_conv; 

20894  153 
in ((lhs, ((name', atts), [([eq], [])])), lthy3) end; 
154 

21570  155 
val ((lhss, facts), lthy') = lthy0 > fold_map def args >> split_list; 
21433  156 
val (res, lthy'') = lthy' > LocalTheory.notes kind facts; 
20894  157 
in (lhss ~~ map (apsnd the_single) res, lthy'') end; 
158 

159 
end; 

160 

161 

162 
(* notes *) 

163 

21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

164 
(* FIXME 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

165 
fun import_export inner outer (name, raw_th) = 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

166 
let 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

167 
val th = norm_hhf_protect raw_th; 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

168 
val (defs, th') = LocalDefs.export inner outer th; 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

169 
val n = Thm.nprems_of th'  Thm.nprems_of th; 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

170 

2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

171 
val result = PureThy.name_thm true (name, th'); (* FIXME proper thm definition!? *) 
21585  172 

21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

173 
val concl_conv = Tactic.rewrite true defs (Thm.cprop_of th); 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

174 
val assms = map (Tactic.rewrite_rule defs o Thm.assume) (Assumption.assms_of inner); 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

175 
val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms); 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

176 
val reimported_result = 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

177 
(case SINGLE (Seq.INTERVAL assm_tac 1 n) result of 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

178 
NONE => raise THM ("Failed to reimport result", 0, [result]) 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

179 
 SOME res => res) COMP (concl_conv COMP_INCR Drule.equal_elim_rule2); 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

180 
val _ = reimported_result COMP (th COMP_INCR Drule.remdups_rl) handle THM _ => 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

181 
(warning "FIXME"; asm_rl); 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

182 
in (reimported_result, result) end; 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

183 
*) 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

184 
fun import_export inner outer (_, th) = 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

185 
(singleton (ProofContext.standard inner) th, Assumption.export false inner outer th); 
21585  186 

21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

187 
fun notes loc kind facts lthy = 
21585  188 
let 
21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

189 
val is_loc = loc <> ""; 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

190 
val thy = ProofContext.theory_of lthy; 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

191 
val thy_ctxt = ProofContext.init thy; 
21585  192 

21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

193 
val facts' = facts 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

194 
> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (fst a)) bs)) 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

195 
> PureThy.map_facts (import_export lthy thy_ctxt); 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

196 
val local_facts = facts' 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

197 
> PureThy.map_facts #1 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

198 
> Element.facts_map (Element.morph_ctxt (Morphism.thm_morphism Drule.local_standard)); 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

199 
val global_facts = facts' 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

200 
> PureThy.map_facts #2 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

201 
> Element.generalize_facts lthy thy_ctxt 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

202 
> PureThy.map_facts Drule.local_standard 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

203 
> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy); 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

204 
in 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

205 
lthy > LocalTheory.theory (fn thy => thy 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

206 
> Sign.qualified_names 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

207 
> PureThy.note_thmss_i kind global_facts > #2 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

208 
> Sign.restore_naming thy) 
21585  209 

21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

210 
> is_loc ? (fn lthy' => lthy' > LocalTheory.target 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

211 
(Locale.add_thmss loc kind 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

212 
(Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy')) local_facts))) 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

213 

21433  214 
> ProofContext.set_stmt true 
20894  215 
> ProofContext.qualified_names 
21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

216 
> ProofContext.note_thmss_i kind (Attrib.map_facts (Attrib.attribute_i thy) local_facts) 
20894  217 
> ProofContext.restore_naming lthy 
21433  218 
> ProofContext.restore_stmt lthy 
20894  219 
end; 
220 

221 

21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

222 
(* target declarations *) 
20894  223 

21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

224 
fun decl _ "" f = 
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

225 
LocalTheory.theory (Context.theory_map (f Morphism.identity)) #> 
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

226 
LocalTheory.target (Context.proof_map (f Morphism.identity)) 
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

227 
 decl add loc f = 
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

228 
LocalTheory.target (add loc (Context.proof_map o f)); 
20894  229 

21498
6382c3a1e7cf
uniform interface for type_syntax/term_syntax/declaration, dependent on morphism;
wenzelm
parents:
21467
diff
changeset

230 
val type_syntax = decl Locale.add_type_syntax; 
6382c3a1e7cf
uniform interface for type_syntax/term_syntax/declaration, dependent on morphism;
wenzelm
parents:
21467
diff
changeset

231 
val term_syntax = decl Locale.add_term_syntax; 
6382c3a1e7cf
uniform interface for type_syntax/term_syntax/declaration, dependent on morphism;
wenzelm
parents:
21467
diff
changeset

232 
val declaration = decl Locale.add_declaration; 
20894  233 

21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

234 
fun target_morphism loc lthy = 
21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

235 
ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $> 
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

236 
Morphism.thm_morphism Drule.local_standard; 
21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

237 

bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

238 
fun target_name "" lthy = Sign.full_name (ProofContext.theory_of lthy) 
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

239 
 target_name _ lthy = ProofContext.full_name (LocalTheory.target_of lthy); 
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

240 

20894  241 

242 
(* init and exit *) 

243 

21293  244 
fun begin loc = 
245 
Data.put (if loc = "" then NONE else SOME loc) #> 

21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

246 
LocalTheory.init (NameSpace.base loc) 
21293  247 
{pretty = pretty loc, 
21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset

248 
consts = consts (loc <> ""), 
21293  249 
axioms = axioms, 
250 
defs = defs, 

251 
notes = notes loc, 

21498
6382c3a1e7cf
uniform interface for type_syntax/term_syntax/declaration, dependent on morphism;
wenzelm
parents:
21467
diff
changeset

252 
type_syntax = type_syntax loc, 
21293  253 
term_syntax = term_syntax loc, 
254 
declaration = declaration loc, 

21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

255 
target_morphism = target_morphism loc, 
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

256 
target_name = target_name loc, 
21293  257 
reinit = fn _ => begin loc o (if loc = "" then ProofContext.init else Locale.init loc), 
21533
bada5ac1216a
simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents:
21498
diff
changeset

258 
exit = LocalTheory.target_of}; 
20894  259 

20962  260 
fun init_i NONE thy = begin "" (ProofContext.init thy) 
261 
 init_i (SOME loc) thy = begin loc (Locale.init loc thy); 

20894  262 

21276  263 
fun init (SOME "") thy = init_i NONE thy 
264 
 init loc thy = init_i (Option.map (Locale.intern thy) loc) thy; 

20894  265 

266 
end; 