XML Cursor Monad

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
{-# LANGUAGE RankNTypes #-}

module XmlCursor
    (
      C.toTree
    , C.fromElement
    , C.Cursor(..)
    , update
    , execUpdate
    , Update
    , module Text.XML.Light

    -- * Primitive operations
    , perform
    , perform'
    , performAnd
    , asks

    -- * Containing effects
    , failed
    , sandbox
    , run
    , run'
    , runAll

    -- * Moving around
    , parent
    , root
    , getChild
    , firstChild
    , lastChild
    , left
    , right
    , nextDF

    -- ** Searching
    , findChild
    , findLeft
    , findRight
    , findRec

    -- * Node classification
    , isRoot
    , isFirst
    , isLast
    , isLeaf
    , isChild
    , hasChildren
    , getNodeIndex

    -- * Updates
    , setContent
    , modifyContent
    -- , modifyContentM

    -- ** Inserting content
    , insertLeft
    , insertRight
    , insertGoLeft
    , insertGoRight

    -- ** Removing content
    , removeLeft
    , removeRight
    , removeGoLeft
    , removeGoRight
    , removeGoUp

    )
    where

import Control.Applicative
import Control.Monad

import Data.Maybe

import Text.XML.Light hiding
    ( findChild
    )
import qualified Text.XML.Light.Cursor as C

-- | The 'Update' type represents actions on an
-- XML cursor, where the Cursor is passed implictly
-- between statements.
-- Auxillary combinators may be used to update
-- or move the cursor into the XML document.
newtype Update a
    = Update { unUpdate :: forall r .
               (a -> C.Cursor -> r)  -- success
               -> r                  -- failure
               -> C.Cursor           -- initial state
               -> r
             }

instance Functor Update where
    f `fmap` (Update k)
        = Update $ \succ fail s ->
          k (succ . f) fail s

instance Applicative Update where
    pure x = Update $ \succ _ -> succ x
    (Update fk) <*> (Update ak)
        = Update $ \succ fail ->
          flip fk fail $ \f ->
          flip ak fail $ \a ->
          succ (f a)
    (Update ak) *> (Update bk)
        = Update $ \succ fail ->
          flip ak fail $ \_ ->
          flip bk fail $ \b ->
          succ b
    (Update ak) <* (Update bk)
        = Update $ \succ fail ->
          flip ak fail $ \a ->
          flip bk fail $ \_ ->
          succ a

instance Alternative Update where
    empty = Update $ \_ fail _ -> fail

    -- Note that we explicitly hang on
    -- to a reference to the passed in
    -- state while executing 'left',
    -- so we can pass it to 'right'
    -- if left fails. No other primitive
    -- combinator referrs to the internal
    -- state (other than the state-updating
    -- functions).
    (Update kLeft) <|> (Update kRight)
        = Update $ \succ fail s ->
          kLeft succ (kRight succ fail s) s
              

instance Monad Update where
    return = pure
    (>>) = (*>)
    fail = const empty
    (Update ma) >>= mf
        = Update $ \succ fail ->
          flip ma fail $ \a ->
          case mf a of Update k ->
                          k succ fail

instance MonadPlus Update where
    mzero = empty
    mplus = (<|>)

-- | Perform an action on a 'Cursor'. The action
-- may fail, in which case 'Nothing' is returned.
update :: C.Cursor -> Update a -> Maybe (a, C.Cursor)
update c (Update k) = k (\a s' -> Just (a, s')) Nothing c

