coalton-lang / coalton

Coalton is an efficient, statically typed functional programming language that supercharges Common Lisp.
https://coalton-lang.github.io/
MIT License
1.15k stars 70 forks source link

Package import, import-from and export statements #1091

Closed jbouwman closed 3 months ago

jbouwman commented 7 months ago

Extend the package form to support full package definitions.

Supported syntax:

(package <package-name>
  "documentation string"
  (import <other-package-name>
          (<other-package-name2 as <other2>)
          ...)
  (import-from <other-package-name>
          <symbol1>
          <symbol2>
          ...)
  (export <symbol3>
          <symbol4>
          ...))

Example:

(package my-package
  (import coalton-library/list)
  (export sum-even-numbers))

Import

import allows you to specify which packages a package depends on:

(package my-package
  (import coalton-library/list)
  ...)

You can use nicknames for imported packages using the as keyword:

(package my-package
  (import (coalton-library/list as list))
  ...)

Import-from

The import-from form allows you to specify which symbols from an imported package should be made available within your package:

(package my-package
  (import-from coalton-library/list
    filter)
  ...)

Export

(package my-package
  ...
  (export sum-even-numbers))
eliaslfox commented 5 months ago

I would prefer to merge asdf support for .coal files before this. Without that it's difficult to tell if the compiler is generating valid fasls that can survive restarts.

The initial Coalton prototype had a problem where restarts would clear the type database. Solving this required building the current environment system. I think similar care will be needed to ensure that packages are re-created when cached fasls are loaded.

jbouwman commented 5 months ago

I would prefer to merge asdf support for .coal files before this. Without that it's difficult to tell if the compiler is generating valid fasls that can survive restarts.

1111 contains support for coal->fasl compilation: there is an open and blocking question there over the necessity to include a new generic to supplement and possibly supersede the use of make-load-form.

eliaslfox commented 4 months ago

I'm trying this out locally and I can't get asdf to compile a file.

test.asd

(asdf:defsystem #:test
  :serial t
  :pathname "src"
  :depends-on (#:coalton #:coalton-asdf)
  :components ((:coalton-file "test.coal")))

src/test.coal

(package test)

(define x 5)
CL-USER> (asdf:load-system :test)
T
CL-USER> (in-package :test)
The name "TEST" does not designate any package.
Izaakwltn commented 4 months ago
(asdf:defsystem #:test
  :serial t
  :pathname "src"
  :defsystem-depends-on (#:coalton-asdf)
  :depends-on (#:coalton)
  :components ((:coalton-file "test")))

I think the system needs :defsystem-depends-on (#:coalton-asdf)

[Jesse - I sneakily edited izaak's comment to take out the .coal extension from the components entry and now it seems to work. It's odd to me that the :coalton-file entry was ignored in the case where :defsystem-depends-on was absent.]

eliaslfox commented 4 months ago

I think the system needs :defsystem-depends-on (#:coalton-asdf)

Thanks! I added defsystem-depends-on and got a different error.

Error while trying to load definition for system meh from
pathname /Users/ejlawsonfox/lisp/test/meh.asd:
   COMPILE-FILE-ERROR while
   compiling #<CL-SOURCE-FILE "coalton-asdf" "coalton-asdf">

 READ error during COMPILE-FILE: Symbol "COMPILE-COALTON" not found in the COALTON-IMPL/ENTRY package
jbouwman commented 4 months ago

READ error during COMPILE-FILE: Symbol "COMPILE-COALTON" not found in the COALTON-IMPL/ENTRY package

I wonder if it's looking at a different coalton system, that symbol seems to be present and exported.

eliaslfox commented 4 months ago

I wonder if it's looking at a different coalton system, that symbol seems to be present and exported.

Good catch. My lisp environment was screwed up.

The system is compiling now but it doesn't look like the compiled code is being run. I get compile errors, but I still can't in-package.

Izaakwltn commented 4 months ago

The system is compiling now but it doesn't look like the compiled code is being run. I get compile errors, but I still can't in-package.

I just tried and it's working for me:

asd-test.asd

(asdf:defsystem #:asd-test
  :serial t
  :defsystem-depends-on (#:coalton-asdf)
  :depends-on (#:coalton)
  :pathname "src"
  :components ((:coalton-file "test")))

src/test.coal

(package test)

(define x 5)

In the REPL

CL-USER> (asdf:load-system :asd-test)
STYLE-WARNING: Generic function FSET:ITERATOR clobbers an earlier FTYPE proclamation (FUNCTION (T &KEY &ALLOW-OTHER-KEYS) (VALUES FUNCTION &REST T)) for the same name with (FUNCTION (T &KEY &ALLOW-OTHER-KEYS) *).
T
CL-USER> (in-package :test)
#<COMMON-LISP:PACKAGE "TEST">
TEST> x
5
eliaslfox commented 4 months ago

That works for me. I was doing something else wrong :/

I think I ran into an actual issue. The fasl files produced don't appear to recreate their packages at load time.

(asdf:load-system :asd-test)
;; restart inferior lisp
(asdf:load-system :asd-test)

I got the following error:

The loader tried loading the symbol named X into the package named TEST, but the package did not get defined, and does not exist.
stylewarning commented 4 months ago

(Side comment: Can we test that ASDF corner cases work or fail appropriately? Like bad extension, missing file, etc. In a separate PR maybe.)

jbouwman commented 4 months ago

The loader tried loading the symbol named X into the package named TEST, but the package did not get defined, and

Aha, it wasn't emitting the defpackage form: I've restored that -- https://github.com/coalton-lang/coalton/pull/1091/files#diff-dbe076ada6d143bc146af69913f741b08735798471bd7bc03e0ca4a2dd7d089fR240 -- followed your steps, and it now appears to work.

jbouwman commented 4 months ago

(Side comment: Can we test that ASDF corner cases work or fail appropriately? Like bad extension, missing file, etc. In a separate PR maybe.)

https://github.com/coalton-lang/coalton/pull/1091/files#diff-dbe076ada6d143bc146af69913f741b08735798471bd7bc03e0ca4a2dd7d089fR240 might be a good place to add those

Also, touching each error case for invalid package forms.

jbouwman commented 4 months ago

Outcome of steering meeting:

jbouwman commented 3 months ago

@stylewarning @eliaslfox guidance on this changeset?