curry.toolchain.flat2icurry package

The translation from FlatCurry to ICurry.

This is a port of the Curry package icurry 3.1.0 by Michael Hanus. It reads the FlatCurry of a module and the FlatCurry interfaces of its imports, and it produces the ICurry term that icurry writes to a .icy file. The passes run in this order:

  1. The rewrite step of the routes (rewrite_prog()), when the caller asks for it: the binding optimization (bindingopt; the Boolean equalities whose value is required to be True become equational constraints, as the preprocessing of PAKCS makes them) and the set0 lift (setlift; set0 e over the captured variables of e becomes setk L xs). The routes from Curry to ICurry do not ask for it here: they apply it to the FlatCurry file itself before the translation (rewrite_file()), as PAKCS applies its preprocessing, so the file on disk is the rewritten program and the icurry program sees it too. The flag serves a caller that must not touch the file, such as the option --rewrite of the command line. The oracle tests do not ask for it. The command line of rewrite applies the step to files by hand.

  2. Newtype elimination (elimnewtype).

  3. Case completion against the data declarations of the module and its imports (casecompletion).

  4. Lifting of nested cases, lets, and free declarations into new functions (caselifting).

  5. The name-to-index maps and the ICurry generation (compiler).

showterm() prints the result as the PAKCS showTerm does, so the text is byte-identical to the output of icurry under PAKCS. write_flatcurry() writes a FlatCurry program as the front end does.

One defect of icurry 3.1.0 is kept behind the flag icurry_compat, which is on by default: a type annotation at the root of a rule hides a case, a let, or a free declaration from the block builder. See compiler.to_iblock(). The oracle tests keep the flag on, so the output stays byte-identical to the files icurry wrote. The build route (curry.toolchain._frontend) turns it off, so the programs it compiles run.

exception curry.toolchain.flat2icurry.Flat2ICurryError[source]

Bases: CompileError

An error in the translation from FlatCurry to ICurry.

class curry.toolchain.flat2icurry.InterfaceFinder(searchdirs, subdirs, progs=())[source]

Bases: object

Looks up the interface of a module by name.

Parameters:
  • searchdirs – The directories to search, as a Curry path.

  • subdirs – The names of the subdirectories of .curry that hold the front end output, e.g., ['pakcs-3.4.1'].

  • progs – Programs already in hand, used before any file is read.

candidates(modname)[source]
filename(modname)[source]

The file that holds the interface of modname.

find(modname)[source]

The FlatCurry interface of modname.

curry.toolchain.flat2icurry.load_flatcurry(filename)

Reads a FlatCurry program (or interface) from a file.

curry.toolchain.flat2icurry.load_interface(filename)[source]

Reads a FlatCurry file. Files are cached by name, size, and time.

curry.toolchain.flat2icurry.module_root(fcyfile, modname)[source]

The directory from which the module modname was compiled, given its FlatCurry file. The file of module A.B lies at <root>/.curry/<subdir>/A/B.fcy or at <root>/A/.curry/<subdir>/B.fcy.

curry.toolchain.flat2icurry.optimize_bindings(prog, equivalence=True)[source]

Applies the binding optimization to a FlatCurry program. Returns the transformed program when an equality was replaced, else prog itself: transbooleq writes a file only then, so a rule whose equalities all stay is rebuilt (its $ reduced) only in a program that is transformed.

curry.toolchain.flat2icurry.optimize_file(fcyfile, equivalence=True)[source]

Applies the binding optimization to the FlatCurry file fcyfile in place. The file is written again, in the format of the front end, when the pass replaced an equality; else it is left as it is, with its time. Returns the number of equalities replaced.

curry.toolchain.flat2icurry.optimize_set0(prog)[source]

Applies the set0 lift to a FlatCurry program. Returns the transformed program when a call was lifted, else prog itself.

curry.toolchain.flat2icurry.product_path(filename)[source]

Splits the path of a front-end product into (root, subdir, tail). The file lies at <root>/.curry/<subdir>/<tail>/M.ext; tail is empty for a module at the top level and A/B for a module under A.B. Returns None when no .curry directory encloses the file.

curry.toolchain.flat2icurry.read_flatcurry(text)

Reads a FlatCurry program (or interface) from .fcy text.

curry.toolchain.flat2icurry.rewrite_file(fcyfile, equivalence=True)[source]

Applies the rewrite step to the FlatCurry file fcyfile in place, as the routes do after the front end wrote it. The file is written again, in the format of the front end, when a pass changed the program; else it is left as it is, with its time. Returns the pair of counts of rewrite_prog().

curry.toolchain.flat2icurry.rewrite_prog(prog, equivalence=True)[source]

Applies the rewrite step of the routes to a FlatCurry program in memory: the binding optimization, then the set0 lift. Returns the program and the pair of counts (equalities replaced, set0 calls lifted); the program is prog itself when both counts are zero. The result of the binding optimization counts only when it replaced an equality, as bindingopt.optimize_bindings() has it (a rule it rebuilt without a replacement, its $ reduced, is not handed on), so the lift runs over the program the former route translated. Without equivalence the binding optimization replaces === and /== alone (see bindingopt.transform_prog()).

curry.toolchain.flat2icurry.show_flatcurry(prog)

The text of a FlatCurry program (or interface) as the front end writes it: the show of Haskell, on one line, without a newline at the end.

curry.toolchain.flat2icurry.showhaskell(term)[source]

Prints a term as the show of Haskell does with the derived Show instances: the format of the FlatCurry files of the Curry front end. A constructor with arguments is in parentheses when it is an argument; a negative number is in parentheses when it is an argument; the elements of a list and of a tuple are not. There is no newline.

curry.toolchain.flat2icurry.showterm(term)[source]

Prints a term as the PAKCS showTerm does. There is no newline.

curry.toolchain.flat2icurry.translate(prog, interfaces, icurry_compat=True, rewrite=False)[source]

Translates a FlatCurry program to ICurry.

Parameters:
  • prog – The FlatCurry program.

  • interfaces – The FlatCurry interfaces of the imports of prog, in import order.

  • icurry_compat – Whether to keep the output of icurry 3.1.0 for a type annotation at the root of a rule. See compiler.to_iblock().

  • rewrite – Whether to apply the rewrite step of the routes first, the binding optimization and the set0 lift. See rewrite_prog().

Returns:

The ICurry program, an IProg term.

curry.toolchain.flat2icurry.translate_file(fcyfile, finder, icurry_compat=True, rewrite=False)[source]

Translates the FlatCurry file fcyfile. finder is an InterfaceFinder that supplies the interfaces of the imports.

curry.toolchain.flat2icurry.write_flatcurry(prog, filename)

Writes a FlatCurry program to a file in the format of the front end. The text goes to a file beside the target first, which then takes the place of the target, so a reader never sees a partial file.

curry.toolchain.flat2icurry.write_icurry(iprog, filename)[source]

Writes an ICurry program to a .icy file.

Submodules