numerals-0.4.1: Convert numbers to number words

Safe HaskellNone
LanguageHaskell98

Text.Numeral.Rules

Contents

Description

Rules to convert numbers to an expression language.

Synopsis

The Rule type

type Rule a = (a -> Exp) -> a -> Exp Source #

A rule on how to convert a number into an expression language. Notice how this type is similar to the type of the $ operator.

Rule combinators

conditional Source #

Arguments

:: (a -> Bool)

Predicate on input value ("if").

-> Rule a

Rule to apply when predicate holds ("then").

-> Rule a

Rule to apply when predicate does not hold ("else").

-> Rule a 

The 'if-then-else' concept for rules. Applies the first rule if the predicate holds on the input value, otherwise applies the second rule.

combine :: Rule a -> Rule a -> Rule a Source #

Tries to apply the first rule, if that produces an Unknown value it applies the second rule.

mapRule :: (a -> a) -> Rule a -> Rule a Source #

Transform a value before it is given to a rule.

findRule Source #

Arguments

:: (Ord a, Num a) 
=> (a, Rule a)

First interval rule.

-> [(a, Rule a)]

Interval rule list.

-> a

Upper bound of the last interval.

-> Rule a 

Chooses which rule to apply to an input value based on a interval list of rules.

Rules

unknown :: Rule a Source #

A rule that always fails to convert a value. It constantly produces the unknown value.

>>> (fix unknown) (3 :: Integer) :: Exp
Unknown

lit :: Integral a => Rule a Source #

The literal rule. Converts its argument into a literal expression.

>>> lit (fix unknown) (3 :: Integer) :: Exp
Lit 3

In this example lit is applied to the nonsense rule "fix unknown". Lit ignores that function, which is why we can pass it anything we want, including itself.

>>> lit (fix undefined) (3 :: Integer) :: Exp
Lit 3
>>> (fix lit) (3 :: Integer) :: Exp
Lit 3

lit1 :: Integral a => Rule a Source #

A variant on the lit rule which always multiplies its argument with 1. Useful for languages which have numerals of the form "one hundred and three" as opposed to "hundred and three".

>>> lit1 (fix unknown) (3 :: Integer) :: Exp
Mul (Lit 1) (Lit 3)

pos :: (Ord a, Num a) => Rule a Source #

>>> (pos $ lit $ fix unknown) (3 :: Integer) :: Exp
Lit 3
>>> (pos $ lit $ fix unknown) (-3 :: Integer) :: Exp
Neg (Lit 3)

checkPos :: (Ord a, Num a) => Rule a Source #

>>> (checkPos $ lit $ fix unknown) (3 :: Integer) :: Exp
Lit 3
>>> (checkPos $ lit $ fix unknown) (-3 :: Integer) :: Exp
Unknown

add :: Num a => a -> Side -> Rule a Source #

>>> (add 10 L $ lit $ fix unknown) (13 :: Integer) :: Exp
Add (Lit 3) (Lit 10)

mul :: Integral a => a -> Side -> Side -> Rule a Source #

>>> (mul 10 R L $ lit $ fix unknown) (42 :: Integer) :: Exp
Add (Mul (Lit 4) (Lit 10)) (Lit 2)

mul1 :: Integral a => a -> Side -> Side -> Rule a Source #

sub :: Integral a => a -> Rule a Source #

>>> (sub 20 $ lit $ fix unknown) (18 :: Integer) :: Exp
Sub (Lit 2) (Lit 20)

mulScale_ Source #

Arguments

:: Integral a 
=> ((a -> Exp) -> a -> Exp -> Side -> Exp)

Performs the multiplication.

-> a

Base.

-> a

Offset.

-> Side

Add side.

-> Side

Mul side.

-> Rule a

Big num rule.

-> Rule a 

mulScale Source #

Arguments

:: Integral a 
=> a

Base.

-> a

Offset.

-> Side

Add side.

-> Side

Mul side.

-> Rule a

Big num rule.

-> Rule a 

mulScale1 Source #

Arguments

:: Integral a 
=> a

Base.

-> a

Offset.

-> Side

Add side.

-> Side

Mul side.

-> Rule a

Big num rule.

-> Rule a 

shortScale Source #

Arguments

:: Integral a 
=> Side

Add side.

-> Side

Mul side.

-> Rule a

Big num rule.

-> Rule a 

longScale Source #

Arguments

:: Integral a 
=> Side

Add side.

-> Side

Mul side.

-> Rule a

Big num rule.

-> Rule a 

pelletierScale Source #

Arguments

:: Integral a 
=> Side

Add side.

-> Side

Mul side.

-> Rule a

Big num rule.

-> Rule a 

shortScale1 Source #

Arguments

:: Integral a 
=> Side

Add side.

-> Side

Mul side.

-> Rule a

Big num rule.

-> Rule a 

longScale1 Source #

Arguments

:: Integral a 
=> Side

Add side.

-> Side

Mul side.

-> Rule a

Big num rule.

-> Rule a 

pelletierScale1 Source #

Arguments

:: Integral a 
=> Side

Add side.

-> Side

Mul side.

-> Rule a

Big num rule.

-> Rule a 

mkStep Source #

Arguments

:: Integral a 
=> Rule a

lit rule

-> (a -> Side -> Rule a)

add rule

-> (a -> Side -> Side -> Rule a)

mul rule

-> a 
-> a 
-> Side 
-> Side 
-> Rule a 

step :: Integral a => a -> a -> Side -> Side -> Rule a Source #

step1 :: Integral a => a -> a -> Side -> Side -> Rule a Source #

Grammar Rules