free-foil-0.6.1: Efficient Type-Safe Capture-Avoiding Substitution for Free (Scoped Monads)
Safe HaskellNone
LanguageHaskell2010

Data.ZipMatchK

Description

Kind-polymorphic syntactic (first-order) unification.

Synopsis

Documentation

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 #

Perform one level of equality testing:

  • when k = Type, values are compared directly (e.g. via Eq);
  • when k = Type -> Type, term constructors are compared. Unequal constructors give Nothing, and equal ones have all their components paired up with a given function.

Since: 0.2.0

default zipMatchWithK :: forall (as :: LoT k) (bs :: LoT k) (cs :: LoT k). (GenericK f, GZipMatch (RepK f), ReqsZipMatchWith (RepK f) as bs cs) => Mappings as bs cs -> (f :@@: as) -> (f :@@: bs) -> Maybe (f :@@: cs) Source #

Instances

Instances details
ZipMatchK Either Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type -> Type)) (bs :: LoT (Type -> Type -> Type)) (cs :: LoT (Type -> Type -> Type)). Mappings as bs cs -> (Either :@@: as) -> (Either :@@: bs) -> Maybe (Either :@@: cs) Source #

ZipMatchK (,) Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type -> Type)) (bs :: LoT (Type -> Type -> Type)) (cs :: LoT (Type -> Type -> Type)). Mappings as bs cs -> ((,) :@@: as) -> ((,) :@@: bs) -> Maybe ((,) :@@: cs) Source #

ZipMatchK NonEmpty Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) (cs :: LoT (Type -> Type)). Mappings as bs cs -> (NonEmpty :@@: as) -> (NonEmpty :@@: bs) -> Maybe (NonEmpty :@@: cs) Source #

ZipMatchK Maybe Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) (cs :: LoT (Type -> Type)). Mappings as bs cs -> (Maybe :@@: as) -> (Maybe :@@: bs) -> Maybe (Maybe :@@: cs) Source #

ZipMatchK [] Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) (cs :: LoT (Type -> Type)). Mappings as bs cs -> ([] :@@: as) -> ([] :@@: bs) -> Maybe ([] :@@: cs) Source #

ZipMatchK a => ZipMatchK (Either a :: Type -> Type) Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) (cs :: LoT (Type -> Type)). Mappings as bs cs -> (Either a :@@: as) -> (Either a :@@: bs) -> Maybe (Either a :@@: cs) Source #

ZipMatchK a => ZipMatchK ((,) a :: Type -> Type) Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) (cs :: LoT (Type -> Type)). Mappings as bs cs -> ((,) a :@@: as) -> ((,) a :@@: bs) -> Maybe ((,) a :@@: cs) Source #

(ZipMatchK ann, ZipMatchK sig) => ZipMatchK (AnnSig ann sig :: Type -> Type -> Type) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Annotated

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type -> Type)) (bs :: LoT (Type -> Type -> Type)) (cs :: LoT (Type -> Type -> Type)). Mappings as bs cs -> (AnnSig ann sig :@@: as) -> (AnnSig ann sig :@@: bs) -> Maybe (AnnSig ann sig :@@: cs) Source #

(ZipMatchK f, ZipMatchK g) => ZipMatchK (Product f g :: Type -> Type) Source #

Note: instance is limited to Type-kinded functors f and g.

Instance details

Defined in Data.ZipMatchK.Functor

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) (cs :: LoT (Type -> Type)). Mappings as bs cs -> (Product f g :@@: as) -> (Product f g :@@: bs) -> Maybe (Product f g :@@: cs) Source #

(ZipMatchK f, ZipMatchK g) => ZipMatchK (Sum f g :: Type -> Type) Source #

Note: instance is limited to Type-kinded functors f and g.

Instance details

Defined in Data.ZipMatchK.Functor

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) (cs :: LoT (Type -> Type)). Mappings as bs cs -> (Sum f g :@@: as) -> (Sum f g :@@: bs) -> Maybe (Sum f g :@@: cs) Source #

(ZipMatchK f, ZipMatchK g) => ZipMatchK (Product f g :: Type -> Type -> Type) Source #

Note: instance is limited to Type-kinded bifunctors f and g.

Instance details

Defined in Data.ZipMatchK.Bifunctor

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type -> Type)) (bs :: LoT (Type -> Type -> Type)) (cs :: LoT (Type -> Type -> Type)). Mappings as bs cs -> (Product f g :@@: as) -> (Product f g :@@: bs) -> Maybe (Product f g :@@: cs) Source #

(ZipMatchK f, ZipMatchK g) => ZipMatchK (Sum f g :: Type -> Type -> Type) Source #

Note: instance is limited to Type-kinded bifunctors f and g.

Instance details

Defined in Data.ZipMatchK.Bifunctor

