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

(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

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.

3 participants