pred-trie-0.6.0: Predicative tries

Copyright(c) 2015 Athan Clark
LicenseBSD-style
Maintainerathan.clark@gmail.com
Stabilityexperimental
PortabilityGHC
Safe HaskellNone
LanguageHaskell2010

Data.Trie.Pred.Interface

Contents

Description

This module defines a "builder" monad, which aides in the process of building a trie. It's a monad transformer, so you can use it alongside whichever context you're already working in.

myBuilder :: ( Eq k
             , Hashable k
             , MonadIO m
             ) => PTBuilder String Int m ()
myBuilder = do
  insertHere 0
  insert ("some" ./ "path" ./ nil) 1
  insert ("some" ./ pred "pred-chunk" upperPred ./ nil) 2
  prefix ("some") $ do
    insert ("thing" ./ nil) 3
    insert ("else" ./ nil) 4
    data <- liftIO (doSomething)
    insert ("another" ./ "thing" ./ nil) data
  where
    uppderPred :: String -> Maybe String
    uppderPred s | all isUpperCase s = Just s
                 | otherwise         = Nothing

Then we can get our trie to perform lookups by executing the monad:

main :: IO ()
main = do
  trie <- execPTBuilderT myBuilder
  print (lookup ["foo", "bar", "baz"] trie)

Synopsis

Construction

Builder Monad

newtype PTBuilderT k v m a Source #

Constructors

PTBuilderT 

Fields

Instances

MonadTrans (PTBuilderT k v) Source # 

Methods

lift :: Monad m => m a -> PTBuilderT k v m a #

(Monad m, Eq k, Hashable k) => MonadWriter (RootedPredTrie k v) (PTBuilderT k v m) Source # 

Methods

writer :: (a, RootedPredTrie k v) -> PTBuilderT k v m a #

tell :: RootedPredTrie k v -> PTBuilderT k v m () #

listen :: PTBuilderT k v m a -> PTBuilderT k v m (a, RootedPredTrie k v) #

pass :: PTBuilderT k v m (a, RootedPredTrie k v -> RootedPredTrie k v) -> PTBuilderT k v m a #

Monad m => MonadState (RootedPredTrie k v) (PTBuilderT k v m) Source # 

Methods

get :: PTBuilderT k v m (RootedPredTrie k v) #

put :: RootedPredTrie k v -> PTBuilderT k v m () #

state :: (RootedPredTrie k v -> (a, RootedPredTrie k v)) -> PTBuilderT k v m a #

Monad m => Monad (PTBuilderT k v m) Source # 

Methods

(>>=) :: PTBuilderT k v m a -> (a -> PTBuilderT k v m b) -> PTBuilderT k v m b #

(>>) :: PTBuilderT k v m a -> PTBuilderT k v m b -> PTBuilderT k v m b #

return :: a -> PTBuilderT k v m a #

fail :: String -> PTBuilderT k v m a #

Functor m => Functor (PTBuilderT k v m) Source # 

Methods

fmap :: (a -> b) -> PTBuilderT k v m a -> PTBuilderT k v m b #

(<$) :: a -> PTBuilderT k v m b -> PTBuilderT k v m a #

Monad m => Applicative (PTBuilderT k v m) Source # 

Methods

pure :: a -> PTBuilderT k v m a #

(<*>) :: PTBuilderT k v m (a -> b) -> PTBuilderT k v m a -> PTBuilderT k v m b #

liftA2 :: (a -> b -> c) -> PTBuilderT k v m a -> PTBuilderT k v m b -> PTBuilderT k v m c #

(*>) :: PTBuilderT k v m a -> PTBuilderT k v m b -> PTBuilderT k v m b #

(<*) :: PTBuilderT k v m a -> PTBuilderT k v m b -> PTBuilderT k v m a #

execPTBuilderT :: (Monad m, Eq k, Hashable k) => PTBuilderT k v m a -> m (RootedPredTrie k v) Source #

Combinators

insert :: (Monad m, Eq k, Hashable k, Singleton (PathChunks k xs) childContent (RootedPredTrie k resultContent), cleanxs ~ CatMaybes xs, ArityTypeListIso childContent cleanxs resultContent) => PathChunks k xs -> childContent -> PTBuilderT k resultContent m () Source #

insertHere :: (Monad m, Eq k, Hashable k) => v -> PTBuilderT k v m () Source #

prefix :: (Monad m, Eq k, Hashable k, cleanxs ~ CatMaybes xs, ExtrudeSoundly k cleanxs xs childContent resultContent) => PathChunks k xs -> PTBuilderT k childContent m () -> PTBuilderT k resultContent m () Source #

Specifying Paths

only :: k -> PathChunk k Nothing Source #

Match a literal key

pred :: k -> (k -> Maybe r) -> PathChunk k (Just r) Source #

Match with a predicate against the url chunk directly.

