HList-0.3.4.1: Heterogeneous lists

Safe HaskellNone

Data.HList.RecordPuns

Description

 

Synopsis

Documentation

>>> :set -XQuasiQuotes -XViewPatterns
patterns
>>> let y = Label :: Label "y"
>>> let x = Label :: Label "x"
>>> [pun| x y |] <- return (x .=. 3 .*. y .=. "hi" .*. emptyRecord)
>>> print (x,y)
(3,"hi")
expressions

Compare with the standard way to construct records above

>>> let x = 3; y = "hi"
>>> [pun|x y|]
Record{x=3,y="hi"}
nesting

Nesting is supported. The idea is that variables inside { } are in another record. More concretely:

 [pun| ab@{ a b } y z c{d} |]

as a pattern, it will bindings from an original record x, if you interpret (.) as a left-associative field lookup (as it is in other languages):

 let ab = xab
     a = x.ab.a
     b = x.ab.b
     y = x.y
     z = x.z
     -- c is not bound
     d = x.c.d

as an expression, it creates a new record which needs the variables ab a b y z d in-scope. ab needs to be a record, and if it has fields called a or b they are overridden by the values of a and b which are in scope.

( ) parens mean the same thing as { }, except the pattern match restricts the fields in the record supplied to be exactly the ones provided. In other words

 [pun| (x y) |] = list
 -- desugars to something like:
 Record (HCons (Tagged x :: Tagged "x" s1)
              (HCons (Tagged x :: Tagged "y" s2)
               HNil)) = list

Where the s1 and s2 are allowed to fit whatever is in the HList.

See also examples/pun.hs.

pun :: QuasiQuoterSource

requires the use of Data.HList.Label6 (ie. the label for foo is Label :: Label "foo")