Satz der Stunde

Heisse News

Quicksearch

Kategorien

Archives

Display comments as
(Linear | Threaded)

"It is perfectly valid to have functions with signature (Monoid a => Int -> Int)"

Citation needed.

Citation needed.

Functions with a signature like (Monoid a => Int -> Int) are not allowed, AFAIK. You'll get an 'ambiguous constraint' error.

And even if you could use that type, what could such a function possibly do with it that it couldn't do with Int -> Int?

Assume the class in question were Num and assume ScopedTypeVariables. Then the code could run something like (1 :: a) >= (0 :: a) which depends on the Num and Ord instance in question, without inputing or outputting data.

If the function is an IO computation, reading from and writing to disk, such a thing could actually make sense :-)

If the function is an IO computation, reading from and writing to disk, such a thing could actually make sense :-)

You can even turn your dictionary back, safely into a type class using reflection.

The problem with carrying around a dictionary in data is as you mention "mappend has the choice between three dictionaries This is not a good sign, but let’s hope that they are all the same."

With some oleggery you can, however, define a typeclass as in

http://hackage.haskell.org/packages/archive/reflection/0.3.0/doc/html/Data-Reflection.html

which provides:

data Tagged s a = Tagged a

class Reifies s a | s -> a where

reflect :: Tagged s a

reify :: a -> (forall s. Reifies s a => Tagged s w) -> w

With that you can build a monoid out of arbitrary functions, and still make sure due to the phantom type parameter that they all agree on the dictionary.

Using an old version of that library I posted:

http://www.mail-archive.com/haskell-cafe@haskell.org/msg57747.html

It is an interesting exercise to update that example to use the current safer reflection API.

The problem with carrying around a dictionary in data is as you mention "mappend has the choice between three dictionaries This is not a good sign, but let’s hope that they are all the same."

With some oleggery you can, however, define a typeclass as in

http://hackage.haskell.org/packages/archive/reflection/0.3.0/doc/html/Data-Reflection.html

which provides:

data Tagged s a = Tagged a

class Reifies s a | s -> a where

reflect :: Tagged s a

reify :: a -> (forall s. Reifies s a => Tagged s w) -> w

With that you can build a monoid out of arbitrary functions, and still make sure due to the phantom type parameter that they all agree on the dictionary.

Using an old version of that library I posted:

http://www.mail-archive.com/haskell-cafe@haskell.org/msg57747.html

It is an interesting exercise to update that example to use the current safer reflection API.

Thanks, very interesting pointer. Encoding the value of a pointer in a type expression seems to be, well, bold, but it does go quite far.

## Trackbacks