src/Pure/Isar/locale.ML
changeset 28862 53f13f763d4f
parent 28822 7ca11ecbc4fb
child 28865 194e8f3439fe
equal deleted inserted replaced
28861:f53abb0733ee 28862:53f13f763d4f
   134 fun merge_alists eq xs = merge_lists (eq_fst eq) xs;
   134 fun merge_alists eq xs = merge_lists (eq_fst eq) xs;
   135 
   135 
   136 
   136 
   137 (* auxiliary: noting with structured name bindings *)
   137 (* auxiliary: noting with structured name bindings *)
   138 
   138 
   139 fun global_note_prefix kind ((binding, atts), facts_atts_s) thy =
   139 fun global_note_prefix kind ((b, atts), facts_atts_s) thy =
   140   (*FIXME belongs to theory_target.ML ?*)
   140   (*FIXME belongs to theory_target.ML ?*)
   141   thy
   141   thy
   142   |> Sign.qualified_names
   142   |> Sign.qualified_names
   143   |> Sign.name_decl (fn (bname, (atts, facts_atts_s)) =>
   143   |> Sign.name_decl (fn (bname, (atts, facts_atts_s)) =>
   144     yield_singleton (PureThy.note_thmss kind) ((Name.binding bname, atts), facts_atts_s))
   144     yield_singleton (PureThy.note_thmss kind) ((Name.binding bname, atts), facts_atts_s))
   145       (binding, (atts, facts_atts_s))
   145       (b, (atts, facts_atts_s))
   146   |>> snd
   146   |>> snd
   147   ||> Sign.restore_naming thy;
   147   ||> Sign.restore_naming thy;
   148 
   148 
   149 fun local_note_prefix kind ((binding, atts), facts_atts_s) ctxt =
   149 fun local_note_prefix kind ((b, atts), facts_atts_s) ctxt =
   150   (*FIXME belongs to theory_target.ML ?*)
   150   (*FIXME belongs to theory_target.ML ?*)
   151   ctxt
   151   ctxt
   152   |> ProofContext.qualified_names
   152   |> ProofContext.qualified_names
   153   |> ProofContext.name_decl (fn (bname, (atts, facts_atts_s)) =>
   153   |> ProofContext.name_decl (fn (bname, (atts, facts_atts_s)) =>
   154     yield_singleton (ProofContext.note_thmss_i kind) ((Name.binding bname, atts), facts_atts_s))
   154     yield_singleton (ProofContext.note_thmss_i kind) ((Name.binding bname, atts), facts_atts_s))
   155       (binding, (atts, facts_atts_s))
   155       (b, (atts, facts_atts_s))
   156   |>> snd
   156   |>> snd
   157   ||> ProofContext.restore_naming ctxt;
   157   ||> ProofContext.restore_naming ctxt;
   158 
   158 
   159 
   159 
   160 (** locale elements and expressions **)
   160 (** locale elements and expressions **)
   659   distinct (eq_fst (op = : string * string -> bool)) (maps (snd o fst) elemss);
   659   distinct (eq_fst (op = : string * string -> bool)) (maps (snd o fst) elemss);
   660 
   660 
   661 fun params_of' elemss =
   661 fun params_of' elemss =
   662   distinct (eq_fst (op = : string * string -> bool)) (maps (snd o fst o fst) elemss);
   662   distinct (eq_fst (op = : string * string -> bool)) (maps (snd o fst o fst) elemss);
   663 
   663 
   664 fun param_prefix locale_name params = (NameSpace.base locale_name, space_implode "_" params);
   664 fun param_prefix locale_name params = (NameSpace.base locale_name ^ "_locale", space_implode "_" params);
   665 
   665 
   666 
   666 
   667 (* CB: param_types has the following type:
   667 (* CB: param_types has the following type:
   668   ('a * 'b option) list -> ('a * 'b) list *)
   668   ('a * 'b option) list -> ('a * 'b) list *)
   669 fun param_types ps = map_filter (fn (_, NONE) => NONE | (x, SOME T) => SOME (x, T)) ps;
   669 fun param_types ps = map_filter (fn (_, NONE) => NONE | (x, SOME T) => SOME (x, T)) ps;
   952         fun lookup_syn x = (case Symtab.lookup syn x of SOME Structure => NONE | opt => opt);
   952         fun lookup_syn x = (case Symtab.lookup syn x of SOME Structure => NONE | opt => opt);
   953         val locale_params' = map (fn p => (p, Symtab.lookup tenv p |> the)) locale_params;
   953         val locale_params' = map (fn p => (p, Symtab.lookup tenv p |> the)) locale_params;
   954         val mode' = map_mode (map (Element.map_witness (inst_wit all_params))) mode;
   954         val mode' = map_mode (map (Element.map_witness (inst_wit all_params))) mode;
   955         val ren = map fst ps ~~ map (fn p => (p, lookup_syn p)) params;
   955         val ren = map fst ps ~~ map (fn p => (p, lookup_syn p)) params;
   956         val [env] = unify_parms ctxt all_params [map (apfst (Element.rename ren) o apsnd SOME) ps];
   956         val [env] = unify_parms ctxt all_params [map (apfst (Element.rename ren) o apsnd SOME) ps];
   957         val (locale_name, pprfx) = param_prefix name params;
   957         val (lprfx, pprfx) = param_prefix name params;
   958         val add_prefices = pprfx <> "" ? Name.add_prefix false pprfx
   958         val add_prefices = pprfx <> "" ? Name.add_prefix false pprfx
   959           #> Name.add_prefix false locale_name;
   959           #> Name.add_prefix false lprfx;
   960         val elem_morphism =
   960         val elem_morphism =
   961           Element.rename_morphism ren $>
   961           Element.rename_morphism ren $>
   962           Morphism.name_morphism add_prefices $>
   962           Morphism.name_morphism add_prefices $>
   963           Element.instT_morphism thy env;
   963           Element.instT_morphism thy env;
   964         val elems' = map (Element.morph_ctxt elem_morphism) elems;
   964         val elems' = map (Element.morph_ctxt elem_morphism) elems;
  1255             (a, (close_frees (#2 (LocalDefs.cert_def ctxt t)), no_binds ps))))
  1255             (a, (close_frees (#2 (LocalDefs.cert_def ctxt t)), no_binds ps))))
  1256         | e => e)
  1256         | e => e)
  1257       end;
  1257       end;
  1258 
  1258 
  1259 
  1259 
  1260 fun finish_ext_elem parms _ (Fixes fixes, _) = Fixes (map (fn (binding, _, mx) =>
  1260 fun finish_ext_elem parms _ (Fixes fixes, _) = Fixes (map (fn (b, _, mx) =>
  1261       let val x = Name.name_of binding
  1261       let val x = Name.name_of b
  1262       in (binding, AList.lookup (op =) parms x, mx) end) fixes)
  1262       in (b, AList.lookup (op =) parms x, mx) end) fixes)
  1263   | finish_ext_elem parms _ (Constrains _, _) = Constrains []
  1263   | finish_ext_elem parms _ (Constrains _, _) = Constrains []
  1264   | finish_ext_elem _ close (Assumes asms, propp) =
  1264   | finish_ext_elem _ close (Assumes asms, propp) =
  1265       close (Assumes (map #1 asms ~~ propp))
  1265       close (Assumes (map #1 asms ~~ propp))
  1266   | finish_ext_elem _ close (Defines defs, propp) =
  1266   | finish_ext_elem _ close (Defines defs, propp) =
  1267       close (Defines (map #1 defs ~~ map (fn [(t, ps)] => (t, ps)) propp))
  1267       close (Defines (map #1 defs ~~ map (fn [(t, ps)] => (t, ps)) propp))
  1638   in ((tinst, inst), wits, eqns) end;
  1638   in ((tinst, inst), wits, eqns) end;
  1639 
  1639 
  1640 
  1640 
  1641 (* compute and apply morphism *)
  1641 (* compute and apply morphism *)
  1642 
  1642 
  1643 fun name_morph phi_name (locale_name, pprfx) binding =
  1643 fun name_morph phi_name (lprfx, pprfx) b =
  1644   binding
  1644   b
  1645   |> (if not (Name.is_nothing binding) andalso pprfx <> ""
  1645   |> (if not (Name.is_nothing b) andalso pprfx <> ""
  1646         then Name.add_prefix false pprfx else I)
  1646         then Name.add_prefix false pprfx else I)
  1647   |> (if not (Name.is_nothing binding)
  1647   |> (if not (Name.is_nothing b)
  1648         then Name.add_prefix false (locale_name ^ "_locale") else I)
  1648         then Name.add_prefix false lprfx else I)
  1649   |> phi_name;
  1649   |> phi_name;
  1650 
  1650 
  1651 fun inst_morph thy phi_name param_prfx insts prems eqns export =
  1651 fun inst_morph thy phi_name param_prfx insts prems eqns export =
  1652   let
  1652   let
  1653     (* standardise export morphism *)
  1653     (* standardise export morphism *)
  2453       "interpret" NONE after_qed' (map (pair (Name.no_binding, [])) (prep_propp propss))
  2453       "interpret" NONE after_qed' (map (pair (Name.no_binding, [])) (prep_propp propss))
  2454     |> Element.refine_witness |> Seq.hd
  2454     |> Element.refine_witness |> Seq.hd
  2455     |> pair morphs
  2455     |> pair morphs
  2456   end;
  2456   end;
  2457 
  2457 
  2458 fun standard_name_morph interp_prfx binding =
  2458 fun standard_name_morph interp_prfx b =
  2459   if Name.is_nothing binding then binding
  2459   if Name.is_nothing b then b
  2460   else Name.map_prefix (fn ((locale_name, _) :: pprfx) =>
  2460   else Name.map_prefix (fn ((lprfx, _) :: pprfx) =>
  2461     fold (Name.add_prefix false o fst) pprfx
  2461     fold (Name.add_prefix false o fst) pprfx
  2462     #> interp_prfx <> "" ? Name.add_prefix true interp_prfx
  2462     #> interp_prfx <> "" ? Name.add_prefix true interp_prfx
  2463     #> Name.add_prefix false locale_name
  2463     #> Name.add_prefix false lprfx
  2464   ) binding;
  2464   ) b;
  2465 
  2465 
  2466 in
  2466 in
  2467 
  2467 
  2468 val interpretation = gen_interpretation prep_global_registration;
  2468 val interpretation = gen_interpretation prep_global_registration;
  2469 fun interpretation_cmd interp_prfx = snd ooo gen_interpretation prep_global_registration_cmd
  2469 fun interpretation_cmd interp_prfx = snd ooo gen_interpretation prep_global_registration_cmd