argumentcomputer / lurk

Lurk is a Turing-complete programming language for zk-SNARKs. It is a statically scoped dialect of Lisp, influenced by Scheme and Common Lisp.
https://docs.argument.xyz
MIT License
46 stars 3 forks source link

loop detected in recursive function #366

Closed porcuquine closed 2 weeks ago

porcuquine commented 2 weeks ago

Here is the function:

!(defrec match-star (lambda (matcher s)
                      (let ((first-match (matcher s)))
                        (if first-match
                            (let ((rest-match (match-star matcher (cdr s))))
                              (cons (cons (car first-match)
                                          (car rest-match))
                                    (cdr rest-match)))
                            (cons nil s)))))

This leads to the following error:

!(def match-char (lambda (c s)
                   (if (eq c (car s))
                       (cons (car s) (cdr s)))))

!(assert-eq '(('a' 'a' 'a') . "b") (match-star (match-char 'a') "aaab"))

=> Error: loop detcted

I expect this to succeed. As a sanity check, I ported to Common Lisp:

(defun match-char (c s)
  (if (eql c (car s))
      (cons (car s) (cdr s))))

(defun match-star (matcher s)
  (let ((first-match (funcall matcher s)))
    (if first-match
        (let ((rest-match (match-star matcher (cdr s))))
          (cons (cons (car first-match)
                      (car rest-match))
                (cdr rest-match)))
        (cons nil s))))

(match-star (lambda (x) (match-char 'a x)) '(a a a b))
=> ((A A A) B)
porcuquine commented 2 weeks ago

The above report is actually not reproducible. I traced the actual error to a different piece of code in a longer source file.