Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are side effects everything that cannot be found in a pure function?

Is it safe to say that the following dichotomy holds:

Each given function is

  • either pure
  • or has side effects

If so, side effects (of a function) are anything that can't be found in a pure function.

like image 583
Trident D'Gao Avatar asked May 01 '16 16:05

Trident D'Gao


People also ask

Do pure functions have side effects?

A pure function does not have side-effects and its result does not depend on anything other than its inputs. A pure function guarantees that for a given input it will produce the same output no matter how many times it is called.

What is a pure function or a function with no side effects?

A pure function is a function which: Given the same input, always returns the same output. Produces no side effects.

What are functions with side effects?

Side Effects & Pure Functions A side effect is when a function relies on, or modifies, something outside its parameters to do something. For example, a function which reads or writes from a variable outside its own arguments, a database, a file, or the console can be described as having side effects.

What is not a pure function?

An impure function is a function that contains one or more side effects. A pure function is a function without any side effects.


2 Answers

This very much depends on the definitions that you choose. It is definitely fair to say that a function is pure or impure. A pure function always returns the same result and does not modify the environment. An impure function can return different results when it is executed repeatedly (which can be caused by doing something to the environment).

Are all impurities side-effects? I would not say so - a function can depend on something in the environment in which it executes. This could be reading some configuration, GPS location or reading data from the internet. These are not really "side-effects" because it does not do anything to the world.

I think there are two different kinds of impurities:

  • Output impurity is when a function does something to the world. In Haskell, this is modelled using monads - an impure function a -> b is actually a function a -> M b where M captures the other things that it does to the world.

  • Input impurity is when a function requires something from the environment. An impure function a -> b can be modelled as a function C a -> b where the type C captures other things from the environment that the function may access.

Monads and output impurities are certainly better known, but I think input impurities are equally important. I wrote my PhD thesis about input impurities which I call coeffects, so I this might be a biased answer though.

like image 132
Tomas Petricek Avatar answered Sep 29 '22 16:09

Tomas Petricek


For a function to be pure it must:

  1. not be affected by side-effects (i.e. always return same value for same arguments)
  2. not cause side-effects

But, you see, this defines functional purity with the property or absence of side-effects. You are trying to apply backwards logic to deduct the definition of side-effects using pure functions, which logically should work, but practically the definition of a side-effect has nothing to do with functional purity.

like image 24
artemonster Avatar answered Sep 29 '22 14:09

artemonster