https://wiki.haskell.org/index.php?title=Idiom_brackets&feed=atom&action=history
Idiom brackets - Revision history
2024-03-28T12:06:33Z
Revision history for this page on the wiki
MediaWiki 1.35.5
https://wiki.haskell.org/index.php?title=Idiom_brackets&diff=62129&oldid=prev
Chris-martin: also link to Applicative Programming with Effects
2017-09-19T18:50:28Z
<p>also link to Applicative Programming with Effects</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 18:50, 19 September 2017</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:Idioms]]</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:Idioms]]</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>In July 2007, Conor McBride described `idiom brackets'<del class="diffchange diffchange-inline">: </del></div></td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>In July 2007, Conor McBride described `idiom brackets'<ins class="diffchange diffchange-inline">.</ins></div></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>See also the 2008 paper [http://www.staff.city.ac.uk/~ross/papers/Applicative.html Applicative programming with effects] by Conor Mcbride and Ross Paterson.</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Motivation ==</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Motivation ==</div></td>
</tr>
</table>
Chris-martin
https://wiki.haskell.org/index.php?title=Idiom_brackets&diff=14184&oldid=prev
DonStewart: neat `idiom brackets' example
2007-07-11T12:26:40Z
<p>neat `idiom brackets' example</p>
<p><b>New page</b></p><div>[[Category:Idioms]]<br />
<br />
In July 2007, Conor McBride described `idiom brackets': <br />
<br />
== Motivation ==<br />
<br />
Using Control.Applicative you could write:<br />
<br />
<haskell><br />
f <$> x <*> y<br />
</haskell><br />
<br />
Since f is not a pure function, it's f :: x -> y -> m c. The correct form would be<br />
<br />
<haskell><br />
join $ f <$> x <*> y<br />
</haskell><br />
<br />
But maybe some type-class hackery can be used to eliminate the join.<br />
<br />
== Idiom brackets ==<br />
<br />
Type class hackery to eliminate the 'join':<br />
<br />
<haskell><br />
class Applicative i => Idiomatic i f g | g -> f i where<br />
idiomatic :: i f -> g<br />
<br />
iI :: Idiomatic i f g => f -> g<br />
iI = idiomatic . pure<br />
<br />
data Ii = Ii<br />
<br />
instance Applicative i => Idiomatic i x (Ii -> i x) where<br />
idiomatic xi Ii = xi<br />
<br />
instance Idiomatic i f g => Idiomatic i (s -> f) (i s -> g) where<br />
idiomatic sfi si = idiomatic (sfi <*> si)<br />
</haskell><br />
<br />
So that<br />
<br />
<haskell><br />
iI f x y Ii = f <$> x <*> y<br />
</haskell><br />
<br />
Now add<br />
<br />
<haskell><br />
data Ji = Ji<br />
<br />
instance (Monad i, Applicative i) => Idiomatic i (i x) (Ji -> i x) where<br />
idiomatic xii Ji = join xii<br />
</haskell><br />
<br />
and you've got<br />
<br />
<haskell><br />
iI f x y Ji = join $ f <$> x <*> y<br />
</haskell><br />
<br />
or, more flexibly,<br />
<br />
<haskell><br />
data J = J<br />
<br />
instance (Monad i, Idiomatic i f g) => Idiomatic i (i f) (J -> g) where<br />
idiomatic fii J = idiomatic (join fii)<br />
</haskell><br />
<br />
so you can insert joins wherever you like, thus:<br />
<br />
<haskell><br />
iI f x y J z Ii = join (f <$> x <*> y) <*> z<br />
= do {x' <- x; y' <- y; f' <- f x y; z' <- z; return (f' z')}<br />
</haskell><br />
<br />
Of course, the implementation is an ugly hack, made uglier still by ASCII.<br />
Worse, for reasons I have never entirely understood, the type-class hackery<br />
doesn't allow these brackets to nest as they should. Even so, I find them a<br />
considerable convenience. I always assumed that was down to peculiarity on my<br />
part.<br />
<br />
I thought I'd present it as a curio illustrating part of the design space, but<br />
I don't imagine there's that big a market for an "idiom brackets done properly"<br />
proposal.</div>
DonStewart