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

Cannot define functions the same way as Wolfram (using Pattern and Blank, not Function) #74

Open
light-matters opened this issue Jul 17, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@light-matters
Copy link
Contributor

light-matters commented Jul 17, 2024

When writing the docs for said behaviour, I came across this issue (which I'm not sure existed previously).

(wl/eval (wl/->clj "SetDelayed[f[Pattern[x, Blank[]]], Power[x, 2]]"))

Returns "$Failed". The string can be evaluated inside Wolfram fine and corresponds to a basic function definition:

f[x_]:=x^2

I can get the desired behaviour with

(wl/eval (w/_= 'f (w/fn [x] (w/Power x 2))))

, but this is only because w/fn is an especially coded exception.

It's also worth noting that we can no longer write

(wl/eval ((w/Function 'x
                      (w/Power 'x 2)) 
                5))

, but instead must write

(wl/eval ((wl/eval (w/Function 'x
                               (w/Power 'x 2))) 5))
@light-matters light-matters added the bug Something isn't working label Jul 17, 2024
@holyjak
Copy link
Member

holyjak commented Sep 13, 2024

Ok, so the following two approaches work:

(wl/eval (w/_= 'f (w/fn [x] (w/Power x 2))))
=> nil
(wl/eval '(SetDelayed f2 (Function [x] (Power x 2))))
=> nil
(wl/eval "SetDelayed[f3, Function[{x}, Power[x, 2]]]")

(wl/eval '(f 2)) ; or f2, or f3
=> 4

but this, which should be equivalent except that it uses not Function but Pattern + Blank, does not:

(wl/eval "SetDelayed[f[Pattern[x, Blank[]]], Power[x, 2]]")
=> $Failed

(-> (wl/wolfram-expr "SetDelayed[f[Pattern[x, Blank[]]], Power[x, 2]]") 
      (wolframite.base.convert/convert {:jlink-instance (wolframite.impl.jlink-instance/get)}))
=> #object[com.wolfram.jlink.Expr 0x31a78c09 "SetDelayed[f[Pattern[x, Blank[]]], Power[x, 2]]"]
(wolframite.base.evaluate/evaluate *1 {:jlink-instance (wolframite.impl.jlink-instance/get)})
=> #object[com.wolfram.jlink.Expr 0x2b6a7d2 "$Failed"]

So the seemingly same SetDelayed... call behaves differently.
TODO: Explore the Expr instace in detail. Its string representation could mean different things... .

In Wolfram, this works:

In[4]:= SetDelayed[f4[Pattern[x, Blank[]]], Power[x, 2]]                                                                                                                           
In[5]:= f4[2]                                                                                                                                                                      
Out[5]= 4

@holyjak holyjak changed the title Cannot define functions the same way as Wolfram Cannot define functions the same way as Wolfram (using Pattern and Blank, not Function) Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants