Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two techniques that are used by purely functional programming languages to model side effects:</p> <p>1) A world type that represents external state, where each value of that type is guaranteed by the type system to be used only once.</p> <p>In a language that uses this approach the function <code>print</code> and <code>read</code> might have the types <code>(string, world) -&gt; world</code> and <code>world -&gt; (string, world)</code> respectively.</p> <p>They might be used like this:</p> <pre><code>let main w = let w1 = print ("What's your name?", w) in let (str, w2) = read w1 in let w3 = print ("Your name is " ^ name, w2) in w3 </code></pre> <p>But not like this:</p> <pre><code>let main w = let w1 = print ("What's your name?", w) in let (str, w2) = read w in let w3 = print ("Your name is " ^ name, w2) in w3 </code></pre> <p>(because w is used twice)</p> <p>All built-in functions with side-effects would take and return a world value. Since all functions with side-effects are either built-ins or call other functions with side-effects, this means that all functions with side-effects need to take and return a world.</p> <p>This way it is not possible to call a function with side-effects twice with the same arguments and referential transparency is not violated.</p> <p>2) An IO monad where all operations with side effects have to be executed inside that monad.</p> <p>With this approach all operations with side effects would have type <code>io something</code>. For example <code>print</code> would be a function with type <code>string -&gt; io unit</code> and <code>read</code> would have type <code>io string</code>.</p> <p>The only way to access the value of performing operation would be to use the "monadic bind" operation (called >>= in haskell for example) with the IO operation as one argument and a function describing what to do with the result as the other operand.</p> <p>The example from above would look like this with monadic IO:</p> <pre><code>let main = (print "What's your name?") &gt;&gt;= (lambda () -&gt; read &gt;&gt;= (lambda name -&gt; print ("Your name is " ^ name))) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload