| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.ZipMatchK
Description
Kind-polymorphic syntactic (first-order) unification.
Synopsis
- module Data.ZipMatchK.Mappings
- class ZipMatchK (f :: k) where
- zipMatchK :: forall {k} (f :: k) (as :: LoT k) (bs :: LoT k). (ZipMatchK f, PairMappings as bs) => (f :@@: as) -> (f :@@: bs) -> Maybe (f :@@: ZipLoT as bs)
- class ZipMatchFoldK (f :: k) where
- zipMatchAllK :: forall {k} (f :: k) (as :: LoT k) (bs :: LoT k). ZipMatchFoldK f => Folds Bool as bs -> (f :@@: as) -> (f :@@: bs) -> Maybe Bool
- zipMatchAnyK :: forall {k} (f :: k) (as :: LoT k) (bs :: LoT k). ZipMatchFoldK f => Folds Bool as bs -> (f :@@: as) -> (f :@@: bs) -> Maybe Bool
- zipMatchViaEq :: forall {k} a (as :: LoT k) (bs :: LoT k) (cs :: LoT k). Eq a => Mappings as bs cs -> a -> a -> Maybe a
- zipMatchViaChooseLeft :: forall {k} (as :: LoT k) (bs :: LoT k) (cs :: LoT k) a. Mappings as bs cs -> a -> a -> Maybe a
- zipMatchWith1 :: ZipMatchK f => (a -> a' -> Maybe a'') -> f a -> f a' -> Maybe (f a'')
- zipMatch1 :: ZipMatchK f => f a -> f a' -> Maybe (f (a, a'))
- zipMatchFold1 :: (ZipMatchFoldK f, Monoid m) => (a -> a' -> m) -> f a -> f a' -> Maybe m
- zipMatchAll1 :: ZipMatchFoldK f => (a -> a' -> Bool) -> f a -> f a' -> Maybe Bool
- zipMatchAny1 :: ZipMatchFoldK f => (a -> a' -> Bool) -> f a -> f a' -> Maybe Bool
- zipMatchWith2 :: ZipMatchK f => (a -> a' -> Maybe a'') -> (b -> b' -> Maybe b'') -> f a b -> f a' b' -> Maybe (f a'' b'')
- zipMatch2 :: ZipMatchK f => f a b -> f a' b' -> Maybe (f (a, a') (b, b'))
- zipMatchFold2 :: (ZipMatchFoldK f, Monoid m) => (a -> a' -> m) -> (b -> b' -> m) -> f a b -> f a' b' -> Maybe m
- zipMatchAll2 :: ZipMatchFoldK f => (a -> a' -> Bool) -> (b -> b' -> Bool) -> f a b -> f a' b' -> Maybe Bool
- zipMatchAny2 :: ZipMatchFoldK f => (a -> a' -> Bool) -> (b -> b' -> Bool) -> f a b -> f a' b' -> Maybe Bool
Documentation
module Data.ZipMatchK.Mappings
class ZipMatchK (f :: k) where Source #
Kind-polymorphic syntactic (first-order) unification of two values.
Note that f is expected to be a traversable n-functor. There is no
TraversableK constraint to say so at the moment.
The default implementation is generic, via RepK. It is
convenient, but it reflects each node into a representation and back on
every comparison, and a constructor is represented as a chain of
L1/R1 wrappers as long as its index, so the cost grows with the size of
the signature. Comparing terms is most of what a typechecker does, and on a
large signature the generic instance costs roughly twice what the
written-out one does. Use deriveZipMatchK (or
deriveZipMatchK2, for a signature bifunctor with extra
parameters) to generate the written-out instance instead.
Since: 0.2.0
Minimal complete definition
Nothing
Methods
zipMatchWithK :: forall (as :: LoT k) (bs :: LoT k) (cs :: LoT k). Mappings as bs cs -> (f :@@: as) -> (f :@@: bs) -> Maybe (f :@@: cs) Source #
Instances
| ZipMatchK Either Source # | |
| ZipMatchK (,) Source # | |
| ZipMatchK NonEmpty Source # | |
| ZipMatchK Maybe Source # | |
| ZipMatchK [] Source # | |
| ZipMatchK a => ZipMatchK (Either a :: Type -> Type) Source # | |
| ZipMatchK a => ZipMatchK ((,) a :: Type -> Type) Source # | |
| (ZipMatchK ann, ZipMatchK sig) => ZipMatchK (AnnSig ann sig :: Type -> Type -> Type) Source # | |
| (ZipMatchK f, ZipMatchK g) => ZipMatchK (Product f g :: Type -> Type) Source # | Note: instance is limited to |
| (ZipMatchK f, ZipMatchK g) => ZipMatchK (Sum f g :: Type -> Type) Source # | Note: instance is limited to |
| (ZipMatchK f, ZipMatchK g) => ZipMatchK (Product f g :: Type -> Type -> Type) Source # | Note: instance is limited to |
| (ZipMatchK f, ZipMatchK g) => ZipMatchK (Sum f g :: Type -> Type -> Type) Source # | Note: instance is limited to |
zipMatchK :: forall {k} (f :: k) (as :: LoT k) (bs :: LoT k). (ZipMatchK f, PairMappings as bs) => (f :@@: as) -> (f :@@: bs) -> Maybe (f :@@: ZipLoT as bs) Source #
Perform one level of equality testing for two values and pair up
corresponding components using (,), for every type parameter:
zipMatchK = zipMatchWithK pairMappings
Since: 0.2.0
class ZipMatchFoldK (f :: k) where Source #
Kind-polymorphic syntactic matching of two values that folds the paired components into a monoid instead of rebuilding the value.
zipMatchFoldK answers Nothing exactly when zipMatchWithK with
functions that always succeed would: when the constructors differ, or a
component compared by its own ZipMatchK differs. Otherwise it combines
the folding functions applied to the paired components, left to right with
<>. Nothing else is built, neither the matched value nor a pair per
component, so a check of all components (with All, say)
allocates nothing for the components and stops at the first one that
fails.
The default implementation is generic, via RepK, with the
costs described at ZipMatchK. deriveZipMatchK derives
a written-out instance together with the one of ZipMatchK.
Since: 0.6.0
Minimal complete definition
Nothing
Methods
zipMatchFoldK :: forall (as :: LoT k) (bs :: LoT k) m. Monoid m => Folds m as bs -> (f :@@: as) -> (f :@@: bs) -> Maybe m Source #
Perform one level of equality testing and fold the paired components.
Since: 0.6.0
Instances
| ZipMatchFoldK Either Source # | |
| ZipMatchFoldK (,) Source # | |
| ZipMatchFoldK NonEmpty Source # | |
| ZipMatchFoldK Maybe Source # | |
| ZipMatchFoldK [] Source # | |
| ZipMatchK a => ZipMatchFoldK (Either a :: Type -> Type) Source # | |
| ZipMatchK a => ZipMatchFoldK ((,) a :: Type -> Type) Source # | |
| (ZipMatchFoldK ann, ZipMatchFoldK sig) => ZipMatchFoldK (AnnSig ann sig :: Type -> Type -> Type) Source # | Matching for The generic default would rebuild the Generics.Kind view of every node on
every comparison, and comparing terms is most of what a typechecker does, so
for an annotated signature, where it lands on the hottest path, that is a
measurable cost. |
| (ZipMatchFoldK f, ZipMatchFoldK g) => ZipMatchFoldK (Product f g :: Type -> Type) Source # | Note: instance is limited to |
| (ZipMatchFoldK f, ZipMatchFoldK g) => ZipMatchFoldK (Sum f g :: Type -> Type) Source # | Note: instance is limited to |
| (ZipMatchFoldK f, ZipMatchFoldK g) => ZipMatchFoldK (Product f g :: Type -> Type -> Type) Source # | Note: instance is limited to |
| (ZipMatchFoldK f, ZipMatchFoldK g) => ZipMatchFoldK (Sum f g :: Type -> Type -> Type) Source # | Note: instance is limited to |
zipMatchAllK :: forall {k} (f :: k) (as :: LoT k) (bs :: LoT k). ZipMatchFoldK f => Folds Bool as bs -> (f :@@: as) -> (f :@@: bs) -> Maybe Bool Source #
Check a relation on all paired components: Nothing when the shapes
differ, as for zipMatchFoldK, and otherwise whether every component
satisfies its relation. The components are checked left to right, and the
check stops at the first that fails.
zipMatchAllK = fmap getAll . zipMatchFoldK (coerce relations)
Since: 0.6.0
zipMatchAnyK :: forall {k} (f :: k) (as :: LoT k) (bs :: LoT k). ZipMatchFoldK f => Folds Bool as bs -> (f :@@: as) -> (f :@@: bs) -> Maybe Bool Source #
Check a relation on some paired component: Nothing when the shapes
differ, as for zipMatchFoldK, and otherwise whether some component
satisfies its relation. The components are checked left to right, and the
check stops at the first that succeeds.
zipMatchAnyK = fmap getAny . zipMatchFoldK (coerce relations)
Since: 0.6.0
Specializations
Unification of plain Types
zipMatchViaEq :: forall {k} a (as :: LoT k) (bs :: LoT k) (cs :: LoT k). Eq a => Mappings as bs cs -> a -> a -> Maybe a Source #
Unify values via Eq.
Can be used as an implementation of zipMatchWithK when k = .Type
Since: 0.2.0
zipMatchViaChooseLeft :: forall {k} (as :: LoT k) (bs :: LoT k) (cs :: LoT k) a. Mappings as bs cs -> a -> a -> Maybe a Source #
Always successfully unify any two values of type a by preferring the left value.
Can be used as an implementation of zipMatchWithK when k = .Type
Since: 0.2.0
Unification of Functors
zipMatchWith1 :: ZipMatchK f => (a -> a' -> Maybe a'') -> f a -> f a' -> Maybe (f a'') Source #
zipMatchWithK specialised to functors.
Since: 0.3.0
zipMatch1 :: ZipMatchK f => f a -> f a' -> Maybe (f (a, a')) Source #
zipMatchK specialised to functors.
Since: 0.3.0
zipMatchFold1 :: (ZipMatchFoldK f, Monoid m) => (a -> a' -> m) -> f a -> f a' -> Maybe m Source #
zipMatchFoldK specialised to functors.
>>>zipMatchFold1 (\x y -> [x + y]) (Just 1) (Just (2 :: Int))Just [3]>>>zipMatchFold1 (\x y -> [x + y]) [1, 2] [10, 20 :: Int]Just [11,22]>>>zipMatchFold1 (\x y -> [x + y]) [1, 2] [10 :: Int]Nothing
Since: 0.6.0
zipMatchAll1 :: ZipMatchFoldK f => (a -> a' -> Bool) -> f a -> f a' -> Maybe Bool Source #
zipMatchAllK specialised to functors.
>>>zipMatchAll1 (==) (Just 1) (Just (1 :: Int))Just True>>>zipMatchAll1 (==) [1, 2] [1, 3 :: Int]Just False>>>zipMatchAll1 (==) [1, 2] [1 :: Int]Nothing
Since: 0.6.0
zipMatchAny1 :: ZipMatchFoldK f => (a -> a' -> Bool) -> f a -> f a' -> Maybe Bool Source #
zipMatchAnyK specialised to functors.
>>>zipMatchAny1 (==) [1, 2] [3, 2 :: Int]Just True>>>zipMatchAny1 (==) [1, 2] [3, 4 :: Int]Just False>>>zipMatchAny1 (==) [] [1 :: Int]Nothing
Since: 0.6.0
Unification of Bifunctors
zipMatchWith2 :: ZipMatchK f => (a -> a' -> Maybe a'') -> (b -> b' -> Maybe b'') -> f a b -> f a' b' -> Maybe (f a'' b'') Source #
zipMatchWithK specialised to bifunctors.
Since: 0.3.0
zipMatch2 :: ZipMatchK f => f a b -> f a' b' -> Maybe (f (a, a') (b, b')) Source #
zipMatchK specialised to bifunctors.
Since: 0.3.0
zipMatchFold2 :: (ZipMatchFoldK f, Monoid m) => (a -> a' -> m) -> (b -> b' -> m) -> f a b -> f a' b' -> Maybe m Source #
zipMatchFoldK specialised to bifunctors. With All it
checks that two nodes have the same constructor and that their components
satisfy a relation, without building anything for the components:
>>>import Data.Monoid (All (..))>>>let same = zipMatchFold2 (\x y -> All (x == y)) (\x y -> All (x == y))>>>getAll <$> same (Left 1) (Left (1 :: Int) :: Either Int Int)Just True>>>getAll <$> same (Left 1) (Left (2 :: Int) :: Either Int Int)Just False>>>getAll <$> same (Left 1) (Right (1 :: Int) :: Either Int Int)Nothing
Since: 0.6.0
zipMatchAll2 :: ZipMatchFoldK f => (a -> a' -> Bool) -> (b -> b' -> Bool) -> f a b -> f a' b' -> Maybe Bool Source #
zipMatchAllK specialised to bifunctors.
>>>zipMatchAll2 (==) (==) (Left 1) (Left (1 :: Int) :: Either Int Int)Just True>>>zipMatchAll2 (==) (==) (Left 1) (Right (1 :: Int) :: Either Int Int)Nothing
Since: 0.6.0
zipMatchAny2 :: ZipMatchFoldK f => (a -> a' -> Bool) -> (b -> b' -> Bool) -> f a b -> f a' b' -> Maybe Bool Source #
zipMatchAnyK specialised to bifunctors.
>>>zipMatchAny2 (==) (==) (Right 1) (Right (2 :: Int) :: Either Int Int)Just False
Since: 0.6.0