-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from fizruk/data-zipmathk
Factor out Data.ZipMatchK, remove ZipMatch
- Loading branch information
Showing
13 changed files
with
305 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
haskell/free-foil/src/Control/Monad/Free/Foil/TH/ZipMatch.hs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
{-# OPTIONS_GHC -Wno-redundant-constraints #-} | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
{-# LANGUAGE PolyKinds #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
-- | Kind-polymorphic syntactic (first-order) unification. | ||
module Data.ZipMatchK ( | ||
module Data.ZipMatchK.Mappings, | ||
ZipMatchK(..), | ||
zipMatchK, | ||
-- * Specializations | ||
-- ** Unification of plain 'Data.Kind.Type's | ||
zipMatchViaEq, | ||
zipMatchViaChooseLeft, | ||
-- ** Unification of 'Data.Functor.Functor's | ||
zipMatchWith1, | ||
zipMatch1, | ||
-- ** Unification of 'Data.Bifunctor.Bifunctor's | ||
zipMatchWith2, | ||
zipMatch2, | ||
) where | ||
|
||
import Generics.Kind | ||
import Data.Bitraversable | ||
|
||
import Data.ZipMatchK.Generic | ||
import Data.ZipMatchK.Mappings | ||
|
||
-- | Perform one level of equality testing for two values and pair up components using @(,)@: | ||
-- | ||
-- > zipMatchK = zipMatchWithK (\x y -> Just (,) :^: M0) | ||
zipMatchK :: forall f as bs. (ZipMatchK f, PairMappings as bs) => f :@@: as -> f :@@: bs -> Maybe (f :@@: ZipLoT as bs) | ||
zipMatchK = zipMatchWithK @_ @f @as @bs pairMappings | ||
|
||
-- | Unify values via 'Eq'. | ||
-- Can be used as an implementation of 'zipMatchWithK' when @k = 'Data.Kind.Type'@. | ||
zipMatchViaEq :: Eq a => Mappings as bs cs -> a -> a -> Maybe a | ||
zipMatchViaEq _ x y | ||
| x == y = Just x | ||
| otherwise = Nothing | ||
|
||
-- | Always successfully unify any two values of type @a@ by preferring the left value. | ||
-- Can be used as an implementation of 'zipMatchWithK' when @k = 'Data.Kind.Type'@. | ||
zipMatchViaChooseLeft :: Mappings as bs cs -> a -> a -> Maybe a | ||
zipMatchViaChooseLeft _ x _ = Just x | ||
|
||
-- | 'zipMatchWithK' specialised to functors. | ||
-- | ||
-- Note: 'Traversable' is a morally correct constraint here. | ||
zipMatchWith1 | ||
:: (Traversable f, ZipMatchK f) | ||
=> (a -> a' -> Maybe a'') | ||
-> f a -> f a' -> Maybe (f a'') | ||
zipMatchWith1 f = zipMatchWithK (f :^: M0) | ||
|
||
-- | 'zipMatchK' specialised to functors. | ||
-- | ||
-- Note: 'Traversable' is a morally correct constraint here. | ||
zipMatch1 :: (Traversable f, ZipMatchK f) => f a -> f a' -> Maybe (f (a, a')) | ||
zipMatch1 = zipMatchWith1 pairA | ||
-- | 'zipMatchWithK' specialised to bifunctors. | ||
-- | ||
-- Note: 'Bitraversable' is a morally correct constraint here. | ||
zipMatchWith2 | ||
:: (Bitraversable f, ZipMatchK f) | ||
=> (a -> a' -> Maybe a'') | ||
-> (b -> b' -> Maybe b'') | ||
-> f a b -> f a' b' -> Maybe (f a'' b'') | ||
zipMatchWith2 f g = zipMatchWithK (f :^: g :^: M0) | ||
|
||
-- | 'zipMatchK' specialised to bifunctors. | ||
-- | ||
-- Note: 'Bitraversable' is a morally correct constraint here. | ||
zipMatch2 :: (Bitraversable f, ZipMatchK f) => f a b -> f a' b' -> Maybe (f (a, a') (b, b')) | ||
zipMatch2 = zipMatchWith2 pairA pairA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
{-# LANGUAGE PolyKinds #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
-- | This module provides 'GenericK' and 'ZipMatchK' instances for 'Sum' and 'Product', | ||
-- to enable the use of 'ZipMatchK' with the data types à la carte approach. | ||
module Data.ZipMatchK.Bifunctor where | ||
|
||
import Data.Kind (Type) | ||
import Generics.Kind | ||
import Data.Bitraversable | ||
import Data.Bifunctor.Sum | ||
import Data.Bifunctor.Product | ||
|
||
import Data.ZipMatchK | ||
|
||
instance GenericK (Sum f g) where | ||
type RepK (Sum f g) = | ||
Field ((Kon f :@: Var0) :@: Var1) | ||
:+: Field ((Kon g :@: Var0) :@: Var1) | ||
instance GenericK (Product f g) where | ||
type RepK (Product f g) = | ||
Field ((Kon f :@: Var0) :@: Var1) | ||
:*: Field ((Kon g :@: Var0) :@: Var1) | ||
|
||
-- | Note: instance is limited to 'Type'-kinded bifunctors @f@ and @g@. | ||
instance (Bitraversable f, Bitraversable g, ZipMatchK f, ZipMatchK g) => ZipMatchK (Sum f (g :: Type -> Type -> Type)) | ||
-- | Note: instance is limited to 'Type'-kinded bifunctors @f@ and @g@. | ||
instance (Bitraversable f, Bitraversable g, ZipMatchK f, ZipMatchK g) => ZipMatchK (Product f (g :: Type -> Type -> Type)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
{-# LANGUAGE PolyKinds #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
-- | This module provides 'GenericK' and 'ZipMatchK' instances for 'Sum' and 'Product', | ||
-- to enable the use of 'ZipMatchK' with the data types à la carte approach. | ||
module Data.ZipMatchK.Functor where | ||
|
||
import Data.Kind (Type) | ||
import Generics.Kind | ||
import Data.Functor.Sum | ||
import Data.Functor.Product | ||
|
||
import Data.ZipMatchK | ||
|
||
instance GenericK (Sum f g) where | ||
type RepK (Sum f g) = | ||
Field (Kon f :@: Var0) | ||
:+: Field (Kon g :@: Var0) | ||
instance GenericK (Product f g) where | ||
type RepK (Product f g) = | ||
Field (Kon f :@: Var0) | ||
:*: Field (Kon g :@: Var0) | ||
|
||
-- | Note: instance is limited to 'Type'-kinded bifunctors @f@ and @g@. | ||
instance (Traversable f, Traversable g, ZipMatchK f, ZipMatchK g) => ZipMatchK (Sum f (g :: Type -> Type)) | ||
-- | Note: instance is limited to 'Type'-kinded bifunctors @f@ and @g@. | ||
instance (Traversable f, Traversable g, ZipMatchK f, ZipMatchK g) => ZipMatchK (Product f (g :: Type -> Type)) |
Oops, something went wrong.