The infix-math Reference Manual

Table of Contents

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

The infix-math Reference Manual

This is the infix-math Reference Manual, generated automatically by Declt version 2.4 "Will Decker" on Wed Jun 20 12:01:16 2018 GMT+0.


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

1 Introduction

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.

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>

License

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

infix-math/infix-math

Exported Definitions

$ (macro)

Internal Definitions

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

3.1.3 infix-math/data/lisp.lisp

Parent

infix-math/data (system)

Location

data.lisp

Packages

infix-math/data

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

infix-math/symbols

Exported Definitions
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

infix-math/infix-math

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
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

infix-math/symbols

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

infix-math/infix-math

Source

lisp.lisp (file)

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

infix-math/data

Source

lisp.lisp (file)

Macro: declare-unary-operator NAME
Package

infix-math/data

Source

lisp.lisp (file)

Macro: unary OP ARG

Pretend unary operators are binary operators.

Package

infix-math/data

Source

lisp.lisp (file)


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

5.1.3 Compiler macros

Compiler Macro: % A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Compiler Macro: & A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Compiler Macro: << A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Compiler Macro: >> A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Compiler Macro: ^ A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Compiler Macro: over A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Compiler Macro: × A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Compiler Macro: ÷ A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Compiler Macro: A
Package

infix-math/symbols

Source

lisp.lisp (file)


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

5.1.4 Functions

Function: % A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Function: & A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Function: << A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Function: >> A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Function: ^ A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Function: operator? OPERATOR
Package

infix-math/data

Source

lisp.lisp (file)

Function: over A B
Package

infix-math/symbols

Source

lisp.lisp (file)

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

infix-math/data

Source

lisp.lisp (file)

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

infix-math/data

Source

lisp.lisp (file)

Function: trim-dotted-operator OPERATOR
Package

infix-math/data

Source

lisp.lisp (file)

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

infix-math/data

Source

lisp.lisp (file)

Function: × A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Function: ÷ A B
Package

infix-math/symbols

Source

lisp.lisp (file)

Function: A
Package

infix-math/symbols

Source

lisp.lisp (file)


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

5.1.5 Types

Type: precedence ()
Package

infix-math/data

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

infix-math/data

Source

lisp.lisp (file)

Special Variable: *precedence*

Table of operator precedence.

Package

infix-math/data

Source

lisp.lisp (file)

Special Variable: *right-associative*
Package

infix-math/data

Source

lisp.lisp (file)

Special Variable: *unary*
Package

infix-math/data

Source

lisp.lisp (file)


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

5.2.2 Symbol macros

Symbol Macro: e
Package

infix-math/symbols

Source

lisp.lisp (file)

Expansion

(exp 1.0d0)

Symbol Macro: i
Package

infix-math/symbols

Source

lisp.lisp (file)

Expansion

(sqrt -1)


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

5.2.3 Macros

Macro: binary-operator NEW OLD
Package

infix-math/symbols

Source

lisp.lisp (file)

Macro: binary-operators &body BODY
Package

infix-math/symbols

Source

lisp.lisp (file)

Macro: nodef PLACE OPERATOR
Package

infix-math/infix-math

Source

lisp.lisp (file)

Macro: unary-operator NEW OLD
Package

infix-math/symbols

Source

lisp.lisp (file)

Macro: unary-operators &body BODY
Package

infix-math/symbols

Source

lisp.lisp (file)


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

5.2.4 Functions

Function: ash- I C
Package

infix-math/symbols

Source

lisp.lisp (file)

Function: dotted-operator? SYM
Package

infix-math/data

Source

lisp.lisp (file)

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

infix-math/infix-math

Source

lisp.lisp (file)

Function: expand-expression EXPRS
Package

infix-math/infix-math

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

infix-math/infix-math

Source

lisp.lisp (file)

Function: looks-like-operator? SYM

Does SYM start and end with an operator char?

Package

infix-math/data

