Skip to content

Commit

Permalink
initial public commit
Browse files Browse the repository at this point in the history
  • Loading branch information
guicho271828 committed May 29, 2019
0 parents commit 82d0ab6
Show file tree
Hide file tree
Showing 18 changed files with 1,803 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
.DS_Store
*.fasl
*.dx32fsl
*.dx64fsl
*.lx32fsl
*.lx64fsl
*.x86f
*~
.#*
*.backup
41 changes: 41 additions & 0 deletions .travis.yml
@@ -0,0 +1,41 @@
language: common-lisp
sudo: false

addons:
apt:
packages:
- libc6-i386
- clisp
- openjdk-7-jre

env:
global:
- PATH=~/.roswell/bin:$PATH
- ROSWELL_INSTALL_DIR=$HOME/.roswell
matrix:
- LISP=sbcl-bin
- LISP=ccl-bin
- LISP=abcl
- LISP=clisp
- LISP=ecl
- LISP=cmucl
- LISP=alisp

matrix:
allow_failures:
- env: LISP=clisp
- env: LISP=abcl
- env: LISP=ecl
- env: LISP=cmucl
- env: LISP=alisp

install:
- curl -L https://raw.githubusercontent.com/snmsts/roswell/release/scripts/install-for-ci.sh | sh

cache:
directories:
- $HOME/.roswell
- $HOME/.config/common-lisp

script:
- ./testscr.ros
29 changes: 29 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,29 @@
# Developer's Certificate of Origin

To handle the legal aspects of contribution, we ask each Pull Request submitter
to sign the Developer's Certificate of Origin 1.1 (DCO) [1] that the Linux®
Kernel community uses [2] to manage code contributions.

[1] legal/DCO1.1.txt
[2] https://elinux.org/Developer_Certificate_Of_Origin

We simply ask that when submitting a patch for review, the developer must
include a sign-off statement in the commit message.

Here is an example Signed-off-by line, which indicates that the submitter
accepts the DCO linked above:

Signed-off-by: Your Name <your@mail.org>

As of now, you need to add a sign-off message to every commit. To reduce the burden,
we recommend squashing the commits in the PR as much as possible, or use the following:

You can include this automatically when you commit a change to your local git
repository using the following command:

git commit -s

Alternatively/in addition you can have a template for your commit text in a file
like git-commit-template with the DCO above, and run `git config commit.template
git-commit-template` once to have it as your default when running `git commit`.

1 change: 1 addition & 0 deletions LICENSE
2 changes: 2 additions & 0 deletions PULL_REQUEST_TEMPLATE.md
@@ -0,0 +1,2 @@
Please read the [guidelines for contribution](CONTRIBUTING.md) about signing off
Developer's Certificate of Origin.
213 changes: 213 additions & 0 deletions README.org
@@ -0,0 +1,213 @@

* GTYPE

This library is part of NUMCL.
It provides Julia-like runtime parametric type correctness in CL, based
on CLtL2 extensions.
*The main target of this library is the runtime correctness*, but it also adds the equivalent
type declarations (statically checked by SBCL).
It supports both SBCL and CCL, but CCL would not enjoy the the speed gain.

#+begin_src lisp
(defun fn (a b)
(declare (gtype (array ?t 1) a))
(declare (gtype ?t b))
(resolve
...
(values (length a) b ?t)))

