eo-phi-normalizer-2.2.2: Command line normalizer of 𝜑-calculus expressions.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Language.EO.Phi.Rules.Common

Synopsis

Documentation

>>> :set -XOverloadedStrings
>>> :set -XOverloadedLists
>>> import Language.EO.Phi.Syntax

unsafeParseWith :: ([Token] -> Either String a) -> String -> a Source #

Parse a Object from a String. May throw an error if input has a syntactical or lexical errors.

type EvaluationState = () Source #

State of evaluation is not needed yet, but it might be in the future

data Context Source #

Constructors

Context 

Fields

type Rule = Context -> Object -> [Object] Source #

A rule tries to apply a transformation to the root object, if possible.

propagateName1 :: (a -> b) -> (name, a) -> (name, b) Source #

Given a unary function that operates only on plain objects, converts it to a function that operates on named objects

propagateName2 :: (a -> b -> c) -> (name, a) -> b -> (name, c) Source #

Given a binary function that operates only on plain objects, converts it to a function that operates on named objects

applyRules :: Context -> Object -> [Object] Source #

Apply rules until we get a normal form.

applyRulesWith :: ApplicationLimits -> Context -> Object -> [Object] Source #

A variant of applyRules with a maximum application depth.

Chain variants

data LogEntry log Source #

Constructors

LogEntry 

Instances

Instances details
Functor LogEntry Source # 
Instance details

Defined in Language.EO.Phi.Rules.Common

Methods

fmap :: (a -> b) -> LogEntry a -> LogEntry b #

(<$) :: a -> LogEntry b -> LogEntry a #

Show log => Show (LogEntry log) Source # 
Instance details

Defined in Language.EO.Phi.Rules.Common

Methods

showsPrec :: Int -> LogEntry log -> ShowS #

show :: LogEntry log -> String #

showList :: [LogEntry log] -> ShowS #

newtype Chain log result Source #

Constructors

Chain 

Fields

Instances

Instances details
MonadFail (Chain a) Source # 
Instance details

Defined in Language.EO.Phi.Rules.Common

Methods

fail :: String -> Chain a a0 #

Applicative (Chain a) Source # 
Instance details

Defined in Language.EO.Phi.Rules.Common

Methods

pure :: a0 -> Chain a a0 #

(<*>) :: Chain a (a0 -> b) -> Chain a a0 -> Chain a b #

liftA2 :: (a0 -> b -> c) -> Chain a a0 -> Chain a b -> Chain a c #

(*>) :: Chain a a0 -> Chain a b -> Chain a b #

(<*) :: Chain a a0 -> Chain a b -> Chain a a0 #

Functor (Chain log) Source # 
Instance details

Defined in Language.EO.Phi.Rules.Common

Methods

fmap :: (a -> b) -> Chain log a -> Chain log b #

(<$) :: a -> Chain log b -> Chain log a #

Monad (Chain a) Source # 
Instance details

Defined in Language.EO.Phi.Rules.Common

Methods

(>>=) :: Chain a a0 -> (a0 -> Chain a b) -> Chain a b #

(>>) :: Chain a a0 -> Chain a b -> Chain a b #

return :: a0 -> Chain a a0 #

logStep :: String -> info -> Chain info () Source #

incLogLevel :: Chain info a -> Chain info a Source #

choose :: [a] -> Chain log a Source #

msplit :: Chain log a -> Chain log (Maybe (a, Chain log a)) Source #

transformLogs :: (log1 -> log2) -> Chain log1 a -> Chain log2 a Source #

listen :: Chain log a -> Chain log (a, [LogEntry log]) Source #

withContext :: Context -> Chain log a -> Chain log a Source #

modifyContext :: (Context -> Context) -> Chain log a -> Chain log a Source #

applyRulesChain :: Object -> NormalizeChain Object Source #

Apply the rules until the object is normalized, preserving the history (chain) of applications.

applyRulesChainWith :: ApplicationLimits -> Object -> NormalizeChain Object Source #

A variant of applyRulesChain with a maximum application depth.

Helpers

lookupBinding :: Attribute -> [Binding] -> Maybe Object Source #

Lookup a binding by the attribute name.

padLeft :: Int -> [Char] -> [Char] Source #

chunksOf :: Int -> [a] -> [[a]] Source #

Split a list into chunks of given size. All lists in the result are guaranteed to have length less than or equal to the given size.

>>> chunksOf 2 "012345678"
["01","23","45","67","8"]

See paddedLeftChunksOf for a version with padding to guarantee exact chunk size.

paddedLeftChunksOf :: a -> Int -> [a] -> [[a]] Source #

Split a list into chunks of given size, padding on the left if necessary. All lists in the result are guaranteed to have given size.

>>> paddedLeftChunksOf '0' 2 "1234567"
["01","23","45","67"]
>>> paddedLeftChunksOf '0' 2 "123456"
["12","34","56"]
n > 0  ==>  all (\chunk -> length chunk == n) (paddedLeftChunksOf c n s)

