-
Notifications
You must be signed in to change notification settings - Fork 35
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
Support monads #184
Comments
If we're adding an instance Monoid a => Applicative ((,) a) But currently, we don't have a |
Also, if we're going to be adding mapM :: (Traversable t, Monad m) => (a -> m b) -> t a -> m (t b) Which is re-exported from Of course, newtype Identity a = Identity { runIdentity :: a }
newtype Const a (b :: Type) = Const { getConst :: a } -- Kind-restricted Why would we need these? Because they're used in two definitions in fmapDefault :: forall t a b . Traversable t => (a -> b) -> t a -> t b
fmapDefault f = runIdentity . traverse (Identity . f)
foldMapDefault :: forall t m a . (Traversable t, Monoid m) => (a -> m) -> t a -> m
foldMapDefault f = getConst . traverse (Const . f) (Plus, As far as I know, nothing is blocking us from adding |
Also, I figured out what CUSK issue you're referring to. Here's a minimized example: {-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -ddump-splices #-}
module Bug where
import Data.Kind
import Data.Singletons.TH
$(singletons
[d| class App (f :: * -> *) where
(**>) :: f a -> f b -> f b
|]) This generates the following defunctionalization data type:
About which GHC complains:
It turns out, though, that there is a way to hack around this: $(singletons
[d| class App (f :: * -> *) where
(**>) :: (f :: * -> *) a -> f b -> f b
|]) That's somewhat unsightly, but it's a serviceable workaround. |
Pull request #314 adds the I'll work on adding |
Although |
…n and TypeInType] I discovered this buglet when trying to single a polykinded version of the Const data type (for #184), which critically relies on this when defunctionalizing its Enum instance.
This completes the `Data.Singletons.Prelude.*` changes necessary to support `Monad` and friends. This: * Adds the appropriate modules for the `Foldable` and `Traversable` classes, as well as the `Identity` and `Const` newtypes. * Uncomments various things in `Data.Singletons.Prelude.{Applicative,Monad}` that couldn't be supported before due to the lack of the classes in the previous bullet points. * Reexports the appropriate `Foldable`/`Traversable` functions from `Data.{Promotion,Singletons}.Prelude.List` to mirror what `Data.List` does. This required some reorganization of internal modules, but was otherwise straightforward. Checks off the second bullet point in #184.
Pull request #337 is about the second bullet point ( |
* Promote and single Foldable, Traversable, Const, and Identity This completes the `Data.Singletons.Prelude.*` changes necessary to support `Monad` and friends. This: * Adds the appropriate modules for the `Foldable` and `Traversable` classes, as well as the `Identity` and `Const` newtypes. * Uncomments various things in `Data.Singletons.Prelude.{Applicative,Monad}` that couldn't be supported before due to the lack of the classes in the previous bullet points. * Reexports the appropriate `Foldable`/`Traversable` functions from `Data.{Promotion,Singletons}.Prelude.List` to mirror what `Data.List` does. This required some reorganization of internal modules, but was otherwise straightforward. Checks off the second bullet point in #184. * Use list comprehensions in a couple more places
Finally, #344 implements the last bullet point ( |
* Support deriving Functor, Foldable, and Traversable Fixes #184 for good. * Use a map from stock classes to their decs
We're surprisingly close to being able to support monads (and, with them, list comprehensions). Singletonizing the basic class definitions works. But there's danger on the horizon.
The problem has all to do with CUSKs. As
Note [CUSKification]
in Promote describes, we have little control over when GHC infers a CUSK. And this causes havoc when we want GHC to infer the kind of higher-kinded type variables. Specifically, thePFunctor
class has a CUSK, and so the user needs to writeclass Functor (f :: * -> *)
. Putting in the kind annotations here isn't so, so terrible.What's worse is that we run into similar problems elsewhere. I haven't tracked down the precise details, but compiling Base.hs on the
monads
branch led to an error where a defunctionalization symbol has a CUSK, and thus inferring that a kind variable should be higher-kinded fails.Short of fixing CUSKs in GHC, the solution might be to emit GADT-style declarations of defunctionalization symbols instead of H98-style. This would allow us finer control of whether we have a CUSK. (Recall that, with
TypeInType
, all kind variables must be explicitly quantified in order to have a CUSK. So we just won't do that.) This is tantalizingly close. But I'm not going to get there tonight.Checklist:
Functor
,Applicative
,Monad
,Alternative
,MonadPlus
, andMonadZip
classes, as well as supporting functions. (Fix #184 by adding support for monads #314)Identity
,Const
,Foldable
, andTraversable
, as well as supporting functions. (Promote and single Foldable, Traversable, Const, and Identity #337)Functor
,Foldable
, andTraversable
. (Support deriving Functor, Foldable, and Traversable #344)The text was updated successfully, but these errors were encountered: