2
votes

I'm playing about with some types and the PureScript compiler. These are the types I've created (liberally stolen from purescript-dsl-example):

newtype User = User
  { id :: Int
  , name :: String
  }

data Command a = Add User a
               | Remove Int a
               | ChangeName Int String a

This type checks and compiles. Then, thinking it might be useful to be able to serialise these types into JSON, I installed purescript-foreign-generic and added this:

derive instance genericCommand :: Generic Command _

As a first step towards a Show instance.

The type checker then throws this error:

Error found:
in module Main
at src/Main.purs:33:43 - 33:50 (line 33, column 43 - line 33, column 50)

  Could not match kind

    Type

  with kind

    Type -> Type


while checking the kind of Generic Command (Sum (Constructor "Add" (Product ... ...)) (Sum (Constructor "Remove" ...) (Constructor "ChangeName" ...)))
in value declaration genericCommand

Is there a way to derive a Generic instance from my type? If not, is there a way I can write a Generic instance manually? I have no idea what that would entail, so am okay with admitting I'm not sure if that last question even makes sense.

1

1 Answers

3
votes

Of course it's possible to derive Generic for your type, you're just not using the correct syntax. It should be like this:

derive instance genericCommand :: Generic (Command a) _

When you write Generic Command _, the Command part (which is the first parameter of Generic) has kind Type -> Type - that is, if you apply Command to a Type, then you get a Type. So, for example, Command Int will have the kind Type. But Command itself has kind Type -> Type.

But the Generic class expects the first argument of kind Type, not Type -> Type, that's what the error message is telling you.