A couple typos:<br><br>instance Monad Replacer1 where<br>-><br>instance Monad (Replacer1 k) where<br><br>instance Monad Replacer2 k where<br>-><br>instance Monad (Replacer2 k) where<br><br>I haven't tested any of this code, so you may have to fix some minor type errors.<br>
<br><div class="gmail_quote">On Mon, Jul 30, 2012 at 10:38 PM, Ryan Ingram <span dir="ltr"><<a href="mailto:ryani.spam@gmail.com" target="_blank">ryani.spam@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
To take this a step further, if what you really want is the syntax sugar for do-notation (and I understand that, I love sweet, sweet syntactical sugar), you are probably implementing a Writer monad over some monoid.<br><br>
Here's two data structures that can encode this type;<br><br>data Replacer1 k a = Replacer1 (k -> Maybe k) a<br>
data Replacer2 k a = Replacer2 [(k,k)] a<br><br>instance Monad Replacer1 where<br> return x = Replacer1 (\_ -> Nothing) x<br> Replacer1 ka a >>= f = result where<br> Replacer1 kb b = f a<br>
result = Replacer1 (\x -> ka x `mplus` kb x) b<br><br>(!>) :: Eq k => k -> k -> Replacer1 k ()<br>x !> y = Replacer1 (\k -> if k == x then Just y else Nothing) ()<br><br>replace1 :: Replacer1 k () -> [k] -> [k] -- look ma, no Eq requirement!<br>
replace1 (Replacer k ()) = map (\x -> fromMaybe x $ k x) -- from Data.Maybe<br><br>table1 :: Replacer1 Char ()<br>table1 = do<br> 'a' !> 'b'<br> 'A' !> 'B'<br><br>test = replace1 table1 "All I want"<br>
<br>-- Exercise: what changes if we switch ka and kb in the result of (>>=)? When does it matter?<br>
<br>-- Exercises for you to implement:<br>instance Monad Replacer2 k where<br>replacer :: Eq k => Replacer2 k -> [k] -> [k]<br>($>) :: k -> k -> Replacer2 k<br><br>-- Exercise: Lets make use of the fact that we're a monad!<br>
--<br>-- What if the operator !> had a different type?<br>-- (!>) :: Eq k => k -> k -> Replacer k Integer<br>-- which returns the count of replacements done.<br>--<br>-- table3 = do<br>-- count <- 'a' !> 'b'<br>
-- when (count > 3) ('A' !> 'B')<br>-- return ()<br>--<br>-- Do any of the data structures I've given work? Why or why not?<br>-- Can you come up with a way to implement this?<span class="HOEnZb"><font color="#888888"><br>
<br> -- ryan</font></span><div class="HOEnZb"><div class="h5"><br>
<br><div class="gmail_quote">On Sat, Jul 28, 2012 at 10:07 AM, Steffen Schuldenzucker <span dir="ltr"><<a href="mailto:sschuldenzucker@uni-bonn.de" target="_blank">sschuldenzucker@uni-bonn.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 07/28/2012 03:35 PM, Thiago Negri wrote:<br>
> [...]<div><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
As Monads are used for sequencing, first thing I did was to define the<br>
following data type:<br>
<br>
data TableDefinition a = Match a a (TableDefinition a) | Restart<br>
</blockquote>
<br></div>
So TableDefinition a is like [(a, a)].<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
[...]<br>
</blockquote><div>
><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
So, to create a replacement table:<br>
<br>
table' :: TableDefinition Char<br>
table' =<br>
Match 'a' 'b'<br>
(Match 'A' 'B'<br>
Restart)<br>
<br>
It look like a Monad (for me), as I can sequence any number of<br>
replacement values:<br>
<br>
table'' :: TableDefinition Char<br>
table'' = Match 'a' 'c'<br>
(Match 'c' 'a'<br>
(Match 'b' 'e'<br>
(Match 'e' 'b'<br>
Restart)))<br>
</blockquote>
<br></div>
Yes, but monads aren't just about sequencing. I like to see a monad as a generalized computation (e.g. nondeterministic, involving IO, involving state etc). Therefore, you should ask yourself if TableDefinition can be seen as some kind of abstract "computation". In particular, can you "execute" a computation and "extract" its result? as in<br>
<br>
do<br>
r <- Match 'a' 'c' Restart<br>
if r == 'y' then Restart else Match 2 3 (Match 3 4 Restart)<br>
<br>
Doesn't immediately make sense to me. In particular think about the different possible result types of a TableDefinition computation.<br>
<br>
If all you want is sequencing, you might be looking for a Monoid instance instead, corresponding to the Monoid instance of [b], where b=(a,a) here.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
[...]<br>
</blockquote><div>
><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'd like to define the same data structure as:<br>
<br>
newTable :: TableDefinition Char<br>
newTable = do<br>
'a' :> 'b'<br>
'A' :> 'B'<br>
<br>
But I can't figure a way to define a Monad instance for that. :(<br>
</blockquote>
<br></div>
The desugaring of the example looks like this:<br>
<br>
('a' :> 'b') >> ('A' :> 'B')<br>
<br>
Only (>>) is used, but not (>>=) (i.e. results are always discarded). If this is the only case that makes sense, you're probably looking for a Monoid instead (see above)<span><font color="#888888"><br>
<br>
-- Steffen</font></span><div><div><br>
<br>
______________________________<u></u>_________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br>
</div></div></blockquote></div><br>