Fwd: Reducing boilerplate

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: Reducing boilerplate

Sylvain HENRY
Hi devs,

I would like to add the support for the following automatic
instance-deriving extension:

module M where

class G a where
    doG :: a -> Int

class P a where
  doP :: a -> Int
  doP _ = 10
  deriving instance G a where -- automatically derived instance
    doG = doP

data X = X
instance P X -- derive G X automatically

print (doG X) -- print 10

See the forwarded mail below for the real context. This extension has
been proposed by someone before as InstanceTemplates:
https://ghc.haskell.org/trac/ghc/wiki/InstanceTemplates

I have modified the parser and the renamer accordingly to get:

class M.G a_awb where
  M.doG :: a_awb -> Int
class M.P a_ap6 where
  M.doP :: a_ap6 -> Int
  M.doP _ = 10
  instance M.G a_ap6 where
    M.doG = M.doP

I am new to the compiler part of GHC, so I have a few questions before
I continue:
1a) does it make sense to store the renamed class instance declaration
in an interface file? (supposing it only uses exported methods/types;
we could check that)
1b) will it be possible to create a new instance declaration in
another module by just doing the substitution [a_ap6 -> X] in it?
(i.e. when we parse "instance P X", do we know that it means [a_ap6 ->
X] in class P (and not [a -> X])?)
2) maybe I should go a different way and store only the derived
instance methods as we store class default methods?

Any insight appreciated!

Thanks,
Sylvain


---------- Forwarded message ----------
From: Sylvain Henry <[hidden email]>
Date: 2016-03-05 14:56 GMT+01:00
Subject: Reducing boilerplate
To: Haskell Cafe <[hidden email]>


Hi,

To write FFI bindings, I use c-storable-deriving [1] to automatically
derive CStorable instances for many data types (the only difference
between Storable and CStorable is that CStorable provides default
methods for types that have Generic instances):

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
...
data X = X
   { fieldStatus           :: Vector 24 Word8
   , fieldPadding         :: Word8
   } deriving (Generic, CStorable)

However I also need a Storable instance, hence I have to write (the
"c*" methods are provided by CStorable):

instance Storable X where
   peek        = cPeek
   poke        = cPoke
   alignment = cAlignment
   sizeOf      = cSizeOf

Is there a way to automatically generate this instance for every data
that has an instance of CStorable? Ideally, I would like to say once
and for all:

instance CStorable a => Storable a where
   peek        = cPeek
   poke        = cPoke
   alignment = cAlignment
   sizeOf      = cSizeOf

As I don't think it is currently possible, would it be sensible to add
the support for automatically derived instances attached to classes
(as a GHC extension)?

Regards,
Sylvain

[1] https://hackage.haskell.org/package/c-storable-deriving
_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Reducing boilerplate

Ben Gamari-3
Sylvain Henry <[hidden email]> writes:

> Hi devs,
>
> I would like to add the support for the following automatic
> instance-deriving extension:
>
Hi Sylvain,

I suspect the person most qualified to answer these questions will be
Simon who is currently in the middle of paper-writing season.
Consequently, it may be a while until he is able to answer. That being
said, I'm quite happy to hear that someone is thinking about these
proposals.

Cheers,

- Ben


_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

signature.asc (482 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Reducing boilerplate

Sylvain HENRY
Hi Ben,

Thanks for your answer. No problem, I can wait.

With this proposal, we would have a really nice story about doing FFI
with GHC. I've been playing with DataKinds and other type-related
extensions for a few days (thanks to everyone involved in implementing
them!) and this extension would remove the last glitch:
https://github.com/hsyl20/ViperVM/blob/master/WritingBindings.md
Btw, the Vector part is inspired from what you did here:
https://github.com/expipiplus1/vulkan/pull/1 (thanks!)

Cheers,
Sylvain


2016-03-11 16:51 GMT+01:00 Ben Gamari <[hidden email]>:

> Sylvain Henry <[hidden email]> writes:
>
>> Hi devs,
>>
>> I would like to add the support for the following automatic
>> instance-deriving extension:
>>
> Hi Sylvain,
>
> I suspect the person most qualified to answer these questions will be
> Simon who is currently in the middle of paper-writing season.
> Consequently, it may be a while until he is able to answer. That being
> said, I'm quite happy to hear that someone is thinking about these
> proposals.
>
> Cheers,
>
> - Ben
>
_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

RE: Fwd: Reducing boilerplate

Simon Peyton Jones
Great.  Is there a clearly-articulated design on a wiki page?

Simon

|  -----Original Message-----
|  From: ghc-devs [mailto:[hidden email]] On Behalf Of
|  Sylvain Henry
|  Sent: 11 March 2016 20:59
|  To: Ben Gamari <[hidden email]>
|  Cc: [hidden email]
|  Subject: Re: Fwd: Reducing boilerplate
|  
|  Hi Ben,
|  
|  Thanks for your answer. No problem, I can wait.
|  
|  With this proposal, we would have a really nice story about doing FFI
|  with GHC. I've been playing with DataKinds and other type-related
|  extensions for a few days (thanks to everyone involved in implementing
|  them!) and this extension would remove the last glitch:
|  https://github.com/hsyl20/ViperVM/blob/master/WritingBindings.md
|  Btw, the Vector part is inspired from what you did here:
|  https://github.com/expipiplus1/vulkan/pull/1 (thanks!)
|  
|  Cheers,
|  Sylvain
|  
|  
|  2016-03-11 16:51 GMT+01:00 Ben Gamari <[hidden email]>:
|  > Sylvain Henry <[hidden email]> writes:
|  >
|  >> Hi devs,
|  >>
|  >> I would like to add the support for the following automatic
|  >> instance-deriving extension:
|  >>
|  > Hi Sylvain,
|  >
|  > I suspect the person most qualified to answer these questions will be
|  > Simon who is currently in the middle of paper-writing season.
|  > Consequently, it may be a while until he is able to answer. That
|  being
|  > said, I'm quite happy to hear that someone is thinking about these
|  > proposals.
|  >
|  > Cheers,
|  >
|  > - Ben
|  >
|  _______________________________________________
|  ghc-devs mailing list
|  [hidden email]
|  https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2fmail.ha
|  skell.org%2fcgi-bin%2fmailman%2flistinfo%2fghc-
|  devs&data=01%7c01%7csimonpj%40064d.mgd.microsoft.com%7c8bc41138302f4cc4
|  696e08d349efffd6%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=hSoMQAHQ8
|  E5aWPa%2fK8FXsKcKoUUn7Xv6%2ftpaEX0%2b01M%3d
_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

RE: Fwd: Reducing boilerplate

Ben Gamari-3
Simon Peyton Jones <[hidden email]> writes:

> Great.  Is there a clearly-articulated design on a wiki page?
>
The design for the relevant GHC extension is here [1].

Cheers,

- Ben


[1] https://ghc.haskell.org/trac/ghc/wiki/InstanceTemplates


_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

signature.asc (482 bytes) Download Attachment