Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Proxy properly?

Tags:

haskell

Why this code doesn't work?

class Foo a where 
    foo :: Proxy a -> Int

bar :: Foo a => a -> Int
bar _ = foo (Proxy :: Proxy a)

It fails to compile with the message:

Could not deduce (Foo a0) arising from a use of `foo'
from the context (Foo a)
  bound by the type signature for bar :: Foo a => a -> Int
The type variable `a0' is ambiguous
In the expression: foo (Proxy :: Proxy a)
In an equation for `bar': bar _ = foo (Proxy :: Proxy a)

I tried it with and without ScopedTypeVariables extension

like image 688
Alexey Vagarenko Avatar asked Dec 20 '22 03:12

Alexey Vagarenko


1 Answers

You need ScopedTypeVariables and forall introduction to get a scoped type variable:

{-# LANGUAGE ScopedTypeVariables #-}

bar :: forall a. Foo a => a -> Int
bar _ = foo (Proxy :: Proxy a)
like image 166
András Kovács Avatar answered Feb 19 '23 08:02

András Kovács