Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
CTRex
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Row polymorphism == Some extensible record systems, such as CTRex, support '''row polymorphism'''. This concept is best explained by an example, consider the following function (in CTRex): <haskell> f :: Rec ("x" ::= Double .| "y" ::= Double .| Empty) -> Rec ("x" ::= Double .| "y" ::= Double .| "dist" ::= Double .| Empty) f r = norm := dist ((r.!x * r.!x) + (r.!y * r.!y)) .| r </haskell> Where <hask>(.!)</hask> is the function to get a value from a record. This function takes a record with row <hask>{ x = Double, y = Double }</hask> and add returns the same record with a label <hask>dist</hask> added, which carries the distance of the point <hask>(x,y)</hask> from <hask>(0,0)</hask>. Now suppose we want to call this function with record <hask>{ name = "PointA", x = 10, y = 10 }</hask>. This is not possible, because the row <hask>{ name = String, x = Double, y = Double }</hask> and <hask>{ x = Double, y = Double }</hask> are not the same. For this we need row polymorphism: we want to make the function <hask>f</hask> '''polymorphic''' in the rest of the row as follows: <haskell> f :: ((r :! "x") ~ Double, (r :! "y") ~ Double) => Rec r -> Rec ("norm" ::= Double :| r) f r = norm := dist ((r.!x * r.!x) + (r.!y * r.!y)) .| r </haskell> Now the type <hask> f </hask> reads as follows: Given some record with row <hask>r</hask>where x and y have type Double, return a record in which x,y and dist have type Double and there are also some other fields described by row <hask>r</hask>. The type that GHC infers is even more general : <haskell> f :: (Floating t, (r :! "y") ~ t, (r :! "x") ~ t) => Rec r -> Rec (Extend "norm" (r :! "x") r) </haskell>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width