Lecture 8 — Efficiency
Two questions
In Lecture 2 we wrote e ~> e' for the evaluation relation.
Today we think about the cost of evaluation.
Time: how many steps does it take?
Space: how much memory is needed?
Plan: cost, accumulating parameters, tail calls, stack and heap, trees,
continuations.
- Cost
- Accumulating parameters
- Tail recursion
- Stack and heap
- Trees
- Continuation-passing style
The exercises are at the end of these notes.
1 · Cost
Factorial
let rec fact n =
if n = 0 then
1
else
n * fact (n - 1)
fact 5
~> 5 * fact (5 - 1)
~> 5 * fact 4
~> 5 * (4 * fact 3)
~> 5 * (4 * (3 * fact 2))
~> 5 * (4 * (3 * (2 * fact 1)))
~> 5 * (4 * (3 * (2 * (1 * fact 0))))
~> 5 * (4 * (3 * (2 * (1 * 1))))
~> ...
~> 120
|
let fact5 = fact 5
In the step case we leave the current n "behind".
While we evaluate fact (n - 1), we need to "remember" that we still
have to multiply the result with n.
How many values of n must be remembered?
n of them, at the moment fact 0 is reached.
Time and space (asymptotically) proportional to n.
Memory is finite
Remembering a value means storing it somewhere. Eventually we will run
out of space. For what n does fact n crash F# Interactive?
> fact 1_000_000;;
Stack overflow.
Repeated 261410 times:
--------------------------------
at FSI_0002.fact(Int32)
--------------------------------
|
(The int result would have wrapped around long before. Beside the
point: the evaluation never finishes.)
Naive reversal
Lecture 4 defined rev through fold; this is the definition one
writes first.
let rec naiveRev xs =
match xs with
| [] -> []
| x :: xs' -> naiveRev xs' @ [x]
naiveRev [1; 2; 3; 4; 5]
~> naiveRev [2; 3; 4; 5] @ [1]
~> (naiveRev [3; 4; 5] @ [2]) @ [1]
~> ((naiveRev [4; 5] @ [3]) @ [2]) @ [1]
~> ...
~> (((([] @ [5]) @ [4]) @ [3]) @ [2]) @ [1]
~> ((([5] @ [4]) @ [3]) @ [2]) @ [1]
~> (([5; 4] @ [3]) @ [2]) @ [1]
~> ([5; 4; 3] @ [2]) @ [1]
~> [5; 4; 3; 2] @ [1]
~> [5; 4; 3; 2; 1]
|
let naiveRev5 = naiveRev [1; 2; 3; 4; 5]
In the base case, we have five pending @ operations and we have
remembered five singleton lists.
Space proportional to length \(n\).
Lecture 4: xs @ ys takes steps proportional to the length of xs.
One @ per element, and its first argument grows every time:
\(0 + 1 + \cdots + (n - 1) = n(n - 1)/2\) steps.
Time proportional to \(n^2\).
Factorial and reverse again
The same functions with every intermediate value bound to a name.
let rec fact' n =
if n = 0 then
1
else
let n' = n - 1
let rr = fact' n'
let r = n * rr
r
let rec naiveRev' xs =
match xs with
| [] -> []
| x :: xs' ->
let rr = naiveRev' xs'
let ys = [x]
let r = rr @ ys
r
rr is the recursive result and it is needed to compute the result
r.
2 · Accumulating parameters
A more general problem
The pending operation in fact is multiplication by n (i.e., n * _).
To not leave "behind" pending operations we solve a more general
problem: we compute the factorial of n multiplied with "something".
Specification:
factA n acc = acc * fact n
|
Here acc is an accumulating parameter.
The original problem is the special case where acc = 1:
fact n = 1 * fact n = factA n 1
|
Factorial using accumulators
When n = 0, as fact 0 = 1, we have:
factA 0 acc = acc * fact 0 // specification
= acc * 1 // def. of fact
= acc // simplify
|
When n > 0, as fact n = n * fact (n - 1), we have:
factA n acc = acc * fact n // specification
= acc * (n * fact (n - 1)) // def. of fact
= (acc * n) * fact (n - 1) // * is associative
= factA (n - 1) (acc * n) // specification
|
The last step is the key: (acc * n) * fact (n - 1) is something that
we can compute with factA. We have a recursive definition.
factA
let rec factA n acc =
if n = 0 then
acc
else
factA (n - 1) (acc * n)
let factorial n = factA n 1
val factA: n: int -> acc: int -> int
[lang=text]
factA 5 1
~> factA (5 - 1) (1 * 5)
~> factA 4 5
~> factA 3 20
~> factA 2 60
~> factA 1 120
~> factA 0 120
~> 120
|
let factA51 = factA 5 1
No pending multiplications.
The result of factA 0 120 is the result of factA 5 1.
fact 3: 3 * (2 * (1 * 1)).
factA 3 1: ((1 * 3) * 2) * 1.
Reversing a list using accumulators
The more general problem: reverse a list and append something to
it. Specification:
revA xs acc = naiveRev xs @ acc
|
When xs = []:
revA [] acc = naiveRev [] @ acc // specification
= [] @ acc // def. of naiveRev
= acc // def. of @
|
When xs = x :: xs':
revA (x :: xs') acc = naiveRev (x :: xs') @ acc // specification
= (naiveRev xs' @ [x]) @ acc // def. of naiveRev
= naiveRev xs' @ ([x] @ acc) // @ is associative
= naiveRev xs' @ (x :: acc) // simplify
= revA xs' (x :: acc) // specification
|
revA
let rec revA xs acc =
match xs with
| [] -> acc
| x :: xs' -> revA xs' (x :: acc)
let reverse xs = revA xs []
val revA:
xs: 'a list -> acc: 'a list -> 'a list
[lang=text]
revA [1; 2; 3] []
~> revA [2; 3] (1 :: [])
~> revA [3] [2; 1]
~> revA [] [3; 2; 1]
~> [3; 2; 1]
|
What is revA [1; 2; 3] []?
let revA123 = revA [1; 2; 3] []
let reverse15 = reverse [1; 2; 3; 4; 5]
[3; 2; 1]. One :: per element, no @ at all: time proportional
to the length, nothing pending.
Space proportional to the length (list, accumulator).
Compare with reverse defined using fold from Lecture 4.
3 · Tail recursion
Tail positions and tail recursion
A function call is in tail position when its result is returned as the
result (with no further computation).
A function is tail recursive if every recursive call in its definition
is in tail position.
Example: in the expression g x (x + f y) the function g is applied
in tail position; f is not as further computation depends on its
result.
Where in the body e of let rec f x = e are the tail positions?
let y = e1 in y those of e1
let y = e1 in e2 those of e2 (not e1)
if b then e1 else e2 those of e1 and e2 (not b)
match ... | p1 -> e1 ... those of e1, ..., en (not the scrutinee)
g e the application itself (not e)
e1; e2 those of e2 (not e1)
|
Spot the tail call (I)
Which of these are tail recursive?
let rec countdown n =
if n = 0 then
0
else
countdown (n - 1)
let rec sumTo n =
if n = 0 then
0
else
n + sumTo (n - 1)
let rec dropNeg xs =
match xs with
| [] -> []
| x :: xs' ->
if x < 0 then
dropNeg xs'
else
x :: dropNeg xs'
countdown: yes, tail position of a branch.
sumTo: no, sumTo (n - 1) is an argument to +.
dropNeg: no, the first call is a tail call but the second is an
argument to ::.
let countdown5 = countdown 5
let sumTo5 = sumTo 5
let dropNegEx = dropNeg [1; -2; 3; -4]
Spot the tail call (II)
let rec lastOf xs =
match xs with
| [] -> failwith "empty list"
| [x] -> x
| _ :: xs' -> let r = lastOf xs' in r
let rec sumA n acc =
if n = 0 then
acc
else
sumA (n - 1) (acc + n)
lastOf: yes (no further computation after recursive call).
sumA: yes.
let lastOfEx = lastOf [1; 2; 3]
let sumA50 = sumA 5 0
Two examples
let rec even n =
match n with
| 0 -> true
| 1 -> false
| n -> even (n - 2)
let rec badA n acc =
if n = 0 then
acc
else
n * badA (n - 1) (n * acc)
Accumulators are not necessary for tail recursion: even is tail
recursive.
badA is not tail recursive although it does have a parameter named
acc.
let even10 = even 10
let even7 = even 7
let badA51 = badA 5 1
Tail call elimination/optimisation
Recall: the result of a tail call is returned without any further
(local) computation.
Nothing in the caller's stack frame is needed anymore.
Compiler reuses the current stack frame for the callee instead of
pushing a new stack frame.
That is why factA 1_000_000 1 returned while fact 1_000_000
overflowed. Reusing the current stack frame led to constant stack usage.
Is it tail recursive?
Two tests.
First, factA handles a larger input in F# Interactive. factA 1_000_000 1?
let factA1M = factA 1_000_000 1
0 (int overflows), but it returns.
Second: [<TailCall>] attribute in a compiled project.
[<TailCall>]
let rec fact n =
if n = 0 then 1
else n * fact (n - 1)
$ dotnet build
Program.fs(4,12): warning FS3569:
The member or function 'fact' has the
'TailCallAttribute' attribute, but is
not being used in a tail recursive way.
|
F# Interactive does not report this warning. (Creating a project:
https://fsharp.pages.taltech.ee/tooling/debugging/.)
Iterative functions
Tail recursive functions are sometimes also called iterative functions.
Starting from initial value x : X, iterate a function f : X -> X
while a condition c : X -> bool evaluates to true on the current
value, and then project the final result by applying p : X -> Y.
Example: factA iterates the function fun (n, acc) -> (n - 1, acc * n)
while the following predicate fun (n, acc) -> n <> 0 evaluates to
true, and then projects the result by snd.
(5, 1) ~> (4, 5) ~> (3, 20) ~> (2, 60) ~> (1, 120) ~> (0, 120) ~> 120
|
Imperatively:
let factW n =
let mutable x = (n, 1)
while (fun (n, acc) -> n <> 0) x do
x <- (fun (n, acc) -> (n - 1, acc * n)) x
snd x
let factW5 = factW 5
4 · Stack and heap
Basic layout
Primitive values live on the stack; lists are cons cells on the heap
(Lecture 3).
let xs = [5; 6; 7]
let ys = 3 :: 4 :: xs
let zs = xs @ ys
let n = 27
ys: two new cells, second points to xs. zs: @ copies the cells
of xs, the last cell points to ys.
Lists are immutable and thus such reuse is safe.
Push
let ws =
let us = [1; 2]
let vs = [3; 4]
us @ vs
Evaluating the let-expression pushes a new frame.
The frame has entries for the local us and vs, and an entry for
the result.
Pop, and garbage
When the let-expression completes, sf1 is popped and ws
receives the result.
The cells marked † are unreachable from the stack and thus are
considered garbage. The garbage collector reclaims them behind the
scenes.
Heap is separated into three generations by age. (Most objects die
young.)
Memory during revA
One frame per call, as if no frame were reused (no TCO). To ease
the visualisation we say that a frame consists of two pointers: xs
and acc.
Each call on a nonempty list allocates a new cons cell (new
accumulator).
Nothing in a frame is needed after the recursive call (we just return
the result of the recursive call).
TCO: the compiler reuses the current frame for the recursive call.
Measurements (#time)
#time in F# Interactive reports elapsed time, CPU time and garbage
collections per generation. On this machine (an eight-core Linux
machine, .NET SDK 10.0.112):
> let xs16 = List.replicate 1000000 16;;
> for i in xs16 do fact i |> ignore;;
Real: 00:00:00.035, CPU: 00:00:00.035,
GC gen0: 0, gen1: 0, gen2: 0
> for i in xs16 do factA i 1 |> ignore;;
Real: 00:00:00.014, CPU: 00:00:00.014,
GC gen0: 0, gen1: 0, gen2: 0
> for i in xs16 do ();;
Real: 00:00:00.007, CPU: 00:00:00.007,
GC gen0: 0, gen1: 0, gen2: 0
[lang=text]
> let xs20000 = [1 .. 20000];;
> naiveRev xs20000;;
Real: 00:00:02.711, CPU: 00:00:04.034,
GC gen0: 185, gen1: 2, gen2: 1
> revA xs20000 [];;
Real: 00:00:00.001, CPU: 00:00:00.001,
GC gen0: 0, gen1: 0, gen2: 0
|
The loop alone costs 7 ms, so factA's gain is more than the visible
factor.
Reversal: 2.7 seconds against 1 millisecond, and 188 garbage
collections against none.
Not only is naiveRev quadratic, the way @ is used generates a lot
of garbage.
The limits
let rec bigList n =
if n = 0 then
[]
else
1 :: bigList (n - 1)
let rec bigListA n acc =
if n = 0 then
acc
else
bigListA (n - 1) (1 :: acc)
let bigListA10 = bigListA 10 []
[1; 1; 1; 1; 1; 1; 1; 1; 1; 1]
|
bigList builds the list on the way back from the recursion, with n
pending 1 ::; bigListA builds it on the way down. On this machine:
> bigList 200_000 |> List.length;;
val it: int = 200000
> bigList 300_000;;
Stack overflow.
Repeated 261442 times:
at FSI_0002.bigList(Int32)
> bigListA 10_000_000 [] |> List.length;;
val it: int = 10000000
(heap limited to 1 GB:)
> bigListA 100_000_000 [];;
System.OutOfMemoryException:
Exception of type
'System.OutOfMemoryException' was thrown.
|
(A hundred million cells would take more memory than this machine has;
the heap was limited with DOTNET_GCHeapHardLimit=0x40000000, so that
the limit shows as an exception rather than as swapping.)
The stack: some \(2.6 \cdot 10^5\) frames. The heap: two orders of
magnitude more cells here.
bigListA runs in one frame: the result list grows on the heap.
5 · Trees
From Lecture 6
type 'a BinTree =
| Empty
| Node of 'a BinTree * 'a * 'a BinTree
let leaf x = Node (Empty, x, Empty)
let t = Node (Node (leaf 2, 7, Empty),
9,
Node (leaf 13, 21, leaf 25))
let rec size t =
match t with
| Empty -> 0
| Node (l, _, r) -> size l + 1 + size r
Is size tail recursive?
let sizeT = size t
No: both calls are arguments to +.
Accumulators with trees?
We can try to use accumulators:
let rec sizeA t acc =
match t with
| Empty -> acc
| Node (l, _, r) -> sizeA r (sizeA l (acc + 1))
Is sizeA tail recursive?
let sizeAT0 = sizeA t 0
No: sizeA l (acc + 1) is an argument to the outer recursive call.
Processing r first does not help.
6 · Continuation-passing style
The rest of the computation
What we need: recurse into l (with a tail call) and when l has
been processed continue with r.
This extra piece of information (what to do after l has been
processed? continue with r) must be a parameter to the function.
A continuation is a function that represents "the rest of the
computation": what to do, later, with the result of the current
computation.
With k : int -> int we describe how to get the final result from a
partial result.
We pass k along as an argument; when the partial result n is
known, we "run" k from n by k n.
Factorial using continuations
In fact, the part to do later (pending operations) is n * _.
Generalise fact: compute fact n and apply some function k to the
result. Specification:
When n = 0:
factK 0 k = k (fact 0) // specification
= k 1 // def. of fact
|
Observation: k (n * x) is (fun r -> k (n * r)) x.
When n > 0:
factK n k = k (fact n) // specification
= k (n * fact (n - 1)) // def. of fact
= (fun r -> k (n * r)) (fact (n - 1)) // observation
= factK (n - 1) (fun r -> k (n * r)) // specification
|
factK
let rec factK n k =
match n with
| 0 -> k 1
| n -> factK (n - 1) (fun r -> k (n * r))
factK 3 k
~> factK 2 k3 // k3 = fun r -> k (3 * r)
~> factK 1 k2 // k2 = fun r -> k3 (2 * r)
~> factK 0 k1 // k1 = fun r -> k2 (1 * r)
~> k1 1
~> k2 (1 * 1)
~> k2 1
~> k3 (2 * 1)
~> k3 2
~> k (3 * 2)
~> k 6
|
What is the type of factK?
val factK: n: int -> k: (int -> 'a) -> 'a
|
More general than the int -> int continuation we set out with.
factK 5 id? 120
let factK5 = factK 5 id
Note that both factK and k, k_i are called in tail positions
only; the pending multiplications sit inside the continuation, not on
the stack.
Size of a tree using continuations
Specification: sizeK t k = k (size t).
For Empty:
sizeK Empty k = k (size Empty) // specification
= k 0 // def. of size
|
For Node (l, _, r):
sizeK (Node (l, _, r)) k = k (size (Node (l, _, r))) // specification
= k (size l + 1 + size r) // def. of size
= (fun sl -> k (sl + 1 + size r)) (size l) // rewrite
= sizeK l (fun sl -> k (sl + 1 + size r)) // specification
|
Inside the continuation, abstract size r similarly:
k (sl + 1 + size r) = (fun sr -> k (sl + 1 + sr)) (size r) // rewrite
= sizeK r (fun sr -> k (sl + 1 + sr)) // specification
|
sizeK
let rec sizeK t k =
match t with
| Empty -> k 0
| Node (l, _, r) ->
sizeK l <| fun sl ->
sizeK r <| fun sr ->
k (sl + 1 + sr)
val sizeK:
t: 'a BinTree -> k: (int -> 'b) -> 'b
|
let size' t = sizeK t id
let sizeT' = size' t
Once we have sl and sr we can run the original continuation k.
(e <| f is f e, so the body with parentheses instead reads
sizeK l (fun sl -> sizeK r (fun sr -> k (sl + 1 + sr))).)
Trading stack for heap
let rec spine n acc =
if n = 0 then
acc
else
spine (n - 1) (Node (acc, n, Empty))
let deep = spine 1_000_000 Empty
> size deep;;
Stack overflow.
Repeated 130707 times:
at FSI_0003.size[...](BinTree`1<Int32>)
[lang=text]
> sizeK deep id;;
val it: int = 1000000
|
A left-leaning tree with a million nodes. size overflows the stack,
sizeK computes the result.
let sizeKDeep = sizeK deep id
The pending work has not disappeared: it is represented by the
"growing" continuation on the heap.
Heap is larger but not free. On trees that fit the stack, size is
faster.
Summary
- Function calls consume stack; deep recursion may overflow the stack.
-
Tail call optimisation keeps stack usage bounded; we must be able to
write functions tail recursively.
- Sometimes helps: accumulating parameters.
- General method: continuation-passing style.
-
To write performant code we must be aware of the representation of
data in memory (stack and heap).
Homework and reading
Homework 6 is out this week.
Reading: Hansen & Rischel, Functional Programming Using F#, chapter 9,
"Efficiency".
Exercises
Exercise 1: iteration
Section 3 said that factA iterates a step on a state while a condition
holds and then projects the result. Define a function iterate that
captures this shape: it takes a condition, a step function and a
finishing function, iterates the step on a state while the condition
holds, and then applies the finishing function to the state.
Then define factI and reverseI through iterate (and nothing
recursive of your own), so that factI n equals factA n 1 and
reverseI xs equals revA xs [].
Examples:
factI 5 is 120.
reverseI [1; 2; 3] is [3; 2; 1].
iterate applied to the factorial step on the state (5, 1) gives 120.
Questions: is your iterate tail recursive? Which function of Lecture 4
is iterate on a list?
Exercise 2: tail-recursive list functions
Define
mapA : ('a -> 'b) -> 'a list -> 'b list
appendA : 'a list -> 'a list -> 'a list
|
both tail recursive, so that mapA f xs equals List.map f xs and
appendA xs ys equals xs @ ys.
Examples:
mapA (fun x -> x * x) [1; 2; 3] is [1; 4; 9].
appendA [1; 2] [3; 4] is [1; 2; 3; 4].
mapA ((+) 1) [1 .. 1_000_000] has length 1000000.
Questions: what does the final step of your mapA cost, and does it
change the time bound? Must appendA traverse its second argument?
Exercise 3: continuations on trees
For Lecture 6's 'a BinTree, define
sumK : int BinTree -> (int -> 'a) -> 'a
heightK : 'a BinTree -> (int -> 'b) -> 'b
|
in continuation-passing style, so that sumK t k = k (sum t) where sum
adds the values in the tree, and heightK t k = k (height t) with
Lecture 6's height (the number of nodes on a longest path from the
root).
Examples (t and deep as above):
sumK t id is 77.
heightK t id is 3.
heightK deep id is 1000000.
Question: which calls in your heightK are tail calls?
namespace Itt8060
module Diagrams
from Itt8060
val scaled: factor: float -> Svg -> Svg
val factor: float
Multiple items
val float: value: 'T -> float (requires member op_Explicit)
--------------------
type float = System.Double
--------------------
type float<'Measure> =
float
Multiple items
union case Svg.Svg: string -> Svg
--------------------
type Svg = | Svg of string
A rendered SVG fragment (inline-embeddable, standalone-saveable).
val s: string
val widthAttr: System.Text.RegularExpressions.Regex
namespace System
namespace System.Text
namespace System.Text.RegularExpressions
Multiple items
type Regex =
interface ISerializable
new: pattern: string -> unit + 2 overloads
member Count: input: string -> int + 8 overloads
member EnumerateMatches: input: ReadOnlySpan<char> -> ValueMatchEnumerator + 4 overloads
member EnumerateSplits: input: ReadOnlySpan<char> -> ValueSplitEnumerator + 5 overloads
member GetGroupNames: unit -> string array
member GetGroupNumbers: unit -> int array
member GroupNameFromNumber: i: int -> string
member GroupNumberFromName: name: string -> int
member IsMatch: input: ReadOnlySpan<char> -> bool + 9 overloads
...
<summary>Represents an immutable regular expression.</summary>
--------------------
System.Text.RegularExpressions.Regex( pattern: string) : System.Text.RegularExpressions.Regex
System.Text.RegularExpressions.Regex( pattern: string, options: System.Text.RegularExpressions.RegexOptions) : System.Text.RegularExpressions.Regex
System.Text.RegularExpressions.Regex( pattern: string, options: System.Text.RegularExpressions.RegexOptions, matchTimeout: System.TimeSpan) : System.Text.RegularExpressions.Regex
val w: int
Multiple items
val int: value: 'T -> int (requires member op_Explicit)
--------------------
type int = int32
--------------------
type int<'Measure> =
int
System.Text.RegularExpressions.Regex.Match(input: string) : System.Text.RegularExpressions.Match
System.Text.RegularExpressions.Regex.Match(input: string, startat: int) : System.Text.RegularExpressions.Match
System.Text.RegularExpressions.Regex.Match(input: string, beginning: int, length: int) : System.Text.RegularExpressions.Match
System.Text.RegularExpressions.Regex.Replace(input: string, evaluator: System.Text.RegularExpressions.MatchEvaluator) : string
System.Text.RegularExpressions.Regex.Replace(input: string, replacement: string) : string
System.Text.RegularExpressions.Regex.Replace(input: string, evaluator: System.Text.RegularExpressions.MatchEvaluator, count: int) : string
System.Text.RegularExpressions.Regex.Replace(input: string, replacement: string, count: int) : string
System.Text.RegularExpressions.Regex.Replace(input: string, evaluator: System.Text.RegularExpressions.MatchEvaluator, count: int, startat: int) : string
System.Text.RegularExpressions.Regex.Replace(input: string, replacement: string, count: int, startat: int) : string
val saveScaled: path: string -> factor: float -> svg: Svg -> unit
val path: string
Multiple items
val string: value: 'T -> string
--------------------
type string = System.String
val svg: Svg
val save: path: string -> Svg -> unit
Save a diagram for use from slide decks: `Diagrams.save "img/list567.svg" (consCells ...)`.
Paths are relative to the lecture directory when evaluated by the build.
val named: name: string -> Svg -> Svg
val name: string
val text: string
System.String.Replace(oldValue: string, newValue: string) : string
System.String.Replace(oldChar: char, newChar: char) : string
System.String.Replace(oldValue: string, newValue: string, comparisonType: System.StringComparison) : string
System.String.Replace(oldValue: string, newValue: string, ignoreCase: bool, culture: System.Globalization.CultureInfo) : string
val figRevA: Svg
val stackHeapAligned: frames: Frame list -> cells: (int * HeapCell) list -> Svg
Snapshot of stack frames beside a single *vertical* column of heap
cells, each cell drawn level with a frame row. `frames` is bottom-first
as in `stackHeap`; each cell comes with the index of the row it sits in
(0 = the bottom frame, 1 the frame above it, ...; negative rows continue
the column below the stack, e.g. for an argument list the frames point
into). Links between cells run vertically through the gap next to the
target cell; slot pointers enter the target cell's left edge, as in
`stackHeap`, with the lanes ordered by target depth (the pointer to the
lowest cell takes the leftmost lane), so that an arrow's entry into its
cell never crosses a lane that ends higher up.
val frame: name: string -> slots: Slot list -> Frame
union case Slot.Ptr: name: string * heapId: string -> Slot
union case Slot.Val: name: string * value: string -> Slot
val cell: id: string -> head: string -> next: string option -> HeapCell
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
val figMemory: Svg
val stackHeap: frames: Frame list -> rows: HeapCell list list -> Svg
Snapshot of stack frames and heap cells. Each stack frame is a
*horizontal* row of slots (variable names underneath), and frames are
stacked on top of each other — the last frame in the list is the top of
the stack, drawn topmost. The heap is given as *rows*, one per chain, so
links between chains are drawn as clearly separated elbow arrows.
Pointers (slot -> cell head, cell -> cell) are drawn automatically;
point slots at the head (leftmost cell) of a row.
val figPush: Svg
val figPop: Svg
val fact: n: int -> int
val n: int
val fact5: int
val naiveRev: xs: 'a list -> 'a list
val xs: 'a list
val x: 'a
val xs': 'a list
val naiveRev5: int list
val fact': n: int -> int
val n': int
val rr: int
val r: int
val naiveRev': xs: 'a list -> 'a list
val rr: 'a list
val ys: 'a list
val r: 'a list
val factA: n: int -> acc: int -> int
val acc: int
val factorial: n: int -> int
val factA51: int
val revA: xs: 'a list -> acc: 'a list -> 'a list
val acc: 'a list
val reverse: xs: 'a list -> 'a list
val revA123: int list
val reverse15: int list
val countdown: n: int -> int
val sumTo: n: int -> int
val dropNeg: xs: int list -> int list
val xs: int list
val x: int
val xs': int list
val countdown5: int
val sumTo5: int
val dropNegEx: int list
val lastOf: xs: 'a list -> 'a
val failwith: message: string -> 'T
val r: 'a
val sumA: n: int -> acc: int -> int
val lastOfEx: int
val sumA50: int
val even: n: int -> bool
val badA: n: int -> acc: int -> int
val even10: bool
val even7: bool
val badA51: int
val factA1M: int
val factW: n: int -> int
val mutable x: int * int
val snd: tuple: ('T1 * 'T2) -> 'T2
val factW5: int
val ys: int list
val zs: int list
val ws: int list
val us: int list
val vs: int list
val bigList: n: int -> int list
val bigListA: n: int -> acc: int list -> int list
val acc: int list
val bigListA10: int list
'a
type 'a BinTree =
| Empty
| Node of 'a BinTree * 'a * 'a BinTree
union case Tree.Node: label: string * children: Tree list -> Tree
val leaf: x: 'a -> 'a BinTree
union case BinTree.Node: 'a BinTree * 'a * 'a BinTree -> 'a BinTree
union case BinTree.Empty: 'a BinTree
val t: int BinTree
val size: t: 'a BinTree -> int
val t: 'a BinTree
val l: 'a BinTree
val r: 'a BinTree
val shape: t: 'a BinTree -> BinShape
union case BinShape.BinEmpty: BinShape
union case BinShape.BinNode: BinShape * string * BinShape -> BinShape
val figT: Svg
val binTree: root: BinShape -> Svg
A binary tree with empty children left out (their positions are kept).
val sizeT: int
val sizeA: t: 'a BinTree -> acc: int -> int
val sizeAT0: int
val factK: n: int -> k: (int -> 'a) -> 'a
val k: (int -> 'a)
val factK5: int
val id: x: 'T -> 'T
val sizeK: t: 'a BinTree -> k: (int -> 'b) -> 'b
val k: (int -> 'b)
val sl: int
val sr: int
val size': t: 'a BinTree -> int
val sizeT': int
val spine: n: int -> acc: int BinTree -> int BinTree
val acc: int BinTree
val deep: int BinTree
val sizeKDeep: int
val refIterate: p: ('a -> bool) -> f: ('a -> 'a) -> h: ('a -> 'b) -> s: 'a -> 'b
val p: ('a -> bool)
val f: ('a -> 'a)
val h: ('a -> 'b)
val s: 'a
val refFactI: n: int -> int
val refReverseI: xs: 'a list -> 'a list
Multiple items
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| op_Nil
| op_ColonColon of Head: 'T * Tail: 'T list
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
interface IEnumerable
interface IEnumerable<'T>
member GetReverseIndex: rank: int * offset: int -> int
member GetSlice: startIndex: int option * endIndex: int option -> 'T list
static member Cons: head: 'T * tail: 'T list -> 'T list
member Head: 'T
member IsEmpty: bool
member Item: index: int -> 'T with get
...
val isEmpty: list: 'T list -> bool
val tail: list: 'T list -> 'T list
val head: list: 'T list -> 'T
val refMapA: f: ('a -> 'b) -> xs: 'a list -> 'b list
val f: ('a -> 'b)
val go: xs: 'a list -> acc: 'b list -> 'b list
val acc: 'b list
val refAppendA: xs: 'a list -> ys: 'a list -> 'a list
val refSumK: t: int BinTree -> k: (int -> 'a) -> 'a
val l: int BinTree
val r: int BinTree
val refHeightK: t: 'a BinTree -> k: (int -> 'b) -> 'b
val hl: int
val hr: int
val max: e1: 'T -> e2: 'T -> 'T (requires comparison)
val check: name: string -> expected: 'a -> actual: 'a -> unit (requires equality)
val expected: 'a (requires equality)
val actual: 'a (requires equality)
val failwithf: format: Printf.StringFormat<'T,'Result> -> 'T
val length: list: 'T list -> int
val map: mapping: ('T -> 'U) -> list: 'T list -> 'U list