On Wed, Jun 24, 2009 at 7:56 PM, Hector Guilarte <span dir="ltr">&lt;<a href="mailto:hectorg87@gmail.com">hectorg87@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Thanks for answering so fast.<br><br>Yes, GCL == Guarded Command Language... It is for an assigment I have in my Languages and Machines Course.<br><br>About the nicer/Haskellier solution you proposed: If there is a way of printing right in the moment the Interpreter finds the show instruction then I don&#39;t think the teacher is gonna like this soluttion (I would bet on that) so, can you (or somebody) explain a little bit better how would the ugly solution be? As I said earlier, I&#39;m no expert in monads, actually the truth is that I know almost nothing about monads, so please, explain it to me as if you are explaining it to a Monads newbie...</blockquote>
<div><br>The ugly solution is essentially to write it as an imperative program.  &quot;IO monad&quot; is a fancy word for &quot;imperative programming&quot;.<br><br>I&#39;m beginning to think the best way to teach monads is not to (until the student is essentially begging for them without knowing it), and let them be grown into as the pattern is seen.  So, let&#39;s not use them.<br>
<br>Let&#39;s give the language a semantics of the form Tabla -&gt; ([String], Tabla).  That means that every expression has the meaning of a function which takes a symbol table, and outputs a series of lines and a new symbol table.  This will be the range of your evalInstruccion.<br>
<br>I&#39;ll do the ShowY case and a &quot;Sequence&quot; case for sequencing together multiple instructions, which is important.  The rest should be straightforward from there.<br><br><span style="font-family: courier new,monospace;">evalInstruccion :: Instruccion -&gt; Tabla -&gt; ([String], Tabla)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">evalInstruccion (ShowY showY) tabla = ([evalShow showY tabla], tabla)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">evalInstruccion (Sequence instr1 instr2) tabla =</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    let (out1, tabla&#39;)  = evalInstruccion instr1 tabla</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        (out2, tabla&#39;&#39;) = evalInstruccion instr2 tabla&#39;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    in (out1 ++ out2, tabla&#39;&#39;)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">...</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">evalShow :: ShowY -&gt; Tabla -&gt; String</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">evalShow = ...</span><br style="font-family: courier new,monospace;">
<br>This pattern can be used for more than lists of strings; it can be used for any &quot;monoid&quot;.  If I were to follow the pattern of your code, the monoid would be IO (), and the operation (instead of (++)) would be (&gt;&gt;).  But there would still be no unsafePerformIO, the semantics of the langauge would be Tabla -&gt; (IO (), Tabla), and we would put build one big IO () to return at the end from the output of the subexpressions.<br>
<br>Does this make sense?<br><br>Luke<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>

<br>Also, can somebody explain me how would it be using the Writer Monad? remember is for a Monads newbie...<br><br>Thanks a lot!<br><br>Hector Guilarte<br><br><br><div class="gmail_quote">On Thu, Jun 25, 2009 at 6:04 PM, Jochem Berndsen <span dir="ltr">&lt;<a href="mailto:jochem@functor.nl" target="_blank">jochem@functor.nl</a>&gt;</span> wrote:<br>



<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div>Hector Guilarte wrote:<br>
&gt; I made a GCL compiler using Alex and Happy and now I&#39;m making the<br>
&gt; interpreter to that program. Here&#39;s the deal:<br>
&gt; <br></div></blockquote><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><br>
&gt; First of all, I&#39;m no expert in the usage of monads. Now:<br>
&gt;<br>
&gt; Whenever a &quot;show&quot; instruction is found in any GCL program while the<br>
&gt; interpretation is being done it is supposed to print on the stdout the<br>
&gt; string or the aritmetic expresion it was called with, so I guessed I need to<br>
&gt; run an IO operation and continue the interpretation of my program. I managed<br>
&gt; to do this using unsafePerformIO and `seq` like is shown below. My question<br>
&gt; is: Is it safe to use it this way? So far it is working great, but I need to<br>
&gt; be sure I&#39;m using it in a &quot;safe&quot; way. Like I said, I&#39;m no expert in monads<br>
&gt; and the System.IO.Unsafe documentation says:<br>
&gt;<br>
&gt; &quot;<br>
&gt; *unsafePerformIO* ::<br>
</div>&gt; IO&lt;<a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO" target="_blank">http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO</a>&gt;a<br>
<div>&gt; -&gt; a<br>
&gt; This is the &quot;back door&quot; into the<br>
</div>&gt; IO&lt;<a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO" target="_blank">http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO</a>&gt;monad,<br>




&gt; allowing<br>
&gt; IO&lt;<a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO" target="_blank">http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO</a>&gt;computation<br>




<div>&gt; to be performed at any time. For this to be safe, the<br>
</div>&gt; IO&lt;<a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO" target="_blank">http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO</a>&gt;computation<br>




<div>&gt; should be free of side effects and independent of its<br>
&gt; environment.<br>
&gt; &quot;<br>
&gt;<br>
&gt; I don&#39;t know if the IO computation I&#39;m doing is free of side effects and<br>
&gt; independent of its enviroment :s. (is just hPutStr stdout ....)<br>
<br>
</div>Well, writing to the standard output is certainly a side effect. (This<br>
does not mean that you cannot use unsafePerformIO. The compiler,<br>
however, may assume that any value is free from side effects. This means<br>
that you could get, in theory, less or more output from your program<br>
than you want. In this sense it is not &quot;safe&quot;.)<br>
<div><br>
&gt; Also I&#39;ve read something about my code not being executed for sure or<br>
&gt; something like that. Can somebody check the code and tell me if I&#39;m &quot;safe&quot;<br>
&gt; with it?<br>
<br>
</div>It&#39;s &quot;safe&quot; in the sense that it probably won&#39;t blow up your computer.<br>
It may also work. On the other hand, I would not recommend using<br>
unsafePerformIO in this way.<br>
<br>
I see two possibilities for resolving this issue:<br>
* (ugly) run your GCL (Guarded Command Language?) interpreter in the IO<br>
monad, and using &quot;print&quot;/&quot;putStr&quot;/... whenever you encounter a &#39;show&#39;<br>
statement in the GCL program.<br>
* (nicer/Haskellier) adapt your interpreter such that it returns a list<br>
of Strings to output. You have then a purely functional interpreter, and<br>
in the main function of your program you can print this list. This will<br>
be lazily evaluated as the GCL program runs. You now have a very nice<br>
separation of clean, pure code, and impure code in the IO monad (your<br>
&quot;main&quot; function, which can be pretty small in your case). To avoid<br>
boilerplate, you can use the Writer monad, for example, but others may<br>
have better suggestions.<br>
<br>
Kind regards,<br>
<font color="#888888"><br>
--<br>
Jochem Berndsen | <a href="mailto:jochem@functor.nl" target="_blank">jochem@functor.nl</a><br>
GPG: 0xE6FABFAB<br>
</font></blockquote></div><br>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br>