Exposing newtype coercions to Haskell

Joachim Breitner mail at joachim-breitner.de
Mon Jul 22 14:06:28 CEST 2013


as discussed, I started to work basing the newtype coercions on classes
instead of types. I poked around the code, especially TcDeriv.lhs, and I
have a few questions:

For my current design I wanted to have

        class IsNT a where
        	type Concrete a
        	coercion :: a ~# Concrete a

but that does not work, as methods seem to need to have a boxed type.
Using "a ~ Concrete a" does not work as well, as that has kind
Constraint. So I wrapped ~# myself and created:

        data NT a b = NT ((~#) a b)
        class IsNT a where
            type Concrete a
            coercion :: NT a (Concrete a)

with only IsNT (I’ll think more about the name ;-)) and probably
Concrete exposed; not or coercion. The interface would then be functions

        castNT :: IsNT a => Concrete a -> a
        uncastNT :: IsNT a => a -> Concrete a

Now we want the user to be able to specify
        deriving instance IsNT Age -- with Concrete Age = Int
        deriving instance IsNT a => IsNT [a] -- with Concrete [a] = [Concrete a]
        deriving instance IsNT v => IsNT (Map k v) -- dito
and probably also
        deriving instance IsNT Int -- with Concrete Int = Int
for all non-newtypes.

After adding the definitions to ghc-prim and extending PrelNames I tried
to make the deriving code, starting with the last examle (IsNT Int).
There, the generated code should be something like 
        instance IsNT Int where
        	type Concrete Int = Int
        	coercion = NT (refl Int)

But here I am stuck: The deriving code mechanism expects me to give the
method definitions as HsExpr, but I’d like to generate Core here. How
can I achieve that, or what other options do I have?

(Of course this will need further adjustments when Richard’s role code
is in.)

(If there is a reason to go back to the two parameter type class "NT a
b", no problem, but the problem of HsExpr vs. CoreExpr would still


Joachim “nomeata” Breitner
  mail at joachim-breitner.dehttp://www.joachim-breitner.de/
  Jabber: nomeata at joachim-breitner.de  • GPG-Key: 0x4743206C
  Debian Developer: nomeata at debian.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://www.haskell.org/pipermail/ghc-devs/attachments/20130722/beea952b/attachment.pgp>

More information about the ghc-devs mailing list