| 1 | {- | |
|---|
| 2 | Module :$Header$ |
|---|
| 3 | Description : history management functions |
|---|
| 4 | Copyright : uni-bremen and DFKI |
|---|
| 5 | License : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt |
|---|
| 6 | Maintainer : r.pascanu@jacobs-university.de |
|---|
| 7 | Stability : provisional |
|---|
| 8 | Portability : portable |
|---|
| 9 | |
|---|
| 10 | Interfaces.History contains different functions that deal |
|---|
| 11 | with history |
|---|
| 12 | |
|---|
| 13 | -} |
|---|
| 14 | |
|---|
| 15 | module Interfaces.History |
|---|
| 16 | ( undoOneStep |
|---|
| 17 | , redoOneStep |
|---|
| 18 | , add2history |
|---|
| 19 | ) where |
|---|
| 20 | |
|---|
| 21 | import Interfaces.DataTypes |
|---|
| 22 | import Common.LibName |
|---|
| 23 | |
|---|
| 24 | import Proofs.AbstractState |
|---|
| 25 | import Proofs.EdgeUtils |
|---|
| 26 | |
|---|
| 27 | import Static.DevGraph |
|---|
| 28 | |
|---|
| 29 | import qualified Data.Map as Map |
|---|
| 30 | import Data.List |
|---|
| 31 | |
|---|
| 32 | -- | Datatype used to differentiate between the two actions (so that code does |
|---|
| 33 | -- not get duplicated |
|---|
| 34 | data UndoOrRedo = |
|---|
| 35 | DoUndo |
|---|
| 36 | | DoRedo |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | -- write2Hist :: IORef IntState -> String -> [UndoRedoElem] -> IO() |
|---|
| 40 | -- write2Hist iost name descr |
|---|
| 41 | -- = do |
|---|
| 42 | -- ost <- readIORef iost |
|---|
| 43 | -- let nwst = add2history name ost descr |
|---|
| 44 | -- writeIORef iost nwst |
|---|
| 45 | |
|---|
| 46 | add2history :: String -> IntState -> [UndoRedoElem] -> IntState |
|---|
| 47 | add2history name st descr |
|---|
| 48 | = case undoList $ i_hist st of |
|---|
| 49 | [] -> let nwEl = Int_CmdHistoryDescription { |
|---|
| 50 | cmdName = name, |
|---|
| 51 | cmdDescription = descr } |
|---|
| 52 | hist = i_hist st |
|---|
| 53 | in st{i_hist=hist{ undoList = nwEl:(undoList hist) } } |
|---|
| 54 | h:r -> |
|---|
| 55 | case cmdName h of |
|---|
| 56 | [] -> let nwEl = h { cmdName = name, |
|---|
| 57 | cmdDescription = descr ++ (cmdDescription h) } |
|---|
| 58 | hist = i_hist st |
|---|
| 59 | in st{i_hist=hist{ undoList = nwEl:r }} |
|---|
| 60 | _ -> let nwEl = Int_CmdHistoryDescription { |
|---|
| 61 | cmdName = name, |
|---|
| 62 | cmdDescription = descr } |
|---|
| 63 | hist = i_hist st |
|---|
| 64 | in st{i_hist=hist{ undoList = nwEl :h:r } } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | -- | Undo or redo a command that modified the development graph |
|---|
| 70 | undoRedoDgCmd :: UndoOrRedo -> IntState -> LIB_NAME -> IntState |
|---|
| 71 | undoRedoDgCmd actionType state ln = |
|---|
| 72 | case i_state state of |
|---|
| 73 | -- should I return an error message?? |
|---|
| 74 | Nothing -> state |
|---|
| 75 | Just dgS -> |
|---|
| 76 | let |
|---|
| 77 | -- take ln from the history storage ?? |
|---|
| 78 | -- in contrast to GUI here you operate with only one ln at a time |
|---|
| 79 | dg = lookupDGraph ln (i_libEnv dgS) |
|---|
| 80 | dg' = fst $( case actionType of |
|---|
| 81 | DoUndo -> undoHistStep |
|---|
| 82 | DoRedo -> redoHistStep) dg |
|---|
| 83 | newEnv = Map.insert ln dg' (i_libEnv dgS) |
|---|
| 84 | newst = state { |
|---|
| 85 | i_state = Just $ dgS { |
|---|
| 86 | i_libEnv = newEnv |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | in newst |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | -- | Analyze changes to the selected nodes, return new nodes plus a list |
|---|
| 94 | -- of changes that would undo last changes |
|---|
| 95 | processList :: [ListChange]-> [Int_NodeInfo] |
|---|
| 96 | -> [ListChange] -> ([Int_NodeInfo],[ListChange]) |
|---|
| 97 | processList ls elems acc |
|---|
| 98 | = case ls of |
|---|
| 99 | -- if nothing to process return elements and changes |
|---|
| 100 | [] -> (elems, acc) |
|---|
| 101 | x:l-> |
|---|
| 102 | -- else check what type of change we are dealing with |
|---|
| 103 | case x of |
|---|
| 104 | -- if it is a change in axioms |
|---|
| 105 | AxiomsChange nwaxms nb -> |
|---|
| 106 | -- apply change and store the undo action |
|---|
| 107 | let nwls = map (\y -> |
|---|
| 108 | case y of |
|---|
| 109 | Element ps nb' -> |
|---|
| 110 | case nb' == nb of |
|---|
| 111 | True ->((Element ps{includedAxioms = nwaxms} nb) |
|---|
| 112 | ,[AxiomsChange (includedAxioms ps) nb]) |
|---|
| 113 | False -> (y,[]) ) elems |
|---|
| 114 | nwelems = map (\y -> fst y) nwls |
|---|
| 115 | changesLs = concatMap (\y-> snd y) nwls |
|---|
| 116 | in processList l nwelems (changesLs ++ acc) |
|---|
| 117 | -- if it is a change in goals |
|---|
| 118 | GoalsChange nwgls nb -> |
|---|
| 119 | -- apply change and store the undo action |
|---|
| 120 | let nwls = map (\y -> |
|---|
| 121 | case y of |
|---|
| 122 | Element ps nb' -> |
|---|
| 123 | case nb' == nb of |
|---|
| 124 | True ->((Element ps{selectedGoals = nwgls} nb) |
|---|
| 125 | ,[GoalsChange (selectedGoals ps) nb]) |
|---|
| 126 | False ->(y,[])) elems |
|---|
| 127 | nwelems = map (\y -> fst y) nwls |
|---|
| 128 | changeLs = concatMap (\y ->snd y) nwls |
|---|
| 129 | in processList l nwelems (changeLs ++ acc) |
|---|
| 130 | -- if selected nodes change |
|---|
| 131 | NodesChange nwelems -> |
|---|
| 132 | processList l nwelems ((NodesChange elems):acc) |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | -- | Process one step of undo or redo |
|---|
| 136 | processAny :: UndoOrRedo -> IntState -> IntState |
|---|
| 137 | processAny actype state = |
|---|
| 138 | let hist = case actype of |
|---|
| 139 | -- find out the list of actions according to the action |
|---|
| 140 | -- (undo/redo) |
|---|
| 141 | DoUndo -> undoList $ i_hist state |
|---|
| 142 | DoRedo -> redoList $ i_hist state |
|---|
| 143 | in |
|---|
| 144 | case hist of |
|---|
| 145 | [] -> state |
|---|
| 146 | x:l-> |
|---|
| 147 | let |
|---|
| 148 | (nwst,ch) = processUndoRedoElems actype (cmdDescription x) state [] |
|---|
| 149 | x' = x { cmdDescription = ch } |
|---|
| 150 | nwstate = case actype of |
|---|
| 151 | DoUndo -> nwst { |
|---|
| 152 | i_hist = IntHistory { |
|---|
| 153 | undoList = l, |
|---|
| 154 | redoList =x':(redoList$ i_hist state) |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | DoRedo -> nwst { |
|---|
| 158 | i_hist = IntHistory { |
|---|
| 159 | redoList = l, |
|---|
| 160 | undoList = x':(undoList$i_hist state) |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | in nwstate |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | -- | Process a list of undo or redo changes |
|---|
| 167 | processUndoRedoElems :: UndoOrRedo -> [UndoRedoElem] -> IntState |
|---|
| 168 | -> [UndoRedoElem] ->(IntState,[UndoRedoElem]) |
|---|
| 169 | processUndoRedoElems actype ls state acc |
|---|
| 170 | = case i_state state of |
|---|
| 171 | Nothing -> (state,[]) |
|---|
| 172 | Just ist -> |
|---|
| 173 | case ls of |
|---|
| 174 | [] -> (state,acc) |
|---|
| 175 | (UseThmChange sw):l -> |
|---|
| 176 | let nwst = state { |
|---|
| 177 | i_state = Just $ ist { useTheorems = sw } } |
|---|
| 178 | ch = UseThmChange $ useTheorems ist |
|---|
| 179 | in processUndoRedoElems actype l nwst (ch:acc) |
|---|
| 180 | (Save2FileChange sw):l -> |
|---|
| 181 | let nwst = state { |
|---|
| 182 | i_state = Just $ ist { save2file = sw } } |
|---|
| 183 | ch = Save2FileChange $ save2file ist |
|---|
| 184 | in processUndoRedoElems actype l nwst (ch:acc) |
|---|
| 185 | (ProverChange nwp):l -> |
|---|
| 186 | let nwst = state { |
|---|
| 187 | i_state = Just $ ist { prover = nwp } } |
|---|
| 188 | ch = ProverChange $ prover ist |
|---|
| 189 | in processUndoRedoElems actype l nwst (ch:acc) |
|---|
| 190 | (ConsCheckerChange nwc):l -> |
|---|
| 191 | let nwst = state { |
|---|
| 192 | i_state = Just $ ist { consChecker = nwc} } |
|---|
| 193 | ch = ConsCheckerChange $ consChecker ist |
|---|
| 194 | in processUndoRedoElems actype l nwst (ch:acc) |
|---|
| 195 | (ScriptChange nws):l -> |
|---|
| 196 | let nwst = state { |
|---|
| 197 | i_state = Just $ ist { script = nws } } |
|---|
| 198 | ch = ScriptChange $ script ist |
|---|
| 199 | in processUndoRedoElems actype l nwst (ch:acc) |
|---|
| 200 | (LoadScriptChange sw):l -> |
|---|
| 201 | let nwst = state { |
|---|
| 202 | i_state = Just $ ist { loadScript = sw } } |
|---|
| 203 | ch = LoadScriptChange $ loadScript ist |
|---|
| 204 | in processUndoRedoElems actype l nwst (ch:acc) |
|---|
| 205 | (CComorphismChange nwc):l -> |
|---|
| 206 | let nwst = state { |
|---|
| 207 | i_state = Just $ ist { cComorphism = nwc} } |
|---|
| 208 | ch = CComorphismChange $ cComorphism ist |
|---|
| 209 | in processUndoRedoElems actype l nwst (ch:acc) |
|---|
| 210 | (DgCommandChange nln):l -> |
|---|
| 211 | let nwst = undoRedoDgCmd actype state nln |
|---|
| 212 | ch = DgCommandChange nln |
|---|
| 213 | in processUndoRedoElems actype l nwst (ch:acc) |
|---|
| 214 | (ListChange nls):l -> |
|---|
| 215 | let (nwels,lc) = processList nls (elements ist) [] |
|---|
| 216 | nwst = state { |
|---|
| 217 | i_state = Just $ ist { elements = nwels } } |
|---|
| 218 | ch = ListChange lc |
|---|
| 219 | in processUndoRedoElems actype l nwst (ch:acc) |
|---|
| 220 | (IStateChange nist):l -> |
|---|
| 221 | let nwst = state { i_state = nist } |
|---|
| 222 | ch = IStateChange $ Just ist |
|---|
| 223 | in processUndoRedoElems actype l nwst (ch:acc) |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | undoOneStep :: IntState -> IntState |
|---|
| 227 | undoOneStep = processAny DoUndo |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | redoOneStep :: IntState -> IntState |
|---|
| 231 | redoOneStep = processAny DoRedo |
|---|