(
;; This include file defines various forms of `curry`
;; A "curry" binds values to a function, making them constant,
;; and returning a new function that returns fewer arguments (since the
;; arguments are now fixed).
;; Example: (defun add2 (V1 V2) (+ V1 V2)) ; add two values
;; (curry add2 15) ; this yields a function that accepts ONE argument, and adds 15 to it
; (curry_args sum (list 50 60)) => returns a function that is like (sum 50 60 ...)
(defun curry_args (func list_of_args) (qq (a (q . (unquote func)) (unquote (fix_curry_args list_of_args (q . 1))))))
;; (curry sum 50 60) => returns a function that is like (sum 50 60 ...)
(defun curry (func . args) (curry_args func args))
;; utility function used by `curry_args`
(defun fix_curry_args (items core)
(if items
(qq (c (q . (unquote (f items))) (unquote (fix_curry_args (r items) core))))
core
)
)
;; `curry_sha_args`: curry a function, passing in the hash of the original function,
;; and return (NEW_FUNCTION . SHA_TREE_OF_NEW_FUNCTION)
;; Since this function knows the hash of the original function, it doesn't need to recurse into that function;
;; it just uses the memoized hash.
;; If you're going to curry a function multiple times, using this function can save you
;; currying the core muliple times.
;; Example:
;;
;; Calculate the sha256tree of `add2`
;;
;; run -i clvm_runtime '(mod (X) (defun add2 (A B) (+ A B)) (include sha256tree.clvm) (sha256tree add2))'
;; (q . 0x4212e6549edf9ba3ce6bcd8839ecca37b430ec96ddbbae17e5c669d8e18bd17f)
;; Now calculate the sha256tree of `add2` when curried with `15`, ie. fixing A to 15
;;
;; run -i clvm_runtime '(mod (X) (defun add2 (A B) (+ A B)) (include curry.clvm) (include sha256tree.clvm) (sha256tree (curry add2 15)) )'
;; (q . 0xf90da10ec9e353929eb8ad3294986874e000fae0b62d5810e58ec6806b2a70ed)
;; Now, supposing we know the hash of `add2`, do it again but don't rehash `add2
;;
;; run -i clvm_runtime '(mod (X) (defun add2 (A B) (+ A B)) (include curry.clvm) (include sha256tree.clvm) (curry_sha add2 0x4212e6549edf9ba3ce6bcd8839ecca37b430ec96ddbbae17e5c669d8e18bd17f 15) )'
;; (q (a (q 16 5 11) (c (q . 15) 1)) . 0xf90da10ec9e353929eb8ad3294986874e000fae0b62d5810e58ec6806b2a70ed)
(defun curry_sha_args (func func_hash args)
(c (curry_args func args) (sha256tree_esc (curry_args func_hash args) func_hash))
)
;; return the hash of the curried function
(defun curry_sha (func func_hash . args)
(curry_sha_args func func_hash args)
)
)