3 ms·
> If you have imperative state then toy uses of the state monad might be overkill What's imperative state? These are two very different concepts. Both functio
by zak_mc_kracken 13y ago
> If you have imperative state then toy uses of the state monad might be overkill
What's imperative state? These are two very different concepts.
Both functional and imperative code can be manipulating state, and that state can be either mutable or immutable.
Haskell is often considered to be both a functional and an imperative language (see how linking do statements works).
I'm guessing you meant to say "mutable state" above?
- Chattered 13y agoBy "imperative state" I mean state for which the ordering of state updates is set by the operational semantics of your language, or the execution model, or the way it ends up implemented on an abstract machine. In the case of Haskell, I mean the ordering of state updates you'd get if you used unsafePerformIO whenever you do your get, put and modify, which is generally going to be unpredictable. If I'm working in code where the operational semantics is not fully defined or difficult to reason about, I might find myself reaching for the state monad, regardless of whether my language supports imperative state (such as with Ocaml). I brought up back-tracking parsers as an example.
- nbouscal 13y agoI don't know who you've been talking to that considers Haskell to be an imperative language, but that's nonsense. Haskell is about as far from multi-paradigm as you can get. Do-notation is nothing but syntactic sugar for regular monadic expressions, which are pure functional in every way.
- tel 13y agoBecause monads allow you to basically embed "Algol" into Haskell. It's super imperative, despite it being interpreted as a pure function. I'd actually go so far as to say Haskell is quite multi-paradigm but only the FP side comes close enough to Haskell's "purity" as to make a community translation. I'd be quite willing to say that Haskell has a fantastically correct if slightly difficult to use object system. It just doesn't quack anything at all like Smalltalk or Java's.
- nbouscal 13y agoI strongly disagree, but am sure that that disagreement comes only from us defining terms differently. If you define the term "imperative" widely enough to include Haskell, what language does it not include?
- tel 13y agoI wouldn't claim it excludes many languages. Prolog comes to mind. Imperative, to me, means a language structured over the execution of statements in order. I think that's a fairly common definition.
- nbouscal 13y agoThat still sounds a little weak, but even under that definition, how does Haskell qualify? Do-notation may make things look like statements, but they are still expressions!
- tel 13y agoThey are statements in an embedded language modeled by the monad. The distinction isn't great. Monad allows the implementer to capture linear action if they like. That makes them at least powerful enough to embed Algol.
- zak_mc_kracken 13y agoMerely quoting Simon Peyton Jones and Philip Wadler in their paper from 1993 "Imperative Functional Programming" [1]. Chaining monadic binds with a `do` is the textbook example of imperative programming (it's actually the very reason why the keyword 'do' was chosen for syntactic sugar, because it captures perfectly the imperative aspect: "do this and in this order"). You seem to think it's a bad word but I really don't understand that. Haskell is both functional and imperative, there's really nothing wrong about it. [1] http://research.microsoft.com/en-us/um/people/simonpj/Papers/imperative.ps.Z http://research.microsoft.com/en-us/um/people/simonpj/Papers...
- nbouscal 13y agoHuh. We clearly mean different things by "imperative programming". To me, sequential computation is a necessary but really-far-from-sufficient aspect of imperative programming. A monadic expression is still an expression, not a statement. It is still referentially transparent. I don't know how you can look at a referentially transparent expression and call it imperative other than by being very liberal with your definitions.