src/Pure/Isar/named_target.ML
changeset 39642 dd7b582f6929
parent 39639 3dd559ab878b
child 40782 aa533c5e3f48
equal deleted inserted replaced
39616:8052101883c3 39642:dd7b582f6929
    19 
    19 
    20 (* context data *)
    20 (* context data *)
    21 
    21 
    22 datatype target = Target of {target: string, is_locale: bool, is_class: bool};
    22 datatype target = Target of {target: string, is_locale: bool, is_class: bool};
    23 
    23 
    24 fun make_target target is_locale is_class =
    24 fun named_target _ "" = Target {target = "", is_locale = false, is_class = false}
    25   Target {target = target, is_locale = is_locale, is_class = is_class};
       
    26 
       
    27 val global_target = Target {target = "", is_locale = false, is_class = false};
       
    28 
       
    29 fun named_target _ "" = global_target
       
    30   | named_target thy locale =
    25   | named_target thy locale =
    31       if Locale.defined thy locale
    26       if Locale.defined thy locale
    32       then Target {target = locale, is_locale = true, is_class = Class.is_class thy locale}
    27       then Target {target = locale, is_locale = true, is_class = Class.is_class thy locale}
    33       else error ("No such locale: " ^ quote locale);
    28       else error ("No such locale: " ^ quote locale);
    34 
    29 
   101   Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
    96   Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
   102   #-> (fn (lhs, def) => locale_const_declaration ta Syntax.mode_default ((b, NoSyn), lhs)
    97   #-> (fn (lhs, def) => locale_const_declaration ta Syntax.mode_default ((b, NoSyn), lhs)
   103     #> Class.const target ((b, mx), (type_params, lhs))
    98     #> Class.const target ((b, mx), (type_params, lhs))
   104     #> pair (lhs, def))
    99     #> pair (lhs, def))
   105 
   100 
   106 fun target_foundation (ta as Target {target, is_locale, is_class, ...}) =
   101 fun target_foundation (ta as Target {is_locale, is_class, ...}) =
   107   if is_class then class_foundation ta
   102   if is_class then class_foundation ta
   108   else if is_locale then locale_foundation ta
   103   else if is_locale then locale_foundation ta
   109   else Generic_Target.theory_foundation;
   104   else Generic_Target.theory_foundation;
   110 
   105 
   111 
   106 
   120     lthy
   115     lthy
   121     |> Local_Theory.background_theory (Global_Theory.note_thmss kind global_facts' #> snd)
   116     |> Local_Theory.background_theory (Global_Theory.note_thmss kind global_facts' #> snd)
   122     |> Local_Theory.target (Locale.add_thmss locale kind local_facts')
   117     |> Local_Theory.target (Locale.add_thmss locale kind local_facts')
   123   end
   118   end
   124 
   119 
   125 fun target_notes (ta as Target {target, is_locale, ...}) =
   120 fun target_notes (Target {target, is_locale, ...}) =
   126   if is_locale then locale_notes target
   121   if is_locale then locale_notes target
   127     else fn kind => fn global_facts => fn _ => Generic_Target.theory_notes kind global_facts;
   122     else fn kind => fn global_facts => fn _ => Generic_Target.theory_notes kind global_facts;
   128 
   123 
   129 
   124 
   130 (* abbrev *)
   125 (* abbrev *)
   146     |> Generic_Target.theory_abbrev prmode ((b, mx), global_rhs);
   141     |> Generic_Target.theory_abbrev prmode ((b, mx), global_rhs);
   147 
   142 
   148 
   143 
   149 (* pretty *)
   144 (* pretty *)
   150 
   145 
   151 fun pretty_thy ctxt target is_class =
   146 fun pretty (Target {target, is_locale, is_class, ...}) ctxt =
   152   let
   147   let
   153     val thy = ProofContext.theory_of ctxt;
   148     val thy = ProofContext.theory_of ctxt;
   154     val target_name =
   149     val target_name =
   155       [Pretty.command (if is_class then "class" else "locale"),
   150       [Pretty.command (if is_class then "class" else "locale"),
   156         Pretty.str (" " ^ Locale.extern thy target)];
   151         Pretty.str (" " ^ Locale.extern thy target)];
   159     val assumes = map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])]))
   154     val assumes = map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])]))
   160       (Assumption.all_assms_of ctxt);
   155       (Assumption.all_assms_of ctxt);
   161     val elems =
   156     val elems =
   162       (if null fixes then [] else [Element.Fixes fixes]) @
   157       (if null fixes then [] else [Element.Fixes fixes]) @
   163       (if null assumes then [] else [Element.Assumes assumes]);
   158       (if null assumes then [] else [Element.Assumes assumes]);
   164   in
   159     val body_elems =  if not is_locale then []
   165     if target = "" then []
   160       else if null elems then [Pretty.block target_name]
   166     else if null elems then [Pretty.block target_name]
   161       else [Pretty.block (Pretty.fbreaks (Pretty.block (target_name @ [Pretty.str " ="]) ::
   167     else [Pretty.block (Pretty.fbreaks (Pretty.block (target_name @ [Pretty.str " ="]) ::
   162         map (Pretty.chunks o Element.pretty_ctxt ctxt) elems))]
   168       map (Pretty.chunks o Element.pretty_ctxt ctxt) elems))]
   163   in
   169   end;
   164     Pretty.block [Pretty.command "theory", Pretty.brk 1,
   170 
   165       Pretty.str (Context.theory_name (ProofContext.theory_of ctxt))] :: body_elems
   171 fun pretty (Target {target, is_class, ...}) ctxt =
   166   end;
   172   Pretty.block [Pretty.command "theory", Pretty.brk 1,
       
   173       Pretty.str (Context.theory_name (ProofContext.theory_of ctxt))] ::
       
   174     pretty_thy ctxt target is_class;
       
   175 
   167 
   176 
   168 
   177 (* init *)
   169 (* init *)
   178 
       
   179 local
       
   180 
   170 
   181 fun init_context (Target {target, is_locale, is_class}) =
   171 fun init_context (Target {target, is_locale, is_class}) =
   182   if not is_locale then ProofContext.init_global
   172   if not is_locale then ProofContext.init_global
   183   else if not is_class then Locale.init target
   173   else if not is_class then Locale.init target
   184   else Class.init target;
   174   else Class.init target;
   185 
   175 
   186 fun init_target (ta as Target {target, ...}) thy =
   176 fun init target thy =
   187   thy
   177   let
   188   |> init_context ta
   178     val ta = named_target thy target;
   189   |> Data.put (SOME ta)
   179   in
   190   |> Local_Theory.init NONE (Long_Name.base_name target)
   180     thy
   191      {define = Generic_Target.define (target_foundation ta),
   181     |> init_context ta
   192       notes = Generic_Target.notes (target_notes ta),
   182     |> Data.put (SOME ta)
   193       abbrev = Generic_Target.abbrev (target_abbrev ta),
   183     |> Local_Theory.init NONE (Long_Name.base_name target)
   194       declaration = fn pervasive => target_declaration ta
   184        {define = Generic_Target.define (target_foundation ta),
   195         { syntax = false, pervasive = pervasive },
   185         notes = Generic_Target.notes (target_notes ta),
   196       syntax_declaration = fn pervasive => target_declaration ta
   186         abbrev = Generic_Target.abbrev (target_abbrev ta),
   197         { syntax = true, pervasive = pervasive },
   187         declaration = fn pervasive => target_declaration ta
   198       pretty = pretty ta,
   188           { syntax = false, pervasive = pervasive },
   199       exit = Local_Theory.target_of};
   189         syntax_declaration = fn pervasive => target_declaration ta
   200 
   190           { syntax = true, pervasive = pervasive },
   201 in
   191         pretty = pretty ta,
   202 
   192         exit = Local_Theory.target_of}
   203 fun init target thy = init_target (named_target thy target) thy;
   193   end;
       
   194 
       
   195 val theory_init = init "";
   204 
   196 
   205 fun reinit lthy =
   197 fun reinit lthy =
   206   (case peek lthy of
   198   (case peek lthy of
   207     SOME {target, ...} => init target o Local_Theory.exit_global
   199     SOME {target, ...} => init target o Local_Theory.exit_global
   208   | NONE => error "Not in a named target");
   200   | NONE => error "Not in a named target");
   209 
   201 
   210 val theory_init = init_target global_target;
       
   211 
       
   212 fun context_cmd "-" thy = init "" thy
   202 fun context_cmd "-" thy = init "" thy
   213   | context_cmd target thy = init (Locale.intern thy target) thy;
   203   | context_cmd target thy = init (Locale.intern thy target) thy;
   214 
   204 
   215 end;
   205 end;
   216 
       
   217 end;