execUpdate :: C.Cursor -> Update a -> Maybe C.Cursor
execUpdate c k
    = case update c k of
        Nothing -> Nothing
        Just (_, c') -> Just c'

-- | Perform an update or traversal. If the update
-- returns nothing, the computation fails.
perform :: (C.Cursor -> Maybe C.Cursor) -> Update ()
perform f
    = Update $ \succ fail s ->
      case f s of
        Nothing -> fail
        Just s' -> succ () s'

perform' :: (C.Cursor -> C.Cursor) -> Update ()
perform' f = perform $ Just . f

performAnd :: (C.Cursor -> Maybe (a, C.Cursor)) -> Update a
performAnd f
    = Update $ \succ fail s ->
      case f s of
        Nothing -> fail
        Just (a,s') -> succ a s'

asks :: (C.Cursor -> a) -> Update a
asks f = Update $ \succ _fail s -> succ (f s) s

-- | On failure, the position and state of the cursor
-- remains un-changed. Returns 'True' if the passed-in
-- action was successful.
failed :: Update a -> Update Bool
failed k = k *> return True <|> return False

-- | The passed in computation may not leave
-- the scope of the current cursor. On success,
-- the position of the cursor is garuanteed to
-- not have changed.
-- Siblings may be added, but not removed.
-- If siblings are added, we may finish on
-- one of the added siblings instead of the original
-- node.
sandbox :: Update a -> Update a
sandbox (Update k)
    = Update $ \succ fail s ->
      let s_safe = s {C.parents = [], C.lefts = [], C.rights = []}
          succ' x s'
              = succ x $ 
                (C.root s')
                { C.parents = C.parents s
                , C.lefts = C.lefts s' ++ C.lefts s
                , C.rights = C.rights s' ++ C.rights s  
                }
      in k succ' fail s_safe

-- | The passed in computation may not leave the scope
-- of the current cursor, and on
-- both success and failure we return to the orignal
-- position of the cursor in the document. Perfect for
-- executing actions which may or may not succeed, for which
-- you would like to be in a known state afterwards.
run :: Update a -> Update ()
run k = const () <$> run' k

-- | Similar to 'run', except the caller is notified if their
-- was a failure.
run' :: Update a -> Update Bool
run' k = sandbox (failed k)

-- | Execute 'run' on many actions in turn.
-- Each action may not excape the current cursor
-- scope (although it may land on a different
-- sibling).
runAll :: [Update a] -> Update ()
runAll = foldr (*>) (return ()) . map run


-- wrappers around the cursor functions in the xml package

-- | May fail
parent :: Update ()
parent = perform C.parent

root :: Update ()
root = perform' C.root

-- | May fail
getChild :: Int -> Update ()
getChild = perform . C.getChild

-- | May fail
firstChild :: Update ()
firstChild = perform C.firstChild

-- | May fail
lastChild :: Update ()
lastChild = perform C.lastChild

-- | May fail
left :: Update ()
left = perform C.left

-- | May fail
right :: Update ()
right = perform C.right

-- | May fail
nextDF :: Update ()
nextDF = perform C.nextDF

-- searching

-- | May fail
findChild :: (C.Cursor -> Bool) -> Update ()
findChild = perform . C.findChild

-- | May fail
findLeft :: (C.Cursor -> Bool) -> Update ()
findLeft = perform . C.findLeft

-- | May fail
findRight :: (C.Cursor -> Bool) -> Update ()
findRight = perform . C.findRight

-- | May fail
findRec :: (C.Cursor -> Bool) -> Update ()
findRec = perform . C.findRec

-- Node classification

isRoot :: Update Bool
isRoot = asks C.isRoot

isFirst :: Update Bool
isFirst = asks C.isFirst

isLast :: Update Bool
isLast = asks C.isLast

isLeaf :: Update Bool
isLeaf = asks C.isLeaf

isChild :: Update Bool
isChild = asks C.isChild

hasChildren :: Update Bool
hasChildren = asks C.hasChildren

getNodeIndex :: Update Int
getNodeIndex = asks C.getNodeIndex

-- updates

setContent :: Content -> Update ()
setContent = perform' . C.setContent

modifyContent :: (Content -> Content) -> Update ()
modifyContent = perform' . C.modifyContent

-- insertion

insertLeft :: Content -> Update ()
insertLeft = perform' . C.insertLeft

insertRight :: Content -> Update ()
insertRight = perform' . C.insertRight

insertGoLeft :: Content -> Update ()
insertGoLeft = perform' . C.insertGoLeft

insertGoRight :: Content -> Update ()
insertGoRight = perform' . C.insertGoRight

-- Removing Content

-- | May fail
removeLeft :: Update Content
removeLeft = performAnd C.removeLeft

-- | May fail
removeRight :: Update Content
removeRight = performAnd C.removeRight

-- | May fail
removeGoLeft :: Update ()
removeGoLeft = perform C.removeGoLeft

-- | May fail
removeGoRight :: Update ()
removeGoRight = perform C.removeGoRight

-- | May fail
removeGoUp :: Update ()
removeGoUp = perform C.removeGoUp