Safe Haskell | None |
---|
Trees of predicates.
Exports names which conflict with Prelude names, so you probably want to import this module qualified.
- type Label = Text
- type Hide = Bool
- data Predbox a = Predbox {}
- data Node a
- predicate :: Label -> (a -> Bool) -> Predbox a
- and :: [Predbox a] -> Predbox a
- or :: [Predbox a] -> Predbox a
- not :: Predbox a -> Predbox a
- (&&&) :: Predbox a -> Predbox a -> Predbox a
- (|||) :: Predbox a -> Predbox a -> Predbox a
- always :: Predbox a
- never :: Predbox a
- hide :: Predbox a -> Predbox a
- show :: Predbox a -> Predbox a
- hideTrue :: Predbox a -> Predbox a
- hideFalse :: Predbox a -> Predbox a
- rename :: (Text -> Text) -> Predbox a -> Predbox a
- data Result = Result {}
- data RNode
- evaluate :: Predbox a -> a -> Result
- evaluateNode :: Node a -> a -> RNode
- type IndentAmt = Int
- type Level = Int
- type ShowAll = Bool
- showResult :: IndentAmt -> ShowAll -> Level -> Result -> [Chunk]
- showTopResult :: Text -> IndentAmt -> Level -> ShowAll -> Result -> [Chunk]
- showPredbox :: IndentAmt -> Level -> Predbox a -> [Chunk]
- filter :: Predbox a -> [a] -> [a]
- verboseFilter :: (a -> Text) -> IndentAmt -> ShowAll -> Predbox a -> [a] -> ([Chunk], [a])
- compareBy :: Text -> Text -> (a -> Ordering) -> Ordering -> Predbox a
- compareByMaybe :: Text -> Text -> (a -> Maybe Ordering) -> Ordering -> Predbox a
- greaterBy :: Text -> Text -> (a -> Ordering) -> Predbox a
- lessBy :: Text -> Text -> (a -> Ordering) -> Predbox a
- equalBy :: Text -> Text -> (a -> Ordering) -> Predbox a
- greaterEqBy :: Text -> Text -> (a -> Ordering) -> Predbox a
- lessEqBy :: Text -> Text -> (a -> Ordering) -> Predbox a
- notEqBy :: Text -> Text -> (a -> Ordering) -> Predbox a
- compare :: (Show a, Ord a) => Text -> a -> Ordering -> Predbox a
- greater :: (Show a, Ord a) => Text -> a -> Predbox a
- less :: (Show a, Ord a) => Text -> a -> Predbox a
- equal :: (Show a, Ord a) => Text -> a -> Predbox a
- greaterEq :: (Show a, Ord a) => Text -> a -> Predbox a
- lessEq :: (Show a, Ord a) => Text -> a -> Predbox a
- notEq :: (Show a, Ord a) => Text -> a -> Predbox a
- parseComparer :: Text -> (Ordering -> Predbox a) -> Maybe (Predbox a)
The Predbox tree
A predicate. Each Predbox contains a tree of Node.
And [Predbox a] | Conjunction. If any Predbox in the list is False, the result is False. If the list is empty, the result is True. |
Or [Predbox a] | Disjunction. If at least one Predbox in the list is True, the result it True. If the list is empty, the result is False. |
Not (Predbox a) | Negation |
Predicate (a -> Bool) | Most basic building block. |
Creating Predbox.
All functions create Predbox that are shown by default.
(&&&) :: Predbox a -> Predbox a -> Predbox aSource
Forms a Predbox using and
; assigns a generic label.
(|||) :: Predbox a -> Predbox a -> Predbox aSource
Forms a Predbox using or
; assigns a generic label.
Controlling whether Predbox are shown in the results
hideTrue :: Predbox a -> Predbox aSource
Changes a Predbox so that it is hidden if its result is True.
hideFalse :: Predbox a -> Predbox aSource
Changes a Predbox so that it is hidden if its result is False.
Renaming Predbox
rename :: (Text -> Text) -> Predbox a -> Predbox aSource
Renames the top level of the Predbox. The function you pass will be applied to the old name.
Result
The result from evaluating a Predbox.
Result | |
|
Showing and evaluating Predbox
evaluate :: Predbox a -> a -> ResultSource
Applies a Predbox to a particular value, known as the subject.
evaluateNode :: Node a -> a -> RNodeSource
How many levels of indentation to use. Typically you will start this at zero. It is incremented by one for each level as functions descend through the tree.
:: IndentAmt | Indent each level by this many spaces |
-> ShowAll | If True, shows all Predbox, even ones where |
-> Level | How deep in the tree we are; this increments by one for each level of descent. |
-> Result | The result to show |
-> [Chunk] |
Shows a Result in a pretty way with colors and indentation.
:: Text | Label to add to the top of the tree. |
-> IndentAmt | Indent each level by this many spaces |
-> Level | Indent the top by this many levels |
-> ShowAll | If True, shows all Predbox, even ones where |
-> Result | The result to show |
-> [Chunk] |
Shows the top of a Result tree and all the child Results. Adds a short label at the top of the tree.
showPredbox :: IndentAmt -> Level -> Predbox a -> [Chunk]Source
Shows a Predbox tree without evaluating it.
:: (a -> Text) | How to describe each subject |
-> IndentAmt | Indent each level by this many spaces |
-> ShowAll | If True, shows all Predbox, even ones where |
-> Predbox a | Used to perform the filtering |
-> [a] | |
-> ([Chunk], [a]) |
Filters a list. Also returns chunks describing the process.
Helpers for building common Predbox
Non-overloaded
Each of these functions builds a Predbox that compares two items. The predicate in the Predbox is applied to an item that is considered to be the left hand side of the comparison. The left hand side side can change; the right hand side is baked into the Predbox.
For example, to build a Predbox that returns True if an item is greater than 5:
>>>
:set -XOverloadedStrings
>>>
let p = compareBy "5" "integer" (`Prelude.compare` (5 :: Integer)) GT
>>>
rBool . evaluate p $ 6
True>>>
rBool . evaluate p $ 4
False
:: Text | How to show the item being compared; used to describe the Predbox |
-> Text | Description of the type of thing that is being matched |
-> (a -> Ordering) | How to compare an item against the right hand side. Return LT if the item is less than the right hand side; GT if greater; EQ if equal to the right hand side. |
-> Ordering | When subjects are compared, this ordering must be the result in order for the Predbox to be True; otherwise it is False. The subject will be on the left hand side. |
-> Predbox a |
Build a Predbox that compares items.
:: Text | How to show the item being compared |
-> Text | Description of type of thing being matched |
-> (a -> Maybe Ordering) | How to compare against right hand side. If Nothing, a Predbox that always returns False is returned. |
-> Ordering | Ordering that must result for the Predbox to be True |
-> Predbox a |
Builds a Predbox for items that might fail to return a comparison.
Overloaded
:: (Show a, Ord a) | |
=> Text | Description of the type of thing being matched |
-> a | The right hand side of the comparison. |
-> Ordering | When subjects are compared, this ordering must be the result in order for the Predbox to be True; otherwise it is False. The subject will be on the left hand side. |
-> Predbox a |
Overloaded version of compareBy
.
:: Text | The string with the comparer to be parsed |
-> (Ordering -> Predbox a) | A function that, when given an ordering, returns a Predbox |
-> Maybe (Predbox a) | If an invalid comparer string is given, Nothing; otherwise, the Predbox. |
Parses a string to find the correct comparer; returns the correct function to build a Predbox.