scicloj / wolframite

An interface between Clojure and Wolfram Language (the language of Mathematica)
https://scicloj.github.io/wolframite/
Mozilla Public License 2.0
56 stars 2 forks source link

Wolfram rules converted to form that is no longer supported (Edit: Renamed the issue as the source is quite a different problem) #76

Closed light-matters closed 4 months ago

light-matters commented 4 months ago

As described below,

The minimum example of failure is

(wl/eval (wl/eval (w/-> 'x 5)))

Somehow turning Wolfram Rules into (-> ...) works but the other way doesn't anymore.

Aside: Can we add a practical example of leveraging rules to the docs/demo?


Origina description

Load for-scientists.cavity-physics namespace and try building the html. The same namespace works fine using the wolframite-examples project (where wolframite is imported as a library).

Fails with the falling error:

1. Unhandled java.lang.IllegalArgumentException
   No matching ctor found for class com.wolfram.jlink.Expr

            Reflector.java:  337  clojure.lang.Reflector/invokeConstructor
      jlink_proto_impl.clj:   43  wolframite.impl.jlink-proto-impl.JLinkImpl/expr
               convert.clj:   98  wolframite.base.convert/eval17569/fn
              MultiFn.java:  234  clojure.lang.MultiFn/invoke
               convert.clj:  146  wolframite.base.convert/eval17620/fn/fn
                  core.clj: 2772  clojure.core/map/fn
light-matters commented 4 months ago

It makes no sense, but it stops working at commit c50267a89a1f9dd50eef56081b73df15ce81ffa4

It works at commit 6e209fa13f569c32e62a0f5a19d67a1af92c2e7e

light-matters commented 4 months ago

It seems to be something to do with the double eval.

(ns physics.cavity.expr-debug
  (:require
   [wolframite.core :as wl]
   [wolframite.wolfram-extended :as w]))

(def E1
  (w/+ 't1 (w/* 'r1 (w/- 'E4))))

(def e4 (-> (w/== 'E4 E1)
            (w/Solve 'E4)
            wl/eval
            w/First w/First))
(wl/eval e4)

fails with the error message, but

(def e4 (-> (w/== 'E4 E1)
            (w/Solve 'E4)
            ;;wl/eval
            w/First w/First))
(wl/eval e4)

doesn't.

light-matters commented 4 months ago

The minimum example of failure is actually

(wl/eval (wl/eval (w/-> 'x 5)))

Somehow turning Wolfram rules into (-> ...) works but the other way doesn't anymore.

Still fails with

1. Unhandled java.lang.IllegalArgumentException
   No matching ctor found for class com.wolfram.jlink.Expr

            Reflector.java:  337  clojure.lang.Reflector/invokeConstructor
      jlink_proto_impl.clj:   43  wolframite.impl.jlink-proto-impl.JLinkImpl/expr
light-matters commented 4 months ago

Either we should resupport aliasing -> to Rule or w/-> should evaluate to 'Rule'.

holyjak commented 4 months ago

Well, base-aliases do contain '-> 'Rule so I am not sure that aliasing is the core problem here.

holyjak commented 4 months ago

Not sure what eactly is going on here, but the exception is thrown on the second eval, when we try to turn 5 into an expr using (Expr. 5) - which fails b/c 5 here is Integer, not int, i.e. not primitive.

light-matters commented 4 months ago

good find