Common Lisp eDSL for type declarations
Clone this package into your local project dir (by default: ~/quicklisp/local-projects/
)
git clone https://github.com/SciBourne/definition.git
Check:
(ql:quickload :definition)
Import names into your package:
(defpackage :my-package
(:shadowing-import-from #:def
#:defun)
(:use #:cl
#:def))
(in-package :my-package)
(defun my-function ()
(+ 1 2 3))
macroexpand
(PROGN (EVAL-WHEN (:COMPILE-TOPLEVEL) (SB-C:%COMPILER-DEFUN 'MY-FUNCTION T NIL NIL)) (SB-IMPL::%DEFUN 'MY-FUNCTION (SB-INT:NAMED-LAMBDA MY-FUNCTION NIL (BLOCK MY-FUNCTION (+ 1 2 3))))) T
(defun my-function (x y &optional (z 0))
(+ x y z))
macroexpand
(PROGN (EVAL-WHEN (:COMPILE-TOPLEVEL) (SB-C:%COMPILER-DEFUN 'MY-FUNCTION T NIL NIL)) (SB-IMPL::%DEFUN 'MY-FUNCTION (SB-INT:NAMED-LAMBDA MY-FUNCTION (X Y &OPTIONAL (Z 0)) (BLOCK MY-FUNCTION (+ X Y Z))))) T
(defun my-function (() -> nil)
(pprint "Not return"))
macroexpand
(PROGN (DECLAIM (FTYPE (FUNCTION NIL (VALUES &OPTIONAL)) MY-FUNCTION)) (COMMON-LISP:DEFUN MY-FUNCTION () (PPRINT "Not return"))) T
(defun my-function (() -> 'null)
(format t "Return nil"))
macroexpand
(PROGN (DECLAIM (FTYPE (FUNCTION NIL (VALUES NULL &OPTIONAL)) MY-FUNCTION)) (COMMON-LISP:DEFUN MY-FUNCTION () (FORMAT T "Return nil"))) T
(defun my-function ((any-arg T) -> T)
any-arg)
macroexpand
(PROGN (DECLAIM (FTYPE (FUNCTION (T) *) MY-FUNCTION)) (COMMON-LISP:DEFUN MY-FUNCTION (ANY-ARG) (DECLARE (TYPE T ANY-ARG)) ANY-ARG)) T
(defun my-function ((x 'fixnum y 'fixnum) -> 'fixnum)
(+ x y))
macroexpand
(PROGN (DECLAIM (FTYPE (FUNCTION (FIXNUM FIXNUM) (VALUES FIXNUM &OPTIONAL)) MY-FUNCTION)) (COMMON-LISP:DEFUN MY-FUNCTION (X Y) (DECLARE (TYPE FIXNUM X) (TYPE FIXNUM Y)) (+ X Y))) T
(defun my-function ((x 'fixnum y 'fixnum &optional z '(or fixnum null))
-> '(or fixnum null))
(when z (+ x y)))
macroexpand
(PROGN (DECLAIM (FTYPE (FUNCTION (FIXNUM FIXNUM &OPTIONAL (OR FIXNUM NULL)) (VALUES (OR FIXNUM NULL) &OPTIONAL)) MY-FUNCTION)) (COMMON-LISP:DEFUN MY-FUNCTION (X Y &OPTIONAL Z) (DECLARE (TYPE FIXNUM X) (TYPE FIXNUM Y) (TYPE (OR FIXNUM NULL) Z)) (WHEN Z (+ X Y)))) T
(defun my-function ((x 'fixnum y 'fixnum &aux (z (* x y)) 'fixnum)
-> 'fixnum)
(+ x y z))
macroexpand
(PROGN (DECLAIM (FTYPE (FUNCTION (FIXNUM FIXNUM) (VALUES FIXNUM &OPTIONAL)) MY-FUNCTION)) (COMMON-LISP:DEFUN MY-FUNCTION (X Y &AUX (Z (* X Y))) (DECLARE (TYPE FIXNUM X) (TYPE FIXNUM Y) (TYPE FIXNUM Z)) (+ X Y Z))) T
(defun my-function ((&optional
x '(or fixnum null)
(y 0) 'fixnum
(z 1 z-supplied-p) 'fixnum)
-> '(or fixnum null))
(when z (+ x y)))
macroexpand
(PROGN (DECLAIM (FTYPE (FUNCTION (&OPTIONAL (OR FIXNUM NULL) FIXNUM FIXNUM) (VALUES (OR FIXNUM NULL) &OPTIONAL)) MY-FUNCTION)) (COMMON-LISP:DEFUN MY-FUNCTION (&OPTIONAL X (Y 0) (Z 1 Z-SUPPLIED-P)) (DECLARE (TYPE (OR FIXNUM NULL) X) (TYPE FIXNUM Y) (TYPE FIXNUM Z) (TYPE BOOLEAN Z-SUPPLIED-P)) (WHEN Z (+ X Y)))) T
(defun my-function ((a 'fixnum
b 'fixnum
&rest
rest-plist ; &rest and &body always T
&key
c '(or fixnum null)
(d 0) 'fixnum
(e 1 e-supplied-p) 'fixnum
((:f f)) '(or fixnum null)
((:g g) 2) 'fixnum
((:h h) 3 h-supplied-p) 'fixnum
&allow-other-keys)
-> 'cons)
(list a b c e f g h rest
(list e-supplied-p h-supplied-p)))
macroexpand
(PROGN (DECLAIM (FTYPE (FUNCTION (FIXNUM FIXNUM &REST T &KEY (:C (OR FIXNUM NULL)) (:D FIXNUM) (:E FIXNUM) (:F (OR FIXNUM NULL)) (:G FIXNUM) (:H FIXNUM) &ALLOW-OTHER-KEYS) (VALUES CONS &OPTIONAL)) MY-FUNCTION)) (COMMON-LISP:DEFUN MY-FUNCTION (A B &REST REST-PLIST &KEY C (D 0) (E 1 E-SUPPLIED-P) ((:F F)) ((:G G) 2) ((:H H) 3 H-SUPPLIED-P) &ALLOW-OTHER-KEYS) (DECLARE (TYPE FIXNUM A) (TYPE FIXNUM B) (TYPE (OR FIXNUM NULL) C) (TYPE FIXNUM D) (TYPE FIXNUM E) (TYPE (OR FIXNUM NULL) F) (TYPE FIXNUM G) (TYPE FIXNUM H) (TYPE BOOLEAN E-SUPPLIED-P) (TYPE BOOLEAN H-SUPPLIED-P)) (LIST A B C E F G H REST (LIST E-SUPPLIED-P H-SUPPLIED-P)))) T
(defun my-function ((x 'fixnum y 'fixnum &optional z 'fixnum)
:ignore z
:optimize (speed 3)
-> 'fixnum)
(+ x y))
macroexpand
(PROGN (DECLAIM (FTYPE (FUNCTION (FIXNUM FIXNUM &OPTIONAL FIXNUM) (VALUES FIXNUM &OPTIONAL)) MY-FUNCTION)) (COMMON-LISP:DEFUN MY-FUNCTION (X Y &OPTIONAL Z) (DECLARE (TYPE FIXNUM X) (TYPE FIXNUM Y) (TYPE FIXNUM Z) (OPTIMIZE (SPEED 3)) (IGNORE Z)) (+ X Y))) T
(defun my-function ((*sum* 'fixnum x 'fixnum y 'fixnum k 'cons)
:inline *
:notinline (+ -)
:special *sum*
:dynamic-extent k
:optimize ((debug 3)
(safety 3)
(speed 0)
(space 1))
-> nil)
(setf *sum* (+ (- (* x (first k)) 1) (* y (second k)))))
macroexpand
(PROGN (DECLAIM (FTYPE (FUNCTION (FIXNUM FIXNUM FIXNUM CONS) (VALUES &OPTIONAL)) MY-FUNCTION)) (COMMON-LISP:DEFUN MY-FUNCTION (*SUM* X Y K) (DECLARE (TYPE FIXNUM *SUM*) (TYPE FIXNUM X) (TYPE FIXNUM Y) (TYPE CONS K) (OPTIMIZE (DEBUG 3) (SAFETY 3) (SPEED 0) (SPACE 1)) (INLINE *) (NOTINLINE + -) (SPECIAL *SUM*) (DYNAMIC-EXTENT K)) (SETF *SUM* (+ (- (* X (FIRST K)) 1) (* Y (SECOND K)))))) T
Coming soon…