src/Pure/Isar/locale.ML
changeset 19018 88b4979193d8
parent 18964 67f572e03236
child 19025 596fb1eb7856
equal deleted inserted replaced
19017:5f06c37043e4 19018:88b4979193d8
    84     theory -> ((string * thm list) list * (string * thm list) list) * (Proof.context * theory)
    84     theory -> ((string * thm list) list * (string * thm list) list) * (Proof.context * theory)
    85   val add_thmss: string -> string ->
    85   val add_thmss: string -> string ->
    86     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    86     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    87     cterm list * Proof.context ->
    87     cterm list * Proof.context ->
    88     ((string * thm list) list * (string * thm list) list) * Proof.context
    88     ((string * thm list) list * (string * thm list) list) * Proof.context
       
    89   val add_abbrevs: string -> bool -> (bstring * term * mixfix) list ->
       
    90     cterm list * Proof.context -> Proof.context
    89 
    91 
    90   val theorem: string -> Method.text option -> (thm list list -> theory -> theory) ->
    92   val theorem: string -> Method.text option -> (thm list list -> theory -> theory) ->
    91     string * Attrib.src list -> Element.context element list -> Element.statement ->
    93     string * Attrib.src list -> Element.context element list -> Element.statement ->
    92     theory -> Proof.state
    94     theory -> Proof.state
    93   val theorem_i: string -> Method.text option -> (thm list list -> theory -> theory) ->
    95   val theorem_i: string -> Method.text option -> (thm list list -> theory -> theory) ->
   141        ([statement], axioms), where statement is the locale predicate applied
   143        ([statement], axioms), where statement is the locale predicate applied
   142        to the (assumed) locale parameters.  Axioms contains the projections
   144        to the (assumed) locale parameters.  Axioms contains the projections
   143        from the locale predicate to the normalised assumptions of the locale
   145        from the locale predicate to the normalised assumptions of the locale
   144        (cf. [1], normalisation of locale expressions.)
   146        (cf. [1], normalisation of locale expressions.)
   145     *)
   147     *)
   146   import: expr,                                       (*dynamic import*)
   148   import: expr,                                                     (*dynamic import*)
   147   elems: (Element.context_i * stamp) list,            (*static content*)
   149   elems: (Element.context_i * stamp) list,                          (*static content*)
   148   params: ((string * typ) * mixfix) list * string list,
   150   params: ((string * typ) * mixfix) list * string list,             (*all/local params*)
   149                                                       (*all/local params*)
   151   abbrevs: ((bool * (bstring * term * mixfix) list) * stamp) list,  (*abbreviations*)
   150   regs: ((string * string list) * (term * thm) list) list}
   152   regs: ((string * string list) * (term * thm) list) list}
   151     (* Registrations: indentifiers and witness theorems of locales interpreted
   153     (* Registrations: indentifiers and witness theorems of locales interpreted
   152        in the locale.
   154        in the locale.
   153     *)
   155     *)
   154 
   156 
   261 
   263 
   262   val empty = (NameSpace.empty, Symtab.empty, Symtab.empty);
   264   val empty = (NameSpace.empty, Symtab.empty, Symtab.empty);
   263   val copy = I;
   265   val copy = I;
   264   val extend = I;
   266   val extend = I;
   265 
   267 
   266   fun join_locs _ ({predicate, import, elems, params, regs}: locale,
   268   fun join_locs _ ({predicate, import, elems, params, abbrevs, regs}: locale,
   267       {elems = elems', regs = regs', ...}: locale) =
   269       {elems = elems', abbrevs = abbrevs', regs = regs', ...}: locale) =
   268     SOME {predicate = predicate, import = import,
   270     SOME
       
   271      {predicate = predicate,
       
   272       import = import,
   269       elems = gen_merge_lists (eq_snd (op =)) elems elems',
   273       elems = gen_merge_lists (eq_snd (op =)) elems elems',
   270       params = params,
   274       params = params,
       
   275       abbrevs = Library.merge (eq_snd (op =)) (abbrevs, abbrevs'),
   271       regs = merge_alists regs regs'};
   276       regs = merge_alists regs regs'};
   272   fun merge _ ((space1, locs1, regs1), (space2, locs2, regs2)) =
   277   fun merge _ ((space1, locs1, regs1), (space2, locs2, regs2)) =
   273     (NameSpace.merge (space1, space2), Symtab.join join_locs (locs1, locs2),
   278     (NameSpace.merge (space1, space2), Symtab.join join_locs (locs1, locs2),
   274      Symtab.join (K (SOME o Registrations.join)) (regs1, regs2));
   279      Symtab.join (K (SOME o Registrations.join)) (regs1, regs2));
   275 
   280 
   318     SOME loc => loc
   323     SOME loc => loc
   319   | NONE => error ("Unknown locale " ^ quote name));
   324   | NONE => error ("Unknown locale " ^ quote name));
   320 
   325 
   321 fun change_locale name f thy =
   326 fun change_locale name f thy =
   322   let
   327   let
   323     val {predicate, import, elems , params, regs} = the_locale thy name;
   328     val {predicate, import, elems , params, abbrevs, regs} = the_locale thy name;
   324     val (predicate', import', elems', params', regs') = f (predicate, import, elems, params, regs);
   329     val (predicate', import', elems', params', abbrevs', regs') =
       
   330       f (predicate, import, elems, params, abbrevs, regs);
   325   in
   331   in
   326     put_locale name {predicate = predicate', import = import', elems = elems',
   332     put_locale name {predicate = predicate', import = import', elems = elems',
   327       params = params', regs = regs'} thy
   333       params = params', abbrevs = abbrevs', regs = regs'} thy
   328   end;
   334   end;
   329 
   335 
   330 
   336 
   331 (* access registrations *)
   337 (* access registrations *)
   332 
   338 
   387        GlobalLocalesData.map (fn (space, locs, regs) => (space, locs, f regs))) I;
   393        GlobalLocalesData.map (fn (space, locs, regs) => (space, locs, f regs))) I;
   388 val put_local_registration =
   394 val put_local_registration =
   389      gen_put_registration LocalLocalesData.map ProofContext.theory_of;
   395      gen_put_registration LocalLocalesData.map ProofContext.theory_of;
   390 
   396 
   391 fun put_registration_in_locale name id =
   397 fun put_registration_in_locale name id =
   392   change_locale name (fn (predicate, import, elems, params, regs) =>
   398   change_locale name (fn (predicate, import, elems, params, abbrevs, regs) =>
   393     (predicate, import, elems, params, regs @ [(id, [])]));
   399     (predicate, import, elems, params, abbrevs, regs @ [(id, [])]));
   394 
   400 
   395 
   401 
   396 (* add witness theorem to registration in theory or context,
   402 (* add witness theorem to registration in theory or context,
   397    ignored if registration not present *)
   403    ignored if registration not present *)
   398 
   404 
   403   GlobalLocalesData.map (fn (space, locs, regs) => (space, locs, add_witness id thm regs));
   409   GlobalLocalesData.map (fn (space, locs, regs) => (space, locs, add_witness id thm regs));
   404 
   410 
   405 val add_local_witness = LocalLocalesData.map oo add_witness;
   411 val add_local_witness = LocalLocalesData.map oo add_witness;
   406 
   412 
   407 fun add_witness_in_locale name id thm =
   413 fun add_witness_in_locale name id thm =
   408   change_locale name (fn (predicate, import, elems, params, regs) =>
   414   change_locale name (fn (predicate, import, elems, params, abbrevs, regs) =>
   409     let
   415     let
   410       fun add (id', thms) =
   416       fun add (id', thms) =
   411         if id = id' then (id', thm :: thms) else (id', thms);
   417         if id = id' then (id', thm :: thms) else (id', thms);
   412     in (predicate, import, elems, params, map add regs) end);
   418     in (predicate, import, elems, params, abbrevs, map add regs) end);
   413 
   419 
   414 
   420 
   415 (* printing of registrations *)
   421 (* printing of registrations *)
   416 
   422 
   417 fun gen_print_registrations get_regs mk_ctxt msg show_wits loc thy_ctxt =
   423 fun gen_print_registrations get_regs mk_ctxt msg show_wits loc thy_ctxt =
   718           val {elems, ...} = the_locale thy name;
   724           val {elems, ...} = the_locale thy name;
   719           val ts = List.concat (map
   725           val ts = List.concat (map
   720             (fn (Assumes asms, _) => List.concat (map (map #1 o #2) asms)
   726             (fn (Assumes asms, _) => List.concat (map (map #1 o #2) asms)
   721               | _ => [])
   727               | _ => [])
   722             elems);
   728             elems);
   723           val (axs1, axs2) = splitAt (length ts, axioms);
   729           val (axs1, axs2) = chop (length ts) axioms;
   724         in (((name, parms), (all_ps, Assumed axs1)), axs2) end
   730         in (((name, parms), (all_ps, Assumed axs1)), axs2) end
   725       | axiomify all_ps (id, (_, Derived ths)) axioms =
   731       | axiomify all_ps (id, (_, Derived ths)) axioms =
   726           ((id, (all_ps, Derived ths)), axioms);
   732           ((id, (all_ps, Derived ths)), axioms);
   727 
   733 
   728     (* identifiers of an expression *)
   734     (* identifiers of an expression *)
   840       ((ctxt, mode), [])
   846       ((ctxt, mode), [])
   841   | activate_elem _ ((ctxt, Assumed axs), Assumes asms) =
   847   | activate_elem _ ((ctxt, Assumed axs), Assumes asms) =
   842       let
   848       let
   843         val asms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) asms;
   849         val asms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) asms;
   844         val ts = List.concat (map (map #1 o #2) asms');
   850         val ts = List.concat (map (map #1 o #2) asms');
   845         val (ps, qs) = splitAt (length ts, axs);
   851         val (ps, qs) = chop (length ts) axs;
   846         val (_, ctxt') =
   852         val (_, ctxt') =
   847           ctxt |> fold ProofContext.fix_frees ts
   853           ctxt |> fold ProofContext.fix_frees ts
   848           |> ProofContext.add_assms_i (axioms_export ps) asms';
   854           |> ProofContext.add_assms_i (axioms_export ps) asms';
   849       in ((ctxt', Assumed qs), []) end
   855       in ((ctxt', Assumed qs), []) end
   850   | activate_elem _ ((ctxt, Derived ths), Assumes asms) =
   856   | activate_elem _ ((ctxt, Derived ths), Assumes asms) =
  1169         (* CB: add type information from conclusion and external elements to context *)
  1175         (* CB: add type information from conclusion and external elements to context *)
  1170         val ctxt = fold ProofContext.declare_term (List.concat (map (map fst) all_propp)) ctxt;
  1176         val ctxt = fold ProofContext.declare_term (List.concat (map (map fst) all_propp)) ctxt;
  1171         (* CB: resolve schematic variables (patterns) in conclusion and external elements. *)
  1177         (* CB: resolve schematic variables (patterns) in conclusion and external elements. *)
  1172         val all_propp' = map2 (curry (op ~~))
  1178         val all_propp' = map2 (curry (op ~~))
  1173           (#1 (#2 (ProofContext.bind_propp_schematic_i (ctxt, all_propp)))) (map (map snd) all_propp);
  1179           (#1 (#2 (ProofContext.bind_propp_schematic_i (ctxt, all_propp)))) (map (map snd) all_propp);
  1174         val (concl, propp) = splitAt (length raw_concl, all_propp')
  1180         val (concl, propp) = chop (length raw_concl) all_propp';
  1175       in (propp, (ctxt, concl)) end
  1181       in (propp, (ctxt, concl)) end
  1176 
  1182 
  1177     val (proppss, (ctxt, concl)) =
  1183     val (proppss, (ctxt, concl)) =
  1178       (fold_burrow o fold_burrow) prep_prop raw_proppss (raw_ctxt, raw_concl);
  1184       (fold_burrow o fold_burrow) prep_prop raw_proppss (raw_ctxt, raw_concl);
  1179 
  1185 
  1304     val all_elemss' = map (fn (((_, ps), _), (((n, ps'), mode), elems)) =>
  1310     val all_elemss' = map (fn (((_, ps), _), (((n, ps'), mode), elems)) =>
  1305         (((n, map (fn p => (p, (the o AList.lookup (op =) ps') p)) ps), mode), elems))
  1311         (((n, map (fn p => (p, (the o AList.lookup (op =) ps') p)) ps), mode), elems))
  1306       (ids ~~ all_elemss);
  1312       (ids ~~ all_elemss);
  1307 
  1313 
  1308     (* CB: all_elemss and parms contain the correct parameter types *)
  1314     (* CB: all_elemss and parms contain the correct parameter types *)
  1309     val (ps,qs) = splitAt(length raw_import_elemss, all_elemss')
  1315     val (ps, qs) = chop (length raw_import_elemss) all_elemss';
  1310     val (import_ctxt, (import_elemss, _)) =
  1316     val (import_ctxt, (import_elemss, _)) =
  1311       activate_facts prep_facts (context, ps);
  1317       activate_facts prep_facts (context, ps);
  1312 
  1318 
  1313     val (ctxt, (elemss, _)) =
  1319     val (ctxt, (elemss, _)) =
  1314       activate_facts prep_facts (import_ctxt, qs);
  1320       activate_facts prep_facts (import_ctxt, qs);
  1344 fun cert_context import body ctxt = #1 (cert_ctxt true [] import (map Elem body) [] ctxt);
  1350 fun cert_context import body ctxt = #1 (cert_ctxt true [] import (map Elem body) [] ctxt);
  1345 
  1351 
  1346 val read_context_statement = prep_statement intern read_ctxt;
  1352 val read_context_statement = prep_statement intern read_ctxt;
  1347 val cert_context_statement = prep_statement (K I) cert_ctxt;
  1353 val cert_context_statement = prep_statement (K I) cert_ctxt;
  1348 
  1354 
  1349 fun init loc thy = #2 (cert_context_statement (SOME loc) [] [] (ProofContext.init thy));
       
  1350 
       
  1351 end;
  1355 end;
  1352 
  1356 
  1353 
  1357 
  1354 (* print locale *)
  1358 (* print locale *)
  1355 
  1359 
  1368 
  1372 
  1369 
  1373 
  1370 
  1374 
  1371 (** store results **)
  1375 (** store results **)
  1372 
  1376 
  1373 (* accesses of interpreted theorems *)
  1377 (* naming of interpreted theorems *)
  1374 
  1378 
  1375 local
  1379 fun global_note_prefix_i kind prfx args thy =
  1376 
       
  1377 (*fully qualified name in theory is T.p.r.n where
       
  1378   T: theory name, p: interpretation prefix, r: renaming prefix, n: name*)  (* FIXME not necessarily so *)
       
  1379 fun global_accesses _ [] = []
       
  1380   | global_accesses "" [T, n] = [[T, n], [n]]
       
  1381   | global_accesses "" [T, r, n] = [[T, r, n], [T, n], [r, n], [n]]
       
  1382   | global_accesses _ [T, p, n] = [[T, p, n], [p, n]]
       
  1383   | global_accesses _ [T, p, r, n] = [[T, p, r, n], [T, p, n], [p, r, n], [p, n]]
       
  1384   | global_accesses _ names = error ("Bad name declaration " ^ quote (NameSpace.pack names));
       
  1385 
       
  1386 (*fully qualified name in context is p.r.n where
       
  1387   p: interpretation prefix, r: renaming prefix, n: name*)  (* FIXME not necessarily so *)
       
  1388 fun local_accesses _ [] = []
       
  1389   | local_accesses "" [n] = [[n]]
       
  1390   | local_accesses "" [r, n] = [[r, n], [n]]
       
  1391   | local_accesses _ [p, n] = [[p, n]]
       
  1392   | local_accesses _ [p, r, n] = [[p, r, n], [p, n]]
       
  1393   | local_accesses _ names = error ("Bad name declaration " ^ quote (NameSpace.pack names));
       
  1394 
       
  1395 in
       
  1396 
       
  1397 fun global_note_accesses_i kind prfx args thy =
       
  1398   thy
  1380   thy
  1399   |> Theory.qualified_names
  1381   |> Theory.qualified_force_prefix prfx
  1400   |> Theory.custom_accesses (global_accesses prfx)
       
  1401   |> PureThy.note_thmss_i kind args
  1382   |> PureThy.note_thmss_i kind args
  1402   ||> Theory.restore_naming thy;
  1383   ||> Theory.restore_naming thy;
  1403 
  1384 
  1404 fun local_note_accesses_i prfx args ctxt =
  1385 fun local_note_prefix_i prfx args ctxt =
  1405   ctxt
  1386   ctxt
  1406   |> ProofContext.qualified_names
  1387   |> ProofContext.qualified_force_prefix prfx
  1407   |> ProofContext.custom_accesses (local_accesses prfx)
       
  1408   |> ProofContext.note_thmss_i args |> swap
  1388   |> ProofContext.note_thmss_i args |> swap
  1409   |>> ProofContext.restore_naming ctxt;
  1389   |>> ProofContext.restore_naming ctxt;
  1410 
       
  1411 end;
       
  1412 
  1390 
  1413 
  1391 
  1414 (* collect witness theorems for global registration;
  1392 (* collect witness theorems for global registration;
  1415    requires parameters and flattened list of (assumed!) identifiers
  1393    requires parameters and flattened list of (assumed!) identifiers
  1416    instead of recomputing it from the target *)
  1394    instead of recomputing it from the target *)
  1454         val args' = args |> map (fn ((n, atts), [(ths, [])]) =>
  1432         val args' = args |> map (fn ((n, atts), [(ths, [])]) =>
  1455             ((NameSpace.qualified prfx n,
  1433             ((NameSpace.qualified prfx n,
  1456               map (Attrib.attribute_i thy) (map inst_atts atts @ atts2)),
  1434               map (Attrib.attribute_i thy) (map inst_atts atts @ atts2)),
  1457              [(map (Drule.standard o satisfy_protected prems o
  1435              [(map (Drule.standard o satisfy_protected prems o
  1458             Element.inst_thm thy insts) ths, [])]));
  1436             Element.inst_thm thy insts) ths, [])]));
  1459       in global_note_accesses_i kind prfx args' thy |> snd end;
  1437       in global_note_prefix_i kind prfx args' thy |> snd end;
  1460   in fold activate regs thy end;
  1438   in fold activate regs thy end;
       
  1439 
       
  1440 
       
  1441 (* abbreviations *)
       
  1442 
       
  1443 fun add_abbrevs loc revert decls =
       
  1444   snd #> ProofContext.add_abbrevs_i revert decls #>
       
  1445   ProofContext.map_theory (change_locale loc
       
  1446     (fn (predicate, import, elems, params, abbrevs, regs) =>
       
  1447       (predicate, import, elems, params, ((revert, decls), stamp ()) :: abbrevs, regs)));
       
  1448 
       
  1449 fun init_abbrevs loc ctxt =
       
  1450   fold_rev (uncurry ProofContext.add_abbrevs_i o fst)
       
  1451     (#abbrevs (the_locale (ProofContext.theory_of ctxt) loc)) ctxt;
       
  1452 
       
  1453 fun init loc =
       
  1454   ProofContext.init
       
  1455   #> init_abbrevs loc
       
  1456   #> (#2 o cert_context_statement (SOME loc) [] []);
  1461 
  1457 
  1462 
  1458 
  1463 (* theory/locale results *)
  1459 (* theory/locale results *)
  1464 
  1460 
  1465 fun theory_results kind prefix results (view, ctxt) thy =
  1461 fun theory_results kind prefix results (view, ctxt) thy =
  1475   let
  1471   let
  1476     val (ctxt', ([(_, [Notes args'])], facts)) =
  1472     val (ctxt', ([(_, [Notes args'])], facts)) =
  1477       activate_facts prep_facts (ctxt, [((("", []), Assumed []), [Ext (Notes args)])]);
  1473       activate_facts prep_facts (ctxt, [((("", []), Assumed []), [Ext (Notes args)])]);
  1478     val (facts', thy') =
  1474     val (facts', thy') =
  1479       thy
  1475       thy
  1480       |> change_locale loc (fn (predicate, import, elems, params, regs) =>
  1476       |> change_locale loc (fn (predicate, import, elems, params, abbrevs, regs) =>
  1481         (predicate, import, elems @ [(Notes args', stamp ())], params, regs))
  1477         (predicate, import, elems @ [(Notes args', stamp ())], params, abbrevs, regs))
  1482       |> note_thmss_registrations kind loc args'
  1478       |> note_thmss_registrations kind loc args'
  1483       |> global_results (map (#1 o #1) args' ~~ map #2 facts) (view, ctxt);
  1479       |> global_results (map (#1 o #1) args' ~~ map #2 facts) (view, ctxt);
  1484   in ((facts, facts'), (ProofContext.transfer thy' ctxt', thy')) end;
  1480   in ((facts, facts'), (ProofContext.transfer thy' ctxt', thy')) end;
  1485 
  1481 
  1486 fun gen_note prep_locale prep_facts kind raw_loc args thy =
  1482 fun gen_note prep_locale prep_facts kind raw_loc args thy =
  1587   in ((statement, intro, axioms), defs_thy) end;
  1583   in ((statement, intro, axioms), defs_thy) end;
  1588 
  1584 
  1589 fun assumes_to_notes (Assumes asms) axms =
  1585 fun assumes_to_notes (Assumes asms) axms =
  1590        axms
  1586        axms
  1591        |> fold_map (fn (a, spec) => fn axs =>
  1587        |> fold_map (fn (a, spec) => fn axs =>
  1592             let val (ps, qs) = splitAt (length spec, axs)
  1588             let val (ps, qs) = chop (length spec) axs
  1593             in ((a, [(ps, [])]), qs) end
  1589             in ((a, [(ps, [])]), qs) end) asms
  1594           ) asms
  1590        |-> (pair o Notes)
  1595        |-> (fn asms' => pair (Notes asms'))
       
  1596   | assumes_to_notes e axms = (e, axms);
  1591   | assumes_to_notes e axms = (e, axms);
  1597 
  1592 
  1598 (* CB: changes only "new" elems, these have identifier ("", _). *)
  1593 (* CB: changes only "new" elems, these have identifier ("", _). *)
  1599 
  1594 
  1600 fun change_elemss axioms elemss =
  1595 fun change_elemss axioms elemss =
  1679 
  1674 
  1680     fun axiomify axioms elemss =
  1675     fun axiomify axioms elemss =
  1681       (axioms, elemss) |> foldl_map (fn (axs, (id, elems)) => let
  1676       (axioms, elemss) |> foldl_map (fn (axs, (id, elems)) => let
  1682                    val ts = List.concat (List.mapPartial (fn (Assumes asms) =>
  1677                    val ts = List.concat (List.mapPartial (fn (Assumes asms) =>
  1683                      SOME (List.concat (map (map #1 o #2) asms)) | _ => NONE) elems);
  1678                      SOME (List.concat (map (map #1 o #2) asms)) | _ => NONE) elems);
  1684                    val (axs1, axs2) = splitAt (length ts, axs);
  1679                    val (axs1, axs2) = chop (length ts) axs;
  1685                  in (axs2, ((id, Assumed axs1), elems)) end)
  1680                  in (axs2, ((id, Assumed axs1), elems)) end)
  1686         |> snd;
  1681         |> snd;
  1687     val pred_ctxt = ProofContext.init pred_thy;
  1682     val pred_ctxt = ProofContext.init pred_thy;
  1688     val (ctxt, (_, facts)) = activate_facts (K I)
  1683     val (ctxt, (_, facts)) = activate_facts (K I)
  1689       (pred_ctxt, axiomify predicate_axioms elemss');
  1684       (pred_ctxt, axiomify predicate_axioms elemss');
  1694     pred_thy
  1689     pred_thy
  1695     |> PureThy.note_thmss_qualified "" bname facts' |> snd
  1690     |> PureThy.note_thmss_qualified "" bname facts' |> snd
  1696     |> declare_locale name
  1691     |> declare_locale name
  1697     |> put_locale name {predicate = predicate, import = import,
  1692     |> put_locale name {predicate = predicate, import = import,
  1698         elems = map (fn e => (e, stamp ())) elems',
  1693         elems = map (fn e => (e, stamp ())) elems',
  1699         params = (params_of elemss' |>
  1694         params = (params_of elemss' |> map (fn (x, SOME T) => ((x, T), the (Symtab.lookup syn x))),
  1700           map (fn (x, SOME T) => ((x, T), the (Symtab.lookup syn x))), map #1 (params_of body_elemss)),
  1695           map #1 (params_of body_elemss)),
       
  1696         abbrevs = [],
  1701         regs = []}
  1697         regs = []}
  1702     |> pair (body_ctxt)
  1698     |> pair (body_ctxt)
  1703   end;
  1699   end;
  1704 
  1700 
  1705 in
  1701 in
  1707 val add_locale = gen_add_locale read_context intern_expr;
  1703 val add_locale = gen_add_locale read_context intern_expr;
  1708 val add_locale_i = gen_add_locale cert_context (K I);
  1704 val add_locale_i = gen_add_locale cert_context (K I);
  1709 
  1705 
  1710 end;
  1706 end;
  1711 
  1707 
  1712 val _ = Context.add_setup (
  1708 val _ = Context.add_setup
  1713   add_locale_i true "var" empty [Fixes [(Syntax.internal "x", NONE, NoSyn)]]
  1709  (add_locale_i true "var" empty [Fixes [(Syntax.internal "x", NONE, NoSyn)]] #> snd #>
  1714   #> snd
  1710   add_locale_i true "struct" empty [Fixes [(Syntax.internal "S", NONE, Structure)]] #> snd);
  1715   #> add_locale_i true "struct" empty [Fixes [(Syntax.internal "S", NONE, Structure)]]
       
  1716   #> snd
       
  1717 )
       
  1718 
  1711 
  1719 
  1712 
  1720 
  1713 
  1721 (** locale goals **)
  1714 (** locale goals **)
  1722 
  1715 
  1757     val loc_attss = map (map (prep_src thy) o snd o fst) concl;
  1750     val loc_attss = map (map (prep_src thy) o snd o fst) concl;
  1758     val target = if no_target then NONE else SOME (extern thy loc);
  1751     val target = if no_target then NONE else SOME (extern thy loc);
  1759     val elems = map (prep_elem thy) (raw_elems @ concl_elems);
  1752     val elems = map (prep_elem thy) (raw_elems @ concl_elems);
  1760     val names = map (fst o fst) concl;
  1753     val names = map (fst o fst) concl;
  1761 
  1754 
  1762     val thy_ctxt = ProofContext.init thy;
  1755     val thy_ctxt = init_abbrevs loc (ProofContext.init thy);
  1763     val (_, (loc_view, loc_ctxt), (elems_view, elems_ctxt), propp) =
  1756     val (_, (loc_view, loc_ctxt), (elems_view, elems_ctxt), propp) =
  1764       prep_stmt (SOME raw_loc) elems (map snd concl) thy_ctxt;
  1757       prep_stmt (SOME raw_loc) elems (map snd concl) thy_ctxt;
  1765     val elems_ctxt' = elems_ctxt
  1758     val elems_ctxt' = elems_ctxt
  1766       |> ProofContext.add_view loc_ctxt elems_view
  1759       |> ProofContext.add_view loc_ctxt elems_view
  1767       |> ProofContext.add_view thy_ctxt loc_view;
  1760       |> ProofContext.add_view thy_ctxt loc_view;
  1883     end;
  1876     end;
  1884 
  1877 
  1885 fun global_activate_facts_elemss x = gen_activate_facts_elemss
  1878 fun global_activate_facts_elemss x = gen_activate_facts_elemss
  1886       (fn thy => fn (name, ps) =>
  1879       (fn thy => fn (name, ps) =>
  1887         get_global_registration thy (name, map Logic.varify ps))
  1880         get_global_registration thy (name, map Logic.varify ps))
  1888       (swap ooo global_note_accesses_i "")
  1881       (swap ooo global_note_prefix_i "")
  1889       Attrib.attribute_i Drule.standard
  1882       Attrib.attribute_i Drule.standard
  1890       (fn (name, ps) => put_global_registration (name, map Logic.varify ps))
  1883       (fn (name, ps) => put_global_registration (name, map Logic.varify ps))
  1891       (fn (n, ps) => fn (t, th) =>
  1884       (fn (n, ps) => fn (t, th) =>
  1892          add_global_witness (n, map Logic.varify ps)
  1885          add_global_witness (n, map Logic.varify ps)
  1893          (Logic.unvarify t, Drule.freeze_all th)) x;
  1886          (Logic.unvarify t, Drule.freeze_all th)) x;
  1894 
  1887 
  1895 fun local_activate_facts_elemss x = gen_activate_facts_elemss
  1888 fun local_activate_facts_elemss x = gen_activate_facts_elemss
  1896       get_local_registration
  1889       get_local_registration
  1897       local_note_accesses_i
  1890       local_note_prefix_i
  1898       (Attrib.attribute_i o ProofContext.theory_of) I
  1891       (Attrib.attribute_i o ProofContext.theory_of) I
  1899       put_local_registration
  1892       put_local_registration
  1900       add_local_witness x;
  1893       add_local_witness x;
  1901 
  1894 
  1902 fun gen_prep_registration mk_ctxt is_local read_terms test_reg activate
  1895 fun gen_prep_registration mk_ctxt is_local read_terms test_reg activate
  2008     val ((raw_target_ids, target_syn), _) = flatten (ctxt, I)
  2001     val ((raw_target_ids, target_syn), _) = flatten (ctxt, I)
  2009         (([], Symtab.empty), Expr (Locale target));
  2002         (([], Symtab.empty), Expr (Locale target));
  2010     val fixed = the_locale thy target |> #params |> #1 |> map #1;
  2003     val fixed = the_locale thy target |> #params |> #1 |> map #1;
  2011     val ((all_ids, syn), raw_elemss) = flatten (ctxt, intern_expr thy)
  2004     val ((all_ids, syn), raw_elemss) = flatten (ctxt, intern_expr thy)
  2012         ((raw_target_ids, target_syn), Expr expr);
  2005         ((raw_target_ids, target_syn), Expr expr);
  2013     val (target_ids, ids) = splitAt (length raw_target_ids, all_ids);
  2006     val (target_ids, ids) = chop (length raw_target_ids) all_ids;
  2014     val ((parms, elemss, _), _) = read_elemss false ctxt fixed raw_elemss [];
  2007     val ((parms, elemss, _), _) = read_elemss false ctxt fixed raw_elemss [];
  2015 
  2008 
  2016     (** compute proof obligations **)
  2009     (** compute proof obligations **)
  2017 
  2010 
  2018     (* restore "small" ids, with mode *)
  2011     (* restore "small" ids, with mode *)
  2085                       |> map (apfst (apsnd (fn a => a @ map (Attrib.attribute thy) atts2)))
  2078                       |> map (apfst (apsnd (fn a => a @ map (Attrib.attribute thy) atts2)))
  2086                       |> map (apsnd (map (apfst (map (disch o Element.inst_thm thy insts o satisfy)))))
  2079                       |> map (apsnd (map (apfst (map (disch o Element.inst_thm thy insts o satisfy)))))
  2087                       |> map (apfst (apfst (NameSpace.qualified prfx)))
  2080                       |> map (apfst (apfst (NameSpace.qualified prfx)))
  2088                 in
  2081                 in
  2089                   thy
  2082                   thy
  2090                   |> global_note_accesses_i "" prfx facts'
  2083                   |> global_note_prefix_i "" prfx facts'
  2091                   |> snd
  2084                   |> snd
  2092                 end
  2085                 end
  2093               | activate_elem _ thy = thy;
  2086               | activate_elem _ thy = thy;
  2094 
  2087 
  2095             fun activate_elems (_, elems) thy = fold activate_elem elems thy;
  2088             fun activate_elems (_, elems) thy = fold activate_elem elems thy;