src/Pure/Isar/proof_history.ML
author wenzelm
Sun Jul 08 19:51:58 2007 +0200 (2007-07-08)
changeset 23655 d2d1138e0ddc
parent 23362 de1476695aa6
permissions -rw-r--r--
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
     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 map_current: (Proof.state -> Proof.state) -> T -> T
    15   val current: T -> Proof.state
    16   val previous: T -> Proof.state option
    17   val clear: int -> T -> T
    18   val undo: T -> T
    19   val redo: T -> T
    20   val back: 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 * int;        (*proof node, proof position*)
    36 
    37 datatype T = ProofHistory of proof History.T;
    38 
    39 fun app f (ProofHistory x) = ProofHistory (f x);
    40 fun hist_app f = app (History.apply f);
    41 fun hist_map f = app (History.map_current f);
    42 
    43 fun position (ProofHistory prf) = snd (History.current prf);
    44 
    45 
    46 (* generic history operations *)
    47 
    48 fun init lim st = ProofHistory (History.init lim ((st, Seq.empty), 0));
    49 fun is_initial (ProofHistory prf) = History.is_initial prf;
    50 fun map_current f = hist_map (apfst (apfst f));
    51 fun current (ProofHistory prf) = fst (fst (History.current prf));
    52 fun previous (ProofHistory prf) = Option.map (fst o fst) (History.previous prf);
    53 val clear = app o History.clear;
    54 val undo = app History.undo;
    55 val redo = app History.redo;
    56 
    57 
    58 (* backtracking *)
    59 
    60 val back = hist_app (fn ((_, stq), position) =>
    61   (case Seq.pull stq of
    62     NONE => error "back: no alternatives"
    63   | SOME result => (result, position)));
    64 
    65 
    66 (* apply transformer *)
    67 
    68 fun applys f = hist_app (fn (node as (st, _), position) =>
    69   (case Seq.pull (f st) of
    70     NONE => error "empty result sequence -- proof command failed"
    71   | SOME results => (results, position + 1)));
    72 
    73 fun apply f = applys (Seq.single o f);
    74 
    75 
    76 end;