Skip to content
Matt Bierner edited this page Apr 17, 2014 · 4 revisions

Khepri introduces a standardized syntax for packages. Code for different package management systems may be generated from the base package syntax.

Package Declaration

A program may either be a regular program or a package. Packages are simple declared:

package (EXPORTS) BODY

Exports

EXPORTS is a list of exported symbols defined in the package body.

A math package, exporting two symbols min and max:

package (min max)
{
    min := \x y -> ?x < y : x : y;
    max := \x y -> ?x < y : y : x;
}

The exported symbol name is the name other packages use to access the symbol. Order of exports does not matter, but the exports must be unique.

Exports create local variables for that export in the package body. The value for the exported symbol is taken from the symbol's value at the end of the package body. Package exports start as mutable bindings. The := assignment operator may be used to mark an export immutable after the assignment.

Export Aliases

Sometimes you need to use a different internal name for an exported symbol. Export aliases allow this:

package (
    'exported_name': internal_name)
{
    internal_name = ...;
}

The alias may be any string, and regular and aliased exports can be mixed:

package (
    '+': plus
     min)
{...}

Module Exports

Instead of exporting a set of symbols, you can specify the exported object itself:

package my_exports {
my_exports = {
    'a': 3,
    ...
};
} 

This allows exporting custom object types:

package my_exports {
my_exports = function \x -> x;
} 

Imports

The body of a package must be either a block statement or a with statement. With statements allows other packages to be imported.

package () with
    import 'lib/math' math#{min, max},
    import 'lib/str' {match, search}
{
}

Inside the package body, math is bound the entire math package. min and max are bound to the min and max exports of the math package. Because an as unpack is used, we can also write math.min or math.abs to access members. str on the other hand does not name its package but imports two symbols, match and search. The package cannot be directly accessed but these two values can be.

Imported names are immutable bindings in the package body. All packages reserve the names require, exports, and module as immutable bindings before any of the package is evaluated. All packages also use strict mode.

Package Names and Import Paths

TODO