prednote-0.22.0.2: Build and evaluate trees of predicates

Safe HaskellNone

Data.Prednote.Predbox

Contents

Description

Trees of predicates.

Exports names which conflict with Prelude names, so you probably want to import this module qualified.

Synopsis

The Predbox tree

type Hide = BoolSource

Determines whether a result is shown by default.

data Predbox a Source

A predicate. Each Predbox contains a tree of Node.

Constructors

Predbox 

Fields

pLabel :: Label

Label used when showing the results

pHide :: Bool -> Hide

As results are computed, this function is applied to the result. If this function returns False, then this Predbox will not be shown by default in the results.

pNode :: Node a
 

data Node a Source

Constructors

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.

Instances

Creating Predbox.

All functions create Predbox that are shown by default.

predicate :: Label -> (a -> Bool) -> Predbox aSource

Creates and labels predicates.

and :: [Predbox a] -> Predbox aSource

Creates And Predbox using a generic name

or :: [Predbox a] -> Predbox aSource

Creates Or Predbox using a generic name

not :: Predbox a -> Predbox aSource

Creates Not Predbox using a generic name

(&&&) :: 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.

always :: Predbox aSource

Always True

never :: Predbox aSource

Always False

Controlling whether Predbox are shown in the results

hide :: Predbox a -> Predbox aSource

Changes a Predbox so it is always hidden by default.

show :: Predbox a -> Predbox aSource

Changes a Predbox so it is always shown by default.

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

data Result Source

The result from evaluating a Predbox.

Constructors

Result 

Fields

rLabel :: Label

The label from the original Predbox

rBool :: Bool

The boolean result from evaluating the node. If the node is an predicate, this is the result of applying the predicate function to the subject. Otherwise, this is the result of application of the appropriate boolean operation to the child nodes.

rHide :: Hide

Is this result hidden in the result by default? Hiding only affects presentation; it does not affect how this Predbox affects any parent Predbox.

rNode :: RNode
 

Instances

data RNode Source

Instances

Showing and evaluating Predbox

evaluate :: Predbox a -> a -> ResultSource

Applies a Predbox to a particular value, known as the subject.

type IndentAmt = IntSource

The number of spaces to use for each level of indentation.

type Level = IntSource

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.

showResultSource

Arguments

:: IndentAmt

Indent each level by this many spaces

-> ShowAll

If True, shows all Predbox, even ones where rHide is True. Otherwise, respects rHide and does not show hidden Predbox.

-> 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.

showTopResultSource

Arguments

:: 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 rHide is True. Otherwise, respects rHide and does not show hidden Predbox.

-> 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.

filter :: Predbox a -> [a] -> [a]Source

verboseFilterSource

Arguments

:: (a -> Text)

How to describe each subject

-> IndentAmt

Indent each level by this many spaces

-> ShowAll

If True, shows all Predbox, even ones where rHide is True. Otherwise, respects rHide and does not show hidden Predbox.

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

compareBySource

Arguments

:: 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.

compareByMaybeSource

Arguments

:: 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.

greaterBySource

Arguments

:: 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 two items

-> Predbox a 

lessBySource

Arguments

:: 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 two items

-> Predbox a 

equalBySource

Arguments

:: 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 two items

-> Predbox a 

greaterEqBySource

Arguments

:: 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 two items

-> Predbox a 

lessEqBySource

Arguments

:: 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 two items

-> Predbox a 

notEqBySource

Arguments

:: 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 two items

-> Predbox a 

Overloaded

compareSource

Arguments

:: (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.

greaterSource

Arguments

:: (Show a, Ord a) 
=> Text

How to show the item being compared; used to describe the Predbox

-> a

The right hand side of the comparison.

-> Predbox a 

lessSource

Arguments

:: (Show a, Ord a) 
=> Text

How to show the item being compared; used to describe the Predbox

-> a

The right hand side of the comparison.

-> Predbox a 

equalSource

Arguments

:: (Show a, Ord a) 
=> Text

How to show the item being compared; used to describe the Predbox

-> a

The right hand side of the comparison.

-> Predbox a 

greaterEqSource

Arguments

:: (Show a, Ord a) 
=> Text

How to show the item being compared; used to describe the Predbox

-> a

The right hand side of the comparison.

-> Predbox a 

lessEqSource

Arguments

:: (Show a, Ord a) 
=> Text

How to show the item being compared; used to describe the Predbox

-> a

The right hand side of the comparison.

-> Predbox a 

notEqSource

Arguments

:: (Show a, Ord a) 
=> Text

How to show the item being compared; used to describe the Predbox

-> a

The right hand side of the comparison.

-> Predbox a 

parseComparerSource

Arguments

:: 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.