Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Records in PureScript

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.

like image 639
Archaeron Avatar asked Feb 08 '15 16:02

Archaeron


People also ask

How do you write a function in PureScript?

Functions in PureScript take exactly one argument. While it looks like the insertEntry function takes two arguments, it is in fact an example of a curried function. That is, insertEntry is a function which returns a function!


1 Answers

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.

like image 96
Phil Freeman Avatar answered Oct 13 '22 11:10

Phil Freeman