<commit_before>(*  Title:      Pure/Isar/proof_history.ML
    ID:         $Id$
    Author:     <NAME>, <NAME>

Histories of proof states, with undo / redo and backtracking.
*)

signature PROOF_HISTORY =
sig
  type T
  val position: T -> int
  val init: int option -> Proof.state -> T
  val is_initial: T -> bool
  val current: T -> Proof.state
  val previous: T -> Proof.state option
  val clear: int -> T -> T
  val undo: T -> T
  val redo: T -> T
  val back: T -> T
  val applys: (Proof.state -> Proof.state Seq.seq) -> T -> T
  val apply: (Proof.state -> Proof.state) -> T -> T
end;

structure ProofHistory: PROOF_HISTORY =
struct


(* datatype proof history *)

type node =
  Proof.state *                 (*first result*)
  Proof.state Seq.seq;          (*alternative results*)

type proof = node * node list;

datatype T = ProofHistory of proof History.T;

fun app f (ProofHistory x) = ProofHistory (f x);
fun hist_app f = app (History.apply f);

fun position (ProofHistory prf) = length (snd (History.current prf));


(* generic history operations *)

fun init lim st = ProofHistory (History.init lim ((st, Seq.empty), []));
fun is_initial (ProofHistory prf) = History.is_initial prf;
fun current (ProofHistory prf) = fst (fst (History.current prf));
fun previous (ProofHistory prf) = Option.map (fst o fst) (History.previous prf);
val clear = app o History.clear;
val undo = app History.undo;
val redo = app History.redo;


(* backtracking *)

val back = hist_app (fn ((_, stq), nodes) =>
  (case Seq.pull stq of
    NONE => error "back: no alternatives"
  | SOME result => (result, nodes)));


(* apply transformer *)

fun applys f = hist_app (fn (node as (st, _), nodes) =>
  (case Seq.pull (f st) of
    NONE => error "empty result sequence -- proof command failed"
  | SOME results => (results, node :: nodes)));

fun apply f = applys (Seq.single o f);


end;
<commit_msg>added map_current; simplified internal type proof;<commit_after>(*  Title:      Pure/Isar/proof_history.ML
    ID:         $Id$
    Author:     <NAME>, TU Muenchen

Histories of proof states, with undo / redo and backtracking.
*)

signature PROOF_HISTORY =
sig
  type T
  val position: T -> int
  val init: int option -> Proof.state -> T
  val is_initial: T -> bool
  val map_current: (Proof.state -> Proof.state) -> T -> T
  val current: T -> Proof.state
  val previous: T -> Proof.state option
  val clear: int -> T -> T
  val undo: T -> T
  val redo: T -> T
  val back: T -> T
  val applys: (Proof.state -> Proof.state Seq.seq) -> T -> T
  val apply: (Proof.state -> Proof.state) -> T -> T
end;

structure ProofHistory: PROOF_HISTORY =
struct


(* datatype proof history *)

type node =
  Proof.state *                 (*first result*)
  Proof.state Seq.seq;          (*alternative results*)

type proof = node * int;        (*proof node, proof position*)

datatype T = ProofHistory of proof History.T;

fun app f (ProofHistory x) = ProofHistory (f x);
fun hist_app f = app (History.apply f);
fun hist_map f = app (History.map_current f);

fun position (ProofHistory prf) = snd (History.current prf);


(* generic history operations *)

fun init lim st = ProofHistory (History.init lim ((st, Seq.empty), 0));
fun is_initial (ProofHistory prf) = History.is_initial prf;
fun map_current f = hist_map (apfst (apfst f));
fun current (ProofHistory prf) = fst (fst (History.current prf));
fun previous (ProofHistory prf) = Option.map (fst o fst) (History.previous prf);
val clear = app o History.clear;
val undo = app History.undo;
val redo = app History.redo;


(* backtracking *)

val back = hist_app (fn ((_, stq), position) =>
  (case Seq.pull stq of
    NONE => error "back: no alternatives"
  | SOME result => (result, position)));


(* apply transformer *)

fun applys f = hist_app (fn (node as (st, _), position) =>
  (case Seq.pull (f st) of
    NONE => error "empty result sequence -- proof command failed"
  | SOME results => (results, position + 1)));

fun apply f = applys (Seq.single o f);


end;

{'fraction_non_alphanumeric': 0.13076152304609218, 'fraction_numerical': 0.000501002004008016, 'mean_word_length': 3.142116182572614, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7125400', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1307, 'n_words': 490}