Source

lisp.lisp (file)

Function: make-node TREE OPERATOR
Package

infix-math/infix-math

Source

lisp.lisp (file)

Function: operator-char? C
Package

infix-math/data

Source

lisp.lisp (file)

Function: parse-coefficient STR
Package

infix-math/infix-math

Source

lisp.lisp (file)

Function: parse-expression EXPRESSION
Package

infix-math/infix-math

Source

lisp.lisp (file)

Function: precedence< OP1 OP2
Package

infix-math/infix-math

Source

lisp.lisp (file)

Function: precedence= OP1 OP2
Package

infix-math/infix-math

Source

lisp.lisp (file)

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

infix-math/data

Source

lisp.lisp (file)

Function: save-unary-operator NAME
Package

infix-math/data

Source

lisp.lisp (file)

Function: shunting-yard EXPRESSION &aux TREE STACK
Package

infix-math/infix-math

Source

lisp.lisp (file)

Function: valid? EXPRESSION
Package

infix-math/infix-math

Source

lisp.lisp (file)


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

5.2.5 Types

Type: operator ()
Package

infix-math/data

Source

lisp.lisp (file)


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

Appendix A Indexes


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

A.1 Concepts

Jump to:   F   I   L  
Index Entry  Section

F
File, Lisp, infix-math.asd: The infix-math<dot>asd file
File, Lisp, infix-math/data/lisp.lisp: The infix-math/data/lisp<dot>lisp file
File, Lisp, infix-math/infix-math/lisp.lisp: The infix-math/infix-math/lisp<dot>lisp file
File, Lisp, infix-math/symbols/lisp.lisp: The infix-math/symbols/lisp<dot>lisp file

I
infix-math.asd: The infix-math<dot>asd file
infix-math/data/lisp.lisp: The infix-math/data/lisp<dot>lisp file
infix-math/infix-math/lisp.lisp: The infix-math/infix-math/lisp<dot>lisp file
infix-math/symbols/lisp.lisp: The infix-math/symbols/lisp<dot>lisp file

L
Lisp File, infix-math.asd: The infix-math<dot>asd file
Lisp File, infix-math/data/lisp.lisp: The infix-math/data/lisp<dot>lisp file
Lisp File, infix-math/infix-math/lisp.lisp: The infix-math/infix-math/lisp<dot>lisp file
Lisp File, infix-math/symbols/lisp.lisp: The infix-math/symbols/lisp<dot>lisp file

Jump to:   F   I   L  

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  
Index Entry  Section

$
$: Exported macros

%
%: Exported compiler macros
%: Exported functions

&
&: Exported compiler macros
&: Exported functions