Methods

zipMatchWithK :: forall (as :: LoT (Type -> Type -> Type)) (bs :: LoT (Type -> Type -> Type)) (cs :: LoT (Type -> Type -> Type)). Mappings as bs cs -> (Sum f g :@@: as) -> (Sum f g :@@: bs) -> Maybe (Sum f g :@@: cs) Source #

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

default zipMatchFoldK :: forall (as :: LoT k) (bs :: LoT k) m. (GenericK f, GZipMatchFold (RepK f), Monoid m) => Folds m as bs -> (f :@@: as) -> (f :@@: bs) -> Maybe m Source #

Instances

Instances details
ZipMatchFoldK Either Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type -> Type)) (bs :: LoT (Type -> Type -> Type)) m. Monoid m => Folds m as bs -> (Either :@@: as) -> (Either :@@: bs) -> Maybe m Source #

ZipMatchFoldK (,) Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type -> Type)) (bs :: LoT (Type -> Type -> Type)) m. Monoid m => Folds m as bs -> ((,) :@@: as) -> ((,) :@@: bs) -> Maybe m Source #

ZipMatchFoldK NonEmpty Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) m. Monoid m => Folds m as bs -> (NonEmpty :@@: as) -> (NonEmpty :@@: bs) -> Maybe m Source #

ZipMatchFoldK Maybe Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) m. Monoid m => Folds m as bs -> (Maybe :@@: as) -> (Maybe :@@: bs) -> Maybe m Source #

ZipMatchFoldK [] Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) m. Monoid m => Folds m as bs -> ([] :@@: as) -> ([] :@@: bs) -> Maybe m Source #

ZipMatchK a => ZipMatchFoldK (Either a :: Type -> Type) Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) m. Monoid m => Folds m as bs -> (Either a :@@: as) -> (Either a :@@: bs) -> Maybe m Source #

ZipMatchK a => ZipMatchFoldK ((,) a :: Type -> Type) Source # 
Instance details

Defined in Data.ZipMatchK.Generic

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) m. Monoid m => Folds m as bs -> ((,) a :@@: as) -> ((,) a :@@: bs) -> Maybe m Source #

(ZipMatchFoldK ann, ZipMatchFoldK sig) => ZipMatchFoldK (AnnSig ann sig :: Type -> Type -> Type) Source #

Matching for AnnSig is derived, not left to the generic default.

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. deriveZipMatchK2 generates the written-out instance instead: the annotation matched with the term zipper (an annotation is a functor of the term), and the inner signature with both. Write deriveZipMatchK2 ''YourAnnSig for a bespoke annotated signature.

Instance details

Defined in Control.Monad.Free.Foil.Annotated

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type -> Type)) (bs :: LoT (Type -> Type -> Type)) m. Monoid m => Folds m as bs -> (AnnSig ann sig :@@: as) -> (AnnSig ann sig :@@: bs) -> Maybe m Source #

(ZipMatchFoldK f, ZipMatchFoldK g) => ZipMatchFoldK (Product f g :: Type -> Type) Source #

Note: instance is limited to Type-kinded functors f and g.

Instance details

Defined in Data.ZipMatchK.Functor

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) m. Monoid m => Folds m as bs -> (Product f g :@@: as) -> (Product f g :@@: bs) -> Maybe m Source #

(ZipMatchFoldK f, ZipMatchFoldK g) => ZipMatchFoldK (Sum f g :: Type -> Type) Source #

Note: instance is limited to Type-kinded functors f and g.

Instance details

Defined in Data.ZipMatchK.Functor

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type)) (bs :: LoT (Type -> Type)) m. Monoid m => Folds m as bs -> (Sum f g :@@: as) -> (Sum f g :@@: bs) -> Maybe m Source #

(ZipMatchFoldK f, ZipMatchFoldK g) => ZipMatchFoldK (Product f g :: Type -> Type -> Type) Source #

Note: instance is limited to Type-kinded bifunctors f and g.

Instance details

Defined in Data.ZipMatchK.Bifunctor

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type -> Type)) (bs :: LoT (Type -> Type -> Type)) m. Monoid m => Folds m as bs -> (Product f g :@@: as) -> (Product f g :@@: bs) -> Maybe m Source #

(ZipMatchFoldK f, ZipMatchFoldK g) => ZipMatchFoldK (Sum f g :: Type -> Type -> Type) Source #

Note: instance is limited to Type-kinded bifunctors f and g.

Instance details

Defined in Data.ZipMatchK.Bifunctor

Methods

zipMatchFoldK :: forall (as :: LoT (Type -> Type -> Type)) (bs :: LoT (Type -> Type -> Type)) m. Monoid m => Folds m as bs -> (Sum f g :@@: as) -> (Sum f g :@@: bs) -> Maybe m Source #

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