The `type` herald and parentheses

Consider the following program (requires GHC 9.10):

{-# LANGUAGE ExplicitNamespaces, RequiredTypeArguments #-}

import Data.Functor.Compose

(@$?!) :: a -> forall f -> Applicative f => f a
a @$?! f = pure @f a

-- prints: Compose (Just (Just 1))
main :: IO ()
main = print $
  1 @$?! (type (Compose Maybe Maybe))

Fun stuff! But using the type herald in this way requires two sets of parentheses. This is a pity considering that one of the advantages of using a symbolic operator is that it often reduces the need for parentheses, if precedences are chosen wisely.

Would it be wise to consider changing how this use of type is parsed to make one or both of these sets unnecessary? The outer set could be gotten rid of if 'type' atype is also parsed as an exp10p, and the inner set could be gotten rid of if the atype in that production is changed to btype. Neither change introduces any grammar conflicts, according to Happy. But are there other reasons not to propose them?

2 Likes

That’s how the feature originally worked before this amendment: Amend 281 to use atype instead of ktype by int-index · Pull Request #606 · ghc-proposals/ghc-proposals · GitHub

So, yes, it is possible to do that, though I’d suggest to avoid type altogether. Your example can be rewritten as follows:

{-# LANGUAGE RequiredTypeArguments #-}

import qualified Data.Functor.Compose as T (Compose)
import Data.Functor.Compose

(@$?!) :: a -> forall f -> Applicative f => f a
a @$?! f = pure @f a

-- prints: Compose (Just (Just 1))
main :: IO ()
main = print $
  1 @$?! T.Compose Maybe Maybe

EDIT: yet another option

main :: IO ()
main = print $
  1 @$?! (type Compose) Maybe Maybe
3 Likes

Good advice, thank you!

Not my idea of “fun”. I’ve been feeling for some time GHC’s Haskell is getting too close to line noise.

It looks ugly? Putting a type in an expression is so unusual it deserves to get loudly signalled?

just looks weird.

I’ve never seen the ‘type’ keyword used in this way. What does it do in this case? And, what’s the point of this exercise?

See ghc-proposals/proposals/0281-visible-forall.rst at master · ghc-proposals/ghc-proposals · GitHub

Perhaps start with the examples and explanation.