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