(./) :: PathChunk k mx -> PathChunks k xs -> PathChunks k (mx ': xs) infixr 9 Source #

The cons-cell for building a query path.

nil :: PathChunks k '[] Source #

The basis, equivalent to []

Query

lookup :: (Eq k, Hashable k) => [k] -> RootedPredTrie k a -> Maybe a Source #

match :: (Hashable k, Eq k) => [k] -> RootedPredTrie k a -> Maybe ([k], a, [k]) Source #

matches :: (Hashable k, Eq k) => [k] -> RootedPredTrie k a -> [([k], a, [k])] Source #

Delete

delete :: (Eq k, Hashable k) => [k] -> RootedPredTrie k a -> RootedPredTrie k a Source #

Types

data RootedPredTrie k a Source #

Instances

(Hashable k, Eq k) => Trie [] k RootedPredTrie Source # 

Methods

lookup :: [k] -> RootedPredTrie k a -> Maybe a #

insert :: [k] -> a -> RootedPredTrie k a -> RootedPredTrie k a #

delete :: [k] -> RootedPredTrie k a -> RootedPredTrie k a #

Functor (RootedPredTrie k) Source # 

Methods

fmap :: (a -> b) -> RootedPredTrie k a -> RootedPredTrie k b #

(<$) :: a -> RootedPredTrie k b -> RootedPredTrie k a #

(Show k, Show a) => Show (RootedPredTrie k a) Source # 
(Hashable k, Eq k) => Monoid (RootedPredTrie k a) Source # 
Singleton (PathChunks k ([] (Maybe *))) a (RootedPredTrie k a) Source # 

Methods

singleton :: PathChunks k [Maybe *] -> a -> RootedPredTrie k a Source #

Extrude (PathChunks k ([] (Maybe *))) (RootedPredTrie k a) (RootedPredTrie k a) Source # 
(Eq k, Hashable k, Typeable * r) => Extend (PathChunk k (Just * r)) (RootedPredTrie k (r -> a)) (RootedPredTrie k a) Source #

Existentially quantified case

Methods

extend :: PathChunk k (Just * r) -> RootedPredTrie k (r -> a) -> RootedPredTrie k a Source #

(Eq k, Hashable k) => Extend (PathChunk k (Nothing *)) (RootedPredTrie k a) (RootedPredTrie k a) Source #

Literal case

(Monad m, Eq k, Hashable k) => MonadWriter (RootedPredTrie k v) (PTBuilderT k v m) # 

Methods

writer :: (a, RootedPredTrie k v) -> PTBuilderT k v m a #

tell :: RootedPredTrie k v -> PTBuilderT k v m () #

listen :: PTBuilderT k v m a -> PTBuilderT k v m (a, RootedPredTrie k v) #

pass :: PTBuilderT k v m (a, RootedPredTrie k v -> RootedPredTrie k v) -> PTBuilderT k v m a #

Monad m => MonadState (RootedPredTrie k v) (PTBuilderT k v m) # 

Methods

get :: PTBuilderT k v m (RootedPredTrie k v) #

put :: RootedPredTrie k v -> PTBuilderT k v m () #

state :: (RootedPredTrie k v -> (a, RootedPredTrie k v)) -> PTBuilderT k v m a #

data PathChunks k (xs :: [Maybe *]) Source #

Container when defining route paths

Instances

(Extrude (PathChunks k xs) trie0 trie1, Extend (PathChunk k x) trie1 trie2) => Extrude (PathChunks k ((:) (Maybe *) x xs)) trie0 trie2 Source # 

Methods

extrude :: PathChunks k ((Maybe * ': x) xs) -> trie0 -> trie2 Source #

(Singleton (PathChunks k xs) new trie0, Extend (PathChunk k x) trie0 trie1) => Singleton (PathChunks k ((:) (Maybe *) x xs)) new trie1 Source # 

Methods

singleton :: PathChunks k ((Maybe * ': x) xs) -> new -> trie1 Source #

Singleton (PathChunks k ([] (Maybe *))) a (RootedPredTrie k a) Source # 

Methods

singleton :: PathChunks k [Maybe *] -> a -> RootedPredTrie k a Source #

Extrude (PathChunks k ([] (Maybe *))) (RootedPredTrie k a) (RootedPredTrie k a) Source # 

data PathChunk k (mx :: Maybe *) Source #

Constrained to AttoParsec, Regex-Compat and T.Text

Instances

IsString k => IsString (PathChunk k (Nothing *)) Source #

Use raw strings instead of prepending l

(Eq k, Hashable k, Typeable * r) => Extend (PathChunk k (Just * r)) (RootedPredTrie k (r -> a)) (RootedPredTrie k a) Source #

Existentially quantified case

Methods

extend :: PathChunk k (Just * r) -> RootedPredTrie k (r -> a) -> RootedPredTrie k a Source #

(Eq k, Hashable k) => Extend (PathChunk k (Nothing *)) (RootedPredTrie k a) (RootedPredTrie k a) Source #

Literal case