shen-scheme icon indicating copy to clipboard operation
shen-scheme copied to clipboard

predicate as an argument failed

Open doug719 opened this issue 3 years ago • 7 comments

(any? number? [1 2 3]) Exception in unknown-location: attempt to apply non-procedure number?

This works on shen-cl

(define any? _ [] -> false F [X | Xs] -> (or (F X) (any? F Xs)))

doug719 avatar Apr 28 '21 14:04 doug719

@doug719 unlike in Scheme, in Shen you must wrap function names in function when passing them as arguments.

(any? (function number?) [1 2 3])

tizoc avatar Apr 28 '21 15:04 tizoc

But(any? number? [1 2 3])works ok on shen-cl.Why the difference?

On Wednesday, April 28, 2021, 9:07:30 AM MDT, Bruno Deferrari ***@***.***> wrote:  

@doug719 unlike in Scheme, in Shen you must wrap function names in function when passing them as arguments. (any? (function number?) [1 2 3]) — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

doug719 avatar Apr 28 '21 15:04 doug719

Bruno, All of the schemes implementations and lisp implementations  that I know of (including sbcl) exit on a CTRL-d.  I would think that since shen is part of the lisp family, it would also allow this. Regards,Doug

On Wednesday, April 28, 2021, 9:22:21 AM MDT, T.D. Telford ***@***.***> wrote:  

But(any? number? [1 2 3])works ok on shen-cl.Why the difference?

On Wednesday, April 28, 2021, 9:07:30 AM MDT, Bruno Deferrari ***@***.***> wrote:  

@doug719 unlike in Scheme, in Shen you must wrap function names in function when passing them as arguments. (any? (function number?) [1 2 3]) — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

doug719 avatar Apr 28 '21 15:04 doug719

But(any? number? [1 2 3])works ok on shen-cl.Why the difference?

Because of the difference between Common Lisp and Scheme. Remember that in Shen symbols are self-quoting, so what you are trying to do when you don't wrap the name in function, is trying to apply a symbol instead of a function. Common Lisp allows that, Scheme doesn't, and in Shen it is undefined behavior unless you have the type checker enabled (and in that case you get a type error before the underlying platform even gets a chance to run the code).

In Common Lisp this works:

(funcall '+ 1 2)

but in Scheme this doesn't:

(apply '+ '(1 2))

Now, if you try this in Shen with the type-checker enabled, you will notice that it doesn't catch it. That is a known issue and there are changes in the new spec that take care of that (but for now you will have to wait until that gets released).

tizoc avatar Apr 28 '21 15:04 tizoc

Hello Bruno, Thanks for the explanation.  The decision to quote args is ok for a cl based implementation, but not a good thing for a scheme based one.  Having to enter (function  number?) means that number? is not a first class function.  I am used to first class functions in scheme. Regards,Doug

On Wednesday, April 28, 2021, 9:52:27 AM MDT, Bruno Deferrari ***@***.***> wrote:  

But(any? number? [1 2 3])works ok on shen-cl.Why the difference?

Because of the difference between Common Lisp and Scheme. Remember that in Shen symbols are self-quoting, so what you are trying to do when you don't wrap the name in function, is trying to apply a symbol instead of a function. Common Lisp allows that, Scheme doesn't, and in Shen it is undefined behavior unless you have the type checker enabled (and in that case you get a type error before the underlying platform even gets a chance to run the code).

In Common Lisp this works: (funcall '+ 1 2) but in Scheme this doesn't: (apply '+ '(1 2)) Now, if you try this in Shen with the type-checker enabled, you will notice that it doesn't catch it. That is a known issue and there are changes in the new spec that take care of that (but for now you will have to wait until that gets released).

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

doug719 avatar Apr 28 '21 16:04 doug719

Functions are first class in Shen, it is just that the syntax is different. When you write some-name you are mentioning a symbol (like in Prolog), not a function or variable. It is the same as 'some-name in Scheme or Common Lisp. Any port that changes this will not be following the Shen spec.

If you don't wrap the name in function (or fn in the new kernel and spec, which have not been released as opensource yet), then your code is not portable, and will not work as expected on most Shen ports.

It works on Common Lisp because of what I explained above (it has nothing to do about how the code is compiled from Shen to Lisp or Scheme, both implementations share most of the compiler code), but about how Common Lisp behaves.

Making the Common Lisp port not work like this would make it slower (because extra checks would be needed at runtime to prevent that behavior), in the same way that making other ports accept plain symbols as if they were functions would make them slower (because extra checks and lookups would be required at runtime).

If you want a shorter version in Shen/Scheme, you can write (any? (number?) [1 2 3]), but note that this nay not be portable either (I am not sure if the Shen spec requires that to work).

tizoc avatar Apr 28 '21 21:04 tizoc

Please read the "The Semantics of Symbols in Shen and Kl" section of this document: http://shenlanguage.org/shendoc.htm

tizoc avatar Apr 28 '21 21:04 tizoc