src/Pure/Isar/proof_history.ML
author wenzelm
Tue Dec 05 22:14:42 2006 +0100 (2006-12-05)
changeset 21658 5e31241e1e3c
parent 18678 dd0c569fa43d
child 23362 de1476695aa6
permissions -rw-r--r--
Attrib.internal: morphism;
     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 backtracking.
     6 *)
     7 
     8 signature PROOF_HISTORY =
     9 sig
    10   type T
    11   val position: T -> int
    12   val init: int option -> Proof.state -> T
    13   val is_initial: T -> bool
    14   val current: T -> Proof.state
    15   val previous: T -> Proof.state option
    16   val clear: int -> T -> T
    17   val undo: T -> T
    18   val redo: T -> T
    19   val back: 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 fun app f (ProofHistory x) = ProofHistory (f x);
    39 fun hist_app f = app (History.apply f);
    40 
    41 fun position (ProofHistory prf) = length (snd (History.current prf));
    42 
    43 
    44 (* generic history operations *)
    45 
    46 fun init lim st = ProofHistory (History.init lim ((st, Seq.empty), []));
    47 fun is_initial (ProofHistory prf) = History.is_initial prf;
    48 fun current (ProofHistory prf) = fst (fst (History.current prf));
    49 fun previous (ProofHistory prf) = Option.map (fst o fst) (History.previous prf);
    50 val clear = app o History.clear;
    51 val undo = app History.undo;
    52 val redo = app History.redo;
    53 
    54 
    55 (* backtracking *)
    56 
    57 val back = hist_app (fn ((_, stq), nodes) =>
    58   (case Seq.pull stq of
    59     NONE => error "back: no alternatives"
    60   | SOME result => (result, nodes)));
    61 
    62 
    63 (* apply transformer *)
    64 
    65 fun applys f = hist_app (fn (node as (st, _), nodes) =>
    66   (case Seq.pull (f st) of
    67     NONE => error "empty result sequence -- proof command failed"
    68   | SOME results => (results, node :: nodes)));
    69 
    70 fun apply f = applys (Seq.single o f);
    71 
    72 
    73 end;