tizoc / shen-scheme

Shen/Scheme implementation
Other
100 stars 8 forks source link

predicate as an argument failed #16

Open doug719 opened 3 years ago

doug719 commented 3 years ago

(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)))

tizoc commented 3 years ago

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

(any? (function number?) [1 2 3])
doug719 commented 3 years ago

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 commented 3 years ago

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.

tizoc commented 3 years ago

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).

doug719 commented 3 years ago

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.

tizoc commented 3 years ago

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 commented 3 years ago

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