Skip to content

7. Composition and the library ​

Vocabulary terms may realize their meaning through ordinary Stated definitions. Bindings connect typed semantic roles to formal IDs; declaration order and familiar wording do not supply behavior.

Complete program ​

txt
Define filter-and-copy as a function.
It accepts an integer sequence named input and an integer named threshold.
Call filter-values with the input as input and the threshold as threshold, storing the result as filtered.
Call copy-values with the filtered as input, storing the result as copied.
Return the copied.
Define copy-values as a function.
It accepts an integer sequence named input.
Collect the input values in encounter order into a fresh sequence.
Retain every occurrence.
Define filter-values as a function.
It accepts an integer sequence named input and an integer named threshold.
Collect the input values in encounter order into a fresh sequence.
Retain each value only when it is greater than the threshold.
Replace each retained value with the doubled value of that value before appending it.
go
package generated

import (
	"errors"
	"math"
)

func FilterAndCopy(p0 []int64, p1 int64) ([]int64, error) {
	r0, err0 := FilterValues(p0, p1)
	if err0 != nil {
		return nil, err0
	}
	_ = r0
	r1, err1 := CopyValues(r0)
	if err1 != nil {
		return nil, err1
	}
	_ = r1
	result := make([]int64, len(r1))
	copy(result, r1)
	return result, nil
}

func CopyValues(p0 []int64) ([]int64, error) {
	result := make([]int64, len(p0))
	copy(result, p0)
	return result, nil
}

func FilterValues(p0 []int64, p1 int64) ([]int64, error) {
	result := make([]int64, 0)
	for _, value := range p0 {
		if value > p1 {
			if value > math.MaxInt64/2 || value < math.MinInt64/2 {
				return nil, errors.New("integer overflow")
			}
			result = append(result, value+value)
		}
	}
	return result, nil
}

filter-and-copy calls filter-values and copy-values by the names declared in the same source. A callee may be defined later in the file.

Every activation gets fresh local binders, accumulators, sequences, worklists, and control targets. Formal inputs are read-only. Returned sequences are copied into caller-owned storage.

Exercise ​

Remove First compute … and try to place a copy of the input directly where an existing sequence is required. Why may preparation be necessary?

Verified solution

Some grammar positions accept a computed sequence concept; others require an existing sequence reference. Explicit preparation bridges that boundary and also guarantees compute-once timing.

← Expressions and preparation · State and search →

Stated compiles controlled English deterministically. It does not call an LLM at compile time.