简体   繁体   中英

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 } forall r. { firstName :: String, lastName :: String | r } forall r. { firstName :: String, lastName :: String | r } desugars to forall r. Object (firstName :: String, lastName :: String | r) 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM