0

We know that pure functions:

  1. Always return the same result for a given input
  2. Produce no side-effects

This leads us to referential transparency - where an expression can be replaced with a value without changing the behaviour of the program.

This tells us that a program can be said to be purely functional if it excludes destructive modifications (updates) of entities in the program's running environment.

This commentator wrote:

grappling with what "pure" in an FP setting actually means, considering application itself is a protocol for mutation (the stack)

My question is: What does 'pure' in functional programming mean if an application mutates the stack?

hawkeye
  • 34,745
  • 30
  • 150
  • 304
  • The same as would mean if the stack didn't exist? The stack is just an implementation detail and shouldn't matter when defining a 'pure' function. – andars Mar 11 '16 at 04:05

1 Answers1

2

The fact that the function mutates the stack is a consequence of the implementation of the machine. It doesn't matter for defining pure, just as the fact that using a 'value' requires mutating a register in the processor core doesn't matter.

If a function doesn't mutate (or depend on) anything external to its own stack frame (e.g. global variables, io, randomness), it can still be viewed as pure.

andars
  • 1,384
  • 7
  • 12
  • Would it be possible to implement a machine that didn't mutate the stack? – hawkeye Mar 11 '16 at 04:13
  • There can certainly be implementations that don't mutate 'the stack' or even have a stack, but I'm fairly sure to have a physical machine that computes something is going to be mutating (I would love to hear that I am wrong though). – andars Mar 11 '16 at 04:17
  • Could you point to such a non-stack mutating machine implementation? – hawkeye Mar 11 '16 at 04:22
  • 1
    Does it need to have subroutines? :) I'm not sure exactly what you are looking for, but you might try [the RS08](https://en.wikipedia.org/wiki/Freescale_RS08) (which doesn't have a system stack / stack pointer), [stackless python](https://en.wikipedia.org/wiki/Stackless_Python) (which pretty much still has a stack, it is just in the heap), and [cps](https://en.wikipedia.org/wiki/Continuation-passing_style) (which may be implemented using either the system stack or the heap). – andars Mar 11 '16 at 04:34