Skip to content

Commit

Permalink
Implicitly constrain return types to the specified number of values
Browse files Browse the repository at this point in the history
  • Loading branch information
ruricolist committed Oct 22, 2023
1 parent 2f6a1f1 commit 4e99d79
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions types.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,30 @@ Literal keywords, numbers, and characters are also treated as `eql' type specifi
(defpattern tuple (&rest args)
`(list ,@args))

(deftype -> (args &optional values)
"The type of a function from ARGS to VALUES."
`(function ,args ,@(when values
(list values))))

(defmacro -> (function (&rest args) &optional values)
"Declaim the ftype of FUNCTION from ARGS to VALUES.
(deftype -> (arg-typespec &optional value-typespec)
"The type of a function from ARG-TYPESPEC to VALUE-TYPESPEC.
VALUE-TYPESPEC must specify all values returned."
(let ((value-typespec
(match value-typespec
(() nil)
((list* 'values value-types)
(list
(if (intersection value-types lambda-list-keywords)
value-typespec
(append value-typespec '(&optional)))))
(t (list (list 'values value-typespec '&optional))))))
`(function ,arg-typespec ,@value-typespec)))

(defmacro -> (function (&rest arg-typespec) &optional value-typespec)
"Declaim the ftype of FUNCTION from ARG-TYPESPEC to VALUE-TYPESPEC.
(-> mod-fixnum+ (fixnum fixnum) fixnum)
(defun mod-fixnum+ (x y) ...)"
`(declaim (ftype (-> (,@args) ,@(when values
(list values)))
(defun mod-fixnum+ (x y) ...)
VALUE-TYPESPEC must specify all values returned."
`(declaim (ftype (-> (,@arg-typespec)
,@(when value-typespec
(list value-typespec)))
,function)))

(defmacro declaim-freeze-type (&rest types)
Expand Down

0 comments on commit 4e99d79

Please sign in to comment.