Crash on attempt to redefine a default function?

For the Compleat Fan
Locked
Cyril
Posts: 183
Joined: Tue Oct 30, 2007 6:27 pm
Location: Moscow, Russia
Contact:

Crash on attempt to redefine a default function?

Post by Cyril »

My very first attempt to hack newlisp:

Code: Select all

newLISP v.9.2.4 on Win32, execute 'newlisp -h' for more info.

> (define (ctx:ctx key val) (if val (set (sym key ctx) val) (eval (sym key ctx))
))
(lambda (key val)
 (if val
  (set (sym key ctx) val)
  (eval (sym key ctx))))
> (ctx "hello" "world")
"world"
> (ctx "hello")
"world"
> (ctx "hello" "peace")
"peace"
> (ctx "hello")
"peace"
> (ctx "ctx" "error")
And at this point the interpreter crashes! This is reproduced on Windows 98 (yes, that old), I haven't checked this on Unix-like systems. Both 9.2.0 and 9.2.4 crashes. Yes, I know, it is not a very good idea to redefine a default function anyway, but I've expected at least a gentle error message. :-(

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I get 'bus error' and a crash on Unix (newLISP v.9.2.3 on OSX UTF-8)

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Post by rickyboy »

It should crash!! :-)

You are trying to pull the rug out from under the function ctx:ctx, by asking it to redefine itself, when you say (ctx "ctx" "error"). In other words, you are asking the settor to set itself to another value while it's in the middle of running itself. Hmm...

I don't know how much the cost of the overhead to check for this impacts newlisp. Lutz?
(λx. x x) (λx. x x)

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

rickyboy wrote:It should crash!! :-)
yes, modifying a function while executing it, will crash most of the time. I thought about this when creating the expanded syntax for 'context', which is used frequently for doing hashes. When doing hashes/dictionaries from web-pages etc., it is probable that the forbidden word (the default functor) comes along. The 'context' function will not allow modifying the default function.

Code: Select all

(define (myhash:myhash key value) 
   (if value 
       (context 'myhash key value) 
       (context 'myhash key)))

; will not allow setting the default functor but return nil

(myhash "myhash" 123) => nil
This one using 'context' instead of 'sym' will not allow using the name of the default symbol and return nil instead, while 'sym' will always try it.

ps: above example is from http://newlisp.org/CodePatterns.html#dicts at the end of the chapter.

Cyril
Posts: 183
Joined: Tue Oct 30, 2007 6:27 pm
Location: Moscow, Russia
Contact:

Post by Cyril »

Lutz wrote:yes, modifying a function while executing it, will crash most of the time.
I believe that a scripting language intended for easy everyday tasks (from really easy everyday tasks up to AI) should not crash on any conditions -- error should be caught and reported. Let us leave segfaults to hardcore C programmers! In fact, the very ability to crash the interpreter (left aside low-level functions like memcpy) is enough reason to search some other language for many people.
Lutz wrote:When doing hashes/dictionaries from web-pages etc., it is probable that the forbidden word (the default functor) comes along.
I think there are two styles of using contexts, and they are not to be mixed: either it is, er, context (object, closure, module etc.), and then all the symbols in it are explicitly written in a program (none are read from the outside world), or it is a dictionary (wrong but commonly called hash), and then all the keys are read from the outside world and none are written in the program. Unless you are mixing them, you are on the safe side. The idea of using the default context function as an interface to the very same context as a dictionary seems wrong to me.

In fact I've crushed into this misfeature when I've write a simple text processing script and feed the script own text to the script itself -- not surprisingly one of the words in input data coincides with one of the symbols in code. ;-)

Locked