Some idioms for Trex
Anthony Clayden <[email protected]> Wed, 6 May 2020 22:37:51 +1200
| Newsgroups | gmane.comp.lang.haskell.hugs.user |
|---|---|
| Message-ID | <CAM7nRYQ_iyNRbLxumDSr8t2d4jNV9neGsKKpX-krSpyk=+nWUg@mail.gmail.com> |
--===============4470064169658838785== Content-Type: multipart/alternative; boundary="000000000000aea11005a4f85bc7" --000000000000aea11005a4f85bc7 Content-Type: text/plain; charset="UTF-8" Making best use of Trex needs a different mindset vs H98 datatypes with named fields. (Or indeed vs named-fields data structures in many languages) There's some examples around in the Trex/Hugs material, but I've seen nothing showing this: > type WPoint r = (x :: Float, y :: Float | r) -- r extended "with Point" > type WRadius r = (radius :: Float | r) > type WColour r = (colour :: String | r) > type ColouredCircle = Rec( WColour (WRadius (WPoint EmptyRow))) The idea is rather than nest 'Point' as a datatype inside 'Circle' 'plug and play' components (such as Point, Radius) to give a structural type. It's interesting and impressive that the Wxxx synonyms take an argument of kind row, return a result of kind row. `EmptyRow` in the last line is a Hugs-defined Row constant that 'plugs' the end of the composed components, just as `emptyRec` does for Trex Records. I'd love to write that last type as > type MkRec r = Rec( r EmptyRow) > type ColouredCircle = MkRec (WColour . WRadius . WPoint) But of course type synonyms -- even with parameters -- are not functions; can't be composed; can't be passed to higher-order type synonyms. Components like WPoint can be arguments to type-parametric functions for record subtyping: such as a generic routine to 'move' any shape by modifying its x, y coordinates. 'Move' in scare quotes because that's returning a copy of the shape's data structure with modified coordinates, not poking values into an object/reference. --000000000000aea11005a4f85bc7 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">Making best use of Trex needs a different mindset vs H98 d= atatypes=C2=A0with named fields. (Or indeed vs named-fields data structures= in many languages) There's some examples around in the Trex/Hugs mater= ial, but I've seen nothing showing this:<div><br></div><div>>=C2=A0 = =C2=A0 type WPoint r =C2=A0=3D (x :: Float, y :: Float | r) =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0-- r extended "with Point"<br>>=C2= =A0 =C2=A0 type WRadius r =3D (radius :: Float | r)<br>>=C2=A0 =C2=A0 ty= pe WColour r =3D (colour :: String | r)<br>>=C2=A0 =C2=A0 type ColouredC= ircle =3D Rec( WColour (WRadius (WPoint EmptyRow)))<br></div><div><br></div= ><div>The idea is rather than nest 'Point' as a datatype inside = 9;Circle' 'plug and play' components (such as Point, Radius) to= give a structural type. It's interesting and impressive that the Wxxx= =C2=A0synonyms take an argument of kind row, return a result of kind row. `= EmptyRow` in the last line is a Hugs-defined Row constant that 'plugs&#= 39; the end of the composed components, just as `emptyRec` does for Trex Re= cords.</div><div><br></div><div>I'd love to write that last type as</di= v><div><br></div><div>>=C2=A0 =C2=A0 type MkRec r =3D Rec( r EmptyRow)</= div><div>>=C2=A0 =C2=A0 type ColouredCircle =3D MkRec (WColour . WRadius= . WPoint)</div><div><br></div><div>But of course type synonyms -- even wit= h parameters -- are not functions; can't be composed; can't be pass= ed to higher-order type synonyms.</div><div><br></div><div>Components like = WPoint can be arguments to type-parametric functions for record subtyping: = such as a generic routine to 'move' any shape by modifying its x, y= coordinates. 'Move' in scare quotes because that's returning a= copy of the shape's data structure with modified coordinates, not poki= ng values into an object/reference.</div></div> --000000000000aea11005a4f85bc7-- --===============4470064169658838785== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KSHVncy1Vc2Vy cyBtYWlsaW5nIGxpc3QKSHVncy1Vc2Vyc0BoYXNrZWxsLm9yZwpodHRwOi8vbWFpbC5oYXNrZWxs Lm9yZy9jZ2ktYmluL21haWxtYW4vbGlzdGluZm8vaHVncy11c2Vycwo= --===============4470064169658838785==--