Skip to content
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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open

Conversation

YarinHeffes
Copy link
Collaborator

@YarinHeffes YarinHeffes commented Oct 8, 2024

This is an implementation of type aliases, including:

simple aliases:

(define-type-alias Coordinate Integer)

aliases of aliases:

(define-type-alias Point (Tuple Coordinate Coordinate))
(define-type-alias Translation (Point -> Point))

and readable printing in error messages.

[Point := (Tuple [Coordinate := Integer] [Coordinate := Integer])]

The implementation also supports parametric type aliases

(define-type-alias (UnaryOperator :a) (:a -> :a))
(define-type-alias UnaryIntegerOperator (UnaryOperator Integer))
---
[UnaryIntegerOperator := (UnaryOperator Integer) := (Integer -> Integer)]

Currently, since the does not change types except by substituting type-variables,

(define x (the Coordinate (the Integer 5)))

will compile, but it will not store the alias Coordinate for the variable x, and it will not display in error messages.

The functions describe-type-alias and describe-type-of are implemented to display the definition of a type alias and the aliases of a defined symbol, respectively.

@stylewarning
Copy link
Member

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.

@YarinHeffes YarinHeffes marked this pull request as draft October 10, 2024 20:12
@Izaakwltn
Copy link
Collaborator

I think it would be a good idea to add some documentation in these files:

  • docs/intro-to-coalton.md
  • docs/coalton-documentation-guide.md
  • maybe in docs/coalton-lisp-interop.md, if there are any promises (or lack thereof) about lisp alias behavior.

@YarinHeffes YarinHeffes marked this pull request as ready for review October 16, 2024 21:58
Copy link
Member

@stylewarning stylewarning left a 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.

docs/coalton-documentation-guide.md Outdated Show resolved Hide resolved
docs/coalton-documentation-guide.md Outdated Show resolved Hide resolved
src/faux-macros.lisp Outdated Show resolved Hide resolved
COALTON-USER> (type-of 'shifted-coordinate)
(TUPLE INTEGER INTEGER)

COALTON-USER> (describe-type-of 'shifted-coordinate)
Copy link
Member

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.

Copy link
Collaborator Author

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?

src/parser/toplevel.lisp Outdated Show resolved Hide resolved
src/typechecker/base.lisp Outdated Show resolved Hide resolved
@stylewarning
Copy link
Member

@YarinHeffes Can you update the PR description with an up-to-date account of the PR?

@YarinHeffes YarinHeffes changed the title adding define-alias adding define-type-alias Oct 17, 2024
@YarinHeffes YarinHeffes changed the title adding define-type-alias Add define-type-alias Oct 17, 2024
"remove unnecessary parentheses")))

;; (define-type-alias (name type-variables+) ...)
(loop :for vars := (cst:rest (cst:second form)) :then (cst:rest vars)
Copy link
Member

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

Copy link
Collaborator Author

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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))
Copy link
Member

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 8619ceb

(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))))))
Copy link
Member

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)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 8619ceb

@@ -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
Copy link
Member

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

Copy link
Collaborator Author

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)
Copy link
Member

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?

Copy link
Collaborator Author

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))
Copy link
Member

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.

Copy link
Member

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 8619ceb

(tgen-id type2)))

(:method (type1 type2)
(declare (ignore type1)
Copy link
Member

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))

Copy link
Collaborator Author

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)))
Copy link

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'

Copy link
Collaborator Author

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.

Copy link

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
Copy link

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 :used in the main package

@Jason94
Copy link

Jason94 commented Dec 10, 2024

Testing uninterning aliases, if you:

  1. Point alias A -> a type, and point alias B -> alias A
  2. then unintern A

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 #:A instead of A:

error: Type mismatch
  --> <macroexpansion>:1:18
   |
 1 |  (COALTON (BAD-ADD "hi" 2))
   |                    ^^^^ Expected type '[B := #:A := INTEGER]' but got 'STRING'

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants