samrushing/irken-compiler

need %%copyrec (for row polymorphic functional record update)

jeske opened this issue · 0 comments

jeske commented

We have a need for two related capabilities:

  • ...a way to make a copy of an open polymorphic record
  • ...a way to make a new record which differs from an existing open polymorphic record only in a subset of fields

The latter could be expressed in terms of the former copy with a few mutates.

The function change_zipcode below performs a functional update to any record type, using a mythical %%copyrec primitive (which clones/copies the input record). Note, it is impossible to build this without a new primitive.

(define (change_zipcode rec new_zip)  : ({zipcode=int ...} -> {zipcode=int ...})
    (let  ((new_rec (%%copyrec rec)))
        (set! new_rec.zipcode new_zip)
        new_rec))

OCaml has a nice syntax for functional record updates that looks like this:

let info = { name = "David" ; zipcode = 90210 }
let new_info = { info with zipcode=41111 }

If translated to Irken, this might look something like:

(define info { name="David" zipcode=90210 })
(define new_info {info with zipcode=41111 })

Which might be translated into the equivalent of:

(define new_info 
   (let (($newrec (%%copyrec info))
        (set! $newrec.zipcode 41111)
       $newrec))