src/Pure/Isar/toplevel.ML
changeset 21007 1bbb31aaf98d
parent 20985 de13e2a23c8e
child 21177 e8228486aa03
equal deleted inserted replaced
21006:ac2732072403 21007:1bbb31aaf98d
    26   val node_case: (generic_theory -> 'a) -> (Proof.state -> 'a) -> state -> 'a
    26   val node_case: (generic_theory -> 'a) -> (Proof.state -> 'a) -> state -> 'a
    27   val context_of: state -> Context.generic
    27   val context_of: state -> Context.generic
    28   val theory_of: state -> theory
    28   val theory_of: state -> theory
    29   val proof_of: state -> Proof.state
    29   val proof_of: state -> Proof.state
    30   val proof_position_of: state -> int
    30   val proof_position_of: state -> int
    31   val enter_forward_proof: state -> Proof.state
    31   val enter_proof_body: state -> Proof.state
    32   val prompt_state_default: state -> string
    32   val prompt_state_default: state -> string
    33   val prompt_state_fn: (state -> string) ref
    33   val prompt_state_fn: (state -> string) ref
    34   val print_state_context: state -> unit
    34   val print_state_context: state -> unit
    35   val print_state_default: bool -> state -> unit
    35   val print_state_default: bool -> state -> unit
    36   val print_state_hook: (bool -> state -> unit) -> unit
    36   val print_state_hook: (bool -> state -> unit) -> unit
    68   val kill: transition -> transition
    68   val kill: transition -> transition
    69   val history: (node History.T -> node History.T) -> transition -> transition
    69   val history: (node History.T -> node History.T) -> transition -> transition
    70   val keep: (state -> unit) -> transition -> transition
    70   val keep: (state -> unit) -> transition -> transition
    71   val keep': (bool -> state -> unit) -> transition -> transition
    71   val keep': (bool -> state -> unit) -> transition -> transition
    72   val imperative: (unit -> unit) -> transition -> transition
    72   val imperative: (unit -> unit) -> transition -> transition
    73   val init_theory: (bool -> theory) -> (theory -> unit) -> (theory -> unit)
    73   val init_theory: (bool -> theory) -> (theory -> unit) -> (theory -> unit) ->
    74     -> transition -> transition
    74     transition -> transition
    75   val theory: (theory -> theory) -> transition -> transition
    75   val theory: (theory -> theory) -> transition -> transition
    76   val theory': (bool -> theory -> theory) -> transition -> transition
    76   val theory': (bool -> theory -> theory) -> transition -> transition
    77   val exit_local_theory: transition -> transition
       
    78   val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
    77   val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
       
    78   val end_local_theory: transition -> transition
    79   val local_theory: xstring option -> (local_theory -> local_theory) -> transition -> transition
    79   val local_theory: xstring option -> (local_theory -> local_theory) -> transition -> transition
    80   val present_local_theory: xstring option -> (bool -> node -> unit) -> transition -> transition
    80   val present_local_theory: xstring option -> (bool -> node -> unit) -> transition -> transition
       
    81   val local_theory_to_proof: xstring option -> (local_theory -> Proof.state) ->
       
    82     transition -> transition
    81   val theory_to_proof: (theory -> Proof.state) -> transition -> transition
    83   val theory_to_proof: (theory -> Proof.state) -> transition -> transition
       
    84   val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
       
    85   val forget_proof: transition -> transition
    82   val proof: (Proof.state -> Proof.state) -> transition -> transition
    86   val proof: (Proof.state -> Proof.state) -> transition -> transition
    83   val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
    87   val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
    84   val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
    88   val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
    85   val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
    89   val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
       
    90   val present_proof: (bool -> node -> unit) -> transition -> transition
    86   val actual_proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
    91   val actual_proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
    87   val skip_proof: (int History.T -> int History.T) -> transition -> transition
    92   val skip_proof: (int History.T -> int History.T) -> transition -> transition
    88   val skip_proof_to_theory: (int -> bool) -> transition -> transition
    93   val skip_proof_to_theory: (int -> bool) -> transition -> transition
    89   val forget_proof: transition -> transition
       
    90   val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
       
    91   val present_proof: (bool -> node -> unit) -> transition -> transition
       
    92   val unknown_theory: transition -> transition
    94   val unknown_theory: transition -> transition
    93   val unknown_proof: transition -> transition
    95   val unknown_proof: transition -> transition
    94   val unknown_context: transition -> transition
    96   val unknown_context: transition -> transition
    95   val apply: bool -> transition -> state -> (state * (exn * string) option) option
    97   val apply: bool -> transition -> state -> (state * (exn * string) option) option
    96   val present_excursion: (transition * (state -> state -> 'a -> 'a)) list -> 'a -> 'a
    98   val present_excursion: (transition * (state -> state -> 'a -> 'a)) list -> 'a -> 'a
   116 
   118 
   117 type generic_theory = Context.generic;    (*theory or local_theory*)
   119 type generic_theory = Context.generic;    (*theory or local_theory*)
   118 
   120 
   119 datatype node =
   121 datatype node =
   120   Theory of generic_theory * Proof.context option | (*theory with presentation context*)
   122   Theory of generic_theory * Proof.context option | (*theory with presentation context*)
   121   Proof of ProofHistory.T * generic_theory |        (*history of proof states, original theory*)
   123   Proof of ProofHistory.T * ((Proof.context -> generic_theory) * generic_theory) |
   122   SkipProof of (int History.T * generic_theory) * generic_theory;
   124     (*history of proof states, finish, original theory*)
       
   125   SkipProof of int History.T * (generic_theory * generic_theory);
   123     (*history of proof depths, resulting theory, original theory*)
   126     (*history of proof depths, resulting theory, original theory*)
   124 
   127 
   125 val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
   128 val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
   126 val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
   129 val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
   127 
   130 
   128 fun cases_node f _ (Theory (gthy, _)) = f gthy
   131 fun cases_node f _ (Theory (gthy, _)) = f gthy
   129   | cases_node _ g (Proof (prf, _)) = g (ProofHistory.current prf)
   132   | cases_node _ g (Proof (prf, _)) = g (ProofHistory.current prf)
   130   | cases_node f _ (SkipProof ((_, gthy), _)) = f gthy;
   133   | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy;
   131 
   134 
   132 fun presentation_context (SOME (Theory (_, SOME ctxt))) NONE = ctxt
   135 fun presentation_context (SOME (Theory (_, SOME ctxt))) NONE = ctxt
   133   | presentation_context (SOME node) NONE = cases_node Context.proof_of Proof.context_of node
   136   | presentation_context (SOME node) NONE = cases_node Context.proof_of Proof.context_of node
   134   | presentation_context (SOME node) (SOME loc) =
   137   | presentation_context (SOME node) (SOME loc) =
   135       TheoryTarget.init (SOME loc) (cases_node Context.theory_of Proof.theory_of node)
   138       TheoryTarget.init (SOME loc) (cases_node Context.theory_of Proof.theory_of node)
   146   | level (State (SOME (node, _))) =
   149   | level (State (SOME (node, _))) =
   147       (case History.current node of
   150       (case History.current node of
   148         Theory (Context.Theory _, _) => 0
   151         Theory (Context.Theory _, _) => 0
   149       | Theory (Context.Proof _, _) => 1
   152       | Theory (Context.Proof _, _) => 1
   150       | Proof (prf, _) => Proof.level (ProofHistory.current prf) + 1
   153       | Proof (prf, _) => Proof.level (ProofHistory.current prf) + 1
   151       | SkipProof ((h, _), _) => History.current h + 2);   (*different notion of proof depth!*)
   154       | SkipProof (h, _) => History.current h + 2);   (*different notion of proof depth!*)
   152 
   155 
   153 fun str_of_state (State NONE) = "at top level"
   156 fun str_of_state (State NONE) = "at top level"
   154   | str_of_state (State (SOME (node, _))) =
   157   | str_of_state (State (SOME (node, _))) =
   155       (case History.current node of
   158       (case History.current node of
   156         Theory (Context.Theory _, _) => "in theory mode"
   159         Theory (Context.Theory _, _) => "in theory mode"
   181 fun proof_position_of state =
   184 fun proof_position_of state =
   182   (case node_of state of
   185   (case node_of state of
   183     Proof (prf, _) => ProofHistory.position prf
   186     Proof (prf, _) => ProofHistory.position prf
   184   | _ => raise UNDEF);
   187   | _ => raise UNDEF);
   185 
   188 
   186 val enter_forward_proof = node_case (Proof.init o Context.proof_of) Proof.enter_forward;
   189 val enter_proof_body = node_case (Proof.init o Context.proof_of) Proof.enter_forward;
   187 
   190 
   188 
   191 
   189 (* prompt state *)
   192 (* prompt state *)
   190 
   193 
   191 fun prompt_state_default (State _) = Source.default_prompt;
   194 fun prompt_state_default (State _) = Source.default_prompt;
   203   | SOME gthy => pretty_context gthy);
   206   | SOME gthy => pretty_context gthy);
   204 
   207 
   205 fun pretty_node prf_only (Theory (gthy, _)) = if prf_only then [] else pretty_context gthy
   208 fun pretty_node prf_only (Theory (gthy, _)) = if prf_only then [] else pretty_context gthy
   206   | pretty_node _ (Proof (prf, _)) =
   209   | pretty_node _ (Proof (prf, _)) =
   207       Proof.pretty_state (ProofHistory.position prf) (ProofHistory.current prf)
   210       Proof.pretty_state (ProofHistory.position prf) (ProofHistory.current prf)
   208   | pretty_node _ (SkipProof ((h, _), _)) =
   211   | pretty_node _ (SkipProof (h, _)) =
   209       [Pretty.str ("skipped proof: depth " ^ string_of_int (History.current h))];
   212       [Pretty.str ("skipped proof: depth " ^ string_of_int (History.current h))];
   210 
   213 
   211 fun pretty_state prf_only state =
   214 fun pretty_state prf_only state =
   212   let val ref (begin_state, end_state, _) = Display.current_goals_markers in
   215   let val ref (begin_state, end_state, _) = Display.current_goals_markers in
   213     (case try node_of state of NONE => []
   216     (case try node_of state of NONE => []
   495 
   498 
   496 val three_buffersN = "three_buffers";
   499 val three_buffersN = "three_buffers";
   497 val print3 = print' three_buffersN;
   500 val print3 = print' three_buffersN;
   498 
   501 
   499 
   502 
   500 (* build transitions *)
   503 (* basic transitions *)
   501 
   504 
   502 val reset = add_trans Reset;
   505 val reset = add_trans Reset;
   503 fun init f exit kill = add_trans (Init (f, (exit, kill)));
   506 fun init f exit kill = add_trans (Init (f, (exit, kill)));
   504 val exit = add_trans Exit;
   507 val exit = add_trans Exit;
   505 val kill = add_trans Kill;
   508 val kill = add_trans Kill;
   514 fun init_theory f exit kill =
   517 fun init_theory f exit kill =
   515   init (fn int => Theory (Context.Theory (f int), NONE))
   518   init (fn int => Theory (Context.Theory (f int), NONE))
   516     (fn Theory (Context.Theory thy, _) => exit thy | _ => raise UNDEF)
   519     (fn Theory (Context.Theory thy, _) => exit thy | _ => raise UNDEF)
   517     (fn Theory (Context.Theory thy, _) => kill thy | _ => raise UNDEF);
   520     (fn Theory (Context.Theory thy, _) => kill thy | _ => raise UNDEF);
   518 
   521 
   519 
   522 val unknown_theory = imperative (fn () => warning "Unknown theory context");
   520 (* typed transitions *)
   523 val unknown_proof = imperative (fn () => warning "Unknown proof context");
       
   524 val unknown_context = imperative (fn () => warning "Unknown context");
       
   525 
       
   526 
       
   527 (* theory transitions *)
   521 
   528 
   522 fun theory' f = app_current (fn int =>
   529 fun theory' f = app_current (fn int =>
   523   (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (f int thy), NONE)
   530   (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (f int thy), NONE)
   524     | _ => raise UNDEF));
   531     | _ => raise UNDEF));
   525 
   532 
   526 fun theory f = theory' (K f);
   533 fun theory f = theory' (K f);
   527 
       
   528 val exit_local_theory = app_current (fn int =>
       
   529   (fn Theory (Context.Proof lthy, _) =>
       
   530       let val (ctxt', thy') = LocalTheory.exit int lthy
       
   531       in Theory (Context.Theory thy', SOME ctxt') end
       
   532     | _ => raise UNDEF));
       
   533 
   534 
   534 fun begin_local_theory begin f = app_current (fn int =>
   535 fun begin_local_theory begin f = app_current (fn int =>
   535   (fn Theory (Context.Theory thy, _) =>
   536   (fn Theory (Context.Theory thy, _) =>
   536         let
   537         let
   537           val lthy = f thy;
   538           val lthy = f thy;
   538           val (ctxt, gthy) =
   539           val (ctxt, gthy) =
   539             if begin then (lthy, Context.Proof lthy)
   540             if begin then (lthy, Context.Proof lthy)
   540             else lthy |> LocalTheory.exit int ||> Context.Theory;
   541             else lthy |> LocalTheory.exit int ||> Context.Theory;
   541         in Theory (gthy, SOME ctxt) end
   542         in Theory (gthy, SOME ctxt) end
   542     | _ => raise UNDEF));
   543     | _ => raise UNDEF));
       
   544 
       
   545 val end_local_theory = app_current (fn int =>
       
   546   (fn Theory (Context.Proof lthy, _) =>
       
   547       let val (ctxt', thy') = LocalTheory.exit int lthy
       
   548       in Theory (Context.Theory thy', SOME ctxt') end
       
   549     | _ => raise UNDEF));
       
   550 
       
   551 local
   543 
   552 
   544 fun local_theory_presentation loc f g = app_current (fn int =>
   553 fun local_theory_presentation loc f g = app_current (fn int =>
   545   (fn Theory (Context.Theory thy, _) =>
   554   (fn Theory (Context.Theory thy, _) =>
   546         let val (ctxt', thy') = TheoryTarget.mapping loc f thy
   555         let val (ctxt', thy') = TheoryTarget.mapping loc f thy
   547         in Theory (Context.Theory thy', SOME ctxt') end
   556         in Theory (Context.Theory thy', SOME ctxt') end
   550           if is_none loc then f lthy |> (fn lthy' => (lthy', LocalTheory.reinit lthy'))
   559           if is_none loc then f lthy |> (fn lthy' => (lthy', LocalTheory.reinit lthy'))
   551           else lthy |> LocalTheory.raw_theory_result (TheoryTarget.mapping loc f)
   560           else lthy |> LocalTheory.raw_theory_result (TheoryTarget.mapping loc f)
   552         in Theory (Context.Proof lthy', SOME ctxt') end
   561         in Theory (Context.Proof lthy', SOME ctxt') end
   553     | _ => raise UNDEF) #> tap (g int));
   562     | _ => raise UNDEF) #> tap (g int));
   554 
   563 
       
   564 in
       
   565 
   555 fun local_theory loc f = local_theory_presentation loc f (K I);
   566 fun local_theory loc f = local_theory_presentation loc f (K I);
   556 fun present_local_theory loc g = local_theory_presentation loc I g;
   567 fun present_local_theory loc g = local_theory_presentation loc I g;
   557 
   568 
   558 fun theory_to_proof f = app_current (fn int =>
   569 end;
   559   (fn Theory (gthy as Context.Theory thy, _) =>
   570 
   560     let
   571 
   561       val prf = f thy;
   572 (* proof transitions *)
       
   573 
       
   574 local
       
   575 
       
   576 fun begin_proof init finish = app_current (fn int =>
       
   577   (fn Theory (gthy, _) =>
       
   578     let 
       
   579       val prf = init gthy;
   562       val schematic = Proof.schematic_goal prf;
   580       val schematic = Proof.schematic_goal prf;
   563     in
   581     in
   564       if ! skip_proofs andalso schematic then
   582       if ! skip_proofs andalso schematic then
   565         warning "Cannot skip proof of schematic goal statement"
   583         warning "Cannot skip proof of schematic goal statement"
   566       else ();
   584       else ();
   567       if ! skip_proofs andalso not schematic then
   585       if ! skip_proofs andalso not schematic then
   568         SkipProof ((History.init (undo_limit int) 0,
   586         SkipProof
   569           Context.Theory (ProofContext.theory_of (Proof.global_skip_proof int prf))), gthy)
   587           (History.init (undo_limit int) 0, (finish gthy (Proof.global_skip_proof int prf), gthy))
   570       else Proof (ProofHistory.init (undo_limit int) prf, gthy)
   588       else Proof (ProofHistory.init (undo_limit int) prf, (finish gthy, gthy))
   571     end
   589     end
   572   | _ => raise UNDEF));
   590   | _ => raise UNDEF));
   573 
   591 
   574 fun proofs' f = map_current (fn int =>
   592 val global_finish = Context.Theory o ProofContext.theory_of;
   575   (fn Proof (prf, orig_gthy) => Proof (ProofHistory.applys (f int) prf, orig_gthy)
   593 val local_finish = Context.Proof o LocalTheory.reinit;
   576     | SkipProof ((h, gthy), orig_gthy) => SkipProof ((History.apply I h, gthy), orig_gthy)
   594 
   577     | _ => raise UNDEF));
   595 fun mixed_finish (Context.Theory _) ctxt = global_finish ctxt
   578 
   596   | mixed_finish (Context.Proof lthy) ctxt =
   579 fun proof' f = proofs' (Seq.single oo f);
   597       Context.Proof (LocalTheory.raw_theory (K (ProofContext.theory_of ctxt)) lthy);
   580 val proofs = proofs' o K;
   598 
   581 val proof = proof' o K;
   599 in
   582 
   600 
   583 fun actual_proof f = map_current (fn _ =>
   601 fun local_theory_to_proof NONE f = begin_proof
   584   (fn Proof (prf, orig_gthy) => Proof (f prf, orig_gthy)
   602       (fn Context.Theory thy => f (TheoryTarget.init NONE thy)
   585     | _ => raise UNDEF));
   603         | Context.Proof lthy => f lthy)
   586 
   604       (fn Context.Theory _ => global_finish
   587 fun skip_proof f = map_current (fn _ =>
   605         | Context.Proof _ => local_finish)
   588   (fn SkipProof ((h, gthy), orig_gthy) => SkipProof ((f h, gthy), orig_gthy)
   606   | local_theory_to_proof loc f =
   589     | _ => raise UNDEF));
   607       begin_proof (f o TheoryTarget.init loc o Context.theory_of) mixed_finish;
   590 
   608 
   591 fun skip_proof_to_theory p = map_current (fn _ =>
   609 fun theory_to_proof f =
   592   (fn SkipProof ((h, thy), _) =>
   610   begin_proof (fn Context.Theory thy => f thy | _ => raise UNDEF) (K global_finish);
   593     if p (History.current h) then Theory (thy, NONE)
   611 
   594     else raise UNDEF
   612 end;
   595   | _ => raise UNDEF));
       
   596 
       
   597 val forget_proof = map_current (fn _ =>
       
   598   (fn Proof (_, orig_gthy) => Theory (orig_gthy, NONE)
       
   599     | SkipProof (_, orig_gthy) => Theory (orig_gthy, NONE)
       
   600     | _ => raise UNDEF));
       
   601 
   613 
   602 fun end_proof f = map_current (fn int =>
   614 fun end_proof f = map_current (fn int =>
   603   (fn Proof (prf, orig_gthy) =>
   615   (fn Proof (prf, (finish, orig_gthy)) =>
   604         let val state = ProofHistory.current prf in
   616         let val state = ProofHistory.current prf in
   605           if can (Proof.assert_bottom true) state then
   617           if can (Proof.assert_bottom true) state then
   606             let
   618             let
   607               val ctxt' = f int state;
   619               val ctxt' = f int state;
   608               val gthy' =
   620               val gthy' = finish ctxt';
   609                 if can Context.the_theory orig_gthy
       
   610                 then Context.Theory (ProofContext.theory_of ctxt')
       
   611                 else Context.Proof (LocalTheory.reinit ctxt');
       
   612             in Theory (gthy', SOME ctxt') end
   621             in Theory (gthy', SOME ctxt') end
   613           else raise UNDEF
   622           else raise UNDEF
   614         end
   623         end
   615     | SkipProof ((h, gthy), _) =>
   624     | SkipProof (h, (gthy, _)) =>
   616         if History.current h = 0 then Theory (gthy, NONE) else raise UNDEF
   625         if History.current h = 0 then Theory (gthy, NONE) else raise UNDEF
   617     | _ => raise UNDEF));
   626     | _ => raise UNDEF));
   618 
   627 
       
   628 val forget_proof = map_current (fn _ =>
       
   629   (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
       
   630     | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
       
   631     | _ => raise UNDEF));
       
   632 
       
   633 fun proofs' f = map_current (fn int =>
       
   634   (fn Proof (prf, x) => Proof (ProofHistory.applys (f int) prf, x)
       
   635     | SkipProof (h, x) => SkipProof (History.apply I h, x)
       
   636     | _ => raise UNDEF));
       
   637 
       
   638 fun proof' f = proofs' (Seq.single oo f);
       
   639 val proofs = proofs' o K;
       
   640 val proof = proof' o K;
       
   641 
   619 fun present_proof f = map_current (fn int =>
   642 fun present_proof f = map_current (fn int =>
   620   (fn node as Proof _ => node | _ => raise UNDEF) #> tap (f int));
   643   (fn node as Proof _ => node
   621 
   644     | node as SkipProof _ => node
   622 val unknown_theory = imperative (fn () => warning "Unknown theory context");
   645     | _ => raise UNDEF) #> tap (f int));
   623 val unknown_proof = imperative (fn () => warning "Unknown proof context");
   646 
   624 val unknown_context = imperative (fn () => warning "Unknown context");
   647 fun actual_proof f = map_current (fn _ =>
       
   648   (fn Proof (prf, x) => Proof (f prf, x)
       
   649     | _ => raise UNDEF));
       
   650 
       
   651 fun skip_proof f = map_current (fn _ =>
       
   652   (fn SkipProof (h, x) => SkipProof (f h, x)
       
   653     | _ => raise UNDEF));
       
   654 
       
   655 fun skip_proof_to_theory p = map_current (fn _ =>
       
   656   (fn SkipProof (h, (gthy, _)) =>
       
   657     if p (History.current h) then Theory (gthy, NONE)
       
   658     else raise UNDEF
       
   659   | _ => raise UNDEF));
   625 
   660 
   626 
   661 
   627 
   662 
   628 (** toplevel transactions **)
   663 (** toplevel transactions **)
   629 
   664