1;;; thunk.el --- Lazy form evaluation  -*- lexical-binding: t -*-
2
3;; Copyright (C) 2015-2021 Free Software Foundation, Inc.
4
5;; Author: Nicolas Petton <nicolas@petton.fr>
6;; Keywords: sequences
7;; Version: 1.0
8;; Package: thunk
9
10;; Maintainer: emacs-devel@gnu.org
11
12;; This file is part of GNU Emacs.
13
14;; GNU Emacs is free software: you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
25;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
26
27;;; Commentary:
28;;
29;; Thunk provides functions and macros to delay the evaluation of
30;; forms.
31;;
32;; Use `thunk-delay' to delay the evaluation of a form (requires
33;; lexical-binding), and `thunk-force' to evaluate it.  The result of
34;; the evaluation is cached, and only happens once.
35;;
36;; Here is an example of a form which evaluation is delayed:
37;;
38;;     (setq delayed (thunk-delay (message "this message is delayed")))
39;;
40;; `delayed' is not evaluated until `thunk-force' is called, like the
41;; following:
42;;
43;;    (thunk-force delayed)
44;;
45;; This file also defines macros `thunk-let' and `thunk-let*' that are
46;; analogous to `let' and `let*' but provide lazy evaluation of
47;; bindings by using thunks implicitly (i.e. in the expansion).
48
49;;; Code:
50
51(require 'cl-lib)
52
53(defmacro thunk-delay (&rest body)
54  "Delay the evaluation of BODY."
55  (declare (debug (def-body)))
56  (cl-assert lexical-binding)
57  `(let (forced
58         (val (lambda () ,@body)))
59     (lambda (&optional check)
60       (if check
61           forced
62         (unless forced
63           (setf val (funcall val))
64           (setf forced t))
65         val))))
66
67(defun thunk-force (delayed)
68  "Force the evaluation of DELAYED.
69The result is cached and will be returned on subsequent calls
70with the same DELAYED argument."
71  (funcall delayed))
72
73(defun thunk-evaluated-p (delayed)
74  "Return non-nil if DELAYED has been evaluated."
75  (funcall delayed t))
76
77(defmacro thunk-let (bindings &rest body)
78  "Like `let' but create lazy bindings.
79
80BINDINGS is a list of elements of the form (SYMBOL EXPRESSION).
81Any binding EXPRESSION is not evaluated before the variable
82SYMBOL is used for the first time when evaluating the BODY.
83
84It is not allowed to set `thunk-let' or `thunk-let*' bound
85variables.
86
87Using `thunk-let' and `thunk-let*' requires `lexical-binding'."
88  (declare (indent 1) (debug let))
89  (cl-callf2 mapcar
90      (lambda (binding)
91        (pcase binding
92          (`(,(pred symbolp) ,_) binding)
93          (_ (signal 'error (cons "Bad binding in thunk-let"
94                                  (list binding))))))
95      bindings)
96  (cl-callf2 mapcar
97      (pcase-lambda (`(,var ,binding))
98        (list (make-symbol (concat (symbol-name var) "-thunk"))
99              var binding))
100      bindings)
101  `(let ,(mapcar
102          (pcase-lambda (`(,thunk-var ,_var ,binding))
103            `(,thunk-var (thunk-delay ,binding)))
104          bindings)
105     (cl-symbol-macrolet
106         ,(mapcar (pcase-lambda (`(,thunk-var ,var ,_binding))
107                    `(,var (thunk-force ,thunk-var)))
108                  bindings)
109       ,@body)))
110
111(defmacro thunk-let* (bindings &rest body)
112  "Like `let*' but create lazy bindings.
113
114BINDINGS is a list of elements of the form (SYMBOL EXPRESSION).
115Any binding EXPRESSION is not evaluated before the variable
116SYMBOL is used for the first time when evaluating the BODY.
117
118It is not allowed to set `thunk-let' or `thunk-let*' bound
119variables.
120
121Using `thunk-let' and `thunk-let*' requires `lexical-binding'."
122  (declare (indent 1) (debug let))
123  (cl-reduce
124   (lambda (expr binding) `(thunk-let (,binding) ,expr))
125   (reverse bindings)
126   :initial-value (macroexp-progn body)))
127
128;; (defalias 'lazy-let  #'thunk-let)
129;; (defalias 'lazy-let* #'thunk-let*)
130
131
132(provide 'thunk)
133;;; thunk.el ends here
134