Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Boolean algebra on encodings
(Experimental, early alpha development stage) This module was not converted to v0.3 style yet.
Grammar
Simple grammar requires boolean terms to be included in parentheses
bool[BinaryOp]:(leftTerm)(rightTerm) bool[UnaryOp]:(term)
Expected behavior is described next to the corresponding combinator.
Since: 0.2.1.0
Synopsis
- encBoolOrLeft :: forall f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, LeftTerm s ~ t) => (Enc xs c str -> f (Enc (t ': xs) c str)) -> Enc xs c str -> f (Enc (s ': xs) c str)
- encBoolOrLeft' :: forall f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, LeftTerm s ~ t, Encode f t t c str) => Enc xs c str -> f (Enc (s ': xs) c str)
- encBoolOrLeft'' :: forall alg f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, LeftTerm s ~ t, Encode f t alg c str) => Enc xs c str -> f (Enc (s ': xs) c str)
- encBoolOrRight :: forall f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, RightTerm s ~ t) => (Enc xs c str -> f (Enc (t ': xs) c str)) -> Enc xs c str -> f (Enc (s ': xs) c str)
- encBoolOrRight' :: forall f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, RightTerm s ~ t, Encode f t t c str) => Enc xs c str -> f (Enc (s ': xs) c str)
- encBoolOrRight'' :: forall alg f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, RightTerm s ~ t, Encode f t alg c str) => Enc xs c str -> f (Enc (s ': xs) c str)
- encBoolAnd :: forall f s t1 t2 xs c str. (BoolOpIs s "and" ~ True, KnownSymbol s, f ~ Either EncodeEx, Eq str, LeftTerm s ~ t1, RightTerm s ~ t2) => (Enc xs c str -> f (Enc (t1 ': xs) c str)) -> (Enc xs c str -> f (Enc (t2 ': xs) c str)) -> Enc xs c str -> f (Enc (s ': xs) c str)
- encBoolAnd' :: forall s t1 t2 xs c str. (BoolOpIs s "and" ~ True, KnownSymbol s, Eq str, LeftTerm s ~ t1, RightTerm s ~ t2, Encode (Either EncodeEx) t1 t1 c str, Encode (Either EncodeEx) t2 t2 c str) => Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str)
- encBoolAnd'' :: forall al1 al2 s t1 t2 xs c str. (BoolOpIs s "and" ~ True, KnownSymbol s, Eq str, LeftTerm s ~ t1, RightTerm s ~ t2, Encode (Either EncodeEx) t1 al1 c str, Encode (Either EncodeEx) t2 al2 c str) => Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str)
- encBoolNot :: forall s t xs c str. (BoolOpIs s "not" ~ True, KnownSymbol s, FirstTerm s ~ t, Restriction t) => (Enc xs c str -> Either EncodeEx (Enc (t ': xs) c str)) -> Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str)
- encBoolNot' :: forall s t xs c str. (BoolOpIs s "not" ~ True, KnownSymbol s, FirstTerm s ~ t, KnownSymbol t, Restriction t, Encode (Either EncodeEx) t t c str) => Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str)
- encBoolNot'' :: forall alg s t xs c str. (BoolOpIs s "not" ~ True, KnownSymbol s, FirstTerm s ~ t, Restriction t, Encode (Either EncodeEx) t alg c str) => Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str)
- decBoolR :: forall f xs t s c str. (NestedR s ~ True, Applicative f) => Enc (s ': xs) c str -> f (Enc xs c str)
- recWithEncBoolR :: forall (s :: Symbol) xs c str. NestedR s ~ True => (Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str)) -> Enc xs c str -> Either RecreateEx (Enc (s ': xs) c str)
- unsafeRecWithEncR :: forall (s :: Symbol) xs c str. (Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str)) -> Enc xs c str -> Either RecreateEx (Enc (s ': xs) c str)
- type family BoolOpIs (s :: Symbol) (op :: Symbol) :: Bool where ...
- type family BoolOp (s :: Symbol) :: Symbol where ...
- type family BoolOpHelper (x :: (Symbol, Symbol)) :: (Symbol, Bool) where ...
- type family IsBool (s :: Symbol) :: Bool where ...
- type family NestedR (s :: Symbol) :: Bool where ...
- type family FirstTerm (s :: Symbol) :: Symbol where ...
- type family SecondTerm (s :: Symbol) :: Symbol where ...
- type family LeftTerm (s :: Symbol) :: Symbol where ...
- type family RightTerm (s :: Symbol) :: Symbol where ...
- type family LDropLast (s :: [Symbol]) :: [Symbol] where ...
- type family LParenCnt (s :: [Symbol]) :: [(Symbol, Nat)] where ...
- data Adjust
- type family AdjHelper (a :: Adjust) (n :: Nat) :: Nat where ...
- type family LParenCntHelper (s :: (Symbol, Adjust)) (sx :: [(Symbol, Nat)]) :: [(Symbol, Nat)] where ...
- type family LTakeFstParen (si :: [(Symbol, Nat)]) :: [Symbol] where ...
- type family LTakeSndParen (n :: Nat) (si :: [(Symbol, Nat)]) :: [Symbol] where ...
Documentation
>>>
:set -XOverloadedStrings -XMultiParamTypeClasses -XDataKinds -XTypeApplications
>>>
import qualified Data.Text as T
>>>
import Data.TypedEncoding.Instances.Restriction.Misc()
encBoolOrLeft :: forall f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, LeftTerm s ~ t) => (Enc xs c str -> f (Enc (t ': xs) c str)) -> Enc xs c str -> f (Enc (s ': xs) c str) Source #
See examples in encBoolOrRight'
encBoolOrLeft' :: forall f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, LeftTerm s ~ t, Encode f t t c str) => Enc xs c str -> f (Enc (s ': xs) c str) Source #
See examples in encBoolOrRight'
encBoolOrLeft'' :: forall alg f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, LeftTerm s ~ t, Encode f t alg c str) => Enc xs c str -> f (Enc (s ': xs) c str) Source #
encBoolOrRight :: forall f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, RightTerm s ~ t) => (Enc xs c str -> f (Enc (t ': xs) c str)) -> Enc xs c str -> f (Enc (s ': xs) c str) Source #
encBoolOrRight' :: forall f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, RightTerm s ~ t, Encode f t t c str) => Enc xs c str -> f (Enc (s ': xs) c str) Source #
>>>
:{
let tst1, tst2, tst3 :: Either EncodeEx (Enc '["boolOr:(r-Word8-decimal)(r-Int-decimal)"] () T.Text) tst1 = encBoolOrLeft' . toEncoding () $ "212" tst2 = encBoolOrRight' . toEncoding () $ "1000000" tst3 = encBoolOrLeft' . toEncoding () $ "1000000" :}
>>>
tst1
Right (UnsafeMkEnc Proxy () "212")
>>>
tst2
Right (UnsafeMkEnc Proxy () "1000000")
>>>
tst3
Left (EncodeEx "r-Word8-decimal" ("Payload does not satisfy format Word8-decimal: 1000000"))
encBoolOrRight'' :: forall alg f s t xs c str. (BoolOpIs s "or" ~ True, Functor f, RightTerm s ~ t, Encode f t alg c str) => Enc xs c str -> f (Enc (s ': xs) c str) Source #
encBoolAnd :: forall f s t1 t2 xs c str. (BoolOpIs s "and" ~ True, KnownSymbol s, f ~ Either EncodeEx, Eq str, LeftTerm s ~ t1, RightTerm s ~ t2) => (Enc xs c str -> f (Enc (t1 ': xs) c str)) -> (Enc xs c str -> f (Enc (t2 ': xs) c str)) -> Enc xs c str -> f (Enc (s ': xs) c str) Source #
encBoolAnd' :: forall s t1 t2 xs c str. (BoolOpIs s "and" ~ True, KnownSymbol s, Eq str, LeftTerm s ~ t1, RightTerm s ~ t2, Encode (Either EncodeEx) t1 t1 c str, Encode (Either EncodeEx) t2 t2 c str) => Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str) Source #
"boolOr:(enc1)(enc2)"
contains strings that encode the same way under both encodings.
for example "boolOr:(r-UPPER)(r-lower)"
valid elements would include "123-34"
but not "abc"
>>>
:{
let tst1, tst2 :: Either EncodeEx (Enc '["boolAnd:(r-Word8-decimal)(r-Int-decimal)"] () T.Text) tst1 = encBoolAnd' . toEncoding () $ "234" tst2 = encBoolAnd' . toEncoding () $ "100000" :}
>>>
tst1
Right (UnsafeMkEnc Proxy () "234")>>>
tst2
Left (EncodeEx "r-Word8-decimal" ("Payload does not satisfy format Word8-decimal: 100000"))
encBoolAnd'' :: forall al1 al2 s t1 t2 xs c str. (BoolOpIs s "and" ~ True, KnownSymbol s, Eq str, LeftTerm s ~ t1, RightTerm s ~ t2, Encode (Either EncodeEx) t1 al1 c str, Encode (Either EncodeEx) t2 al2 c str) => Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str) Source #
encBoolNot :: forall s t xs c str. (BoolOpIs s "not" ~ True, KnownSymbol s, FirstTerm s ~ t, Restriction t) => (Enc xs c str -> Either EncodeEx (Enc (t ': xs) c str)) -> Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str) Source #
encBoolNot' :: forall s t xs c str. (BoolOpIs s "not" ~ True, KnownSymbol s, FirstTerm s ~ t, KnownSymbol t, Restriction t, Encode (Either EncodeEx) t t c str) => Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str) Source #
>>>
:{
let tst1, tst2 :: Either EncodeEx (Enc '["boolNot:(r-Word8-decimal)"] () T.Text) tst1 = encBoolNot' . toEncoding () $ "334" tst2 = encBoolNot' . toEncoding () $ "127" :}
>>>
tst1
Right (UnsafeMkEnc Proxy () "334")>>>
tst2
Left (EncodeEx "boolNot:(r-Word8-decimal)" ("Encoding r-Word8-decimal succeeded"))
encBoolNot'' :: forall alg s t xs c str. (BoolOpIs s "not" ~ True, KnownSymbol s, FirstTerm s ~ t, Restriction t, Encode (Either EncodeEx) t alg c str) => Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str) Source #
decBoolR :: forall f xs t s c str. (NestedR s ~ True, Applicative f) => Enc (s ': xs) c str -> f (Enc xs c str) Source #
Decodes boolean expression if all leaves are "r-"
recWithEncBoolR :: forall (s :: Symbol) xs c str. NestedR s ~ True => (Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str)) -> Enc xs c str -> Either RecreateEx (Enc (s ': xs) c str) Source #
unsafeRecWithEncR :: forall (s :: Symbol) xs c str. (Enc xs c str -> Either EncodeEx (Enc (s ': xs) c str)) -> Enc xs c str -> Either RecreateEx (Enc (s ': xs) c str) Source #
Type family based parser
type family BoolOpIs (s :: Symbol) (op :: Symbol) :: Bool where ... Source #
>>>
:kind! BoolOpIs "boolAnd:(someenc)(otherenc)" "and"
... = 'True
type family BoolOp (s :: Symbol) :: Symbol where ... Source #
This works fast with !kind
but is much slower in declaration
:kind! BoolOp "boolOr:()()"
BoolOp s = Fst (BoolOpHelper (Dupl s)) |
type family NestedR (s :: Symbol) :: Bool where ... Source #
>>>
:kind! NestedR "boolOr:(r-abc)(r-cd)"
... = 'True
>>>
:kind! NestedR "boolOr:(boolAnd:(r-ab)(r-ac))(boolNot:(r-cd))"
... = 'True
>>>
:kind! NestedR "boolOr:(boolAnd:(r-ab)(ac))(boolNot:(r-cd))"
... ... (TypeError ...) ...
type family SecondTerm (s :: Symbol) :: Symbol where ... Source #
returns "" for unary operator
SecondTerm s = RightTerm s |
type family LeftTerm (s :: Symbol) :: Symbol where ... Source #
>>>
:kind! LeftTerm "boolSomeOp:(agag)(222)"
... = "agag"
>>>
:kind! LeftTerm "r-Int-decimal"
... = ""
type family RightTerm (s :: Symbol) :: Symbol where ... Source #
>>>
:kind! RightTerm "boolSomeOp:(agag)(222)"
... = "222"
>>>
:kind! RightTerm "r-Int-decimal"
... = ""
type family LParenCntHelper (s :: (Symbol, Adjust)) (sx :: [(Symbol, Nat)]) :: [(Symbol, Nat)] where ... Source #
LParenCntHelper ((,) x k) '[] = (,) x (AdjHelper k 0) ': '[] | |
LParenCntHelper ((,) x k) ((,) c i ': xs) = (,) x (AdjHelper k i) ': ((,) c i ': xs) |
type family LTakeFstParen (si :: [(Symbol, Nat)]) :: [Symbol] where ... Source #
LTakeFstParen '[] = '[] | |
LTakeFstParen ((,) _ 0 ': xs) = LTakeFstParen xs | |
LTakeFstParen ((,) ")" 1 ': _) = '[")"] | |
LTakeFstParen ((,) a p ': xs) = a ': LTakeFstParen xs |
type family LTakeSndParen (n :: Nat) (si :: [(Symbol, Nat)]) :: [Symbol] where ... Source #
LTakeSndParen _ '[] = '[] | |
LTakeSndParen 0 ((,) ")" 1 ': xs) = LTakeSndParen 1 xs | |
LTakeSndParen 1 ((,) _ 0 ': xs) = LTakeSndParen 1 xs | |
LTakeSndParen 0 ((,) _ _ ': xs) = LTakeSndParen 0 xs | |
LTakeSndParen 1 ((,) a _ ': xs) = a ': LTakeSndParen 1 xs | |
LTakeSndParen n _ = '[] |