
Many problems are easier to state as rules than to program as steps: a grammar, a set of constraints, a build system. Sprite lets a Python developer state those rules in Curry, a functional logic language, and call them from Python. You say what a result must satisfy, and the runtime finds every result that does.
Sprite integrates Curry seamlessly with Python. Write the part of your project
that is naturally a search or a set of rules as a small Curry module, then import
it. Curry follows Haskell's syntax and adds nondeterminism and logic variables.
An expression can then have several values, and a rule can state a condition that
those values must meet. Sprite compiles functions to native code when a C++
compiler or the jit extra is available, and it interprets them otherwise. Its
only dependency is the Curry front end.
Install it with pip, try the example below, and tell us what breaks:
pip install --pre curryHere 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.
The Python examples below continue this one.
Functional patterns, the next step, depend on nondeterminism: an expression can
have any number of values, zero or more. The smallest source of several values
is the choice operator ? of the Prelude, defined by two overlapping rules:
(?) :: a -> a -> a
x ? _ = x
_ ? y = yBoth rules apply, so 1 ? 2 has two values, 1 and 2. curry.parse builds
an expression from Curry text, and curry.eval returns its values:
values = curry.eval(curry.parse('1 ? 2')) # nothing runs yet
print(sorted(values)) # [1, 2]curry.eval returns an iterator over the values. Call one when you expect
exactly one value. It raises an error when there is no value or more than one.
Use next to take the first of several.
A functional pattern uses this idea inside a rule. It is a pattern that contains
a function call, and it matches every argument that the call can produce. Here,
split puts ++ in a pattern to generate every way to split a list in two:
-- Lists.curry
split (xs ++ ys) = (xs, ys)Sprite searches for the pairs that satisfy this condition and returns every one. To evaluate the function and iterate through the results in Python, write:
from curry.lib import Lists
results = curry.eval(Lists.split([1, 2, 3]))
for xs, ys in sorted(results):
print(xs, ys)
# [] [1, 2, 3]
# [1] [2, 3]
# [1, 2] [3]
# [1, 2, 3] []Returning to Logic.curry, render is a function, so a rule can put a call to it
in a pattern, as split puts ++ in one. Leave the formula unknown, and the
result is a parser:
-- Logic.curry, continued
parse :: String -> Formula
parse (render p) = pp is a logic variable, and with the functional pattern around it, the module
is no longer valid Haskell. To match the pattern, Sprite solves for the formula
whose rendering is the given string, and that formula is the answer:
tree = curry.eval(L.parse('((T | F) & !F)')).one()
print(tree) # And (Or T F) (Not F)
assert tree == formulaThe five rules of render serve as the grammar. There's no second copy of the
grammar to keep in step, so the parser cannot disagree with the printer.
The parser above is a toy, so consider a larger problem: C++ overload resolution. When a call matches several functions with the same name, the standard says which one the compiler calls. It does so through many interacting rules about conversions, ranks, and special cases. Implementing those rules by hand takes data structures, a search, and a long run of mechanical detail, and any part of it can be wrong.
Sprite lets you translate the standard directly: each of its rules becomes a rule
of the program, and no separate implementation is needed.
CxxOverload.curry is about 340 lines long and
translates the standard's conversion sequences, viable candidates, and best viable
function. Here is one of its rules, for a null pointer conversion. The rule gives
the rank of a conversion from an argument type, a, to a parameter type, p.
The comment paraphrases the standard, and the rule beneath it states the same
condition, with no implementation detail:
-- A null pointer conversion ([conv.ptr]/1): std::nullptr_t to any pointer
-- or pointer to member.
standard _ a p
| is_null_pointer a && (is_pointer p || is_member_pointer p)
= ConversionYou don't need to read the Curry to follow this. The point is that the rule contains no algorithm. It states the conversion, as the standard does.
The result is a working program that is just a list of rules.
Example 21 uses these rules to pick the
overload a call selects, and to find the argument types that would make a call
ambiguous. Given the overloads f(long), f(double), and f(int const*), one
answer is int: f(1) is ambiguous, because int converts to both long and
double at the same rank. float is not an answer, because it promotes to
double.
A similar query asks which argument types pick f(int const*). The answers
include int*, as expected, but also int[3] and std::nullptr_t. The array
decays to a pointer and gains const, which still counts as an exact match.
std::nullptr_t converts to any pointer, but to neither long nor double.
These are the corners that a hand-rolled implementation might get wrong.
Translating a standard into rules has several benefits:
- Easy to check: each rule can be read against the text it came from.
- Free of mechanical detail: Sprite does the search, so the program has no loops and no backtracking.
- Quick to write: each rule of the source becomes a rule of the program.
- Short: an agent may spend fewer tokens writing it.
- Runnable: it works as written, or as an oracle for testing a later implementation.
Other examples show other uses. In a build system, Curry solves for what to build, and Python checks timestamps and manages subprocesses. A package dependency solver works on the constraints directly, with no loop over candidates, no backtracking code, and no search state.
Curry is a multi-paradigm language. It has algebraic data types, pattern matching, strong typing, type inference, lazy evaluation, nondeterminism, logic variables, and search. Besides Sprite, its implementations include PAKCS, KiCS2, Curry2Go, and KMCC.
The functional side rests on graph rewriting, the well-studied evaluation model behind lazy languages such as Haskell. 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.
Sprite's evaluation strategy, the Fair Scheme, is proved sound, complete, and optimal. It computes all and only the values of any source expression, and every step it takes is one that any strategy must take.
Other evaluation strategies, such as depth-first search, are also available.
pip install --pre curryThe --pre flag is needed while Sprite is in alpha. Sprite runs on Python 3.11.4
through 3.15, on Linux, macOS, and Windows. The package installs three commands:
sprite opens the Curry REPL, sprite-exec runs a Curry program, and
sprite-make compiles one. A C++ compiler is optional. Without one, and without
the jit extra, Sprite runs the code interpreted. For native code without a C++
compiler, add the jit extra. It needs a platform that llvmlite supports:
pip install --pre "curry[jit]"To install without touching your own Python on Linux or macOS, use the installer script. It brings its own Python and installs Sprite under your home directory:
curl -LsSf https://raw.githubusercontent.com/andyjost/Sprite/master/getsprite | shThe script puts the commands in ~/.local/bin. Add that directory to your
PATH, in this shell and in your shell's startup file:
export PATH="$HOME/.local/bin:$PATH"Its Python is private, so import curry in your own Python still needs the pip
command.
The documentation has a Quickstart, a Curry primer for Python programmers, the Python API, the REPL, and a Status page that says what works today. Each of the twenty-six examples has a README, a run script, and recorded output.
Bug reports with a reproducer are the most useful contribution right now; please file them on the issue tracker. The runtime follows a proved strategy, so a change to the runtime is a change to the proof. For that reason we discuss changes before they are written and don't take unsolicited pull requests. If you want to work on something, open an issue first and we'll talk. CONTRIBUTING.md has the setup and the conventions.
BSD 3-Clause. The Curry library under curry/lib is the base library
of PAKCS and carries its license and a notice of its origin.