-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add define-type-alias
#1294
base: main
Are you sure you want to change the base?
Add define-type-alias
#1294
Conversation
I think this PR should be blocked on a complete and tested implementation. I am sympathetic to incomplete library additions, but language features need to be complete and bullet-proof. |
I think it would be a good idea to add some documentation in these files:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some initial thoughts. Not a full review.
COALTON-USER> (type-of 'shifted-coordinate) | ||
(TUPLE INTEGER INTEGER) | ||
|
||
COALTON-USER> (describe-type-of 'shifted-coordinate) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these functions and their output may need some workshopping.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any feedback as far as what is good/bad about them?
@YarinHeffes Can you update the PR description with an up-to-date account of the PR? |
"remove unnecessary parentheses"))) | ||
|
||
;; (define-type-alias (name type-variables+) ...) | ||
(loop :for vars := (cst:rest (cst:second form)) :then (cst:rest vars) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check that the tyvars are unique
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This takes place during the type checking phase, similar to define-struct
and define-type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/typechecker/define-type.lisp
Outdated
(let* ((defined-variables (mapcar #'parser:keyword-src-name (parser:type-definition-vars parsed-type))) | ||
(used-variables (mapcar #'tc:tyvar-id (tc:type-variables aliased-type))) | ||
(unused-variables (loop :for tyvar :in defined-variables | ||
:when (not (member (tc:tyvar-id (partial-type-env-lookup-var partial-env tyvar parsed-type)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slightly shorted code: when not
=> unless
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed in 8619ceb
src/typechecker/define-type.lisp
Outdated
(tc-note parsed-type "Type alias ~S defines unused type variable~P ~{:~A~^ ~}" | ||
(parser:identifier-src-name (parser:type-definition-name parsed-type)) | ||
number-of-unused-variables | ||
(mapcar (lambda (str) (subseq str 0 (- (length str) (1+ (position #\- (reverse str)))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(position a b :from-end t)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed in 8619ceb
src/typechecker/define-type.lisp
Outdated
@@ -420,6 +465,12 @@ | |||
`(member ,@(mapcar #'tc:constructor-entry-compressed-repr ctors))) | |||
(t | |||
name)) | |||
:aliased-type (let ((parser-aliased-type | |||
(parser:type-definition-aliased-type type))) | |||
(when parser-aliased-type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer if
if the value is load-bearing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed in 8619ceb
@@ -69,6 +70,12 @@ | |||
(predicates (util:required 'predicates) :type ty-predicate-list :read-only t) | |||
(type (util:required 'type) :type ty :read-only t)) | |||
|
|||
(defun qualified-ty= (qualified-ty1 qualified-ty2) | |||
(and (equalp (qualified-ty-predicates qualified-ty1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the predicates have different orders? is that possible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would need to look more into this. However, we were previously using equalp
for equality, so this change should not break anything/change any pre-existing behavior.
@@ -71,7 +74,8 @@ | |||
;;; Types | |||
;;; | |||
|
|||
(defstruct (ty (:constructor nil))) | |||
(defstruct (ty (:constructor nil)) | |||
(alias nil :type (or null ty-list) :read-only nil)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Describe in a comment what this field is and when it shoes up. Give an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe a comment that this could be a weak hash table
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed in 8619ceb
src/typechecker/types.lisp
Outdated
(tgen-id type2))) | ||
|
||
(:method (type1 type2) | ||
(declare (ignore type1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ignore can have multiple entries: (declare (ignore a b))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed in 8619ceb
(defun coalton:describe-type-of (symbol) | ||
"Print the type of value SYMBOL along with its type aliases and return it" | ||
(let ((tc:*pprint-type-aliases* t) | ||
(type (tc:lookup-value-type entry:*global-environment* symbol))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like these are inconsistent with how it prints aliases that are imported from other packages.
If you :use a package then this will print the alias without the package. If you don't :use a package, but do something like this:
(declare adds (aliases2:Int -> Integer -> Integer))
then you will get an error message that does indeed print the package:
--> <macroexpansion>:1:15
|
1 | (COALTON (ADDS "hi" 2))
| ^^^^ Expected type '[ALIASES2:INT := INTEGER]' but got 'STRING'
I think, since this is for debugging purposes, it would be helpful to always print the package that a type is from unless it is from the current package or coalton-prelude. Maybe any of the library functions.
Here is another example. In this error message, we don't :use
the package. In the error message, the alias is printed with one : but the type it refers to (from the same package) is printed with two ::'s:
"aliases2"
(in-package :cl-user)
(defpackage :aliases2
(:use
#:coalton
#:coalton-prelude)
(:export
:Int
:Ham
:Swiss
:Cheese))
(in-package :aliases2)
(coalton-toplevel
(define-type MoldyMilk Ham Swiss)
(define-type-alias Cheese MoldyMilk))
(coalton-toplevel
(define-type-alias Int Integer))
aliases (main package)
(in-package :cl-user)
(defpackage :aliases
(:use
#:coalton
#:coalton-prelude))
;; #:aliases2)) <---- not using the other package
(in-package :aliases)
(coalton-toplevel
(declare value-of-cheese (aliases2:Cheese -> Integer))
(define (value-of-cheese chs)
(match chs
((aliases2:Ham) 10)
((aliases2:Swiss) 20))))
(coalton (value-of-cheese "not cheese"))
error: Type mismatch
--> <macroexpansion>:1:26
|
1 | (COALTON (VALUE-OF-CHEESE "not cheese"))
| ^^^^^^^^^^^^ Expected type '[ALIASES2:CHEESE := ALIASES2::MOLDYMILK]' but got 'STRING'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will take a closer look at the behavior of describe-type-of
. But regarding your examples, I believe those are all expected behaviors.
First, the use of :
versus ::
depends on whether is a symbol is exported. If a package p
exports a symbol s
, and you do not want to :use
package p
, then you can use the symbol with p:s
. If p
does not export s
, then you can still use it with p::s
. In your example, aliases2
exports cheese
but not moldymilk
.
Second, when package p
:uses
package q
, the symbols from q
receive the same treatment locally, in p
, as any other symbol defined in p
, so my personal opinion is that it is not inconsistent for those types aliases to be printed without the q:
prefix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense 👍
@@ -0,0 +1,142 @@ | |||
================================================================================ | |||
1 Define type alias |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should add tests around package boundaries. Permutations that come to mind:
- use an alias for a standard type from another package that has been imported with use
- Reference an alias for a standard type from another package without
:use
- Both of the above to a type that is from the same package but was not exported.
- Both of the above, but the alias in package 2 is to a type from a third package which is not
:use
d in the main package
Testing uninterning aliases, if you:
B will still continue to function. You can use it to define new type signatures - works just fine. The error message you get looks like this: Note the
This seems like reasonable behavior, so much as anything would be. This (hopefully) won't come up much, but it's worth verifying that it doesn't crash everything if it does. Here is some code to play around with this: (coalton-toplevel
;; (define-type-alias A Integer)
;; (define-type-alias B A)
;; (declare a-add (A -> A -> Integer))
;; (define a-add +)
)
(coalton-toplevel
(declare bad-add (B -> B -> Integer))
(define (bad-add b1 b2) (+ b1 b2)))
(coalton (bad-add 1 2))
(cl:unintern 'A) |
This is an implementation of type aliases, including:
simple aliases:
(define-type-alias Coordinate Integer)
aliases of aliases:
and readable printing in error messages.
The implementation also supports parametric type aliases
Currently, since
the
does not change types except by substituting type-variables,will compile, but it will not store the alias
Coordinate
for the variablex
, and it will not display in error messages.The functions
describe-type-alias
anddescribe-type-of
are implemented to display the definition of a type alias and the aliases of a defined symbol, respectively.