numerals-0.4: Convert numbers to number words

Text.Numeral.Rules

Contents

Description

Rules to convert numbers to an expression language.

Synopsis

# The Rule type

type Rule α β = (α -> β) -> α -> βSource

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

# Rule combinators

Arguments

 :: (α -> Bool) Predicate on input value ("if"). -> Rule α β Rule to apply when predicate holds ("then"). -> Rule α β Rule to apply when predicate does not hold ("else"). -> Rule α β

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 :: Unknown β => Rule α β -> Rule α β -> Rule α βSource

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

mapRule :: (α -> α) -> Rule α β -> Rule α βSource

Transform a value before it is given to a rule.

Arguments

 :: (Ord α, Num α, Unknown β) => (α, Rule α β) First interval rule. -> [(α, Rule α β)] Interval rule list. -> α Upper bound of the last interval. -> Rule α β

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

# Rules

unknown :: Unknown β => Rule α βSource

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

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

lit :: (Integral α, Lit β) => Rule α βSource

The literal rule. Converts its argument into a `lit`eral 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 α, Lit β, Mul β) => Rule α β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 α, Num α, Lit β, Neg β) => Rule α βSource

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

checkPos :: (Ord α, Num α, Unknown β, Lit β) => Rule α βSource

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

dual :: Dual β => Rule α βSource

````>>> ````(dual \$ lit \$ fix unknown) (3 :: Integer) :: Exp
```Dual (Lit 3)
```

plural :: Plural β => Rule α βSource

````>>> ````(plural \$ lit \$ fix unknown) (3 :: Integer) :: Exp
```Plural (Lit 3)
```

inflection :: Inflection β => (Inf β -> Inf β) -> Rule α βSource

Changes the inflection of a subexpression.

add :: (Num α, Add β) => α -> Side -> Rule α βSource

````>>> ````(add 10 L \$ lit \$ fix unknown) (13 :: Integer) :: Exp
```

mul :: (Integral α, Add β, Mul β) => α -> Side -> Side -> Rule α βSource

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

mul1 :: (Integral α, Lit β, Add β, Mul β) => α -> Side -> Side -> Rule α βSource

sub :: (Integral α, Sub β) => α -> Rule α βSource

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

Arguments

 :: (Integral α, Scale α, Unknown β, Add β, Mul β, Scale β) => ((α -> β) -> α -> β -> Side -> β) Performs the multiplication. -> α Base. -> α Offset. -> Side Add side. -> Side Mul side. -> Rule α β Big num rule. -> Rule α β

Arguments

 :: (Integral α, Scale α, Unknown β, Add β, Mul β, Scale β) => α Base. -> α Offset. -> Side Add side. -> Side Mul side. -> Rule α β Big num rule. -> Rule α β

Arguments

 :: (Integral α, Scale α, Unknown β, Add β, Mul β, Scale β) => α Base. -> α Offset. -> Side Add side. -> Side Mul side. -> Rule α β Big num rule. -> Rule α β

Arguments

 :: (Integral α, Scale α, Unknown β, Add β, Mul β, Scale β) => Side Add side. -> Side Mul side. -> Rule α β Big num rule. -> Rule α β

Arguments

 :: (Integral α, Scale α, Unknown β, Add β, Mul β, Scale β) => Side Add side. -> Side Mul side. -> Rule α β Big num rule. -> Rule α β

Arguments

 :: (Integral α, Scale α, Unknown β, Add β, Mul β, Scale β) => Side Add side. -> Side Mul side. -> Rule α β Big num rule. -> Rule α β

Arguments

 :: (Integral α, Scale α, Unknown β, Add β, Mul β, Scale β) => Side Add side. -> Side Mul side. -> Rule α β Big num rule. -> Rule α β

Arguments

 :: (Integral α, Scale α, Unknown β, Add β, Mul β, Scale β) => Side Add side. -> Side Mul side. -> Rule α β Big num rule. -> Rule α β

Arguments

 :: (Integral α, Scale α, Unknown β, Add β, Mul β, Scale β) => Side Add side. -> Side Mul side. -> Rule α β Big num rule. -> Rule α β

Arguments

 :: (Integral α, Unknown β, Lit β, Add β, Mul β) => Rule α β lit rule -> (α -> Side -> Rule α β) add rule -> (α -> Side -> Side -> Rule α β) mul rule -> α -> α -> Side -> Side -> Rule α β

step :: (Integral α, Unknown β, Lit β, Add β, Mul β) => α -> α -> Side -> Side -> Rule α βSource

step1 :: (Integral α, Unknown β, Lit β, Add β, Mul β) => α -> α -> Side -> Side -> Rule α βSource