Commit b6b7d821 authored by Phoebe Goldman's avatar Phoebe Goldman
Browse files

accept result-type in reducing clauses

incl. minimal documentation and a pair of tests
parent 2d41334a
......@@ -899,6 +899,7 @@ variable is untyped) or @math{0} (if it has a numeric type).
@deffn Clause reducing @v{expr} @k{by} @v{func} @k{&optional}
@k{initial-value} @v{init-val} @k{into} @v{var}
@k{result-type} @v{type}
This is a general way to perform reductions. @var{func} should be a
function of two arguments, the first of which will be the value
......@@ -1479,6 +1479,22 @@
(multiply n result-type integer))
#.(* 2 most-positive-fixnum))
;;; test that reducing result-type with an initform doesn't error
(deftest type.12
(iter (declare (iterate:declare-variables))
(for i from 1 to 4)
(reducing i by #'+ result-type fixnum))
;;; test that reducing result-type uses an initform of the appropriate type.
;; the manual says that this behavior is undefined, but it does work, and it's necessary
;; for sbcl to generate efficient numeric reductions without throwing a fit.
(deftest type.13
(iter (declare (iterate:declare-variables))
(repeat 0)
(reducing 0d0 by #'+ result-type double-float))
(deftest static.error.1
(ignore-errors ; Iterate complains multiple values make no sense here
......@@ -2914,9 +2914,10 @@ e.g. (DSETQ (VALUES (a . b) nil c) form)"
(defsynonym multiplying multiply)
(defclause (reducing expr by op &optional initial-value (init-val nil iv?)
into var-spec)
into var-spec
result-type (type (expr-type-only op)))
"Generalized reduction"
;; VALUE: the value accumulated so far.
;; If we don't know the initial value, we can't use RETURN-REDUCTION-CODE.
......@@ -2932,14 +2933,14 @@ e.g. (DSETQ (VALUES (a . b) nil c) form)"
:expression expr
:test nil
:variable var-spec
:type (expr-type-only op)
:type type
:accum-kind nil)) ; matches anything
(setq expr (walk-expr expr))
(setq var-spec (or var-spec *result-var*))
(let* ((var (extract-var var-spec))
(entry (make-accum-var-default-binding var-spec nil
:using-type-of expr))
:type type))
(prev-first-time-var (third entry)))
(multiple-value-bind (update-code first-time-var)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment