cl-algebraic-data-type icon indicating copy to clipboard operation
cl-algebraic-data-type copied to clipboard

Extend saved constructor meta-info with types

Open jsjolen opened this issue 6 years ago • 4 comments

Saving the constructor types along with constructor name and arity would allow for features such as automatically deriving definitions for Functor (fmap), Foldable (fold), or what have you[0].

Either we add the information such that we change the return value of get-constructors or a new function (constructor-types adt constructor).

The former option might break user code since (destructuring-bind (a b) (g-c adt)) would signal a run-time error. The latter option has no such issues but it's annoying to have to call both functions. This could be solved by exporting a third function (get-constructors-and-types adt) in either cl-a-d-t or in algebraic-data-library.

I can write the implementation of this feature, what I need to know is which solution is preferable.

[0] https://gitlab.haskell.org/ghc/ghc/wikis/commentary/compiler/derive-functor note however that the exact impl. of this must be different for us since the constructors aren't parametric, the type info is still needed.

jsjolen avatar Mar 31 '19 16:03 jsjolen

Furthermore this type information can be used in adt:match to expand to a 'more typed' version. This might lead to the compiler (such as SBCL) giving more useful errors to the user.

jsjolen avatar Apr 01 '19 12:04 jsjolen

Alright so I've extended the interface with two functions get-types and get-parametric-vars.

I've also extended the syntax of defdata to allow parametric type variables. These are never instantiated to actual types, they're just coerced into T. The value they provide is currently for meta-programming.

All of the examples use these new parametric variables.

With that in place I ought to be able to implement the algorithms as described here: https://gitlab.haskell.org/ghc/ghc/wikis/commentary/compiler/derive-functor

All of this is available in my fork.

EDIT: This feature is strictly incompatible with :INCLUDE and so can't be used simultaneously.

jsjolen avatar Apr 25 '19 18:04 jsjolen

@jsjolen Do you want to open a draft PR for it?

stylewarning avatar May 23 '19 22:05 stylewarning

@stylewarning No it's okay for the moment. It's basically a draft of something that could be improved significantly. I have to put in more hours for this to to become usable.

jsjolen avatar May 26 '19 13:05 jsjolen