Crash on attempt to redefine a default function?

Started by Cyril, October 31, 2007, 05:28:11 AM

Previous topic - Next topic

Cyril

My very first attempt to hack newlisp:


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. :-(
With newLISP you can grow your lists from the right side!

cormullion

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

rickyboy

#2
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

#3
Quote from: "rickyboy"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.



(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">http://newlisp.org/CodePatterns.html#dicts at the end of the chapter.

Cyril

#4
Quote from: "Lutz"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.


Quote from: "Lutz"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. ;-)
With newLISP you can grow your lists from the right side!