From: ········@gmail.com
Subject: offering restarts within macro-expansions / exceptional situations 	inside the compiler
Date: 
Message-ID: <9f8f1b15-1e4f-4ef1-98da-f11d66d4acd4@e10g2000prf.googlegroups.com>
so I'm compiling lisp functions from templates (a template being a
file with some crap in it, such as html or xml, in addition to some
arbitrary lisp code). I want to do compile-time checking on certain
"special functions" used only within these templates because it's
important there are no errors with these functions at runtime. I want
the errors to be correctable so that the sys admin can get his stuff
up and running by supplying sensible/default alternatives to buggy
"special function" calls without having to delve into the source code
of the bad templates

it is important to note that in this situation the arguments to the
"special functions" do not need to be evaluated

suppose I have a "special function" called NUMBER-FORM, which takes a
number and prints "The number form is XX"

I want to make sure at compile time that NUMBER-FORM is always called
with a number, and if it's called with something other than a number I
want sys admin to have the option to substitute a number for whatever
crap it was called with

the most obvious way to do it (IMHO) is implement this make a macro
that does this check at macroexpand time that offers the appropriate
restart and throws an error

(defmacro number-form (x)
  (if (numberp x)
      `(.number-form ,x)
      (restart-case (error "NUMBER-FORM called with ~S, when it really
wants a number" x)
	(use-different-form (new-form)
	  :report "supply a number to use instead"
	  :interactive (lambda ()
			 (format t "~%enter a number to use instead of ~S: " x)
			 (list (read)))
	  `(number-form ,new-form)))))

(defun .number-form (x)
  (format t "~%The number form is ~S" x))

this seems to work fine

CL-USER> (number-form 5)

The number form is 5
NIL
CL-USER> (number-form not-a-number)

enter a number to use instead of NOT-A-NUMBER: 3

The number form is 3
NIL
CL-USER>

except: when the MACROEXPAND is invoked by COMPILE the debugger is
never entered (note: I'm using SBCL 1.0.15)

CL-USER> (compile nil
		  (lambda () (number-form not-a-number)))
; in: LAMBDA NIL
;     (NUMBER-FORM NOT-A-NUMBER)
;
; caught ERROR:
;   (in macroexpansion of (NUMBER-FORM NOT-A-NUMBER))
;   (hint: For more precise location, try *BREAK-ON-SIGNALS*.)
;   NUMBER-FORM called with NOT-A-NUMBER, when it really wants a
number
;
; compilation unit finished
;   caught 1 ERROR condition
#<FUNCTION (LAMBDA ()) {129A077D}>
NIL
NIL
CL-USER>

setting *BREAK-ON-SIGNALS* to ERROR enters the debugger, but the
restart is not offered because this is a special invocation of the
debugger that behaves how it wants to

using a compiler macro instead of a macro behaves the same

(defun number-form1 (x)
  (format t "~%The number form is ~S" x))

(define-compiler-macro number-form1 (&whole form x)
  (if (numberp x)
      form
      (restart-case (error "NUMBER-FORM called with ~S, when it really
wants a number" x)
	(use-different-form (new-form)
	  :report "supply a number to use instead"
	  :interactive (lambda ()
			 (format t "~%enter a number to use instead of ~S: " x)
			 (list (read)))
	  `(number-form1 ,new-form)))))

the hyperspec 3.2.5 says

[quote]Both compile and compile-file are permitted (but not required)
to establish a handler for conditions of type error. For example, they
might signal a warning, and restart compilation from some
implementation-dependent point in order to let the compilation proceed
without manual intervention.[/quote]

does this mean I'm SOL if I want to be able to offer restarts from
within macro-expansions or compiler-macro-expansions invoked by
COMPILE?

Nick

ps

hello cll long time no see ;-)

From: Juho Snellman
Subject: Re: offering restarts within macro-expansions / exceptional situations  inside the compiler
Date: 
Message-ID: <877ifgmo7s.fsf@vasara.proghammer.com>
········@gmail.com writes:
> does this mean I'm SOL if I want to be able to offer restarts from
> within macro-expansions or compiler-macro-expansions invoked by
> COMPILE?

You can use BREAK to achieve that.

-- 
Juho Snellman
From: ········@gmail.com
Subject: Re: offering restarts within macro-expansions / exceptional 	situations inside the compiler
Date: 
Message-ID: <5715858a-7ea1-4236-a18d-a0b82dc63b4f@i7g2000prf.googlegroups.com>
On Apr 2, 2:32 pm, Juho Snellman <······@iki.fi> wrote:
> ········@gmail.com writes:
> > does this mean I'm SOL if I want to be able to offer restarts from
> > within macro-expansions or compiler-macro-expansions invoked by
> > COMPILE?
>
> You can use BREAK to achieve that.

holy ever-loving crap, what was I thinking?

thanks

Nick