(fn (make-array 5 :element-type 'single-float) 0.0) ; -> 5, 0.0, 'single-float

(fn (make-array 5 :element-type 'fixnum) 0 ) ; -> 5, 0, 'fixnum

(fn (make-array 5 :element-type 'single-float) 0 ) ;; error -- type unification fails
(fn (make-array 5 :element-type 'fixnum) 0.0) ;; error -- type unification fails

;; now the real fun starts here...

(defun gemm (a b c)
(resolve
(declare (gtype (array ?t (?n1 ?n2)) a)
(gtype (array ?t (?n2 ?n3)) b)
(gtype (array ?t (?n1 ?n3)) c))
(dotimes (i ?n1)
(dotimes (j ?n2)
(dotimes (k ?n3)
(setf (aref c i k) (* (aref a i j) (aref b j k))))))))

(let ((c (make-array '(2 2) :initial-element 0)))
(gemm #2A((0 1) (2 3))
#2A((0 1) (2 3))
c)
(print c))

;; -> #2A((2 3) (6 9))

;; see example.lisp for more usage.
#+end_src

It does not modify/overwrite the =defun= macro or other special operators and
naturally blends into the existing common lisp code. The only requirement is to
use =gtype= declarations instead of =type= declarations, and the use of
=resolve= macro which enforces the type consistency.
There is also =resolving= macro, which is just an alias.

** What it is not

Convenient optimization based on the type is not the purpose of this library. For that purpose,
combine this library with [[../../../specialized-function/][specialized-function]] (to be published).

Defining a algebraic type system is not the purpose of this library. For that purpose,
see [[http://quickdocs.org/cl-algebraic-data-type/][cl-algebraic-data-type]].

Currently this library does not have any instrument for defining a parametric structure.
For that purpose, see [[https://github.com/cosmos72/cl-parametric-types][cl-parametric-type]] at the moment. (but borrowing the idea is planned.)

** Details

=gtype= declarations also add its simplified versions as the standard =type=
declarations via =define-declaration=. Type variables such as =?t= are
automatically converted into =*=.

However, currently no Common Lisp implementations
respect the additional declarations added by =define-declaration=.
Should they do so, it will compile to the same
efficient code as those with the =type= declarations.
CLtL2 [[https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node102.html][specifically mentions]] that the consequences are undefined
when the secondary value of =define-declaration= contains any standard declaration specifier, like =type=.
Maintainers of Lisp implementations (e.g. SBCL maintainers) could interpret it as a freedom of choice.
We also ultimately hope lisp implementations to propagate this =gtype= information
outside the function boundary, allowing Lisp to have a proper parametric compile-time checking.

Even though the outer =gtype= declarations fail to make the compiler recognize the equivalent =type= declarations,
the ones *inside* the =resolving= macro are converted to the =type= declarations.
Also, the outer =gtype= declaration is inherited and recognized by the inner =resolve= macro.
In the example below, the =gtype= of =C= is matched against the =gtype= of =A= and =B=.

#+begin_src lisp
(defun fn6 (a b fn)
(resolving
(declare (gtype (array ?t ?d) a))
(declare (gtype (array ?t ?d) b))
(print (list :first ?t ?d))
(let ((c (funcall fn a b)))
(resolving
(declare (gtype (array ?t ?d) c))
(print (list :second ?t ?d))
(list a b c)))))

(print (fn6 (make-array 2 :element-type 'fixnum)
(make-array 2 :element-type 'fixnum)
(lambda (a b)
(if (zerop (random 1))
a
b))))

;; => (:FIRST FIXNUM (2))
;; => (:SECOND FIXNUM (2))
;; -> (#(0 0) #(0 0) #(0 0))

(print (fn6 (make-array 2 :element-type 'fixnum)
(make-array 2 :element-type 'fixnum)
(lambda (a b)
(declare (ignore a b))
(make-array 2 :element-type 'single-float))))

;; => (:FIRST FIXNUM (2))
;; -| error! type variable ?t fails to match, because fixnum and single-float are disjoint

#+end_src

The name =gtype= comes from generic-type.

** Background

There are several proof-of-concept libraries in CL that
tries to address some aspects of types in CL --- however, they are all subject to
the Lisp Curse of not being complete and practical.
Everyone implements half the feature they need personally, then stops there.
Importantly, none of them naturally fits into the existing code base of Common Lisp.
(This library is different, because it has a clear purpose and motivation --- implementing NUMCL.)

The first one you might have heard (and never tried yourself) is
[[http://quickdocs.org/cl-algebraic-data-type/][cl-algebraic-data-type (or cl-adt in short)]]. *It does not support parametric
types.* From the documentation, it is also not clear if you can define a list
that contains only some type (i.e. the arguments to the type specifier is
evaluated lazily). It comes with its own pattern matcher, but it is not
complete and its performance does not seem like the focus.
As a result, the library is not widely adapted.

The second one is [[https://github.com/cosmos72/cl-parametric-types][cl-parametric-type]], which provides a C++-like templates.
It can define functions/structures/classes:

#+begin_src lisp
(template (<t>)
(defun less (a b)
(declare (type <t> a b))
(< a b)))
(template (&optional (<t1> 'real) (<t2> 'real))
(defun multiply (a b)
(declare (type <t1> a)
(type <t2> b))
(* a b)))
(template (&optional (<t1> t) (<t2> t))
(defstruct pair
(first nil :type <t1>)
(second nil :type <t2>)))
(template (&optional (<t1> t) (<t2> t))
(defclass pair2 ()
(first :type <t1>)
(second :type <t2>)))
#+end_src

but unfortunately has a nonstandard calling rule that depends on macros:

#+begin_src lisp
;; i.e. instead of (MAKE-PAIR :FIRST 1 :SECOND 2) you must also specify
;; the concrete types to instantiate PAIR and MAKE-PAIR:
;;
(make-pair (bit fixnum) :first 1 :second 2)
; instantiating template-type (PAIR BIT FIXNUM) as <PAIR.BIT.FIXNUM>
#S(<PAIR.BIT.FIXNUM> :FIRST 1 :SECOND 2)

(defvar *pair* *) ;; store last result into *pair*
*PAIR*

(pair-first (bit fixnum) *pair*)
1
#+end_src

cl-parametric-type works by instantiating the variant of the function/structs etc., and
the reason for the nonstandard call syntax is that it has to
instantiate those specialized types beforehand.

** Dependencies
This library is at least tested on implementation listed below:

+ SBCL 1.5.2 on X86-64 Linux 4.4.0-146-generic (author's environment)

Also, it depends on the following libraries:

+ trivialib.type-unify :

+ trivial-cltl2 by *Tomohiro Matsuyama* :
Compatibility package exporting CLtL2 functionality
+ trivia by *Masataro Asai* :
NON-optimized pattern matcher compatible with OPTIMA, with extensible optimizer interface and clean codebase
+ alexandria by *Nikodemus Siivola <nikodemus@sb-studio.net>, and others.* :
Alexandria is a collection of portable public domain utilities.
+ iterate by ** :
Jonathan Amsterdam's iterator/gatherer/accumulator facility

** Installation


** Author, License, Copyright

Masataro Asai (guicho2.71828@gmail.com)

Licensed under LGPL v3.

Copyright (c) 2019 IBM Corporation


23 changes: 23 additions & 0 deletions asd-generator-data.asd
@@ -0,0 +1,23 @@
#|
This file is a part of gtype project.
This is a [asd-generator](https://github.com/phoe/asd-generator/) config file.
Run below for auto-regenerating the asd file:
$ ros install phoe/asd-generator
$ update-asdf
Copyright (c) 2019 Masataro Asai (guicho2.71828@gmail.com)
|#

#|
parametric types in CL, based on CLtL2 extensions
Author: Masataro Asai (guicho2.71828@gmail.com)
|#


((:dir :src
(:package)
(:rest)))

15 changes: 15 additions & 0 deletions circle.yml
@@ -0,0 +1,15 @@
machine:
environment:
PATH: ~/.roswell/bin:$PATH

dependencies:
pre:
- curl -L https://raw.githubusercontent.com/snmsts/roswell/master/scripts/install-for-ci.sh | sh
- ros install ccl-bin
cache_directories:
- ~/.roswell/

test:
override:
- ros -L sbcl-bin testscr.ros
- ros -L ccl-bin testscr.ros

0 comments on commit 82d0ab6

Please sign in to comment.