KMCC: The Kiel Monadic Curry Compiler

Description

KMCC is an implementation of the multi-paradigm declarative language Curry. KMCC compiles Curry programs into Haskell programs, i.e., it uses the Glasgow Haskell Compiler as its back end. Similarly to many other implementations of Curry, the distribution contains an interactive environment (read/eval/print loop) to ease the development of Curry applications.

The current implementation includes all the essential features of Curry, like non-deterministic operations, logic variables and unification, encapsulated search, as well as advanced features like functional patterns and various search strategies. The default strategy is a fair search strategy which performs parallel evaluations in a multi-processor environment. In particular, the strategy is operationally complete, i.e., it always computes a value if it exists according to the declarative interpretation of the program. This contrasts KMCC with Prolog's backtracking search strategy and and other sequential Curry implementations, like PAKCS or KiCS2. For instance, KMCC computes a value to the following non-deterministic choice between three expressions, where the leftmost and rightmost are non-terminating:

    KMCC Interactive Environment ...
    Prelude> length [1..] ? 42 ? length [1..]
    42

Another feature of KMCC is unification of arithmetic expressions involving free variables:

    Prelude> 3+x =:= 5 where x free
    { x = 2 } True
    Prelude> 3*x =:= 4.8 where x free
    { x = 1.5999999999999999 } True

Such arithmetic constraints are solved by sending them to the SMT solver Z3, i.e., it is necessary that the executable z3 is in the PATH in order to use this feature. Thus, the solving of arithmetic constraints is not complete but limited to the features of Z3.

Libraries, packages, and tools

The KMCC distribution comes with a collection of base libraries that are useful for basic application programming. A documentation of these libraries can be found here.

The distribution also contains the Curry Package Manager CPM that supports the easy installation of many further libraries and tools. There are more than 100 packages available for installation with KMCC.

Installation

Since KMCC compiles to Haskell, a Haskell implementation is required to install KMCC. An appropriate Haskell compiler is downloaded by Haskell Stack during the installation process. Thus, this build tool is required to be installed on your system. For instance, if you run Ubuntu Linux, you can easily install this tool by

  sudo apt-get install haskell-stack

Now you can install KMCC by unpacking a distribution file and running make:

  tar xvzf kmcc-<version>.tar.gz
  cd kmcc-<version>
  make

The installation takes some time since the compiler, an interactive REPL, and the Curry Package Manager are installed so that one can directly use various Curry packages. After the installation, add the subdirectory .../kmcc-<version>/bin to your path so that you can easily start the KMCC REPL by the command

    > kmcc

If you do not use packages but only the base libraries, add the option -n or --nocypm for faster startup:

    > kmcc -n

If you have already another Curry system and the Curry Package Manager installed, you can also use it to install the KMCC system from the source repository. More information can be found in the detailed installation instructions.

Usage

KMCC can be used similarly to other Curry systems, like PAKCS or KiCS2. After starting the KMCC interactive environment by the command

    > kmcc

the standard prelude is loaded so that one can evaluate expressions. For this purpose, KMCC compiles the current program together with the main expression into an executable and invokes it. Although this is silently done (unless you increase the verbosity to 2 or higher by :set v2), you will notice this process since it takes a few seconds. Actual timings for the compiler and expression evaluation can be shown by setting the timing flag with :set +time.

Programs, i.e., Curry modules, are compiled and loaded by the command

    Prelude> :load ModuleName

Note that the generated intermediate and target Haskell programs are stored relative to the current directory in .curry/kmcc-<version>. This means that one needs read/write access to the current directory.

The interactive KMCC environment supports the direct evaluation of expressions:

    Prelude> 3*4.5
    13.5
    Prelude> "Hello " ++ "World!"
    "Hello World!"

If the initial expression contains free variables, their bindings are shown together with the computed result (if the option bindings is not turned off):

    Prelude> not b where b free
    {b=False} True
    {b=True} False
Why does it take so long to evaluate a simple expression?

You might wonder why it takes a few seconds to start the evaluation of a simple expression like 3*14. The reason is that the REPL is implemented on top of the KMCC compiler, i.e., each initial expression is written into a "main" module which is then transformed into Haskell code by the KMCC compiler (which also requires the reading of the prelude) and into a binary (executable) by the Haskell compiler. Then this binary is invoked to perform the actual evaluation. If you want to see what is going on and where the time is spent, you can increase the verbosity and turn on the option to show the elapsed time during compilation:

    Prelude> :set +time
    Prelude> :set v2
    Prelude> 3*14
    ...

Documentation

The main ideas behind the implementation of KMCC are described in

  • Hanus, M., Prott, K.-O., Teegen, F.: A Monadic Implementation of Functional Logic Programs.
    Proc. of the 24th International Symposium on Principles and Practice of Declarative Programming (PPDP 2022), ACM Press, pp. 1:1-1:15, 2024
    DOI: 10.1145/3551357.3551370 (conference version)
  • Hanus, M., Prott, K.-O., Teegen, F.: A Monadic Implementation of Functional Logic Programs.
    Theory and Practice of Logic Programming, 2026
    DOI: 10.1017/S1471068426100453 (long journal version)

KMCC is implemented by Kai-Oliver Prott so that you can also contact him for specific questions.