Report a paste

Please put a quick comment for the admin.

If it looks like spam, the admin will mark it as spam so that the spam filter picks it up in the future.

If the paste contains something private or offensive, it'll probably just be deleted.

XML Cursor Monad

{-# 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