2014-01-09 04:54:49 +01:00
|
|
|
;;; Hy tail-call optimization
|
2019-02-07 14:57:35 +01:00
|
|
|
;; Copyright 2019 the authors.
|
2017-04-27 23:16:57 +02:00
|
|
|
;; This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
;; license. See the LICENSE.
|
|
|
|
|
2014-01-09 04:54:49 +01:00
|
|
|
;;; The loop/recur macro allows you to construct functions that use tail-call
|
|
|
|
;;; optimization to allow arbitrary levels of recursion.
|
|
|
|
|
2017-03-31 00:49:10 +02:00
|
|
|
(import [hy.contrib.walk [prewalk]])
|
|
|
|
|
2014-01-09 04:54:49 +01:00
|
|
|
(defn --trampoline-- [f]
|
|
|
|
"Wrap f function and make it tail-call optimized."
|
|
|
|
;; Takes the function "f" and returns a wrapper that may be used for tail-
|
|
|
|
;; recursive algorithms. Note that the returned function is not side-effect
|
|
|
|
;; free and should not be called from anywhere else during tail recursion.
|
|
|
|
|
|
|
|
(setv result None)
|
|
|
|
;; We have to put this in a list because of Python's
|
|
|
|
;; weirdness around local variables.
|
|
|
|
;; Assigning directly to it later would cause it to
|
|
|
|
;; shadow in a new scope.
|
|
|
|
(setv active [False])
|
|
|
|
(setv accumulated [])
|
|
|
|
|
|
|
|
(fn [&rest args]
|
|
|
|
(.append accumulated args)
|
|
|
|
(when (not (first active))
|
|
|
|
(assoc active 0 True)
|
|
|
|
(while (> (len accumulated) 0)
|
2017-07-16 01:28:31 +02:00
|
|
|
(setv result (f #* (.pop accumulated))))
|
2014-01-09 04:54:49 +01:00
|
|
|
(assoc active 0 False)
|
|
|
|
result)))
|
|
|
|
|
2014-01-13 00:53:08 +01:00
|
|
|
|
|
|
|
(defmacro/g! fnr [signature &rest body]
|
2017-03-31 00:49:10 +02:00
|
|
|
(setv new-body (prewalk
|
|
|
|
(fn [x] (if (and (symbol? x) (= x "recur")) g!recur-fn x))
|
|
|
|
body))
|
2017-02-04 02:03:06 +01:00
|
|
|
`(do
|
|
|
|
(import [hy.contrib.loop [--trampoline--]])
|
|
|
|
(with-decorator
|
|
|
|
--trampoline--
|
2017-03-26 01:15:10 +01:00
|
|
|
(defn ~g!recur-fn [~@signature] ~@new-body))
|
2017-02-04 02:03:06 +01:00
|
|
|
~g!recur-fn))
|
2014-01-13 00:53:08 +01:00
|
|
|
|
2014-01-14 02:37:25 +01:00
|
|
|
|
2014-01-13 00:53:08 +01:00
|
|
|
(defmacro defnr [name lambda-list &rest body]
|
|
|
|
(if (not (= (type name) HySymbol))
|
|
|
|
(macro-error name "defnr takes a name as first argument"))
|
Give `require` the same features as `import` (#1142)
Give `require` the same features as `import`
You can now do (require foo), (require [foo [a b c]]), (require [foo [*]]), and (require [foo :as bar]). The first and last forms get you macros named foo.a, foo.b, etc. or bar.a, bar.b, etc., respectively. The second form only gets the macros in the list.
Implements #1118 and perhaps partly addresses #277.
N.B. The new meaning of (require foo) will cause all existing code that uses macros to break. Simply replace these forms with (require [foo [*]]) to get your code working again.
There's a bit of a hack involved in the forms (require foo) or (require [foo :as bar]). When you call (foo.a ...) or (bar.a ...), Hy doesn't actually look inside modules. Instead, these (require ...) forms give the macros names that have periods in them, which happens to work fine with the way Hy finds and interprets macro calls.
* Make `require` syntax stricter and add tests
* Update documentation for `require`
* Documentation wording improvements
* Allow :as in `require` name lists
2016-11-03 08:35:58 +01:00
|
|
|
`(do (require hy.contrib.loop)
|
|
|
|
(setv ~name (hy.contrib.loop.fnr ~lambda-list ~@body))))
|
2014-01-13 00:53:08 +01:00
|
|
|
|
|
|
|
|
2014-01-14 02:37:25 +01:00
|
|
|
(defmacro/g! loop [bindings &rest body]
|
2014-01-09 04:54:49 +01:00
|
|
|
;; Use inside functions like so:
|
2015-08-09 09:09:52 +02:00
|
|
|
;; (defn factorial [n]
|
2014-01-09 04:54:49 +01:00
|
|
|
;; (loop [[i n]
|
|
|
|
;; [acc 1]]
|
|
|
|
;; (if (= i 0)
|
|
|
|
;; acc
|
|
|
|
;; (recur (dec i) (* acc i)))))
|
|
|
|
;;
|
|
|
|
;; If recur is used in a non-tail-call position, None is returned, which
|
|
|
|
;; causes chaos. Fixing this to detect if recur is in a tail-call position
|
|
|
|
;; and erroring if not is a giant TODO.
|
2017-02-04 02:03:06 +01:00
|
|
|
(setv fnargs (map (fn [x] (first x)) bindings)
|
|
|
|
initargs (map second bindings))
|
|
|
|
`(do (require hy.contrib.loop)
|
|
|
|
(hy.contrib.loop.defnr ~g!recur-fn [~@fnargs] ~@body)
|
|
|
|
(~g!recur-fn ~@initargs)))
|