src/Pure/Isar/toplevel.ML
changeset 51555 6aa64925db77
parent 51553 63327f679cff
child 51560 5b4ae2467830
equal deleted inserted replaced
51554:041bc3d31f23 51555:6aa64925db77
    10   type state
    10   type state
    11   val toplevel: state
    11   val toplevel: state
    12   val is_toplevel: state -> bool
    12   val is_toplevel: state -> bool
    13   val is_theory: state -> bool
    13   val is_theory: state -> bool
    14   val is_proof: state -> bool
    14   val is_proof: state -> bool
       
    15   val is_skipped_proof: state -> bool
    15   val level: state -> int
    16   val level: state -> int
    16   val presentation_context_of: state -> Proof.context
    17   val presentation_context_of: state -> Proof.context
    17   val previous_context_of: state -> Proof.context option
    18   val previous_context_of: state -> Proof.context option
    18   val context_of: state -> Proof.context
    19   val context_of: state -> Proof.context
    19   val generic_theory_of: state -> generic_theory
    20   val generic_theory_of: state -> generic_theory
   126 datatype node =
   127 datatype node =
   127   Theory of generic_theory * Proof.context option
   128   Theory of generic_theory * Proof.context option
   128     (*theory with presentation context*) |
   129     (*theory with presentation context*) |
   129   Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
   130   Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
   130     (*proof node, finish, original theory*) |
   131     (*proof node, finish, original theory*) |
   131   SkipProof of int * (generic_theory * generic_theory);
   132   Skipped_Proof of int * (generic_theory * generic_theory);
   132     (*proof depth, resulting theory, original theory*)
   133     (*proof depth, resulting theory, original theory*)
   133 
   134 
   134 val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
   135 val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
   135 val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
   136 val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
       
   137 val skipped_proof_node = fn Skipped_Proof _ => true | _ => false;
   136 
   138 
   137 fun cases_node f _ (Theory (gthy, _)) = f gthy
   139 fun cases_node f _ (Theory (gthy, _)) = f gthy
   138   | cases_node _ g (Proof (prf, _)) = g (Proof_Node.current prf)
   140   | cases_node _ g (Proof (prf, _)) = g (Proof_Node.current prf)
   139   | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy;
   141   | cases_node f _ (Skipped_Proof (_, (gthy, _))) = f gthy;
   140 
   142 
   141 val context_node = cases_node Context.proof_of Proof.context_of;
   143 val context_node = cases_node Context.proof_of Proof.context_of;
   142 
   144 
   143 
   145 
   144 (* datatype state *)
   146 (* datatype state *)
   151   | is_toplevel _ = false;
   153   | is_toplevel _ = false;
   152 
   154 
   153 fun level (State (NONE, _)) = 0
   155 fun level (State (NONE, _)) = 0
   154   | level (State (SOME (Theory _), _)) = 0
   156   | level (State (SOME (Theory _), _)) = 0
   155   | level (State (SOME (Proof (prf, _)), _)) = Proof.level (Proof_Node.current prf)
   157   | level (State (SOME (Proof (prf, _)), _)) = Proof.level (Proof_Node.current prf)
   156   | level (State (SOME (SkipProof (d, _)), _)) = d + 1;   (*different notion of proof depth!*)
   158   | level (State (SOME (Skipped_Proof (d, _)), _)) = d + 1;   (*different notion of proof depth!*)
   157 
   159 
   158 fun str_of_state (State (NONE, _)) = "at top level"
   160 fun str_of_state (State (NONE, _)) = "at top level"
   159   | str_of_state (State (SOME (Theory (Context.Theory _, _)), _)) = "in theory mode"
   161   | str_of_state (State (SOME (Theory (Context.Theory _, _)), _)) = "in theory mode"
   160   | str_of_state (State (SOME (Theory (Context.Proof _, _)), _)) = "in local theory mode"
   162   | str_of_state (State (SOME (Theory (Context.Proof _, _)), _)) = "in local theory mode"
   161   | str_of_state (State (SOME (Proof _), _)) = "in proof mode"
   163   | str_of_state (State (SOME (Proof _), _)) = "in proof mode"
   162   | str_of_state (State (SOME (SkipProof _), _)) = "in skipped proof mode";
   164   | str_of_state (State (SOME (Skipped_Proof _), _)) = "in skipped proof mode";
   163 
   165 
   164 
   166 
   165 (* current node *)
   167 (* current node *)
   166 
   168 
   167 fun node_of (State (NONE, _)) = raise UNDEF
   169 fun node_of (State (NONE, _)) = raise UNDEF
   168   | node_of (State (SOME node, _)) = node;
   170   | node_of (State (SOME node, _)) = node;
   169 
   171 
   170 fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
   172 fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
   171 fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
   173 fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
       
   174 fun is_skipped_proof state = not (is_toplevel state) andalso skipped_proof_node (node_of state);
   172 
   175 
   173 fun node_case f g state = cases_node f g (node_of state);
   176 fun node_case f g state = cases_node f g (node_of state);
   174 
   177 
   175 fun presentation_context_of state =
   178 fun presentation_context_of state =
   176   (case try node_of state of
   179   (case try node_of state of
   203 fun print_state_context state =
   206 fun print_state_context state =
   204   (case try node_of state of
   207   (case try node_of state of
   205     NONE => []
   208     NONE => []
   206   | SOME (Theory (gthy, _)) => pretty_context gthy
   209   | SOME (Theory (gthy, _)) => pretty_context gthy
   207   | SOME (Proof (_, (_, gthy))) => pretty_context gthy
   210   | SOME (Proof (_, (_, gthy))) => pretty_context gthy
   208   | SOME (SkipProof (_, (gthy, _))) => pretty_context gthy)
   211   | SOME (Skipped_Proof (_, (gthy, _))) => pretty_context gthy)
   209   |> Pretty.chunks |> Pretty.writeln;
   212   |> Pretty.chunks |> Pretty.writeln;
   210 
   213 
   211 fun print_state prf_only state =
   214 fun print_state prf_only state =
   212   (case try node_of state of
   215   (case try node_of state of
   213     NONE => []
   216     NONE => []
   214   | SOME (Theory (gthy, _)) => if prf_only then [] else pretty_context gthy
   217   | SOME (Theory (gthy, _)) => if prf_only then [] else pretty_context gthy
   215   | SOME (Proof (prf, _)) =>
   218   | SOME (Proof (prf, _)) =>
   216       Proof.pretty_state (Proof_Node.position prf) (Proof_Node.current prf)
   219       Proof.pretty_state (Proof_Node.position prf) (Proof_Node.current prf)
   217   | SOME (SkipProof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)])
   220   | SOME (Skipped_Proof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)])
   218   |> Pretty.markup_chunks Markup.state |> Pretty.writeln;
   221   |> Pretty.markup_chunks Markup.state |> Pretty.writeln;
   219 
   222 
   220 fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
   223 fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
   221 
   224 
   222 
   225 
   509               val ctxt' = f int state;
   512               val ctxt' = f int state;
   510               val gthy' = finish ctxt';
   513               val gthy' = finish ctxt';
   511             in Theory (gthy', SOME ctxt') end
   514             in Theory (gthy', SOME ctxt') end
   512           else raise UNDEF
   515           else raise UNDEF
   513         end
   516         end
   514     | SkipProof (0, (gthy, _)) => Theory (gthy, NONE)
   517     | Skipped_Proof (0, (gthy, _)) => Theory (gthy, NONE)
   515     | _ => raise UNDEF));
   518     | _ => raise UNDEF));
   516 
   519 
   517 local
   520 local
   518 
   521 
   519 fun begin_proof init = transaction (fn int =>
   522 fun begin_proof init = transaction (fn int =>
   527         if is_goal andalso skip andalso no_skip then
   530         if is_goal andalso skip andalso no_skip then
   528           warning "Cannot skip proof of schematic goal statement"
   531           warning "Cannot skip proof of schematic goal statement"
   529         else ();
   532         else ();
   530     in
   533     in
   531       if skip andalso not no_skip then
   534       if skip andalso not no_skip then
   532         SkipProof (0, (finish (Proof.global_skip_proof true prf), gthy))
   535         Skipped_Proof (0, (finish (Proof.global_skip_proof true prf), gthy))
   533       else Proof (Proof_Node.init prf, (finish, gthy))
   536       else Proof (Proof_Node.init prf, (finish, gthy))
   534     end
   537     end
   535   | _ => raise UNDEF));
   538   | _ => raise UNDEF));
   536 
   539 
   537 in
   540 in
   552 
   555 
   553 end;
   556 end;
   554 
   557 
   555 val forget_proof = transaction (fn _ =>
   558 val forget_proof = transaction (fn _ =>
   556   (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
   559   (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
   557     | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
   560     | Skipped_Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
   558     | _ => raise UNDEF));
   561     | _ => raise UNDEF));
   559 
   562 
   560 val present_proof = present_transaction (fn _ =>
   563 val present_proof = present_transaction (fn _ =>
   561   (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
   564   (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
   562     | skip as SkipProof _ => skip
   565     | skip as Skipped_Proof _ => skip
   563     | _ => raise UNDEF));
   566     | _ => raise UNDEF));
   564 
   567 
   565 fun proofs' f = transaction (fn int =>
   568 fun proofs' f = transaction (fn int =>
   566   (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
   569   (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
   567     | skip as SkipProof _ => skip
   570     | skip as Skipped_Proof _ => skip
   568     | _ => raise UNDEF));
   571     | _ => raise UNDEF));
   569 
   572 
   570 fun proof' f = proofs' ((Seq.single o Seq.Result) oo f);
   573 fun proof' f = proofs' ((Seq.single o Seq.Result) oo f);
   571 val proofs = proofs' o K;
   574 val proofs = proofs' o K;
   572 val proof = proof' o K;
   575 val proof = proof' o K;
   574 fun actual_proof f = transaction (fn _ =>
   577 fun actual_proof f = transaction (fn _ =>
   575   (fn Proof (prf, x) => Proof (f prf, x)
   578   (fn Proof (prf, x) => Proof (f prf, x)
   576     | _ => raise UNDEF));
   579     | _ => raise UNDEF));
   577 
   580 
   578 fun skip_proof f = transaction (fn _ =>
   581 fun skip_proof f = transaction (fn _ =>
   579   (fn SkipProof (h, x) => SkipProof (f h, x)
   582   (fn Skipped_Proof (h, x) => Skipped_Proof (f h, x)
   580     | _ => raise UNDEF));
   583     | _ => raise UNDEF));
   581 
   584 
   582 fun skip_proof_to_theory pred = transaction (fn _ =>
   585 fun skip_proof_to_theory pred = transaction (fn _ =>
   583   (fn SkipProof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
   586   (fn Skipped_Proof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
   584     | _ => raise UNDEF));
   587     | _ => raise UNDEF));
   585 
   588 
   586 
   589 
   587 
   590 
   588 (** toplevel transactions **)
   591 (** toplevel transactions **)