probcomp / metaprob

An embedded language for probabilistic programming and meta-programming.
GNU General Public License v3.0
168 stars 17 forks source link

Implement untraced `recur` #96

Open alex-lew opened 5 years ago

alex-lew commented 5 years ago

We have issue #23 for general tail recursion in Metaprob. But there is a special case that may be easier to implement, and which would be useful in a number of common cases.

The idea is that we could enable tail-recursive calls within untraced functions. Use cases include map:

(define map
  (gen [f l]
    (with-explicit-tracer t
       (define helper 
            (gen [l acc i] 
               (if (empty? l) 
                   acc 
                   (recur (rest l) (cons (t i f (first l)) acc) (+ i 1)))
       (helper l '() 0))))

and reduce, replicate, etc. Even though this may be simpler than implementing full TCE in Metaprob, it will still require some implementation work; even though the recursive call is untraced, it can still produce a score (so scores will likely need to be threaded through the interpreter).