Evaluating simple Lisp like S-expressions

I’m looking for the library which can evaluate simple Lisp like S-expressions. Specifically, I have simple function definitions produced by solvers like cvc5, e.g.:

(define-fun f ((color Int) (sortAsc Int) (sortDesc Int)) Int (ite (<= 1 sortAsc) (ite (<= 1 sortDesc) 0 2) 3))

where ite stands for if-then-else. I can do a simple preprocessing to match Lisp or another syntax. What I need is the value of f with specific arguments. I prefer something off-the-shelf rather than writing yet another prefix notation calculator in Python.

Let me ask why you are not using a lisp program to run the lisp code?

Because I’m using Python code, e.g. cvc5, which produces Lisp code.

I found Hy, which can do it:

import hy

s = '''
(setv color 1)
(setv sortAsc 3)
(setv sortDesc 0)
(defn f [color sortAsc sortDesc] (if (<= 1 sortAsc) (if (<= 1 sortDesc) 0 2) 3))
(f color sortAsc sortDesc)
'''

print(hy.eval(hy.read_many(s)))

The author recommended something simpler, e.g. Scheme implementation. Searching PyPi for Scheme doesn’t help, though. Any recommendations?

2 Likes

I wonder if this would be a good moment to try your hand at writing a very simple Lisp interpreter in Python? It shouldn’t be too hard to design a simplistic parser that supports a subset of Lisp functionality, and it’s an excellent exercise.

Maybe, but I have too much work to do. As I wrote in my post: “I prefer something off-the-shelf rather than writing yet another prefix notation calculator in Python.”

Yeah I saw that, but having looked for something off-the-shelf and found little, it may be worth reconsidering.

But it outputs lisp. There is nothing to say that you can not use more then one language in your work.

tkinter waves shyly and glances down at the complete Tcl interpreter bundled with your GUI library…

1 Like

Have a look at Lispy and if you feel you have to use lisp try Steel Bank Common Lisp

Thanks. Lispy is on the top of my list of DIY options. I tested cl4py which uses sbc backend, but had some issues with it and went with Hy for now.

i didn’t understand this! can u rephrase a bit?

Searching PyPi for Scheme (Search results · PyPI) yields completely irrelevant results about various schemas.