src/Pure/Isar/named_target.ML
author haftmann
Thu, 12 Aug 2010 13:42:13 +0200
changeset 38391 ba1cc1815ce1
parent 38389 d7d915bae307
child 38392 8a3ca8b96b23
permissions -rw-r--r--
named target is optional; explicit Name_Target.reinit

(*  Title:      Pure/Isar/theory_target.ML
    Author:     Makarius
    Author:     Florian Haftmann, TU Muenchen

Targets for theory, locale and class.
*)

signature NAMED_TARGET =
sig
  val init: string -> theory -> local_theory
  val theory_init: theory -> local_theory
  val reinit: local_theory -> local_theory -> local_theory
  val context_cmd: xstring -> theory -> local_theory
  val peek: local_theory -> {target: string, is_locale: bool, is_class: bool} option
end;

structure Named_Target: NAMED_TARGET =
struct

(* context data *)

datatype target = Target of {target: string, is_locale: bool, is_class: bool};

fun make_target target is_locale is_class =
  Target {target = target, is_locale = is_locale, is_class = is_class};

val global_target = Target {target = "", is_locale = false, is_class = false};

fun named_target _ "" = global_target
  | named_target thy locale =
      if Locale.defined thy locale
      then Target {target = locale, is_locale = true, is_class = Class.is_class thy locale}
      else error ("No such locale: " ^ quote locale);

structure Data = Proof_Data
(
  type T = target option;
  fun init _ = NONE;
);

val peek = Option.map (fn Target args => args) o Data.get;


(* generic declarations *)

fun locale_declaration locale { syntax, pervasive } decl lthy =
  let
    val add = if syntax then Locale.add_syntax_declaration else Locale.add_declaration;
    val locale_decl = Morphism.transform (Local_Theory.target_morphism lthy) decl;
  in
    lthy
    |> pervasive ? Generic_Target.theory_declaration decl
    |> Local_Theory.target (add locale locale_decl)
  end;

fun target_declaration (Target {target, ...}) { syntax, pervasive } =
  if target = "" then Generic_Target.theory_declaration
  else locale_declaration target { syntax = syntax, pervasive = pervasive };


(* consts in locales *)

fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) ((b, mx), rhs) phi =
  let
    val b' = Morphism.binding phi b;
    val rhs' = Morphism.term phi rhs;
    val arg = (b', Term.close_schematic_term rhs');
    val same_shape = Term.aconv_untyped (rhs, rhs');
    (* FIXME workaround based on educated guess *)
    val prefix' = Binding.prefix_of b';
    val is_canonical_class = is_class andalso
      (Binding.eq_name (b, b')
        andalso not (null prefix')
        andalso List.last prefix' = (Class.class_prefix target, false)
      orelse same_shape);
  in
    not is_canonical_class ?
      (Context.mapping_result
        (Sign.add_abbrev Print_Mode.internal arg)
        (ProofContext.add_abbrev Print_Mode.internal arg)
      #-> (fn (lhs' as Const (d, _), _) =>
          same_shape ?
            (Context.mapping
              (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
             Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
  end;

fun locale_const_declaration (ta as Target {target, ...}) prmode arg =
  locale_declaration target { syntax = true, pervasive = false } (locale_const ta prmode arg);

fun class_target (Target {target, ...}) f =
  Local_Theory.raw_theory f #>
  Local_Theory.target (Class.refresh_syntax target);


(* define *)

fun locale_foundation ta (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
  Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
  #-> (fn (lhs, def) => locale_const_declaration ta Syntax.mode_default ((b, mx), lhs)
    #> pair (lhs, def))

fun class_foundation (ta as Target {target, ...})
    (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
  Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
  #-> (fn (lhs, def) => locale_const_declaration ta Syntax.mode_default ((b, NoSyn), lhs)
    #> class_target ta (Class.declare target ((b, mx), (type_params, lhs)))
    #> pair (lhs, def))

fun target_foundation (ta as Target {target, is_locale, is_class, ...}) =
  if is_class then class_foundation ta
  else if is_locale then locale_foundation ta
  else Generic_Target.theory_foundation;


(* notes *)

fun locale_notes locale kind global_facts local_facts lthy = 
  let
    val global_facts' = Attrib.map_facts (K I) global_facts;
    val local_facts' = Element.facts_map
      (Element.morph_ctxt (Local_Theory.target_morphism lthy)) local_facts;
  in
    lthy
    |> Local_Theory.theory (PureThy.note_thmss kind global_facts' #> snd)
    |> Local_Theory.target (Locale.add_thmss locale kind local_facts')
  end

fun target_notes (ta as Target {target, is_locale, ...}) =
  if is_locale then locale_notes target
    else fn kind => fn global_facts => fn _ => Generic_Target.theory_notes kind global_facts;


(* abbrev *)

fun locale_abbrev ta prmode ((b, mx), t) xs = Local_Theory.theory_result
  (Sign.add_abbrev Print_Mode.internal (b, t)) #->
    (fn (lhs, _) => locale_const_declaration ta prmode
      ((b, mx), Term.list_comb (Logic.unvarify_global lhs, xs)));

fun target_abbrev (ta as Target {target, is_locale, is_class, ...})
    prmode (b, mx) (global_rhs, t') xs lthy =
  if is_locale
    then lthy
      |> locale_abbrev ta prmode ((b, if is_class then NoSyn else mx), global_rhs) xs
      |> is_class ? class_target ta (Class.abbrev target prmode ((b, mx), t'))
    else lthy
      |> Generic_Target.theory_abbrev prmode ((b, mx), global_rhs);


(* pretty *)

fun pretty_thy ctxt target is_class =
  let
    val thy = ProofContext.theory_of ctxt;
    val target_name =
      [Pretty.command (if is_class then "class" else "locale"),
        Pretty.str (" " ^ Locale.extern thy target)];
    val fixes = map (fn (x, T) => (Binding.name x, SOME T, NoSyn))
      (#1 (ProofContext.inferred_fixes ctxt));
    val assumes = map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])]))
      (Assumption.all_assms_of ctxt);
    val elems =
      (if null fixes then [] else [Element.Fixes fixes]) @
      (if null assumes then [] else [Element.Assumes assumes]);
  in
    if target = "" then []
    else if null elems then [Pretty.block target_name]
    else [Pretty.block (Pretty.fbreaks (Pretty.block (target_name @ [Pretty.str " ="]) ::
      map (Pretty.chunks o Element.pretty_ctxt ctxt) elems))]
  end;

fun pretty (Target {target, is_class, ...}) ctxt =
  Pretty.block [Pretty.command "theory", Pretty.brk 1,
      Pretty.str (Context.theory_name (ProofContext.theory_of ctxt))] ::
    pretty_thy ctxt target is_class;


(* init *)

local

fun init_context (Target {target, is_locale, is_class}) =
  if not is_locale then ProofContext.init_global
  else if not is_class then Locale.init target
  else Class.init target;

fun init_target (ta as Target {target, ...}) thy =
  thy
  |> init_context ta
  |> Data.put (SOME ta)
  |> Local_Theory.init NONE (Long_Name.base_name target)
     {define = Generic_Target.define (target_foundation ta),
      notes = Generic_Target.notes (target_notes ta),
      abbrev = Generic_Target.abbrev (target_abbrev ta),
      declaration = fn pervasive => target_declaration ta
        { syntax = false, pervasive = pervasive },
      syntax_declaration = fn pervasive => target_declaration ta
        { syntax = true, pervasive = pervasive },
      pretty = pretty ta,
      exit = Local_Theory.target_of};

in

fun init target thy = init_target (named_target thy target) thy;

fun reinit lthy = case peek lthy
 of SOME {target, ...} => init target o Local_Theory.exit_global
  | NONE => error "Not in a named target";

val theory_init = init_target global_target;

fun context_cmd "-" thy = init "" thy
  | context_cmd target thy = init (Locale.intern thy target) thy;

end;

end;