Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell record syntax desugared

Tags:

haskell

I understand how to use record syntax in Haskell, but I have trouble to understand what the thing inside the curly braces is.

data PairRecord = PR {foo::Int, bar::String} deriving (Eq,Show)
x = (PR {foo=1, bar="init"})
y= x {foo=23}
  • What is this {foo=23} thing? The last line looks as if it was an argument to the function x, which is clearly not the case.
  • Is there anything else I can do with {foo=23} except placing it right behind a record?
  • Is there a formal way to desugar it like what we can do with do notation?
like image 331
Martin Drautzburg Avatar asked Feb 12 '14 10:02

Martin Drautzburg


1 Answers

This syntax is called "record update" or "update using field labels" and described in Section 3.15.3 of the Haskell 2010 report. It can be desugared. The exact rules are given in the report. In your example, the desugaring of x {foo = 23} is

case x of
  PR f b -> PR 23 b
  _      -> error "Update error"

Note that the report uses an auxiliary function called "pick" in the desugaring that's described in the section before, 3.15.2, on "Construction using field labels".

like image 194
kosmikus Avatar answered Sep 22 '22 16:09

kosmikus