(
(setf precedence): Exported functions
(setf right-associative?): Exported functions
(setf unary?): Exported functions

<
<<: Exported compiler macros
<<: Exported functions

>
>>: Exported compiler macros
>>: Exported functions

^
^: Exported compiler macros
^: Exported functions

×
×: Exported compiler macros
×: Exported functions

÷
÷: Exported compiler macros
÷: Exported functions

: Exported compiler macros
: Exported functions

A
ash-: Internal functions

B
binary-operator: Internal macros
binary-operators: Internal macros

C
Compiler Macro, %: Exported compiler macros
Compiler Macro, &: Exported compiler macros
Compiler Macro, <<: Exported compiler macros
Compiler Macro, >>: Exported compiler macros
Compiler Macro, over: Exported compiler macros
Compiler Macro, ^: Exported compiler macros
Compiler Macro, ×: Exported compiler macros
Compiler Macro, ÷: Exported compiler macros
Compiler Macro, : Exported compiler macros

D
declare-binary-operator: Exported macros
declare-unary-operator: Exported macros
dotted-operator?: Internal functions

E
eliminate-common-subexpressions: Internal functions
expand-expression: Internal functions
expand-fancy-symbols: Internal functions

F
Function, %: Exported functions
Function, &: Exported functions
Function, (setf precedence): Exported functions
Function, (setf right-associative?): Exported functions
Function, (setf unary?): Exported functions
Function, <<: Exported functions
Function, >>: Exported functions
Function, ash-: Internal functions
Function, dotted-operator?: Internal functions
Function, eliminate-common-subexpressions: Internal functions
Function, expand-expression: Internal functions
Function, expand-fancy-symbols: Internal functions
Function, looks-like-operator?: Internal functions
Function, make-node: Internal functions
Function, operator-char?: Internal functions
Function, operator?: Exported functions
Function, over: Exported functions
Function, parse-coefficient: Internal functions
Function, parse-expression: Internal functions
Function, precedence: Exported functions
Function, precedence<: Internal functions
Function, precedence=: Internal functions
Function, right-associative?: Exported functions
Function, save-operator: Internal functions
Function, save-unary-operator: Internal functions
Function, shunting-yard: Internal functions
Function, trim-dotted-operator: Exported functions
Function, unary?: Exported functions
Function, valid?: Internal functions
Function, ^: Exported functions
Function, ×: Exported functions
Function, ÷: Exported functions
Function, : Exported functions

L
looks-like-operator?: Internal functions

M
Macro, $: Exported macros
Macro, binary-operator: Internal macros
Macro, binary-operators: Internal macros
Macro, declare-binary-operator: Exported macros
Macro, declare-unary-operator: Exported macros
Macro, nodef: Internal macros
Macro, unary: Exported macros
Macro, unary-operator: Internal macros
Macro, unary-operators: Internal macros
make-node: Internal functions

N
nodef: Internal macros

O
operator-char?: Internal functions
operator?: Exported functions
over: Exported compiler macros
over: Exported functions

P
parse-coefficient: Internal functions
parse-expression: Internal functions
precedence: Exported functions
precedence<: Internal functions
precedence=: Internal functions

R
right-associative?: Exported functions

S
save-operator: Internal functions
save-unary-operator: Internal functions
shunting-yard: Internal functions

T
trim-dotted-operator: Exported functions

U
unary: Exported macros
unary-operator: Internal macros
unary-operators: Internal macros
unary?: Exported functions

V
valid?: Internal functions

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

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

A.3 Variables

Jump to:   *  
E   I   S   Π  
Index Entry  Section

*
*order-of-operations*: Internal special variables
*precedence*: Internal special variables
*right-associative*: Internal special variables
*unary*: Internal special variables

E
e: Internal symbol macros

I
i: Internal symbol macros

S
Special Variable, *order-of-operations*: Internal special variables
Special Variable, *precedence*: Internal special variables
Special Variable, *right-associative*: Internal special variables
Special Variable, *unary*: Internal special variables
Symbol Macro, e: Internal symbol macros
Symbol Macro, i: Internal symbol macros
Symbol Macro, π: Exported symbol macros

Π
π: Exported symbol macros

Jump to:   *  
E   I   S   Π  

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

A.4 Data types

Jump to:   I   O   P   S   T  
Index Entry  Section

I
infix-math: The infix-math system
infix-math/data: The infix-math/data system
infix-math/data: The infix-math/data package
infix-math/infix-math: The infix-math/infix-math system
infix-math/infix-math: The infix-math/infix-math package
infix-math/symbols: The infix-math/symbols system
infix-math/symbols: The infix-math/symbols package

O
operator: Internal types

P
Package, infix-math/data: The infix-math/data package
Package, infix-math/infix-math: The infix-math/infix-math package
Package, infix-math/symbols: The infix-math/symbols package
precedence: Exported types

S
System, infix-math: The infix-math system
System, infix-math/data: The infix-math/data system
System, infix-math/infix-math: The infix-math/infix-math system
System, infix-math/symbols: The infix-math/symbols system

T
Type, operator: Internal types
Type, precedence: Exported types

Jump to:   I   O   P   S   T