Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

predicate as an argument failed #16

Open
doug719 opened this issue Apr 28, 2021 · 7 comments
Open

predicate as an argument failed #16

doug719 opened this issue Apr 28, 2021 · 7 comments

Comments

@doug719
Copy link

doug719 commented Apr 28, 2021

(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
Copy link
Owner

tizoc commented Apr 28, 2021

@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
Copy link
Author

doug719 commented Apr 28, 2021 via email

@doug719
Copy link
Author

doug719 commented Apr 28, 2021 via email

@tizoc
Copy link
Owner

tizoc commented Apr 28, 2021

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
Copy link
Author

doug719 commented Apr 28, 2021 via email

@tizoc
Copy link
Owner

tizoc commented Apr 28, 2021

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
Copy link
Owner

tizoc commented Apr 28, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants