Documentation
¶
Index ¶
- Variables
- func Assign(c Context, op string, left, right Expr, rhs Value)
- func DebugProgString(p progStringer) string
- func FlushState(expr Expr)
- func IndexAssign(context Context, top, left Expr, lvarx *VarExpr, index []Expr, right Expr, ...)
- func IsCompound(x interface{}) bool
- func IsScalarType(c Context, v Value) bool
- func IvyPrint(c Context, out io.Writer, val Value, withParens bool)
- func MaxParallelismForTesting()
- func OrderedCompare(c Context, u, v Value) int
- func ParseString(c Context, s string) (string, error)
- func SetDebugContext(c Context)
- func TraceBinary(c Context, level int, u Value, op string, v Value)
- func TraceUnary(c Context, level int, op string, v Value)
- type BigFloat
- type BigInt
- type BigRat
- type BinaryExpr
- type BinaryOp
- type Char
- type ColonExpr
- type Complex
- type Context
- type Error
- type Expr
- type Frame
- type IfExpr
- type IndexExpr
- type Int
- type Matrix
- func (m *Matrix) Data() *Vector
- func (m *Matrix) ElemSize(c Context) int
- func (m *Matrix) Eval(Context) Value
- func (m *Matrix) Inner() Value
- func (m *Matrix) ProgString() string
- func (m *Matrix) Rank() int
- func (m *Matrix) Shape() []int
- func (m *Matrix) Size(c Context) int
- func (m *Matrix) Sprint(c Context) string
- func (m *Matrix) String() string
- type Pos
- type QuietValue
- type RetExpr
- type Statement
- type StatementList
- type Symtab
- type UnaryExpr
- type UnaryOp
- type Value
- func BinaryEach(c Context, lv Value, op string, rv Value) Value
- func Each(c Context, op string, v Value) Value
- func EvalBlock(context Context, fnName string, body StatementList) Value
- func EvalCharEqual(c Context, u Value, isEqualOp bool, v Value) (Value, bool)
- func EvalFunctionBody(context Context, fnName string, body StatementList, hasRet bool) (v Value)
- func Index(context Context, top, left Expr, index []Expr) Value
- func ParseNumber(c Context, s string) (Value, error)
- func Product(c Context, u Value, op string, v Value) Value
- func QuoRem(op string, c Context, a, b Value) (div, rem Value)
- func Reduce(c Context, op string, v Value) Value
- func ReduceFirst(c Context, op string, v Value) Value
- func Scan(c Context, op string, v Value) Value
- func ScanFirst(c Context, op string, v Value) Value
- type Var
- type VarExpr
- type VarState
- type Variable
- type Vector
- func (v *Vector) All() iter.Seq2[int, Value]
- func (v *Vector) AllChars() bool
- func (v *Vector) AllInts() bool
- func (v *Vector) At(i int) Value
- func (v *Vector) Eval(Context) Value
- func (v *Vector) Inner() Value
- func (v *Vector) Len() int
- func (v *Vector) ProgString() string
- func (v *Vector) Rank() int
- func (v *Vector) Slice(i, j int) iter.Seq2[int, Value]
- func (v *Vector) Sprint(c Context) string
- func (v *Vector) String() string
- type VectorExpr
- type WhileExpr
Constants ¶
This section is empty.
Variables ¶
var BinaryOps = make(map[string]BinaryOp)
var IvyEval func(context Context, s string) Value
Implemented in package run, handled as a func to avoid a dependency loop.
var (
MaxBigInt63 = big.NewInt(int64(^uint64(0) >> 1)) // Used in ../parse/special.go
)
var ( // The parameters for generating coefficients for our gamma apprximation. See // [The Gamma Function via Interpolation by Matthew F. Causley]: https://arxiv.org/pdf/2104.00697v1 // The method is a refinement of Spouge's (1994) approximation for the gamma // function. For most values, the technique allows us to create a // significantly more accurate approximation than Lanczos, which only gets // to about 12 digits. In some parts of the complex plane this code can get // above 50 digits of accuracy. See ../testdata/gamma for details. N is the // number of coefficients. r is chosen as optimizing for best fit at z=6. // That choice is arbitrary. 256 bits of mantissa suffice to hold these // constants, with room. N = 100 )
var UnaryOps = make(map[string]UnaryOp)
Functions ¶
func DebugProgString ¶ added in v0.5.0
func DebugProgString(p progStringer) string
DebugProgString builds a string representation of p, to be used in debugging and error messages. If the argument is a vector or matrix expression, it needs special handling to get parentheses and nesting; otherwise we could just call ProgString.
func FlushState ¶ added in v0.5.0
func FlushState(expr Expr)
FlushState flushes saved Statement parse information in the expression tree.
func IndexAssign ¶ added in v0.1.12
func IndexAssign(context Context, top, left Expr, lvarx *VarExpr, index []Expr, right Expr, rhs Value)
IndexAssign handles general assignment to indexed expressions on the LHS. Left and index will be evaluated (right to left), while top is only for DebugProgString. The caller must check that left is a variable expression and pass lvar, the variable corresponding to left.
func IsCompound ¶ added in v0.3.8
func IsCompound(x interface{}) bool
IsCompound reports whether the item is a non-trivial expression tree, one that may require parentheses around it when printed to maintain correct evaluation order.
func IsScalarType ¶ added in v0.3.0
IsScalarType reports whether u is an actual scalar, an int or float etc.
func IvyPrint ¶ added in v0.5.0
IvyPrint writes to out a version of the value that will recreate it when parsed. Its output depends on the context, unlike that of DebugProgString.
func MaxParallelismForTesting ¶ added in v0.1.12
func MaxParallelismForTesting()
func OrderedCompare ¶ added in v0.3.0
OrderedCompare returns -1, 0, or 1 according to whether u is less than, equal to, or greater than v, according to total ordering rules. Total ordering is not the usual mathematical definition, as we honor things like 1.0 == 1, comparison of int and char is forbidden, and complex numbers do not implement <. Plus other than for scalars we don't need to follow any particular rules at all, just what works for sorting sets.
Thus we amend the usual orderings: - Char is below all other types - All other scalars are compared directly, except... - ...Complex is above all other scalars, unless on the real line: 1j0 == 1. - Vector is above all other types except... - ...Matrix, which is above all other types.
When comparing identically-typed values:
- Complex is ordered first by real component, then by imaginary.
- Vector and Matrix are ordered first by number of elements, then in lexical order of elements.
These are unusual rules, but they are provide a unique ordering of elements sufficient for set membership. // Exported for testing, which is done by the parent directory to avoid a dependency cycle.
func ParseString ¶
ParseString parses a string. Single quotes and double quotes are both allowed (but must be consistent.) The result must contain only valid Unicode code points.
func SetDebugContext ¶ added in v0.4.2
func SetDebugContext(c Context)
func TraceBinary ¶ added in v0.3.10
TraceBinary prints a trace line for a binary operator.
Types ¶
type BigFloat ¶
func (BigFloat) Format ¶
func (f BigFloat) Format()
The fmt package looks for Formatter before Stringer, but we want to use Stringer only. big.Float implements Formatter, and we embed it in our BigFloat type. To make sure that our String gets called rather than the inner Format, we put a non-matching stub Format method into this interface. This is ugly but very simple and cheap.
func (BigFloat) ProgString ¶
type BigInt ¶
func (BigInt) Format ¶
func (i BigInt) Format()
The fmt package looks for Formatter before Stringer, but we want to use Stringer only. big.Int and big.Rat implement Formatter, and we embed them in our BigInt and BigRat types. To make sure that our String gets called rather than the inner Format, we put a non-matching stub Format method into this interface. This is ugly but very simple and cheap.
func (BigInt) ProgString ¶
type BigRat ¶
func (BigRat) ProgString ¶
type BinaryExpr ¶ added in v0.3.8
func (*BinaryExpr) Eval ¶ added in v0.3.8
func (b *BinaryExpr) Eval(context Context) Value
func (*BinaryExpr) ProgString ¶ added in v0.3.8
func (b *BinaryExpr) ProgString() string
type ColonExpr ¶ added in v0.4.1
ColonExpr is a conditional executor: expression ":" expression. It shortcuts execution of an StatementList.
func (*ColonExpr) ProgString ¶ added in v0.4.1
type Complex ¶ added in v0.2.0
type Complex struct {
// contains filtered or unexported fields
}
func NewComplex ¶ added in v0.3.0
func (Complex) Components ¶ added in v0.2.0
func (Complex) ProgString ¶ added in v0.2.0
type Context ¶
type Context interface {
// Config returns the configuration state for evaluation.
Config() *config.Config
// Local returns the named local variable.
Local(name string) *Var
// IsLocal reports whether the local variable is already defined.
IsLocal(name string) bool
// Global returns the named global variable.
// It returns nil if there is no such variable.
Global(name string) *Var
// AssignGlobal assigns to the named global variable,
// creating it if needed.
AssignGlobal(name string, value Value)
// Eval evaluates a list of expressions.
Eval(exprs []Expr) []Value
// EvalUnary evaluates a unary operator.
EvalUnary(op string, right Value) Value
// EvalBinary evaluates a binary operator.
EvalBinary(left Value, op string, right Value) Value
// UserDefined reports whether the specified op is user-defined.
UserDefined(op string, isBinary bool) bool
// Errorf reports an execution error and halts execution
// by panicking with type Error.
Errorf(format string, args ...interface{})
// TraceIndent returns an indentation marker showing the depth of the stack.
TraceIndent() string
// TopOfStack returns the top frame on the stack, or nil if there is no stack.
TopOfStack() *Frame
// StackTrace prints the execution stack.
StackTrace()
// DisableTracing can disable tracing for the "failed" error catcher.
DisableTracing(bool)
// Pos and SetPos handle recording of source position for error reports.
Pos() Pos
SetPos(file string, line, offset int)
}
Context is the execution context for evaluation. The only implementation is ../exec/Context, but the interface is defined separately, here, because of the dependence on Expr and the import cycle that would otherwise result.
type Expr ¶
type Expr interface {
// ProgString returns the unambiguous representation of the
// expression to be used in program source.
ProgString() string
Eval(Context) Value
}
Expr is the interface for a parsed expression. Also implemented by Value.
type Frame ¶ added in v0.4.2
type Frame struct {
Op string
IsBinary bool
Left Expr
Right Expr
Locals []Variable
Globals []Variable
Inited bool // Until set, tracebacks will not attempt to evaluate this frame.
Vars Symtab
}
Frame holds the local execution context for a user-defined op. Used to print helpful error tracebacks. May one day hold the local variables themselves.
type IfExpr ¶ added in v0.4.1
type IfExpr struct {
Cond Expr
Body StatementList
ElseBody StatementList
}
IfExpr is a conditional expression: ":if" expression; statementList [":else" statementList] ":end" If there is an ":elif", it has been parsed into a properly nested ":else" ":if".
func (*IfExpr) ProgString ¶ added in v0.4.1
type IndexExpr ¶ added in v0.3.8
func (*IndexExpr) ProgString ¶ added in v0.3.8
type Matrix ¶
type Matrix struct {
// contains filtered or unexported fields
}
func (*Matrix) ElemSize ¶
ElemSize returns the size of each top-level element of the matrix. Given shape [a, b, c, ...] it is b*c*....
func (*Matrix) ProgString ¶
type QuietValue ¶ added in v0.3.12
type QuietValue struct {
Value
}
QuietValue is an implementation of Value that is created as the result of an assignment or print operator. It can be type-asserted to discover whether to avoid printing the results of the expression.
type RetExpr ¶ added in v0.4.1
RetExpr is an early return from a function. See EvalFunctionBody.
func (*RetExpr) ProgString ¶ added in v0.4.1
type Statement ¶ added in v0.5.0
type Statement struct {
// contains filtered or unexported fields
}
A Statement is an expression, typically a line of input or element of a user-defined op. Unlike other expressions, it does not parse when created. Instead, only when first evaluated (through the Eval method) does it turn into a parsed expression. This is because the parse itself depends on external context that may be different at evaluation time than creation time, perhaps due to originally undefined variables or operators that have been defined since creation. One way this can arise is through mutual recursion:
op foo x = x==0: 1; bar x-1 op bar z = z==0: 0; foo z-1
Another issue is that whether an identifier is a variable or op may also depend on when it is evaluated:
1 j 2 # j is a binary operator; result 1j2 j = 5 # j is now a variable 1 j 2 # result: 1 5 2
Earlier versions of ivy parsed immediately, which caused a number of minor but niggling issues, including not being able to use j as a variable.
func NewStatement ¶ added in v0.5.0
NewStatement creates a statement (expression) defined by the tokens. inOperator records whether the statement is part of a user-defined op, which when true admits :ret expressions.
func (*Statement) Parse ¶ added in v0.5.0
Parse parses the Statement into an Expr. Because it's the APL way, we parse right to left.
func (*Statement) ProgString ¶ added in v0.5.0
func (*Statement) VarsAndRet ¶ added in v0.5.0
VarsAndRet returns a list of identifiers (which may or may not be variables; we find out when evaluating) in the body of the statement. It also reports whether the statement has a :ret.
type StatementList ¶ added in v0.5.0
type StatementList []Expr
func (StatementList) Eval ¶ added in v0.5.0
func (s StatementList) Eval(context Context) Value
func (StatementList) ProgString ¶ added in v0.5.0
func (s StatementList) ProgString() string
type Symtab ¶ added in v0.4.2
type Symtab []*Var
Symtab is a symbol table, a slice of variables. Once placed in a Frame, it is of fixed size.
type UnaryExpr ¶ added in v0.3.8
func (*UnaryExpr) ProgString ¶ added in v0.3.8
type Value ¶
type Value interface {
// String is for internal debugging only. It uses default configuration
// and puts parentheses around every value so it's clear when it is used.
// All user output should call Sprint instead.
String() string
Sprint(Context) string
// Eval evaluates (simplifies) the Value.
Eval(Context) Value
// Inner retrieves the value, without evaluation. But for Assignments,
// it returns the right-hand side.
Inner() Value
// Rank returns the rank of the value: 0 for scalar, 1 for vector, etc.
Rank() int
// ProgString is like String, but suitable for program listing.
// For instance, it ignores the user format for numbers and
// puts quotes on chars, guaranteeing a correct representation.
ProgString() string
// contains filtered or unexported methods
}
func BinaryEach ¶ added in v0.3.7
BinaryEach computes the result of applying op to lv and rv, applying the "each" expansion depending on how many times @ appears on the left and right ends of op.
func Each ¶ added in v0.3.7
Each computes the result of running op on each element of v. The trailing @ has been removed.
func EvalBlock ¶ added in v0.4.1
func EvalBlock(context Context, fnName string, body StatementList) Value
EvalBlock evaluates the list of expressions inside a block, with the empty expression as the default value.
func EvalCharEqual ¶ added in v0.3.0
EvalCharEqual handles == and != in a special case: If comparing a scalar against a Char, avoid the conversion. The logic of type promotion in EvalBinary otherwise interferes with comparison because it tries to force scalar types to be the same, and char doesn't convert to any other type.
func EvalFunctionBody ¶ added in v0.1.1
func EvalFunctionBody(context Context, fnName string, body StatementList, hasRet bool) (v Value)
EvalFunctionBody evaluates the list of expressions inside a function, with no default value.
func Index ¶ added in v0.1.12
Index returns left[index]. Left and index will be evaluated (right to left), while top is only for DebugProgString.
func ParseNumber ¶ added in v0.5.0
ParseNumber parses a string to create a number.
func Product ¶
Product computes a compound product, such as an inner product "+.*" or outer product "o.*". The op is known to contain a period. The operands are all at least vectors, and for inner product they must both be vectors.
func QuoRem ¶ added in v0.3.0
QuoRem uses Euclidean division to return the quotient and remainder for a/b. The quotient will be an integer, possibly negative; the remainder is always positive and may be fractional. Returned values satisfy the identity that
quo = a div b such that rem = a - b*quo with 0 <= rem < |y|
See comment for math/big.Int.DivMod for details. Exported for testing.
func ReduceFirst ¶ added in v0.3.0
ReduceFirst computes a reduction such as +/% along the first axis. The slash-percent has been removed.
type Var ¶ added in v0.3.12
type Var struct {
// contains filtered or unexported fields
}
A Var is a named variable in an Ivy execution.
type VarExpr ¶ added in v0.3.8
type VarExpr struct {
Name string
// contains filtered or unexported fields
}
VarExpr identifies a variable to be looked up and evaluated.
func NewVarExpr ¶ added in v0.3.12
func (*VarExpr) ProgString ¶ added in v0.3.8
type VarState ¶ added in v0.5.0
type VarState int
VarState holds the dynamic state of a variable, which is determined by the first action taken with it. Assuming we are in a user-defined op:
Unknown: Nothing has happened yet. LocalVar: The first action was an assignment. GlobalVar: The first action was a read (evaluation).
Within an op, a Var identifying a global variable holds only this state. The data itself is in the globals table. Implemented by the Assign and VarExpr.Eval functions.
type Variable ¶ added in v0.5.0
A Variable is a name mentioned in a function and records the first action done to the associated variable.
type Vector ¶
type Vector struct {
// contains filtered or unexported fields
}
func NewIntVector ¶
func NewVectorSeq ¶ added in v0.3.12
NewVectorSeq creates a new vector from a sequence.
func (*Vector) ProgString ¶
type VectorExpr ¶ added in v0.3.8
type VectorExpr []Expr
VectorExpr holds a syntactic vector to be verified and evaluated.
func (VectorExpr) Eval ¶ added in v0.3.8
func (e VectorExpr) Eval(context Context) Value
func (VectorExpr) ProgString ¶ added in v0.3.8
func (e VectorExpr) ProgString() string
type WhileExpr ¶ added in v0.4.1
type WhileExpr struct {
Cond Expr
Body StatementList
}
WhileExpr is a loop expression: ":while" expression; statementList; ":end"