Sprite

Sprite is an implementation of the multi-paradigm programming language Curry, embedded in Python. It seamlessly integrates features of functional programming and logic programming into your Python project. These features include algebraic data types, pattern matching, strong typing, type inference, lazy evaluation, nondeterminism, logic variables, and search. Curry follows Haskell’s syntax and adds its own semantics. Sprite’s only dependency is one small package, the Curry front end.

The functional side rests on graph rewriting, the well-studied evaluation model behind lazy languages such as Haskell. Here, a program is a set of rewrite rules, applied lazily and only as far as a result demands. The logic side rests on nondeterminism, which lets an expression have more than one value. Here, you state what a desired value has to satisfy and leave the search to the runtime. Many programs then read as their own specification, which keeps them simple, short, and obviously correct. Sprite’s evaluation strategy, the Fair Scheme, is proved sound, complete, and optimal. This means it computes all and only the values of any source expression, and every step it takes is one that any strategy must take. The strategy and its proofs are written up in a dissertation and a shorter paper.

Write the part of your project that is naturally a search or a set of rules as a small Curry module, then import it. The first import translates the module and caches the result beside the source, without a C++ compiler. For speed, the same code compiles to native machine code, either just in time as the program runs (the jit extra) or ahead of time into an object you can embed in an application.

Install it with pip install --pre curry. Try it and tell us what breaks on the issue tracker.

Getting started

Here is a Curry module for Boolean formulas:

-- Logic.curry
data Formula = T | F | Not Formula | And Formula Formula | Or Formula Formula

render :: Formula -> String
render T = "T"
render F = "F"
render (Not p) = "!" ++ render p
render (And p q) = "(" ++ render p ++ " & " ++ render q ++ ")"
render (Or p q) = "(" ++ render p ++ " | " ++ render q ++ ")"

The module declares a data type, Formula, with five constructors, and a function, render, over that type. This Curry snippet is also valid Haskell. Save it next to your Python code, then import it, construct a formula, and render it as a string:

import curry
curry.path.insert(0, '.')          # Curry's sys.path
from curry.lib import Logic as L   # compiles and imports ./Logic.curry

formula = L.And(L.Or(L.T, L.F), L.Not(L.F))
goal = L.render(formula)
print(curry.eval(goal).one())      # the one value: ((T | F) & !F)

curry.lib is a virtual package. It holds no modules of its own; an import from it finds the Curry module of that name on curry.path and loads it into Python. The constructors of Formula and the render function appear as Python objects. L.render(formula) builds an expression without running it; curry.eval evaluates it, and Sprite converts the result to a Python string.

Where to go next

  • Installation installs Sprite with pip, or from source.

  • The Quickstart takes the next steps. The Examples are the tutorial: twenty-six programs, from a first Curry program to applications.

  • The Curry Primer teaches the language to a Python programmer.

  • Status says what Sprite is today.

For Curry researchers: Sprite uses the Curry front end of PAKCS and ships the base library of PAKCS 3.10.0, so most programs written for PAKCS run unchanged; sprite-exec runs a program, the REPL opens a prompt and Status lists what differs. The Curry homepage has the tutorial and the report of the language, and the live demo runs Curry in a browser.