Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type families for concrete optics #736

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Control/Lens/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
#if __GLASGOW_HASKELL__ >= 706
{-# LANGUAGE PolyKinds #-}
#endif
Expand Down Expand Up @@ -60,6 +62,8 @@ module Control.Lens.Type
, IndexedLensLike, IndexedLensLike'
, Optical, Optical'
, Optic, Optic'
, GetConcreteFunctor, GetConcreteArgProfunctor
, GetConcreteTransformedProfunctor
) where

import Control.Applicative
Expand Down Expand Up @@ -622,3 +626,53 @@ type Over p f s t a b = p a (f b) -> s -> f t
-- type 'Over'' p f = 'Simple' ('Over' p f)
-- @
type Over' p f s a = Over p f s s a a

-- | Extract the concrete 'Functor' from a concrete 'ALens', 'AnIso', etc.
--
-- @
-- GetConcreteFunctor (ALens s t a b) = Pretext (->) a b
-- GetConcreteFunctor (AnIso s t a b) = Identity
-- @
#if __GLASGOW_HASKELL__ >= 800
type family GetConcreteFunctor (x :: *) :: GetConcreteFunctorKind x
type instance GetConcreteFunctor x = GetConcreteFunctorType (GetConcreteFunctorKind x) x
type family GetConcreteFunctorKind (x :: *) :: *
type instance GetConcreteFunctorKind (pafb -> p s ((f :: j -> k) t)) = j -> k
type family GetConcreteFunctorType (k :: *) (x :: *) :: k
type instance GetConcreteFunctorType (i -> o) (pafb -> p s ((f :: i -> o) t)) = f
#else
type family GetConcreteFunctor (x :: *) :: * -> *
type instance GetConcreteFunctor (pafb -> (p :: * -> * -> *) s ((f :: * -> *) t)) = f
#endif

-- | Extract the concrete 'Profunctor' from a concrete 'ALens', 'AnIso', etc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to fix up the documentation to reflect the split into GetConcreteArgProfunctor and GetConcreteTransformedProfunctor.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could delegate to another type family recursively to handle (p s (f t)) which delegates to another to force p to have kind * -> * -> *. This would likely require ~3 type families though.

--
-- @
-- GetConcreteProfunctor (ALens s t a b) = (->)
-- GetConcreteProfunctor (AnIso s t a b) = Exchange a b
-- @
#if __GLASGOW_HASKELL__ >= 800
type family GetConcreteArgProfunctor (x :: *) :: GetConcreteArgProfunctorKind x
type instance GetConcreteArgProfunctor x =
GetConcreteArgProfunctorType (GetConcreteArgProfunctorKind x) x

type family GetConcreteTransformedProfunctor (x :: *) :: GetConcreteTransformedProfunctorKind x
type instance GetConcreteTransformedProfunctor x =
GetConcreteTransformedProfunctorType (GetConcreteTransformedProfunctorKind x) x

type family GetConcreteArgProfunctorKind (x :: *) :: *
type instance GetConcreteArgProfunctorKind ((p :: j -> k -> *) a (f b) -> psft) = j -> k -> *
type family GetConcreteArgProfunctorType (k :: *) (x :: *) :: k
type instance GetConcreteArgProfunctorType (i1 -> i2 -> *) ((p :: i1 -> i2 -> *) a (f b) -> psft) = p

type family GetConcreteTransformedProfunctorKind (x :: *) :: *
type instance GetConcreteTransformedProfunctorKind (pafb -> (p :: j -> k -> *) s (f t)) = j -> k -> *

type family GetConcreteTransformedProfunctorType (k :: *) (x :: *) :: k
type instance GetConcreteTransformedProfunctorType (i1 -> i2 -> *) (pafb -> (p :: i1 -> i2 -> *) s (f t)) = p
#else
type family GetConcreteArgProfunctor (x :: *) :: * -> * -> *
type instance GetConcreteArgProfunctor ((p :: * -> * -> *) a ((f :: * -> *) b) -> psft) = p
type family GetConcreteTransformedProfunctor (x :: *) :: * -> * -> *
type instance GetConcreteTransformedProfunctor (pafb -> (p :: * -> * -> *) s ((f :: * -> *) t)) = p
#endif