rm-hull / infix

A Clojure library for expressing LISP expressions as infix rather than prefix notation
https://www.destructuring-bind.org/infix/
MIT License
106 stars 11 forks source link

Way to discover needed aliases/bindings? #35

Closed dsbw closed 3 years ago

dsbw commented 3 years ago

I'm trying to give my users a way to script formulae, a la Excel. Let's say one puts in the formula for Body Mass Index (BMI) which is:

(wt / (ht**2)) * 703

Is there a way I can find that the symbols wt and ht are needed? Basically, I'm going to be coming in with a map of values and I want to be able to say "Hey, bub, your map has wt but not ht.

(I edited this to remove an extraneous issue).

rm-hull commented 3 years ago

Now included on README.md:

(def hypot
  (from-string [x y]
    "sqrt(x**2 + y**2)"))
; => #'user/hypot

(hypot 3 4)
; => 5

(meta hypot)
; => {:params [:x :y], :doc "sqrt(x**2 + y**2)"}

[This will be built into the next published release]

dsbw commented 3 years ago

Okay, that's cool! But what if it's...

(def area 
  (from-string [r] 
               "pi * (r**2)"))

And you want to know that it uses "pi"?

rm-hull commented 3 years ago

At the point of invoking the function, the only thing that can affect the output is the value of r. As long as pi Is bound in the environment it will compile. If you had pie instead for example, it would throw an error along the lines of pie is not bound in environment.

I guess we have the grammar, so rather than just evaluating the expression it could additionally return the AST in the metadata.

dsbw commented 3 years ago

Just to clarify, I want the users to be able to enter something like "=(pi * r**2)" and when they go to evaluate, to be able to say, "Hey, you asked for r but didn't supply r." If I make them supply the parameters, like "=[r](pi * r**2)" it's a thing that can go wrong. For two variables, like "bmi=[wt ht](wt/ht**2)*703", I have the additional possibility of passing in the parameters in the wrong order, like "bmi(ht, wt)".