2

I am currently trying to figure out in which order Haskell tries to applicate certain functions. If I've got a function call like:

exec:: Stm -> State -> State
exec (Seq s1 s2) = \s -> exec s2 (exec s1 s) 

This is part of a bigger program executing a program where each code fragment could possibly alter the structure of the environment, which keeps track of the current variable assignment. In that case how can I be sure that haskell not accidentally executes the wrong part first? Im asking because if I do this exact line in continuation style, it would look like this:

type Cont = State -> State
exec:: Stm -> Cont -> Cont
exec (Seq s1 s2) = \c -> exec s1 (exec s2 c) 

If haskell executes the inner parts in the brackets first it could probably change the meaning of the progam because it messes up the order. I would really appreciate some help because I cant seem to figure this out by myself.

Sushiman
  • 55
  • 6
  • 7
    No. Order of execution is not relevant in Haskell (unless you go crazy and start `unsafePerformIO`ing all around). – MigMit Jan 22 '14 at 18:50
  • Well, but if for example s1 assigns a variable r to 1 and saves it in State and s2 increments r thus updating the variable value in State, the final interpreting of the program requires some kind of order, doesnt it? Otherwise it wouldnt be clear whether my result for r is 1 or 2 (if r was initialized as 0). – Sushiman Jan 22 '14 at 18:59
  • 2
    The state that `exec s2` sees is the one being computed by `exec s1 s`. This is specified by your input expression. The exact reduction order does not matter for the result (as long as `exec s1 s` terminates, at least). So in your example, if some component of the `State` is set to `1` by `s1` and incremented by `s2`, then the result will be `2`. – kosmikus Jan 22 '14 at 19:17
  • 8
    There is no such thing as "updating the variable". You have State `s`, you have State `exec s1 s`, you have State `exec s2 (exec s1 s)`. All of them coexist, you can use them together any time you want. They are DIFFERENT variables. – MigMit Jan 22 '14 at 19:22
  • 7
    @Sushiman I highly suggest you read an introductory tutorial to Haskell. [Learn You a Haskell for Great Good](http://learnyouahaskell.com/) is often recommended. Right now you're trying to use a mental model for the meaning of those statements that is very inappropriate for understanding the actual behavior of Haskell. In truth, what happens is much simpler than you describe, but it takes some practice and exposure before you can begin to forget about all the unneeded notions that you're using now. – J. Abrahamson Jan 22 '14 at 20:27
  • 1
    This question appears to be off-topic because it is based on a misconception about the language. – Joshua Taylor Jun 13 '14 at 20:15
  • @Sushiman Try some curry. – Aadit M Shah May 08 '15 at 11:11

0 Answers0