[Haskell-cafe] issues with ad-hoc polymorphic typeclass instances

Weylin Lam lam.dev.hs at gmail.com
Sat Sep 2 05:10:36 UTC 2017


Hello !

I'm trying to write a polymorphic class instance in function of another
class. So far it's pretty hard, I have trouble with headsizes and so on. I
actually had to switch to type families pretty fast, and i'm still not out
of the woods.
http://lpaste.net/2650337298029215744

The context:
It concerns "tagless final interpreters" for EDSLs as described in
http://okmij.org/ftp/tagless-final/course/lecture.pdf
A comparison with free monads: http://yowconference.com.au/slides/
yowlambdajam2016/Hopkins-StopPayingForFreeMonads.pdf

The technique is fairly simple: instead of using ADTs to encode the
operations of the language, we use haskell's class system. Languages become
type classes, and interpreters are the instances which give various
different meanings to the language expressions, which become simple
haskell, ad-hoc polymorphic values.

It has strong advantages. For example, to write some expression in the
composition of two languages (two typeclasses), you only need to combine
the constraints:
class WriteFile h m where
  writeFile :: h -> String -> m ()
class ReadFile h m where
  readFile :: h -> m String

-- we use Monad m to have access to monadic operations, but the language
itself
-- does not care that m be a monad or not.
myExpression :: (WriteFile h m, ReadFile h m, Monad m) => h -> h -> String
-> m String
myExpression h h2 str = writeFile h str *> readFile h2

The fusion of both languages is utterly seamless, which is the beauty of
typeclasses.

When writing DSLs, there are at least two basic operations that need to be
done: combining DSLs to create richer languages (and as just shown, it's
really simple with this technique), and translating one DSL into another.

That latter operation between DSLs is a bit more complicated with classes.
I haven't found examples of how to do so in the wild (the technique doesn't
seem very much used, esp compared with free monads), so if anybody knows
how to do it or has references on that, it'd be much appreciated.

Theoretical example of what i'm trying to perform:
class A a where
  subtract :: a -> a -> a
class B a where
  add :: a -> a -> a
  neg :: a -> a

-- interpreter from A to B: (doesn't work of course)
class (B a) => A a where
  subtract x y = add x (neg y)

-- interpreter for B in terms of Int:
instance B Int where
  add = (+)
  neg = negate

expression :: (A a) => a
expression = subtract (subtract 4 5) 6
-- to get it to be interpreted, we need to select the type. usually we use
a monomorphic version of id:
asInt = identity :: Int -> Int
intExp = asInt expression :: Int

For the instance B Int to be re-usable as instance/interpreter of (A Int)
expressions, we need a way to write an interpretation of A a in terms of
any pre-existing interpretation of B b.
Usually there are some issues, the need to wrap the types into newtype
wrappers among others.

But in the case i'm trying to solve, it still doesn't work. To see where
i'm stuck, see above my lpaste.

Any help or ideas would be very welcome! :) Thanks a lot in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170902/66f16183/attachment.html>


More information about the Haskell-Cafe mailing list