Your server as a function [pdf]
monkey.org
monkey.org
For example, on a very high level, an HTTP request signal function might look like this:
request :: Signal Request -> Signal Response
And a database request function might roughly look like this: requestDB :: Signal Query -> Signal [Record]
Having the basic building blocks, one could imagine building a function to e.g. extract all users: extractUsers :: Signal Request -> Signal Response
extractUsers = request . createUserQuery . requestDB . showUsers
Similar signal functions could theoretically be constructed for routes, system functions, static files and so on. And since all signal functions are pure, composing those and creating ever more powerful abstractions without having to worry about system state and environment context may be a nicer way of doing server programming.[1] Functional Reactive Programming - http://en.wikipedia.org/wiki/Functional_reactive_programming
FRP signals (specifically, behaviors, the dual of events) are most definitely not pure. Yes, the functions which define them are. But the entire point of FRP is to allow one to manage state in a functional context. (Otherwise you could not manipulate a database -- an inherently stateful construct! -- as you do in your example.) You definitely still have to worry about state -- you're just forced to structure it a little better now.
FRP just deals with time explicitly, i.e. a fold over time over a signal is just as pure as a normal fold over a list (even if the latter is ultimately translated into a stateful loop).
A system if stateless by definition iff its observable behavior is independent of its input history over time. (This definition applies to disciplines other than computer science, BTW.) That is not true of any FRP system which generates behavior signals from event signals (as a database does), unless you want to execute said system over fixed data (your "list fold" example), therefore making time irrelevant, making the system non-interactive, and making the example uninteresting to the problem FRP is trying to solve.
Does a FRP description of a system eschew global mutable variables and non-local effects? Yes.
Does a FRP description of a system force state to be handled explicitly? Yes.
Do FRP languages encourage decoupling of state into small pieces? Yes.
Are FRP programs described by referentially transparent pieces? Yes.
Are useful FRP systems as a whole not stateful? Absolutely not.
In the real world, a database is of course stateful, but then again so are CPU registers. There's no fundamental difference.
sqrt :: Double -> Double
is pure. Still, when evaluated on a CPU somewhere a register or a memory location gets mutated, i.e. it produces a side effect. In that sense nothing would be pure.So if sqrt is pure, why is
addUser :: User -> DB -> DB
impure?The system formed by that function being repeatedly applied to the value of DB over time is stateful. Statefulness is a property of systems.
Also, forget about CPU-level stuff. That's a straw man and is irrelevant to the discussion; everyone in this thread is talking about the holistic (i.e. system-level) definition of state, as that is what FRP manages.
Beside, the mathematical mechanism by which a stateful system (the CPU) can be considered to be holistically stateless (a purely functional program) is the exact same mechanism by which a stateless system (a functional description of a state machine) can be considered to be holistically stateful -- that is by the definition I gave above and will repeat below.
Again: the definition of a stateful system is one whose observable behavior is dependent on historical inputs. You are conflating this well-accepted notion of state with the independent concepts of referential transparency, (non)locality, and (non)determinism.
You realize that's true of, like, every digital computing system ever, right?