Safe Haskell | None |
---|---|
Language | Haskell2010 |
Regular expression as types.
Synopsis
- data RE a
- type E = E
- type V = V
- type S = S
- type (<>) a b = a :<> b
- type (\/) a b = a :\/ b
- data HList :: [*] -> * where
- hsingleton :: a -> HList '[a]
- hlength :: HList xs -> Int
- type family Append (xs :: [*]) (ys :: [*]) :: [*] where ...
- append :: HList xs -> HList ys -> HList (Append xs ys)
- split :: SList xs -> HList (Append xs ys) -> (HList xs, HList ys)
- data SList :: [*] -> * where
- class SListI (xs :: [*]) where
- hlistToSList :: HList xs -> SList xs
- data Match :: RE * -> [*] -> * where
- MatchE :: Match E '[]
- MatchV :: forall a. Match (V a) '[a]
- MatchA :: forall re re' xs ys. SList xs -> Match re xs -> Match re' ys -> Match (re <> re') (Append xs ys)
- MatchL :: forall re re' xs. Match re xs -> Match (re \/ re') xs
- MatchR :: forall re re' xs. Match re' xs -> Match (re \/ re') xs
- MatchN :: forall re. Match (S re) '[]
- MatchC :: forall re xs ys. SList xs -> Match re xs -> Match (S re) ys -> Match (S re) (Append xs ys)
- MatchT :: forall xs. SList xs -> Match T xs
- class MatchI (re :: RE *) (xs :: [*]) where
- justMatchIt :: Match re xs
- matchE :: Match E '[]
- matchV :: Match (V a) '[a]
- matchA :: SListI xs => Match r xs -> Match s ys -> Match (r <> s) (Append xs ys)
- matchL :: Match r xs -> Match (r \/ s) xs
- matchR :: Match s xs -> Match (r \/ s) xs
- matchN :: Match (S re) '[]
- matchC :: SListI xs => Match r xs -> Match (S r) ys -> Match (S r) (Append xs ys)
- withE :: x -> Match E xs -> HList xs -> x
- withV :: (a -> x) -> Match (V a) xs -> HList xs -> x
- withUnion :: (Match r xs -> HList xs -> x) -> (Match r' xs -> HList xs -> x) -> Match (r \/ r') xs -> HList xs -> x
- withAppend :: (x -> y -> z) -> (forall xs. Match r xs -> HList xs -> x) -> (forall ys. Match r' ys -> HList ys -> y) -> Match (r <> r') zs -> HList zs -> z
- withStarMon :: forall x r zs. Monoid x => (forall xs. Match r xs -> HList xs -> x) -> Match (S r) zs -> HList zs -> x
- withStarR :: forall x y r zs. (x -> y -> y) -> y -> (forall xs. Match r xs -> HList xs -> x) -> Match (S r) zs -> HList zs -> y
- withAppend3 :: (x -> y -> z -> w) -> (forall xs. Match r0 xs -> HList xs -> x) -> (forall ys. Match r1 ys -> HList ys -> y) -> (forall zs. Match r2 zs -> HList zs -> z) -> Match (r0 <> (r1 <> r2)) ws -> HList ws -> w
- data REList :: RE * -> * where
- mkREList :: MatchI re xs => HList xs -> REList re
- withREList :: forall re x. REList re -> (forall xs. Match re xs -> HList xs -> x) -> x
- withREListI :: forall re x. REList re -> (forall xs. MatchI re xs => HList xs -> x) -> x
- class ListInduction (xs :: [*]) where
- listInduction :: f '[] -> (forall y ys. f ys -> f (y ': ys)) -> f xs
- class ListInductionC (c :: * -> Constraint) (xs :: [*]) where
- listInductionC :: Proxy c -> f '[] -> (forall y ys. c y => f ys -> f (y ': ys)) -> f xs
- listInductionP :: forall f xs. f '[] -> (forall y ys. f ys -> f (y ': ys)) -> SList xs -> f xs
- class REInductionC (c :: * -> Constraint) (re :: RE *) where
- reInductionC :: Proxy c -> f '[] -> (forall x. c x => f '[x]) -> (forall xs ys. SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall xs. f xs -> f xs) -> (forall xs. f xs -> f xs) -> f '[] -> (forall xs ys. SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall xs. SList xs -> f xs) -> Match re zs -> f zs
- reInductionP :: forall f re zs. f '[] -> (forall x. f '[x]) -> (forall xs ys. SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall xs. f xs -> f xs) -> (forall xs. f xs -> f xs) -> f '[] -> (forall xs ys. SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall xs. SList xs -> f xs) -> Match re zs -> f zs
- haskelly :: Match re xs -> HList xs -> Haskelly re
- type family Haskelly (re :: RE *) :: * where ...
- data Key (s :: Symbol) = Key
- key :: Key s -> Key s
- keyVal :: KnownSymbol s => Key s -> String
- rval :: Iso (REList (V a)) (REList (V b)) a b
- rpair :: Iso (REList (a <> b)) (REList (c <> d)) (REList a, REList b) (REList c, REList d)
- rfst :: Lens (REList (a <> c)) (REList (b <> c)) (REList a) (REList b)
- rsnd :: Lens (REList (c <> a)) (REList (c <> b)) (REList a) (REList b)
- rsum :: Iso (REList (a \/ b)) (REList (c \/ d)) (Either (REList a) (REList b)) (Either (REList c) (REList d))
- rleft :: Prism (REList (a \/ c)) (REList (b \/ c)) (REList a) (REList b)
- rright :: Prism (REList (c \/ a)) (REList (c \/ b)) (REList a) (REList b)
- rstar :: Traversal (REList (S a)) (REList (S b)) (REList a) (REList b)
- rnil :: Prism' (REList (S a)) ()
- rcons :: Prism (REList (S a)) (REList (S b)) (REList a, REList (S a)) (REList b, REList (S b))
- type T = T
- matchT :: SListI xs => Match T xs
- withTop :: y -> Match T zs -> HList zs -> y
Regular expressions
Regular expressions.
Tickless aliases
Heterogenous list
hsingleton :: a -> HList '[a] Source #
Create singleton HList
.
Append and Split
Match
data Match :: RE * -> [*] -> * where Source #
Proof of match.
forall
's are required, to better document the argument order for
Core-term construction in the plugin.
MatchE :: Match E '[] | |
MatchV :: forall a. Match (V a) '[a] | |
MatchA :: forall re re' xs ys. SList xs -> Match re xs -> Match re' ys -> Match (re <> re') (Append xs ys) | |
MatchL :: forall re re' xs. Match re xs -> Match (re \/ re') xs | |
MatchR :: forall re re' xs. Match re' xs -> Match (re \/ re') xs | |
MatchN :: forall re. Match (S re) '[] | |
MatchC :: forall re xs ys. SList xs -> Match re xs -> Match (S re) ys -> Match (S re) (Append xs ys) infixr 5 | |
MatchT :: forall xs. SList xs -> Match T xs |
Implicit match
class MatchI (re :: RE *) (xs :: [*]) where Source #
This class has no defined instances.
KleenePlugin can solve the constraints for you.
justMatchIt :: Match re xs Source #
Smart constructors
Smart destructors
withUnion :: (Match r xs -> HList xs -> x) -> (Match r' xs -> HList xs -> x) -> Match (r \/ r') xs -> HList xs -> x Source #
RE-indexed list
data REList :: RE * -> * where Source #
RE
-indexed list.
withREList :: forall re x. REList re -> (forall xs. Match re xs -> HList xs -> x) -> x Source #
>>>
withREListI (REList matchV $ hsingleton True) $ withV not justMatchIt
False
withREListI :: forall re x. REList re -> (forall xs. MatchI re xs => HList xs -> x) -> x Source #
>>>
withREListI (REList matchV $ hsingleton True) $ withV not justMatchIt
False
Inductions
There are two ways to write induction,
Using a structure proof, like
SList
. If we fold overHList
, we could use it as a structure proof too; but for constucting it, we'd needSList
. This is 'listInductionP.The benefit of this approach is its simplicity. However, the drawback is that we need to define an auxiliary type family to provide constrained version. See
All
ingenerics-sop
package (version 0.3).Alternatively, we can define a type class directly parametrised by the type-level structure.
This way additional constraints for different constructors can be added naturally. See
ListInducton
andListInductionC
.
List structure
class ListInduction (xs :: [*]) where Source #
(Type-level) list induction.
Instances
ListInduction ([] :: [Type]) Source # | |
Defined in Kleene.Type listInduction :: f [] -> (forall y (ys :: [Type]). f ys -> f (y ': ys)) -> f [] Source # | |
ListInduction xs => ListInduction (x ': xs) Source # | |
Defined in Kleene.Type listInduction :: f [] -> (forall y (ys :: [Type]). f ys -> f (y ': ys)) -> f (x ': xs) Source # |
class ListInductionC (c :: * -> Constraint) (xs :: [*]) where Source #
Constrained ListInduction
.
Instances
ListInductionC c ([] :: [Type]) Source # | |
Defined in Kleene.Type listInductionC :: Proxy c -> f [] -> (forall y (ys :: [Type]). c y => f ys -> f (y ': ys)) -> f [] Source # | |
(c x, ListInductionC c xs) => ListInductionC c (x ': xs) Source # | |
Defined in Kleene.Type listInductionC :: Proxy c -> f [] -> (forall y (ys :: [Type]). c y => f ys -> f (y ': ys)) -> f (x ': xs) Source # |
This is the third variant of list induction, using a proof object SList
.
RE structure
class REInductionC (c :: * -> Constraint) (re :: RE *) where Source #
Constrainted RE
induction of lists!
We need the Match
object, as it connects re
and xs
.
Note: This is an induction on the structure of the Match
, not the xs
list.
:: Proxy c | |
-> f '[] | case: |
-> (forall x. c x => f '[x]) | case: |
-> (forall xs ys. SList xs -> f xs -> f ys -> f (Append xs ys)) | case: |
-> (forall xs. f xs -> f xs) | case: |
-> (forall xs. f xs -> f xs) | case: |
-> f '[] | case: |
-> (forall xs ys. SList xs -> f xs -> f ys -> f (Append xs ys)) | case: |
-> (forall xs. SList xs -> f xs) | case: |
-> Match re zs |
|
-> f zs |
Instances
REInductionC c (T :: RE Type) Source # | |
Defined in Kleene.Type reInductionC :: Proxy c -> f [] -> (forall x. c x => f (x ': [])) -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). f xs -> f xs) -> (forall (xs :: [Type]). f xs -> f xs) -> f [] -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). SList xs -> f xs) -> Match T zs -> f zs Source # | |
REInductionC c (E :: RE Type) Source # | |
Defined in Kleene.Type reInductionC :: Proxy c -> f [] -> (forall x. c x => f (x ': [])) -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). f xs -> f xs) -> (forall (xs :: [Type]). f xs -> f xs) -> f [] -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). SList xs -> f xs) -> Match E zs -> f zs Source # | |
REInductionC c x => REInductionC c (S x) Source # | |
Defined in Kleene.Type reInductionC :: Proxy c -> f [] -> (forall x0. c x0 => f (x0 ': [])) -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). f xs -> f xs) -> (forall (xs :: [Type]). f xs -> f xs) -> f [] -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). SList xs -> f xs) -> Match (S x) zs -> f zs Source # | |
c x => REInductionC c (V x) Source # | |
Defined in Kleene.Type reInductionC :: Proxy c -> f [] -> (forall x0. c x0 => f (x0 ': [])) -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). f xs -> f xs) -> (forall (xs :: [Type]). f xs -> f xs) -> f [] -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). SList xs -> f xs) -> Match (V x) zs -> f zs Source # | |
(REInductionC c r, REInductionC c s) => REInductionC c (r :\/ s) Source # | |
Defined in Kleene.Type reInductionC :: Proxy c -> f [] -> (forall x. c x => f (x ': [])) -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). f xs -> f xs) -> (forall (xs :: [Type]). f xs -> f xs) -> f [] -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). SList xs -> f xs) -> Match (r :\/ s) zs -> f zs Source # | |
(REInductionC c r, REInductionC c s) => REInductionC c (r :<> s) Source # | |
Defined in Kleene.Type reInductionC :: Proxy c -> f [] -> (forall x. c x => f (x ': [])) -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). f xs -> f xs) -> (forall (xs :: [Type]). f xs -> f xs) -> f [] -> (forall (xs :: [Type]) (ys :: [Type]). SList xs -> f xs -> f ys -> f (Append xs ys)) -> (forall (xs :: [Type]). SList xs -> f xs) -> Match (r :<> s) zs -> f zs Source # |
:: f '[] | case: |
-> (forall x. f '[x]) | case: |
-> (forall xs ys. SList xs -> f xs -> f ys -> f (Append xs ys)) | case: |
-> (forall xs. f xs -> f xs) | case: |
-> (forall xs. f xs -> f xs) | case: |
-> f '[] | case: |
-> (forall xs ys. SList xs -> f xs -> f ys -> f (Append xs ys)) | case: |
-> (forall xs. SList xs -> f xs) | case: |
-> Match re zs |
|
-> f zs |
Induction using Match
.
Conversion to Haskelly types.
haskelly :: Match re xs -> HList xs -> Haskelly re Source #
Convert to haskelly types1
Note: Inverse would require RE
singleton, SRE
.
Key
Lenses
Naming convention is simple: r
+ 3-5 characters.
rsum :: Iso (REList (a \/ b)) (REList (c \/ d)) (Either (REList a) (REList b)) (Either (REList c) (REList d)) Source #
rcons :: Prism (REList (S a)) (REList (S b)) (REList a, REList (S a)) (REList b, REList (S b)) Source #