# The infix-math Reference Manual

Next: , Previous: , Up: (dir)   [Contents][Index]

# The infix-math Reference Manual

This is the infix-math Reference Manual, generated automatically by Declt version 2.3 "Robert April" on Wed Mar 14 04:04:43 2018 GMT+0.

Next: , Previous: , Up: Top   [Contents][Index]

# Infix-Math

Infix-Math is a library that provides a special-purpose syntax for transcribing mathematical formulas into Lisp.

Bitter experience has taught me that the more the formula on screen resembles the formula on paper, the better. The more the formula on screen resembles the formula on paper, the easier it is to prevent bugs from transcription errors. The easier it is to prevent transcription errors, the easier it is to trace the source of any bugs that do occur – because sometimes the formula is wrong.

(Having to transcribe formulas from crooked, blurry scans of ancient pre-LaTeX typescripts is bad enough without having to parse operator precedence in your head.)

Even if you end up rewriting the formula for speed or numerical stability, having the specification in an executable form is invaluable for reference and testing.

## Examples

The macro `\$` is the entry point into Infix-Math.

``````(\$ 2 + 2)     => 4
(\$ 1 + 2 * 3) => 7
``````

Operator precedence parsing in Infix-Math is reliable – it uses Dijkstra’s shunting yard algorithm.

The parser automatically descends into function argument lists, which means that the total number of parentheses is never greater than it would be in a purely infix language.

``````(\$ (tan pi * (p - 1/2)))
≡ (tan (* pi (- p 1/2)))
≅ tan(pi*(p-0.5))
``````

Common subexpression elimination is automatic and aggressive. All forms are assumed to be pure. Math does not have side effects.

