5

Given I have the folowing records in purescript:

let name = {name: "Jim"}
let age = {age: 37}

is it possible to combine those two records some how in a generic way? Something like:

name 'comb' age

such that I get the following record:

{name: "Jim", age: 37}

Somehow it seems to be possible with the Eff rowtype, but I'm curious if it would be possible with 'normal' records. I'm new to purescript and it's record syntax.

Thanks a lot.

5

It's not possible to do this at the moment, as we don't have a way of saying that a row lacks some label or other. It is possible to have an open record type:

something :: forall r. { name :: String | r } -> ...

But this only allows us to accept a record with name and any other labels, it doesn't help us out if we want to combine, extend, or subtract from records as it stands.

The issue with combining arbitrary records is we'd have a type signature like this:

comb :: forall r1 r2. { | r1 } -> { | r2 } -> ???

We need some way to say the result (???) is the union of r1 and r2, but also we'd perhaps want to say that r1's labels do not overlap with r2's.

In the future this may be possible via row constraints.

  • Cool, thanks for the explanation. But just to clarify the matters a bit: How does it work with the Eff rowtype. As far as I understand the Eff row type is some how "composed" out of different effect types. How does it work there? – Tim de Putter Mar 14 '16 at 14:33
  • It relies on the types being unified, so in the example above, if we call something with { name :: String, age :: Int, address :: String } we end up with r ~ (age :: Int, address :: String) in { name :: String | r }. The Eff stuff works a similar way, we never actually have something where two different eff values are combined from different arguments to produce a new one. – gb. Mar 14 '16 at 14:51
  • ok, I think I undestand. Thanks a lot. – Tim de Putter Mar 14 '16 at 15:50
  • @gb. I think that it would be good to add a warning line that the situation has changed since then ;-) – paluh May 13 '18 at 18:43
8

EDIT:

It seems that currently the official package for handling record manipulations is purescript-record - you can find Builder.purs there which provides merge and build functions:

> import Data.Record.Builder (build, merge)
> name = {name: "Jim"}
> age = {age: 37}
> :t (build (merge age) name)
{ name :: String
, age :: Int
}

API NOTE:

This API looks overcomplicated at first glance - especially when you compare it to simple unionMerge name age call (unionMerge is intoduced at the end of this answer). The reason behind Builder existence (and so this API) is performance. I can assure you that this:

> build (merge name >>> merge age) {email: "someone@example.com"}

creates only one new record. But this:

> unionMerge name (unionMerge age {email: "someone@example.com"})

creates two records during execution.

What is even more interesting is how Builder, build and merge are implemented - Builder is newtype wrapper around function (and its composition is just function composition) and build is just function application on copied version of a record:

newtype Builder a b = Builder (a -> b)

build (Builder b) r1 = b (copyRecord r1)

In merge there is unsafeMerge performed:

merge r2 = Builder \r1 -> unsafeMerge r1 r2

So why we are gaining anything?? Because we can be sure that intermediate results can't escape function scope, so all transformation can be performed in place. In other words this intermediate value:

> intermediate = unionMerge name {email: "someone@example.com"}
> unionMerge age intermediate

can't be "extracted" from here:

> build (merge name >>> merge age) {email: "someone@example.com"}

TYPESYSTEM COMMENT:

It seems that Purescript type system can handle this now thanks to the Union type class from Prim:

The Union type class is used to compute the union of two rows 
of types (left-biased, including duplicates).

The third type argument represents the union of the first two.

Which has this "magic type" (source: slide 23):

Union r1 r2 r3 | r1 r2 -> r3, r1 r3 -> r2

OLD METHOD (still valid but not preferred):

There is purescript-records package which exposes unionMerge which does exactly what you want (in new psci we don't have to use let):

> import Data.Record (unionMerge)
> name = {name: "Jim"}
> age = {age: 37}
> :t (unionMerge age name)
{ name :: String
, age :: Int
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.