Lab4 call by value

170 views
Skip to first unread message

Alex Evert

unread,
Mar 7, 2014, 2:38:34 AM3/7/14
to proglang-c...@googlegroups.com
For me GHC seems very set on using the call by need paradigm. To make it actually loop with
the fifth testprogram I now have code like

case x == nonsense of
  True -> some code
  otherwise -> same code

What's the standard way of forcing evaluation in Haskell?
$!! seems somewhat problematic in this case.

Kind regards
Alex

Alex Evert

unread,
Mar 10, 2014, 9:51:12 AM3/10/14
to proglang-c...@googlegroups.com
Saw that Patrik had the same problem. 
One solution is to use the pragma

{-# LANGUAGE BangPatterns #-}

and put a bang  (exclamation mark) before the expression that needs to be strict.

Guess I could have also changed the return type of my eval function to Integer, but then it would be nontrivial
for me to see exactly what some expressions evaluated to.

Kind regards
Alex

Aarne Ranta

unread,
Mar 10, 2014, 10:27:35 AM3/10/14
to proglang-c...@googlegroups.com
Sorry for the delayed reply.

But a clean way to force evaluation in Haskell is to use a monad as return type. For instance, my eval function uses the error monad,

  eval :: Env -> Exp -> Err Val

At every step, the monad needs to get either Ok Val or Bad String to know how to continue, which forces the evaluation of Exp.

Regards

  Aarne.

Alex Evert

unread,
Mar 11, 2014, 5:58:49 PM3/11/14
to proglang-c...@googlegroups.com
Thanks! 
Your solution seems much more in line with the previous labs and course in general.
Would you consider it an rejectable offence not to use monads and
a Val type?

Kind regards,
Alex

Emil Bryngelsson

unread,
Mar 12, 2014, 1:50:48 PM3/12/14
to proglang-c...@googlegroups.com
In the test program 2, we have trouble forcing the evaluation to make it endlessly loop with call by value.

The only way we found to make it loop is to use trace (show vars) $  in the beginning of the eval function, this explicitly evaluates (and prints) the environment.

Rewriting the eval function to use the Err monad didn't help, the program still passes without an endless loop.

Cases as suggested above didn't help, neither did the pragma {-# LANGUAGE BangPatterns #-} ,

The test suite passes with our default implementation which returns 5 as it wants, but we can't force it to "crash"



Den måndagen den 10:e mars 2014 kl. 15:27:35 UTC+1 skrev Aarne Ranta:
Reply all
Reply to author
Forward
0 new messages