Records in PureScript
Asked Answered
P

1

5

I don't quite understand why this works:

module Records where

type Element e = { element :: String, label :: String | e }
type Sel = ( value :: Number, values :: [Number] )

type Select = Element Sel

while this says Cannot unify # * with *.

module Records where

type Element e = { element :: String, label :: String | e }
type Sel = { value :: Number, values :: [Number] }

type Select = Element Sel

(Note the '()' around the right hand side of Sel instead of the '{}'.)

I've read here https://leanpub.com/purescript/read#leanpub-auto-objects-and-rows that forall r. { firstName :: String, lastName :: String | r } desugars to forall r. Object (firstName :: String, lastName :: String | r)

I'm still a bit confused, why you can't use the record-sugar for extending records.

Pact answered 8/2, 2015 at 16:7 Comment(0)
D
13

The Object type constructor is parameterized by a row of types. In kind notation, Object has kind # * -> *. That is, it takes a row of types to a type.

( value :: Number, values :: [Number] ) denotes a row of types (something of kind # *), so it can be passed to Object to construct a type, namely

Object ( value :: Number, values :: [Number] )

Note that { ... } is just syntactic sugar for the Object type constructor, so this is the same as

{ value :: Number, values :: [Number] }

Both have kind *, so it doesn't make sense to pass this thing as an argument to Element, since the type variable e in Element has kind # *.

Put another way, Element Sel in your second example unrolls to

{ element :: String, label :: String | { value :: Number, values :: [Number] } }

which desugars to

Object (element :: String, label :: String | Object (value :: Number, values :: [Number]) )

which fails to kind-check due to the thing of kind * in the tail of the outer row.

Dawn answered 8/2, 2015 at 19:49 Comment(2)
thank you very much. This explains everything :) I think it would be really nice to have an entry about records in the wiki. Like what the { value :: Number, values :: [Number] } desugars into. And an example how you would extend a record would be really helpful. Thanks againPact
No problem. Feel free to copy this onto the wiki if you like.Dawn

© 2022 - 2024 — McMap. All rights reserved.