normalizeBytes :: String -> String Source #

Normalize the bytestring representation to fit valid Bytes token.

>>> normalizeBytes "238714ABCDEF"
"23-87-14-AB-CD-EF"
>>> normalizeBytes "0238714ABCDEF"
"00-23-87-14-AB-CD-EF"
>>> normalizeBytes "4"
"04-"

concatBytes :: Bytes -> Bytes -> Bytes Source #

Concatenate Bytes. FIXME: we should really use ByteString instead of the underlying String representation.

>>> concatBytes "00-" "01-02"
Bytes "00-01-02"
>>> concatBytes "03-04" "01-02"
Bytes "03-04-01-02"
>>> concatBytes "03-04" "01-"
Bytes "03-04-01"

sliceBytes :: Bytes -> Int -> Int -> Bytes Source #

Select a slice (section) of Bytes.

>>> sliceBytes "12-34-56" 1 1
Bytes "34-"
>>> sliceBytes "12-34-56" 1 0
Bytes "00-"
>>> sliceBytes "12-34-56" 0 2
Bytes "12-34"

intToBytes :: Int -> Bytes Source #

Convert an Int into Bytes representation.

>>> intToBytes 7
Bytes "00-00-00-00-00-00-00-07"
>>> intToBytes (3^33)
Bytes "00-13-BF-EF-A6-5A-BB-83"
>>> intToBytes (-1)
Bytes "FF-FF-FF-FF-FF-FF-FF-FF"

bytesToInt :: Bytes -> Int Source #

Parse Bytes as Int.

>>> bytesToInt "00-13-BF-EF-A6-5A-BB-83"
5559060566555523
>>> bytesToInt "AB-"
171

May error on invalid Bytes:

>>> bytesToInt "s"
*** Exception: Prelude.head: empty list
...
...
...
...
...
...

boolToBytes :: Bool -> Bytes Source #

Convert Bool to Bytes.

>>> boolToBytes False
Bytes "00-"
>>> boolToBytes True
Bytes "01-"

bytesToBool :: Bytes -> Bool Source #

Interpret Bytes as Bool.

Zero is interpreted as False.

>>> bytesToBool "00-"
False
>>> bytesToBool "00-00"
False

Everything else is interpreted as True.

>>> bytesToBool "01-"
True
>>> bytesToBool "00-01"
True
>>> bytesToBool "AB-CD"
True

stringToBytes :: String -> Bytes Source #

Encode String as Bytes.

>>> stringToBytes "Hello, world!"
Bytes "48-65-6C-6C-6F-2C-20-77-6F-72-6C-64-21"
>>> stringToBytes "Привет, мир!"
Bytes "D0-9F-D1-80-D0-B8-D0-B2-D0-B5-D1-82-2C-20-D0-BC-D0-B8-D1-80-21"
>>> stringToBytes  "hello, 大家!"
Bytes "68-65-6C-6C-6F-2C-20-E5-A4-A7-E5-AE-B6-21"

bytesToString :: Bytes -> String Source #

Decode String from Bytes.

>>> bytesToString "48-65-6C-6C-6F-2C-20-77-6F-72-6C-64-21"
"Hello, world!"

floatToBytes :: Double -> Bytes Source #

Encode Double as Bytes following IEEE754.

Note: it is called "float" in EO, but it actually occupies 8 bytes so it corresponds to Double.

>>> floatToBytes 0
Bytes "00-00-00-00-00-00-00-00"
>>> floatToBytes (-0.1)
Bytes "BF-B9-99-99-99-99-99-9A"
>>> floatToBytes (1/0)       -- Infinity
Bytes "7F-F0-00-00-00-00-00-00"
>>> floatToBytes (asin 2) `elem` ["FF-F8-00-00-00-00-00-00", "7F-F8-00-00-00-00-00-00"]  -- sNaN or qNaN
True

bytesToFloat :: Bytes -> Double Source #

Decode Double from Bytes following IEEE754.

>>> bytesToFloat "00-00-00-00-00-00-00-00"
0.0
>>> bytesToFloat "BF-B9-99-99-99-99-99-9A"
-0.1
>>> bytesToFloat "7F-F0-00-00-00-00-00-00"
Infinity
>>> bytesToFloat "FF-F8-00-00-00-00-00-00"
NaN

Orphan instances

IsString Attribute Source # 
Instance details

IsString Binding Source # 
Instance details

Methods

fromString :: String -> Binding #

IsString MetaId Source # 
Instance details

Methods

fromString :: String -> MetaId #

IsString Object Source # 
Instance details

Methods

fromString :: String -> Object #

IsString ObjectHead Source # 
Instance details

IsString PeeledObject Source # 
Instance details

IsString Program Source # 
Instance details

Methods

fromString :: String -> Program #

IsString RuleAttribute Source # 
Instance details