doc/oleg/reified-exceptions.txt
author Tony Garnock-Jones <tonygarnockjones@gmail.com>
Wed, 16 Jan 2019 17:15:58 +0000
changeset 438 1fe179d53161
parent 1 e3e8313b3acc
permissions -rw-r--r--
Add missing primitive implementation for the plain interpreter.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
     1
From posting-system@google.com Mon Jun 10 17:16:34 2002
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
     2
Date: Mon, 10 Jun 2002 13:16:23 -0700
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
     3
From: oleg@pobox.com (oleg@pobox.com)
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
     4
Newsgroups: comp.lang.scheme
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
     5
Subject: Re: multilambda implementation: recreational macrology challenge!
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
     6
References: <3CFE82D7.1355074F@sonic.net> <87lm9tgqh6.fsf@radish.petrofsky.org> <3CFEEA0F.6040009@sonic.net>
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
     7
Message-ID: <7eb8ac3e.0206101216.1eb26713@posting.google.com>
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
     8
Status: OR
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
     9
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    10
This article demonstrates that a lexical-scoped exception handling is
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    11
possible to some extent. We shall also show how to add 1 to the
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    12
_value_ of (car '()) and store the result, without terminating the
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    13
computation.
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    14
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    15
The technique of this article may be considered R5RS. The modality in
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    16
the previous sentence comes entirely from R5RS. Section 6.4 of R5RS
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    17
specifically says "Calling force on an object that is not a promise
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    18
may simply return the object; Some implementations may implement
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    19
'implicit forcing, where the value of a promise is forced by
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    20
primitive procedures like cdr and +:". Such implementations do
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    21
exist. For example, in Gambit-3.0 a force applied to a value that is
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    22
not a promise simply returns that value. Gambit-3.0 _compiler_ with an
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    23
undocumented flag can implicitly force promises. In this article, we
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    24
will use Bigloo 2.4b system, which permits neither of these
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    25
extensions. Therefore, we have to appropriately generalize force
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    26
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    27
     (set! force
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    28
	   (let ((force force))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    29
	     (lambda (x) (if (procedure? x) (force x) x))))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    30
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    31
and explicitly forcify +, -, car, cdr (but not cons or vector)
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    32
procedures. We also assume SRFI-12 (which has been implemented for
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    33
Bigloo and Gambit: http://pobox.com/~oleg/ftp/Scheme/util.html#srfi-12
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    34
Chicken also supports SRFI-12).
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    35
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    36
   (define (force-catch proc . args)
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    37
     (handle-exceptions exc
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    38
	(begin (cerr "Exception: " exc nl) (delay (abort exc)))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    39
	(apply proc (map force args))))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    40
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    41
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    42
   (define (if-error x thunk)
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    43
     (handle-exceptions exc (thunk) (force x)))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    44
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    45
   (set! + (let ((+ +)) (lambda args (apply force-catch (cons + args)))))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    46
   (set! - (let ((- -)) (lambda args (apply force-catch (cons - args)))))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    47
   (set! * (let ((* *)) (lambda args (apply force-catch (cons * args)))))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    48
   (set! / (let ((/ /)) (lambda args (apply force-catch (cons / args)))))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    49
   (set! car (let ((car car)) 
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    50
	(lambda args (apply force-catch (cons car args)))))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    51
   (set! cdr (let ((cdr cdr)) (lambda args (apply force-catch
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    52
	(cons cdr args)))))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    53
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    54
With the preliminaries out of the way, let the fun begin.
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    55
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    56
     (display "Fun Begins") (newline)
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    57
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    58
     (display (+ 4 (/ 1 1))) (newline)
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    59
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    60
     (display 
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    61
      (if-error (+ 4 (/ 1 0))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    62
		(lambda () "error")))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    63
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    64
We see "5" and "error" printed. One can say, what is the big deal? We
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    65
generate an exception and catch it at the appropriate moment. Well,
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    66
let's enter the following definition:
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    67
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    68
     (define wv (vector 1 (/ 1 0) (car '())))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    69
     (display "OK\n")
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    70
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    71
The computation completed without any error! We can go further, and
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    72
add 1 to each element of the above vector:
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    73
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    74
     (define wl (map (lambda (x) (+ 1 x)) (vector->list wv)))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    75
     (display "OK\n")
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    76
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    77
And it is still OK. We can even print what we've got:
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    78
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    79
     (display (if-error (car wl) (lambda () "error")))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    80
     (newline)
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    81
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    82
which prints the expected result. It's only
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    83
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    84
     (display (if-error (cadr wl) (lambda () "error")))
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    85
     (newline)
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    86
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    87
that prints an error. The examples tested on Bigloo 2.4b and Gambit 3.0.
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    88
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    89
Thus we can have our exceptions, and deal with them lexically, too.
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    90
e3e8313b3acc Interesting documents from Oleg about monads and exceptions
Tony Garnock-Jones <tonyg@lshift.net>
parents:
diff changeset
    91