``````(macroexpand '(\$ 2 ^ 2x * 2 ^ 2x)
=> ‘(let ((#:subexp11325 (^ 2 (* 2 x))))
(* #:subexp11325 #:subexp11325))
``````

Infix-Math knows about the following arithmetic and bitwise operators, in descending order of precedence.

• unary -, sqrt
• expt, log
• *, /, rem, mod, floor, ffloor, ceiling, fceiling, truncate, ftruncate, round, fround, scale-float, gcd, lcm, atan
• +, -
• ash
• logand, logandc1, logandc2, lognand
• logxor, logeqv
• logior, logorc1, logorc2, lognor
• min, max
• over

Operations at the same level of precedence are always evaluated left-to-right.

``````(+ 0.1d0 (+ 0.2d0 0.3d0)) => 0.6d0
(+ (+ 0.1d0 0.2d0) 0.3d0) => 0.6000000000000001D0
(\$ 0.1d0 + 0.2d0 + 0.3d0) => 0.6000000000000001D0
``````

Parentheses can be used for grouping.

``````(\$ 0.1d0 + (0.2d0 + 0.3d0)) => 0.6d0
``````

Variables can be written with literal numbers as coefficients.

``````(\$ 2x)  => 10
(\$ -2x) => 10
``````

Literal coefficients have very high priority.

``````(\$ 2 ^ 2 * x) ≡ (* (expt 2 2) x)     => 20
(\$ 2 ^ 2x)    ≡ (expt 2 (* 2 x))     => 1024
``````

A literal coefficient of 1 can be omitted.

``````(\$ -x) ≡ (\$ -1x) ≡ (* -1 x)
``````

Literal coefficients are parsed as decimals, rather than floats.

``````(\$ 1.5x) ≡ (* 3/2 x)
``````

You can also use fractions as literal coefficients.

``````(\$ 1/3x) ≡ (* 1/3 x)
``````

Among other things, literal coefficients are very convenient for units of measurement.

(The idea for literal coefficients comes from Julia.)

## Symbols

Infix-Math exports only five symbols: `\$`, `^`, `over`, and two macros for declaring operators: `declare-unary-operator` and `declare-binary-operator`.

The symbol `^` is just a shorthand for `expt`.

``````(\$ 1 + 2 * 3 ^ 4) => 163
``````

(`^` is from Dylan.)

The symbol `over` represents the same operation as `/`, but at a much lower priority. Using `over` lets you avoid introducing parentheses for grouping when transcribing fractions.

``````(setf x 5)
(\$ x * 2 / x * 3)     ≡ (* (/ (* x 2) x) 3) => 6
(\$ (x * 2) / (x * 3)) ≡ (/ (* x 2) (* x 3)) => 2/3
(\$ x * 2 over x * 3)  ≡ (/ (* x 2) (* x 3)) => 2/3
``````

You can also spell `over` with a series of dashes or underscores.

``````(\$ x * 2
-----
x * 3)
=> 2/3
``````

If you want more math symbols, the package `infix-math/symbols` provides a few more.

## Calculator

You can use Infix-Math to turn your REPL into a calculator.

First, load the `infix-math/calc` system:

``````(asdf:load-system "infix-math/calc")
``````

Then, at the REPL, start the calculator:

``````(infix-math/calc:calc)
``````

This will put you at a calculator prompt. You can type in mathematical expressions directly:

``````\$> 2 + 2
4
``````

A single form entered at the REPL is interpreted as ordinary CL.

``````\$> *package*
:infix-math/calc-user
``````

You can assign to variables using the `<-` operator.

``````\$> x <- 2 + 2
4
\$> x
4
``````

Certain one-letter variables are provided for you to assign to, such as `x`, `y`, and `z`. You can see the full list by evaluating `:v` at the calculator prompt.

To quit, use `:q`. The value of the last expression evaluated will be returned.

``````\$> 2 + 2
4
\$> :q
4
CL-USER> *
4
``````

## Extending

Infix-Math is easily to extend. In fact, you may not even need to extend it.

Any symbol that consists entirely of operator characters is interpreted as an infix operator, with the highest non-unary priority. Operator characters are anything but dashes, underscores, whitespace or alphanumeric characters.

``````(defun <*> (x y)
"Matrix multiplication, maybe."
...)

(macroexpand '(\$ x * y <*> z)) => (* x (<*> y z))
``````

(This approach is taken from Haskell.)

You can use any function as an infix operator by surrounding its name with dots.

``````(defun choose (n k)
"Binomial coefficient, maybe."
...)

(macroexpand '(\$ n .choose. k)) => '(choose n k)
``````

Again, the operator has the highest non-unary priority.

(This approach is taken from Haskell and Fortran.)

If you need more flexibility, declare the operators using `declare-binary-operator` or `declare-unary-operator`.

To declare a unary operator:

``````(declare-unary-operator √)
``````

To copy the precedence of another operator:

``````(declare-binary-operator <*> :from *)
``````

To declare an operator right-associative:

``````(declare-binary-operator ?
:from *
:right-associative t)
``````

Next: , Previous: , Up: Top   [Contents][Index]

## 2 Systems

The main system appears first, followed by any subsystem dependency.

Next: , Previous: , Up: Systems   [Contents][Index]

### 2.1 infix-math

Author

Paul M. Rodriguez <pmr@ruricolist.com>

MIT

Description

An extensible infix syntax for math in Common Lisp.

Defsystem Dependency

asdf-package-system

Dependency

infix-math/infix-math (system)

Source

infix-math.asd (file)

Next: , Previous: , Up: Systems   [Contents][Index]

### 2.2 infix-math/infix-math

Dependencies
Source

infix-math.asd (file)

Component

lisp.lisp (file)

Next: , Previous: , Up: Systems   [Contents][Index]

### 2.3 infix-math/data

Dependencies
Source

infix-math.asd (file)

Component

lisp.lisp (file)

Previous: , Up: Systems   [Contents][Index]

### 2.4 infix-math/symbols

Source

infix-math.asd (file)

Component

lisp.lisp (file)

Next: , Previous: , Up: Top   [Contents][Index]

## 3 Files

Files are sorted by type and then listed depth-first from the systems components trees.

Previous: , Up: Files   [Contents][Index]

### 3.1 Lisp

Next: , Previous: , Up: Lisp files   [Contents][Index]

#### 3.1.1 infix-math.asd

Location

infix-math.asd

Systems

Next: , Previous: , Up: Lisp files   [Contents][Index]

#### 3.1.2 infix-math/infix-math/lisp.lisp

Parent

infix-math/infix-math (system)

Location

infix-math.lisp

Packages
Exported Definitions

\$ (macro)

Internal Definitions

#### 3.1.3 infix-math/data/lisp.lisp

Parent

infix-math/data (system)

Location

data.lisp

Packages
Exported Definitions
Internal Definitions

Previous: , Up: Lisp files   [Contents][Index]

#### 3.1.4 infix-math/symbols/lisp.lisp

Parent

infix-math/symbols (system)

Location

symbols.lisp

Packages
Exported Definitions
• % (compiler macro)
• % (function)
• & (compiler macro)
• & (function)
• << (compiler macro)
• << (function)
• >> (compiler macro)
• >> (function)
• ^ (compiler macro)
• ^ (function)
• over (compiler macro)
• over (function)
• × (compiler macro)
• × (function)
• ÷ (compiler macro)
• ÷ (function)
• π (symbol macro)
• (compiler macro)
• (function)
Internal Definitions

Next: , Previous: , Up: Top   [Contents][Index]

## 4 Packages

Packages are listed by definition order.

Next: , Previous: , Up: Packages   [Contents][Index]

### 4.1 infix-math/infix-math

Source

lisp.lisp (file)

Nickname

infix-math

Use List
Exported Definitions

\$ (macro)

Internal Definitions

Next: , Previous: , Up: Packages   [Contents][Index]

### 4.2 infix-math/data

Source

lisp.lisp (file)

Use List
Used By List
Exported Definitions
Internal Definitions

Previous: , Up: Packages   [Contents][Index]

### 4.3 infix-math/symbols

Source

lisp.lisp (file)

Use List

common-lisp

Used By List
Exported Definitions
• % (compiler macro)
• % (function)
• & (compiler macro)
• & (function)
• << (compiler macro)
• << (function)
• >> (compiler macro)
• >> (function)
• ^ (compiler macro)
• ^ (function)
• over (compiler macro)
• over (function)
• × (compiler macro)
• × (function)
• ÷ (compiler macro)
• ÷ (function)
• π (symbol macro)
• (compiler macro)
• (function)
Internal Definitions

Next: , Previous: , Up: Top   [Contents][Index]

## 5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.

Next: , Previous: , Up: Definitions   [Contents][Index]

### 5.1 Exported definitions

Next: , Previous: , Up: Exported definitions   [Contents][Index]

#### 5.1.1 Symbol macros

Symbol Macro: π
Package
Source

lisp.lisp (file)

Expansion

pi

Next: , Previous: , Up: Exported definitions   [Contents][Index]

#### 5.1.2 Macros

Macro: \$ &rest FORMULA

Compile a mathematical formula in infix notation.

Package
Source

lisp.lisp (file)

Macro: declare-binary-operator NEW &body &key
Package
Source

lisp.lisp (file)

Macro: declare-unary-operator NAME
Package
Source

lisp.lisp (file)

Macro: unary OP ARG

Pretend unary operators are binary operators.

Package
Source

lisp.lisp (file)

Next: , Previous: , Up: Exported definitions   [Contents][Index]

#### 5.1.3 Compiler macros

Compiler Macro: % A B
Package
Source

lisp.lisp (file)

Compiler Macro: & A B
Package
Source

lisp.lisp (file)

Compiler Macro: << A B
Package
Source

lisp.lisp (file)

Compiler Macro: >> A B
Package
Source

lisp.lisp (file)

Compiler Macro: ^ A B
Package
Source

lisp.lisp (file)

Compiler Macro: over A B
Package
Source

lisp.lisp (file)

Compiler Macro: × A B
Package
Source

lisp.lisp (file)

Compiler Macro: ÷ A B
Package
Source

lisp.lisp (file)

Compiler Macro: A
Package
Source

lisp.lisp (file)

Next: , Previous: , Up: Exported definitions   [Contents][Index]

#### 5.1.4 Functions

Function: % A B
Package
Source

lisp.lisp (file)

Function: & A B
Package
Source

lisp.lisp (file)

Function: << A B
Package
Source

lisp.lisp (file)

Function: >> A B
Package
Source

lisp.lisp (file)

Function: ^ A B
Package
Source

lisp.lisp (file)

Function: operator? OPERATOR
Package
Source

lisp.lisp (file)

Function: over A B
Package
Source

lisp.lisp (file)

Function: precedence OPERATOR
Function: (setf precedence) VALUE OPERATOR
Package
Source

lisp.lisp (file)

Function: right-associative? OPERATOR
Function: (setf right-associative?) VALUE OPERATOR
Package
Source

lisp.lisp (file)

Function: trim-dotted-operator OPERATOR
Package
Source

lisp.lisp (file)

Function: unary? OPERATOR
Function: (setf unary?) VALUE OPERATOR
Package
Source

lisp.lisp (file)

Function: × A B
Package
Source

lisp.lisp (file)

Function: ÷ A B
Package
Source

lisp.lisp (file)

Function: A
Package
Source

lisp.lisp (file)

Previous: , Up: Exported definitions   [Contents][Index]

#### 5.1.5 Types

Type: precedence ()
Package
Source

lisp.lisp (file)

Previous: , Up: Definitions   [Contents][Index]

### 5.2 Internal definitions

Next: , Previous: , Up: Internal definitions   [Contents][Index]

#### 5.2.1 Special variables

Special Variable: *order-of-operations*

Basic C-style operator precedence, with some differences.

The use of MIN, MAX, GCD and LCM as infix operators is after Dijkstra (see EWD 1300). Perl 6 is also supposed to use them this way, and I have adopted its precedence levels.

Package
Source

lisp.lisp (file)

Special Variable: *precedence*

Table of operator precedence.

Package
Source

lisp.lisp (file)

Special Variable: *right-associative*
Package
Source

lisp.lisp (file)

Special Variable: *unary*
Package
Source

lisp.lisp (file)

Next: , Previous: , Up: Internal definitions   [Contents][Index]

#### 5.2.2 Symbol macros

Symbol Macro: e
Package
Source

lisp.lisp (file)

Expansion

(exp 1.0d0)

Symbol Macro: i
Package
Source

lisp.lisp (file)

Expansion

(sqrt -1)

Next: , Previous: , Up: Internal definitions   [Contents][Index]

#### 5.2.3 Macros

Macro: binary-operator NEW OLD
Package
Source

lisp.lisp (file)

Macro: binary-operators &body BODY
Package
Source

lisp.lisp (file)

Macro: nodef PLACE OPERATOR
Package
Source

lisp.lisp (file)

Macro: unary-operator NEW OLD
Package
Source

lisp.lisp (file)

Macro: unary-operators &body BODY
Package
Source

lisp.lisp (file)

Next: , Previous: , Up: Internal definitions   [Contents][Index]

#### 5.2.4 Functions

Function: ash- I C
Package
Source

lisp.lisp (file)

Function: dotted-operator? SYM
Package
Source

lisp.lisp (file)

Function: eliminate-common-subexpressions FORM &optional ENV
Package
Source

lisp.lisp (file)

Function: expand-expression EXPRS
Package
Source

lisp.lisp (file)

Function: expand-fancy-symbols FORM

Expand -x into (- x) and 2x into (* 2 x).

Literal coefficients have the same precedence as unary operators.

Literal coefficients are assumed to be in base 10.

Package
Source

lisp.lisp (file)

Function: looks-like-operator? SYM

Does SYM start and end with an operator char?

Package
Source

lisp.lisp (file)

Function: make-node TREE OPERATOR
Package
Source

lisp.lisp (file)

Function: operator-char? C
Package
Source

lisp.lisp (file)

Function: parse-coefficient STR
Package
Source

lisp.lisp (file)

Function: parse-expression EXPRESSION
Package
Source

lisp.lisp (file)

Function: precedence< OP1 OP2
Package
Source

lisp.lisp (file)

Function: precedence= OP1 OP2
Package
Source

lisp.lisp (file)

Function: save-operator &key NAME FROM RIGHT-ASSOCIATIVE
Package
Source

lisp.lisp (file)

Function: save-unary-operator NAME
Package
Source

lisp.lisp (file)

Function: shunting-yard EXPRESSION &aux TREE STACK
Package
Source

lisp.lisp (file)

Function: valid? EXPRESSION
Package
Source

lisp.lisp (file)

Previous: , Up: Internal definitions   [Contents][Index]

#### 5.2.5 Types

Type: operator ()
Package
Source

lisp.lisp (file)

Previous: , Up: Top   [Contents][Index]

## Appendix A Indexes

Next: , Previous: , Up: Indexes   [Contents][Index]

### A.1 Concepts

Next: , Previous: , Up: Indexes   [Contents][Index]

### A.2 Functions

Jump to: \$   %   &   (   <   >   ^   ×   ÷   √   A   B   C   D   E   F   L   M   N   O   P   R   S   T   U   V
Jump to: \$   %   &   (   <   >   ^   ×   ÷   √   A   B   C   D   E   F   L   M   N   O   P   R   S   T   U   V

Next: , Previous: , Up: Indexes   [Contents][Index]