@@ -5,12 +5,10 @@ Simple Effects
55==============
66
77So far we have seen how to write programs with locally mutable state
8−using the ``STATE`` effect. To recap, we have the definitions in
9−Listing :ref:`eff-state` in a module ``Effect.State``.
8+using the ``STATE`` effect. To recap, we have the definitions below
9+in a module ``Effect.State``
1010
11−.. _eff-state:
1211.. code-block:: idris
13− :caption: State Effect
1412
1513 module Effect.State
1614
@@ -45,16 +43,14 @@ section, are summarised in Appendix :ref:`sect-appendix`.
4543Console I/O
4644-----------
4745
48−Console I/O (Listing :ref:`eff-stdio`) is supported with the ``STDIO``
46+Console I/O is supported with the ``STDIO``
4947effect, which allows reading and writing characters and strings to and
5048from standard input and standard output. Notice that there is a
5149constraint here on the computation context ``m``, because it only
5250makes sense to support console I/O operations in a context where we
53−can perform (or at the very least simulate) console I/O.
51+can perform (or at the very least simulate) console I/O:
5452
55−.. _eff-stdio:
5653.. code-block:: idris
57− :caption: Console I/0 Effect
5854
5955 module Effect.StdIO
6056
@@ -138,14 +134,13 @@ than using a default value with ``run``) we can run the
138134Exceptions
139135----------
140136
141−Listing :ref:`eff-exception` gives the definition of the ``EXCEPTION``
142−effect, declared in module ``Effect.Exception``. This allows programs
137+The ``EXCEPTION``
138+effect is declared in module ``Effect.Exception``. This allows programs
143139to exit immediately with an error, or errors to be handled more
144−generally.
140+generally:
145141
146142.. _eff-exception:
147143.. code-block:: idris
148− :caption: Exception Effect
149144
150145 module Effect.Exception
151146
@@ -240,11 +235,9 @@ Random Numbers
240235--------------
241236
242237Random number generation is also implemented by the library, in module
243−``Effect.Random``. Listing :ref:`eff-random` gives its definition.
238+``Effect.Random``:
244239
245−.. _eff-random:
246240.. code-block:: idris
247− :caption: Random Number Effect
248241
249242 module Effect.Random
250243
@@ -282,12 +275,30 @@ correct:
282275 guess : Int -> { [STDIO] } Eff ()
283276
284277For reference, the code for ``guess`` is given in Listing
285−:ref:`eff-game`. Note that we use ``parseNumber`` as defined
286−previously to read user input, but we don’t need to list the
287−``EXCEPTION`` effect because we use a nested ``run`` to invoke
288−``parseNumber``, independently of the calling effectful program.
278+:ref:`eff-game`.
289279
290−To invoke these, we pick a random number within the range 0–100,
280+.. _eff-game:
281+.. code-block:: idris
282+
283+ guess : Int -> { [STDIO] } Eff ()
284+ guess target
285+ = do putStr "Guess: "
286+ case run {m=Maybe} (parseNumber 100 (trim !getStr)) of
287+ Nothing => do putStrLn "Invalid input"
288+ guess target
289+ Just (v ** _) =>
290+ case compare v target of
291+ LT => do putStrLn "Too low"
292+ guess target
293+ EQ => putStrLn "You win!"
294+ GT => do putStrLn "Too high"
295+ guess target
296+
297+Note that we use ``parseNumber`` as defined previously to read user input, but
298+we don’t need to list the ``EXCEPTION`` effect because we use a nested ``run``
299+to invoke ``parseNumber``, independently of the calling effectful program.
300+
301+To invoke this, we pick a random number within the range 0–100,
291302having set up the random number generator with a seed, then run
292303``guess``:
293304
@@ -305,35 +316,16 @@ predictable game, some better source of randomness would be required,
305316for example taking an initial seed from the system time. To see how to
306317do this, see the ``SYSTEM`` effect described in :ref:`sect-appendix`.
307318
308−.. _eff-game:
309−.. code-block:: idris
310− :caption: Guessing Game
311−
312− guess : Int -> { [STDIO] } Eff ()
313− guess target
314− = do putStr "Guess: "
315− case run {m=Maybe} (parseNumber 100 (trim !getStr)) of
316− Nothing => do putStrLn "Invalid input"
317− guess target
318− Just (v ** _) =>
319− case compare v target of
320− LT => do putStrLn "Too low"
321− guess target
322− EQ => putStrLn "You win!"
323− GT => do putStrLn "Too high"
324− guess target
325319
326320Non-determinism
327321---------------
328322
329−Listing :ref:`eff-select` gives the definition of the non-determinism
323+The listing below gives the definition of the non-determinism
330324effect, which allows a program to choose a value non-deterministically
331325from a list of possibilities in such a way that the entire computation
332−succeeds.
326+succeeds:
333327
334−.. _eff-select:
335328.. code-block:: idris
336− :caption: Non-determinism Effect
337329
338330 import Effects
339331 import Effect.Select
@@ -444,7 +436,6 @@ does require that the lengths are checked and any discrepancy is dealt
444436with gracefully.
445437
446438.. code-block:: idris
447− :caption: Reading a vector from the console.
448439
449440 read_vec : { [STDIO] } Eff (p ** Vect p Int)
450441 read_vec = do putStr "Number (-1 when done): "