demux-lambda.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ;;; Expand case-lambda and lambda* into simple dispatchers
  2. ;;; Copyright (C) 2024 Free Software Foundation, Inc.
  3. ;;;
  4. ;;; This library is free software: you can redistribute it and/or modify
  5. ;;; it under the terms of the GNU Lesser General Public License as
  6. ;;; published by the Free Software Foundation, either version 3 of the
  7. ;;; License, or (at your option) any later version.
  8. ;;;
  9. ;;; This library is distributed in the hope that it will be useful, but
  10. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;; Lesser General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU Lesser General Public
  15. ;;; License along with this program. If not, see
  16. ;;; <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;;
  19. ;;; We can partition lambdas into simple and complex. A simple lambda
  20. ;;; has just one clause and no optional, rest, or keyword arguments.
  21. ;;; Any other lambda is complex. This pass aims to facilitate reduction
  22. ;;; of complex lambdas to simple lambdas. It does so by eta-expanding
  23. ;;; lexically-bound complex lambdas into simple dispatchers that
  24. ;;; tail-call simple lambda body procedures. This will allow peval to
  25. ;;; elide the complex lambdas in many cases.
  26. ;;;
  27. ;;; Code:
  28. (define-module (language tree-il demux-lambda)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (ice-9 match)
  31. #:use-module (language tree-il)
  32. #:export (demux-lambda))
  33. (define (make-binding name sym val) (vector name sym val))
  34. (define (demux-clause func-name clause)
  35. (match clause
  36. (#f (values '() clause))
  37. (($ <lambda-case> src req opt rest kw inits gensyms body alternate)
  38. (call-with-values (lambda () (demux-clause func-name alternate))
  39. (lambda (bindings alternate)
  40. (define simple-req
  41. (append req (or opt '()) (if rest (list rest) '())
  42. (match kw
  43. ((aok? (kw name sym) ...) name)
  44. (#f '()))))
  45. (define simple-clause
  46. (make-lambda-case src simple-req '() #f #f '() gensyms body #f))
  47. (define simple-func (make-lambda src '() simple-clause))
  48. (define simple-sym (gensym "demuxed"))
  49. (define simple-binding
  50. (make-binding func-name simple-sym simple-func))
  51. (define renamed-syms
  52. (map (lambda (_) (gensym "demux")) gensyms))
  53. (define rename-sym
  54. (let ((renamed (map cons gensyms renamed-syms)))
  55. (lambda (sym) (or (assq-ref renamed sym) sym))))
  56. (define renamed-kw
  57. (match kw
  58. ((aok? (kw name sym) ...)
  59. (cons aok? (map list kw name (map rename-sym sym))))
  60. (#f #f)))
  61. (define renamed-inits
  62. (map (lambda (init)
  63. (post-order
  64. (lambda (exp)
  65. (match exp
  66. (($ <lexical-ref> src name sym)
  67. (make-lexical-ref src name (rename-sym sym)))
  68. (($ <lexical-set> src name sym exp)
  69. (make-lexical-set src name (rename-sym sym) exp))
  70. (_ exp)))
  71. init))
  72. inits))
  73. (define dispatch-call
  74. (make-call src (make-lexical-ref src func-name simple-sym)
  75. (map (lambda (name sym)
  76. (make-lexical-ref src name sym))
  77. simple-req renamed-syms)))
  78. (define dispatch-clause
  79. (make-lambda-case src req opt rest renamed-kw renamed-inits
  80. renamed-syms dispatch-call alternate))
  81. (values (cons simple-binding bindings)
  82. dispatch-clause))))))
  83. (define (demux-lambda exp)
  84. (define (complex-lambda? val)
  85. (match val
  86. (($ <lambda> src meta
  87. ($ <lambda-case> src req opt rest kw inits gensyms body alternate))
  88. (or (pair? opt) rest (pair? kw) alternate))
  89. (_ #f)))
  90. (define (demux-binding name gensym val)
  91. (if (complex-lambda? val)
  92. (match val
  93. (($ <lambda> src meta clause)
  94. (call-with-values (lambda () (demux-clause name clause))
  95. (lambda (extra-bindings clause)
  96. (let ((val (make-lambda src meta clause)))
  97. (append extra-bindings
  98. (list (make-binding name gensym val))))))))
  99. (list (make-binding name gensym val))))
  100. (define (demux-lexically-bound-complex-lambdas exp)
  101. (match exp
  102. (($ <letrec> src in-order? names gensyms vals body)
  103. (match (append-map demux-binding names gensyms vals)
  104. ((#(name gensym val) ...)
  105. (make-letrec src in-order? name gensym val body))))
  106. (($ <let> src names gensyms vals body)
  107. (if (or-map lambda? vals)
  108. (demux-lexically-bound-complex-lambdas
  109. (make-letrec src #f names gensyms vals body))
  110. exp))
  111. (_ exp)))
  112. (post-order demux-lexically-bound-complex-lambdas exp))