src/Pure/Isar/proof_history.ML
author wenzelm
Wed Sep 29 13:49:07 1999 +0200 (1999-09-29)
changeset 7632 25a0d2ba3a87
parent 7366 22a64baa7013
child 7731 51d59734743d
permissions -rw-r--r--
removed extra shyps error;
     1 (*  Title:      Pure/Isar/proof_history.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Histories of proof states, with undo / redo and prev / back.
     6 *)
     7 
     8 signature PROOF_HISTORY =
     9 sig
    10   type T
    11   exception FAIL of string
    12   val position: T -> int
    13   val init: int option -> Proof.state -> T
    14   val is_initial: T -> bool
    15   val current: T -> Proof.state
    16   val clear: T -> T
    17   val undo: T -> T
    18   val redo: T -> T
    19   val back: bool -> T -> T
    20   val applys: (Proof.state -> Proof.state Seq.seq) -> T -> T
    21   val apply: (Proof.state -> Proof.state) -> T -> T
    22 end;
    23 
    24 structure ProofHistory: PROOF_HISTORY =
    25 struct
    26 
    27 
    28 (* datatype proof history *)
    29 
    30 type node =
    31   Proof.state *			(*first result*)
    32   Proof.state Seq.seq; 		(*alternative results*)
    33 
    34 type proof = node * node list;
    35 
    36 datatype T = ProofHistory of proof History.T;
    37 
    38 exception FAIL of string;
    39 
    40 fun app f (ProofHistory x) = ProofHistory (f x);
    41 fun hist_app f = app (History.apply f);
    42 
    43 fun position (ProofHistory prf) = length (snd (History.current prf));
    44 
    45 
    46 (* generic history operations *)
    47 
    48 fun init lim st = ProofHistory (History.init lim ((st, Seq.empty), []));
    49 fun is_initial (ProofHistory prf) = History.is_initial prf;
    50 fun current (ProofHistory prf) = fst (fst (History.current prf));
    51 val clear = app History.clear;
    52 val undo = app History.undo;
    53 val redo = app History.redo;
    54 
    55 
    56 (* backtrack *)
    57 
    58 fun back_fun recur ((_, stq), nodes) =
    59   (case Seq.pull stq of
    60     None =>
    61       if recur andalso not (null nodes) then
    62         (writeln "back: trying previous proof state ..."; back_fun recur (hd nodes, tl nodes))
    63       else raise FAIL "back: no alternatives"
    64   | Some result => (result, nodes));
    65 
    66 val back = hist_app o back_fun;
    67 
    68 
    69 (* apply transformer *)
    70 
    71 fun applys f = hist_app (fn (node as (st, _), nodes) =>
    72   (case Seq.pull (f st) of
    73     None => raise FAIL "empty result sequence -- proof command failed"
    74   | Some results => (results, node :: nodes)));
    75 
    76 fun apply f = applys (Seq.single o f);
    77 
    78 
    79 end;