Answer architecture round 3, stability round 1 and product-owner round 1: CalcName and proven.OfCalc for one meaning per name, a lazy load placed where its lookup found it, DataType's constants typed under it, and cover printing the module's total

This commit is contained in:
2026-10-05 15:53:56 +02:00
committed by Lillem4n
parent 679bd43c7c
commit 4f484cd780
11 changed files with 34 additions and 33 deletions
+4 -4
View File
@@ -43,9 +43,9 @@ type builtin struct {
prints datatype.DataType
}
// Calc is the name of the builtin whose operands the caller checks and proves, as nodes only
// it holds.
const Calc = "calc"
// CalcName is the builtin whose operands the engine checks and proves, since only the engine
// holds their nodes.
const CalcName = "calc"
// rng is the randomness a builtin sample draws from, which *drawstate.State satisfies.
type rng interface {
@@ -115,7 +115,7 @@ func numberCalls(text bool) string {
}
}
slices.Sort(calls)
return strings.Join(calls, ", ") + " or {calc()}"
return strings.Join(calls, ", ") + " or {" + CalcName + "()}"
}
func plural(n int) string {
+3 -3
View File
@@ -64,9 +64,9 @@ var builtins = withTransforms(map[string]builtin{
cc := a[0]
return func(s *drawstate.State, _ string, _ []string) string { return iban(s, cc) }
}},
"date": {arity: -1, checkArgs: dateArgs, prep: datePrep},
"time": {arity: -1, checkArgs: timeArg, prep: timePrep},
Calc: {arity: -1, checkArgs: checkCalc, prep: calcPrep, operands: calcOperands, noRefOperands: true},
"date": {arity: -1, checkArgs: dateArgs, prep: datePrep},
"time": {arity: -1, checkArgs: timeArg, prep: timePrep},
CalcName: {arity: -1, checkArgs: checkCalc, prep: calcPrep, operands: calcOperands, noRefOperands: true},
// seq is the one stateful builtin: a per-generator counter from 1, advancing on
// each call. An optional name selects an independent counter; no name uses the
// default one. Deterministic by construction, so seeded output stays stable.
+2 -2
View File
@@ -47,8 +47,8 @@ func (v Value) Or(w Value) Value {
return v
}
// Calc bounds a calc expression from its operands, or says why it cannot.
func Calc(expr grammar.CalcNode, operand func(name string) Value) (Value, string) {
// OfCalc bounds a calc expression from its operands, or says why it cannot.
func OfCalc(expr grammar.CalcNode, operand func(name string) Value) (Value, string) {
v, doubt := bound(expr, operand)
if doubt == "" && !(magnitude(v) <= limit) {
doubt = grammar.CalcText(expr) + " is not proven within 1e300"
+7 -7
View File
@@ -41,7 +41,7 @@ func TestOrKeepsTheBoundsOfBothAndTheFirstReason(t *testing.T) {
}
}
func TestCalcBoundsAnExpressionFromItsOperands(t *testing.T) {
func TestOfCalcBoundsAnExpressionFromItsOperands(t *testing.T) {
operand := func(name string) Value {
return map[string]Value{"a": Bounded(1, 9, true), "b": Bounded(0, 9, true), "big": Bounded(0, 1e299, true), "word": Unproven("it is text")}[name]
}
@@ -57,20 +57,20 @@ func TestCalcBoundsAnExpressionFromItsOperands(t *testing.T) {
if err != nil {
t.Fatal(err)
}
v, doubt := Calc(c.Expr, operand)
v, doubt := OfCalc(c.Expr, operand)
if doubt != want {
t.Errorf("Calc(%s) doubts %q, want %q", expr, doubt, want)
t.Errorf("OfCalc(%s) doubts %q, want %q", expr, doubt, want)
}
if expr == "a * 2 + 1" && (v.Lo != 3 || v.Hi != 19 || !v.Integral || v.NonZero != 3) {
t.Errorf("Calc(%s) = %+v, want [3, 19], integral, nonzero by 3", expr, v)
t.Errorf("OfCalc(%s) = %+v, want [3, 19], integral, nonzero by 3", expr, v)
}
if expr == "-(a - 10)" && (v.Lo != 1 || v.Hi != 9) {
t.Errorf("Calc(%s) = %+v, want [1, 9]", expr, v)
t.Errorf("OfCalc(%s) = %+v, want [1, 9]", expr, v)
}
}
lone, _ := grammar.ParseCalc("x")
if _, doubt := Calc(lone.Expr, func(string) Value { return Bounded(0, 1e301, true) }); doubt != "x is not proven within 1e300" {
t.Errorf("Calc(x) doubts %q, want a lone operand held to the limit", doubt)
if _, doubt := OfCalc(lone.Expr, func(string) Value { return Bounded(0, 1e301, true) }); doubt != "x is not proven within 1e300" {
t.Errorf("OfCalc(x) doubts %q, want a lone operand held to the limit", doubt)
}
}