src/Pure/Isar/local_theory.ML
changeset 72153 bdbd6ff5fd0b
parent 69708 1c201e4792cb
child 72154 2b41b710f6ef
equal deleted inserted replaced
72152:3fa75db844f5 72153:bdbd6ff5fd0b
   125 (
   125 (
   126   type T = lthy list;
   126   type T = lthy list;
   127   fun init _ = [];
   127   fun init _ = [];
   128 );
   128 );
   129 
   129 
       
   130 
   130 (* nested structure *)
   131 (* nested structure *)
   131 
   132 
   132 val level = length o Data.get;  (*1: main target at bottom, >= 2: nested context*)
   133 val level = length o Data.get;  (*1: main target at bottom, >= 2: nested target context*)
   133 
   134 
   134 fun assert lthy =
   135 fun assert lthy =
   135   if level lthy = 0 then error "Missing local theory context" else lthy;
   136   if level lthy = 0 then error "Missing local theory context" else lthy;
   136 
   137 
   137 fun assert_bottom lthy =
   138 fun assert_bottom lthy =
   353 
   354 
   354 
   355 
   355 
   356 
   356 (** manage targets **)
   357 (** manage targets **)
   357 
   358 
   358 (* outermost target *)
   359 (* main target *)
   359 
   360 
   360 fun init {background_naming, exit} operations target =
   361 fun init {background_naming, exit} operations target =
   361   target |> Data.map
   362   target |> Data.map
   362     (fn [] => [make_lthy (background_naming, operations, I, exit, false, target)]
   363     (fn [] => [make_lthy (background_naming, operations, I, exit, false, target)]
   363       | _ => error "Local theory already initialized");
   364       | _ => error "Local theory already initialized");
   365 val exit_of = #exit o bottom_of;
   366 val exit_of = #exit o bottom_of;
   366 
   367 
   367 fun exit lthy = exit_of lthy lthy;
   368 fun exit lthy = exit_of lthy lthy;
   368 val exit_global = Proof_Context.theory_of o exit;
   369 val exit_global = Proof_Context.theory_of o exit;
   369 
   370 
   370 fun exit_result f (x, lthy) =
   371 fun exit_result decl (x, lthy) =
   371   let
   372   let
   372     val ctxt = exit lthy;
   373     val ctxt = exit lthy;
   373     val phi = standard_morphism lthy ctxt;
   374     val phi = standard_morphism lthy ctxt;
   374   in (f phi x, ctxt) end;
   375   in (decl phi x, ctxt) end;
   375 
   376 
   376 fun exit_result_global f (x, lthy) =
   377 fun exit_result_global decl (x, lthy) =
   377   let
   378   let
   378     val thy = exit_global lthy;
   379     val thy = exit_global lthy;
   379     val thy_ctxt = Proof_Context.init_global thy;
   380     val thy_ctxt = Proof_Context.init_global thy;
   380     val phi = standard_morphism lthy thy_ctxt;
   381     val phi = standard_morphism lthy thy_ctxt;
   381   in (f phi x, thy) end;
   382   in (decl phi x, thy) end;
   382 
   383 
   383 
   384 
   384 (* nested targets *)
   385 (* nested targets *)
   385 
   386 
   386 fun init_target {background_naming, after_close} operations lthy =
   387 fun init_target {background_naming, after_close} operations lthy =
   387   let
   388   let
   388     val _ = assert lthy;
   389     val _ = assert lthy;
   389     val after_close' = Proof_Context.restore_naming lthy #> after_close;
   390     val after_close' = Proof_Context.restore_naming lthy #> after_close;
   390     val (scope, target) = Proof_Context.new_scope lthy;
   391     val (scope, target) = Proof_Context.new_scope lthy;
   391     val lthy' =
   392     val entry = make_lthy (background_naming, operations, after_close', exit_of lthy, true, target);
   392       target
   393     val lthy' = Data.map (cons entry) target;
   393       |> Data.map (cons (make_lthy (background_naming, operations, after_close', exit_of lthy, true, target)));
       
   394   in (scope, lthy') end;
   394   in (scope, lthy') end;
   395 
   395 
   396 fun open_target lthy =
   396 fun open_target lthy =
   397   init_target {background_naming = background_naming_of lthy, after_close = I}
   397   init_target {background_naming = background_naming_of lthy, after_close = I}
   398     (operations_of lthy) lthy;
   398     (operations_of lthy) lthy;
   401   let
   401   let
   402     val _ = assert_not_bottom lthy;
   402     val _ = assert_not_bottom lthy;
   403     val ({after_close, ...} :: rest) = Data.get lthy;
   403     val ({after_close, ...} :: rest) = Data.get lthy;
   404   in lthy |> Data.put rest |> reset |> after_close end;
   404   in lthy |> Data.put rest |> reset |> after_close end;
   405 
   405 
   406 fun subtarget g lthy =
   406 fun subtarget body = open_target #> #2 #> body #> close_target;
   407   lthy
   407 
   408   |> open_target
   408 fun subtarget_result decl body lthy =
   409   |> snd
   409   let
   410   |> g
   410     val (x, inner_lthy) = lthy |> open_target |> #2 |> body;
   411   |> close_target;
   411     val lthy' = inner_lthy |> close_target;
   412 
   412     val phi = Proof_Context.export_morphism inner_lthy lthy';
   413 fun subtarget_result f g lthy =
   413   in (decl phi x, lthy') end;
   414   let
       
   415     val (x, inner_lthy) =
       
   416       open_target lthy
       
   417       |> snd
       
   418       |> g
       
   419     val lthy' =
       
   420       inner_lthy
       
   421       |> close_target;
       
   422     val phi = Proof_Context.export_morphism inner_lthy lthy'
       
   423   in (f phi x, lthy') end;
       
   424 
   414 
   425 end;
   415 end;