-
Notifications
You must be signed in to change notification settings - Fork 77
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
Module system for Catala #88
Comments
A dump from a Zulip discussion : @AltGr says:
I reply:
|
Thanks for the details! One more thing pops to mind: does that mean that being in a module is actually optional, so you have separate files starting with (it's just a detail actually, internally it makes no difference to have no module or an implicit "Toplevel" module ; but I feel that things get perceived a bit differently from an interface point of view) |
Hum good point. Yes we can make module declaration optional, but the implementation in the compiler should have a special |
The problem
Right now, the only abstraction mechanism in Catala are the scopes, which correspond roughly to functions. However, as programs get bigger and bigger, we will want to group functions into modules in order to split and compartmentalize implementations.
Even though it is possible to split an implementation across multiple files currently, this can only be done via a primitive textual include mechanism that should ultimately be complemented by a proper module system.
Design of a simple module system
Syntax
This proposal is heavily inspired from the F* and Rust module system. Each file is its own module. The name of the module is introduced at the top of the file with the syntax (taking #84 into account):
> Module Foo
The name of the file must correspond to the ASCII snake case version of the module name.
Scopes and types declared in a module can be accessed without prefix in the same module. To use types and scopes declared and defined in other module
Bar
, insert at the top of the file:> Using Bar
Then simply refer to
Bar
's scopes and types withBar.<name of type/scope>
. Important: it is impossible to add rules and definitions toBar
insideFoo
via ascope Bar: ...
.Prefixes can be omitted for certain modules with the mention
> Using Bar implicit
For scopes or types with names defined in two distinct
implicit
modules, name resolution selects the one corresponding to the lastimplicit
when traversing the file top to bottom.Local aliases can be defined for modules with
> Using Bar as B
External module lookup.
When invoking the compiler with
catala bar.catala_en
, the compiler retrieves the list of the names external modules used inbar.catala_en
and proceeds to find.catala_en
files corresponding to those module names in the current directory. Additional lookup directories can be added with the-I
option.Compilation units
For the OCaml backend and future backends, each Catala module should be translated to a different target language file. Hence, the
-o
option should take an output directory instead of file and the generated files names should be the same as the source files, but with a different extension.Literate programming
When invoking Catala to produce a literate programming output from a particular file, the output should only contain that single file and not the modules on which this file depends. To produce a literate programming output containing multiple files, one can uss the master file and dependencies list syntax like
catala/examples/us_tax_code/us_tax_code.catala_en
Lines 1 to 9 in 9282e5e
Implementation
The implementation of this module system is going to vastly affect the compiler code. First, programs in the different intermediate representations should be represented as a list of modules (each of them containing types and scopes). The list should be ordered according to the topological order of the dependency graph between modules, which will have to be constructed. Cyclic dependencies between modules are forbidden. The passage to a collections of modules to an ordered list can be implemented in the
surface -> desugared
translation.When parsing expressions like
<expression>.x.y
that are transformed intoDotted
catala/src/catala/surface/ast.mli
Lines 151 to 152 in 9282e5e
the name resolution of
x
andy
should be resolved in that order:Dotted
qualifier that can be a scope or a type belonging to that moduleDotted
qualifier containing a field name belonging to that structBefore starting this implementation, fixing #47 seems to be a good pre-requisite as we don't want to bother with ordering the types and scopes declaration correctly as long as they are cycle-free.
The text was updated successfully, but these errors were encountered: