mighty-gerbils / gerbil

Gerbil Scheme
https://cons.io
GNU Lesser General Public License v2.1
1.16k stars 112 forks source link

Contracts and Type Annotations #912

Open vyzo opened 1 year ago

vyzo commented 1 year ago

Following some prototype work some years ago in #417, I have reached some conclusions about the functionality that we want.

vyzo commented 1 year ago

A better syntax proposal that doesn't use keywords:

So here is a definition for a procedure that mixes all of them:

(def (do-something (a := A?) (b :- fixnum?)) :> fixnum?
  ... ; body
)

This could also be attached to an interface:

(interface X
  (method-a (a := A?) (b := fixnum?)) :> fixnum?
  ...)

And method can simply make type assertions because the interface facade has already checked the contracts:

(defmethod {X x-class}
  (lambda (self (a :- A?) (b :- fixnum?)) :> fixnum?
   ...))
vyzo commented 1 year ago

We can also extend struct and class definitions to add type annotations to members:

(defstruct X ((a :- fixnum?) ...) ...)
(defclass X ((a :- fixnum?) ...) ...)
fare commented 1 year ago

: or :: is a widely accepted standard for type annotations (including return type annotations), and I see no reason to depart from it. For other annotations, sure do our own things.

fare commented 1 year ago

Don't we need a regular prefix syntax, too? Or are we becoming Rhombus? Better read the Rhombus paper, then... is it out?

vyzo commented 1 year ago

Yes, the prefix syntax will be raw annotation: (begin-annotation (type ...) expr).

The :std/contract macros will expand to that.

vyzo commented 1 year ago

ok, fair enough, we can us :: for return type.

vyzo commented 1 year ago

although... I don't want a keyword really, I want a syntactic token.

vyzo commented 1 year ago

: is unclear -- is it contract or type assertion? We have both, hence the discriminant with :- and :=.

vyzo commented 1 year ago

Return type annotation can also use :- and := -- Semantics:

vyzo commented 1 year ago

Note that this opens the door for the inevitable dependent types further down the road.

vyzo commented 1 year ago

Following discussion with fare, we have reached consensus, although we are not sure about the exact assertion operator name.

The current proposal:

vyzo commented 1 year ago

Other possible symbols for type assertions (@fare wants to uglify them, I want to keep them tidy):

vyzo commented 1 year ago

Preliminaries in #934; here is the syntactic tokens we settled on:

vyzo commented 6 months ago

this is mostly done with types gerbil, awaiting the v0.18.2 release.