[Haskell-cafe] RankNTypes question

14 views
Skip to first unread message

Nikolay Amiantov

unread,
Feb 26, 2015, 1:19:10 PM2/26/15
to haskel...@haskell.org
Hi Cafe,

I can't find how can I do this:

{-# LANGUAGE RankNTypes #-}

example :: forall a. (forall b. b) -> a
example = undefined

The first `forall` is for cosmetic reasons. If I understand correctly
this is a safe and legit function (please correct me if I'm wrong!). I
thought that I can just define `example = id` and GHC would do the rest,
but alas, that didn't happen.

Thanks!

--
Nikolay.

_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Nikolay Amiantov

unread,
Feb 26, 2015, 1:36:50 PM2/26/15
to haskel...@haskell.org
Hm, now that I look at it, I've given the wrong example which badly
describes what I want -- sorry. Let me give you a more specific one:

{-# LANGUAGE RankNTypes #-}

test :: (forall a. (a -> a)) -> (Int -> Int)
test = id-- something else should be here

i.e., I want to "specialize" a polymorphic value -- in this case, a
polymorphic function to the function on `Int`s.

On 02/26/2015 09:24 PM, Marcin Mrotek wrote:
> Hello,
>
> id has type a -> a, a function from some type to exactly the same
> type. Your function would be from any type to any other (the same type
> as given being a special case). It seem to me that
>
> example _ = undefined
>
> is the only legal definition. What do you exactly want the function to do?
>
> Kind regards,
> Marcin Mrotek

Nikolay Amiantov

unread,
Feb 26, 2015, 1:50:40 PM2/26/15
to haskel...@haskell.org
Ah, thanks! For some reason I haven't even thought of it -- I knew that
`id` was wrong because of the type, but had no idea what else to try.
Feeling stupid now.

On 02/26/2015 09:46 PM, Marcin Mrotek wrote:
> Well still, id is (a -> a). So given some function (forall b. b -> b)
> it's result is a function (forall b. b -> b). This works:
>
> test :: (forall a. a -> a) -> Int -> Int
> test f a = f a

Marcin Mrotek

unread,
Feb 26, 2015, 1:53:48 PM2/26/15
to Nikolay Amiantov, Haskell-Cafe
Ah sorry, I think I responded only to you, not to the whole list. If
anyone else is interested, my answer was:

test :: (forall a. a -> a) -> Int -> Int
test f a = f a

Also consider this:

foo :: Maybe a -> Maybe b
foo Nothing = Nothing

vs.

foo :: Maybe a -> Maybe b
fooo a@Nothing = a

You don't actually need RankNTypes to bump into this. Don't feel
stupid, I remember running into this kind of things frequently when I
was trying to define Prisms (from lens) by hand ;)

Kind regards,
Marcin Mrotek
Reply all
Reply to author
Forward
0 new messages