Search order and memory

A short program can describe a search that takes a lot of memory and time. Two things affect that cost: where the program makes its choices, and the order in which Sprite tries the alternatives. An alternative is one path through the choices of a program. This page shows each of the two with a small program.

The place of a choice

A search can cost much less when the program decides a choice first and then computes with the chosen value than when it computes with the choice still inside its data. The two programs below differ only in that.

In Shared.curry, oneOf [1 .. 12] is a choice among twelve numbers. run builds a list from the chosen number and takes its length twice. No list has a length both above and below zero, so the condition of run never holds and run has no value:

oneOf :: [a] -> a
oneOf (x:xs) = x ? oneOf xs

big :: Int -> Int -> [Int]
big n a = [a .. a + n]

run :: Int -> Int
run n = let a = oneOf [1 .. 12]
            w = big n a
        in if length w > 0 && length w < 0 then a else failed
>>> import curry
>>> curry.path.insert(0, '.')
>>> Shared = curry.import_('Shared')
>>> list(curry.eval(Shared.run(10)))
[]

run 10000 builds lists of 10,001 numbers. That run stopped at a limit of 2 GB of address space after 75 seconds. This figure and the others of this section are for the default order of a search; the next section describes the orders.

First.curry is Shared.curry with run replaced by two definitions. body is the computation, and run decides the choice before it calls body. It does so with the operator $!, which evaluates its argument before it applies the function; for an Int the result is the number:

body :: Int -> Int -> Int
body n a = let w = big n a
           in if length w > 0 && length w < 0 then a else failed

run :: Int -> Int
run n = body n $! oneOf [1 .. 12]

run 10000 has no value here either, and it ends after 1.0 seconds with a peak of 145 MB.

The difference comes from pull-tabbing, the way Sprite evaluates a choice (Curry Semantics). When a step needs a value and finds a choice, the choice moves up past that step, then past the step that waits for it, and so on to the root of the expression. There Sprite makes two alternatives, which share every part of the expression that the choice did not pass.

In Shared.curry the choice a is inside the list. Every cell needs the bound a + n, so every cell that length reads meets the choice. In the current alternative that choice was decided long before, but Sprite can tell only when the choice reaches the root. The length of the Prelude adds one per cell (length (_:xs) = 1 + length xs), so at cell i there are i additions waiting, and the choice moves up through a copy of each. The work therefore grows with the square of the length of the list. The copies stay in memory, because the alternative that waits still reaches them through the second length w.

In First.curry each choice reaches the root before the list exists, and body computes on a number.

These rules follow for a program that searches:

  • Decide a choice before you build on it, with f $! choice or a case on the choice. The choice then reaches the root while little waits above it.

  • Keep a choice out of data that later steps share. $! evaluates only the outer form of its argument. For a number that is the whole value, but a choice inside a list or a tuple stays inside. Choose the parts first and build the structure from the chosen values.

  • Use an accumulator in a long traversal. length above keeps one addition waiting per cell, and a choice met at a cell moves up through all of them. With an accumulating length in place of length, Shared.run 10000 ends after 5 seconds but still peaks at 1.3 GB, because every cell still meets the choice and every move leaves a copy. So an accumulator shortens each move and does not replace the first two rules.

  • Force only what every result needs. If the program can produce a result without the forced value, and that value fails or does not end, the rewritten program loses the result.