Curry Semantics

This page describes nondeterminism, free variables and set functions. The values shown are those Sprite gives, in the order the REPL prints them.

Nondeterminism

A Curry expression can have several values. The simplest source of several values is the choice operator ?:

coin :: Int
coin = 0 ? 1

coin has the values 0 and 1. An evaluation of coin in Sprite yields both, one after the other; a program that needs one of them takes the first and discards the rest.

Choices

A choice is a value, not an action. The two occurrences of coin in coin + coin are two independent choices, so the expression has four values:

coin + coin   -- 0, 1, 1, 2

A variable bound to a choice is one choice, shared by every use of the variable. This is call-time choice, the rule of Curry:

let c = coin in c + c   -- 0, 2

Sprite implements it with pull-tabbing: a choice is moved to the root of the expression step by step and the two alternatives share every part that the choice did not touch. The same rule applies to a parameter: a function called with coin sees one value of it, whatever the function does with the parameter.

Overlapping rules

A function whose rules overlap is nondeterministic too. Every rule that matches contributes its values. insert places an element at every position of a list and perm gives every permutation:

insert :: a -> [a] -> [a]
insert x xs     = x : xs
insert x (y:ys) = y : insert x ys

perm :: [a] -> [a]
perm []     = []
perm (x:xs) = insert x (perm xs)

perm [1,2,3]
-- [1,2,3], [2,1,3], [1,3,2], [3,1,2], [2,3,1], [3,2,1]

A rule whose guard fails or a pattern that matches nothing contributes no value. An expression with no value at all fails; a failure is not an error and the other alternatives of a search go on. Search in Curry is the evaluation of such expressions: a generator produces the candidates as values and a guard removes the ones that do not fit.

Free variables

A variable declared free has no value until the computation binds it. The equational constraint =:= binds a variable to make both sides equal:

xs ++ [3] =:= [1,2,3] where xs free   -- {xs=[1,2]} True

A function applied to a free variable narrows it: the variable is bound to each constructor of its type that lets a rule match, one choice per constructor. Arithmetic and ordering on a free variable of a built-in type suspend until another part of the computation binds it. An equality == a guard requires to be True binds it instead: Sprite, like PAKCS, treats it as =:= there. f | x == 3 = x where x free gives 3; an if x == 3 then ... condition suspends (Values and types). A free variable has a Data type, so a function type cannot be free. A function whose rule unifies a value of a polymorphic type, as a functional pattern does, carries a Data a => constraint in its signature.

Functional patterns

A functional pattern is a pattern that contains a function call. The rule matches an argument that the call can produce and the variables of the pattern are bound to the pieces:

last' :: Data a => [a] -> a
last' (_ ++ [x]) = x

last' [1,2,3]   -- 3

The match is a search over the values of the pattern: _ ++ [x] is narrowed until it equals the argument. The variables a functional pattern binds stay narrowed in the value; Status describes the cost of using that value in later comparisons.

Set functions

A set function collects the values of a function into a set, so a program can ask whether a search has a value, how many or which is the smallest, without leaving the program. set1 f is the set function of the unary f and set0 to set7 cover the other arities. The module Control.SetFunctions provides them with sortValues, isEmpty and the other operations on a set. The prompt of the REPL sees the loaded module and the Prelude alone (see The REPL), so the examples of this section are definitions of the module:

import Control.SetFunctions

f :: Int -> Int
f x = x ? (x + 1)

main1 :: [Int]
main1 = sortValues (set1 f 1)   -- [1,2]

The set function encapsulates the nondeterminism of the body of f: the two values of f 1 are the elements of one set. The nondeterminism of the argument is not encapsulated. A choice that comes from outside the call produces one set per alternative and the choice is shared with the rest of the expression, as call-time choice requires:

main2 :: [Int]
main2 = sortValues (set1 f (1 ? 5))                  -- [1,2], [5,6]
main3 :: [Int]
main3 = sortValues (set1 id (1 ? 2))                 -- [1], [2]
main4 :: (Int, [Int])
main4 = let x = 1 ? 2 in (x, sortValues (set1 f x))  -- (1,[1,2]), (2,[2,3])

In the last line each value of x goes with the set computed from that value. The same holds for a free variable in the argument: each binding of it gets its own set, whenever the binding is made.

A partial application’s arguments and the variables a lambda captures count as arguments of the call, so their nondeterminism stays outside the set:

main5 :: ([Int], Int)
main5 = let x = 1 ? 2 in (sortValues (set1 (\y -> x + y) 10), x)
                                              -- ([11],1), ([12],2)

set0 e encapsulates e alone. Variables it captures from the enclosing context stay outside the set. Thus let x = coin in allValues x yields one set per value of x, as in the paper on weakly encapsulated search by Christiansen, Hanus, Reck and Seidel (PPDP 2013). An expression without captured variables is encapsulated whole:

main6 :: [Int]
main6 = let x = 0 ? 1 in sortValues (set0 (x + 1))   -- [1], [2]
main7 :: [Int]
main7 = sortValues (set0 (0 ? 1))                  -- [0,1]

captureS of Control.SetFunctions is the explicit way to put the nondeterminism of an argument inside the set: evalS (set f `captureS` (1 ? 2)) has one value, the set of f 1 and f 2 together.

A function that fails has the empty set:

source :: String -> String
source (stem ++ ".o") = stem ++ ".c"

main8 :: Bool
main8 = isEmpty (set1 source "main.c")   -- True

A failure that comes from an argument is not a failure of the function. It drops the alternative that demands it and the set function fails when no alternative gives a value and an outside failure was recorded, as the semantics of weakly encapsulated search prescribes: isEmpty (set1 id failed) has no value and sortValues (set1 (\a -> a ? True) failed) is [True]. An argument the function does not demand fails nothing. The same rule holds for a failing captured variable:

main9 :: [Int]
main9 = let w = failed :: Int in sortValues (set0 (id w))   -- no value
main10 :: [Int]
main10 = let w = failed :: Int in sortValues (set0 (w ? 7))  -- [7]

Sprite computes the values of a set as the program consumes them, and a choice that diverges inside a set function does not stop the others.