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 4.0 beta 2 "William Riker" on Wed Jun 15 04:50:43 2022 GMT+0.

Table of Contents


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)

2 Systems

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


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

2.1 infix-math

An extensible infix syntax for math in Common Lisp.

Author

Paul M. Rodriguez <pmr@ruricolist.com>

License

MIT

Defsystem Dependency

asdf-package-system (system).

Dependency

infix-math/infix-math (system).

Source

infix-math.asd.


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

2.2 infix-math/infix-math

Author

Paul M. Rodriguez <pmr@ruricolist.com>

License

MIT

Dependencies
Source

infix-math.asd.


2.3 infix-math/symbols

Author

Paul M. Rodriguez <pmr@ruricolist.com>

License

MIT

Source

infix-math.asd.


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

2.4 infix-math/data

Author

Paul M. Rodriguez <pmr@ruricolist.com>

License

MIT

Dependencies
Source

infix-math.asd.


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


3.1.1 infix-math/infix-math.asd

Source

infix-math.asd.

Parent Component

infix-math (system).

ASDF Systems

3.1.2 infix-math/infix-math/file-type.lisp

Source

infix-math.asd.

Parent Component

infix-math/infix-math (system).

Packages

infix-math/infix-math.

Public Interface

$ (macro).

Internals

3.1.3 infix-math/symbols/file-type.lisp

Source

infix-math.asd.

Parent Component

infix-math/symbols (system).

Packages

infix-math/symbols.

Public Interface
  • % (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).
Internals

3.1.4 infix-math/data/file-type.lisp

Source

infix-math.asd.

Parent Component

infix-math/data (system).

Packages

infix-math/data.

Public Interface
Internals

4 Packages

Packages are listed by definition order.


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

4.1 infix-math/infix-math

Source

file-type.lisp.

Nickname

infix-math

Use List
Public Interface

$ (macro).

Internals

4.2 infix-math/symbols

Source

file-type.lisp.

Use List

common-lisp.

Used By List
Public Interface
  • % (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).
Internals

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

4.3 infix-math/data

Source

file-type.lisp.

Use List
Used By List

infix-math/infix-math.

Public Interface
Internals

5 Definitions

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


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

5.1 Public Interface


Next: , Previous: , Up: Public Interface   [Contents][Index]

5.1.1 Symbol macros

Symbol Macro: π
Package

infix-math/symbols.

Source

file-type.lisp.


5.1.2 Macros

Macro: $ (&rest formula)

Compile a mathematical formula in infix notation.

Package

infix-math/infix-math.

Source

file-type.lisp.

Macro: declare-binary-operator (new &body &key)
Package

infix-math/data.

Source

file-type.lisp.

Macro: declare-unary-operator (name)
Package

infix-math/data.

Source

file-type.lisp.

Macro: unary (op arg)

Pretend unary operators are binary operators.

Package

infix-math/data.

Source

file-type.lisp.


5.1.3 Compiler macros

Compiler Macro: % (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Compiler Macro: & (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Compiler Macro: << (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Compiler Macro: >> (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Compiler Macro: ^ (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Compiler Macro: over (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Compiler Macro: × (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Compiler Macro: ÷ (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Compiler Macro: (a)
Package

infix-math/symbols.

Source

file-type.lisp.


Next: , Previous: , Up: Public Interface   [Contents][Index]

5.1.4 Ordinary functions

Function: % (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Function: & (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Function: << (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Function: >> (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Function: ^ (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Function: operator? (operator)
Package

infix-math/data.

Source

file-type.lisp.

Function: over (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Function: precedence (operator)
Package

infix-math/data.

Source

file-type.lisp.

Function: (setf precedence) (operator)
Package

infix-math/data.

Source

file-type.lisp.

Function: right-associative? (operator)
Package

infix-math/data.

Source

file-type.lisp.

Function: (setf right-associative?) (operator)
Package

infix-math/data.

Source

file-type.lisp.

Function: trim-dotted-operator (operator)
Package

infix-math/data.

Source

file-type.lisp.

Function: unary? (operator)
Package

infix-math/data.

Source

file-type.lisp.

Function: (setf unary?) (operator)
Package

infix-math/data.

Source

file-type.lisp.

Function: × (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Function: ÷ (a b)
Package

infix-math/symbols.

Source

file-type.lisp.

Function: (a)
Package

infix-math/symbols.

Source

file-type.lisp.


5.1.5 Types

Type: precedence ()
Package

infix-math/data.

Source

file-type.lisp.


5.2 Internals


Next: , Previous: , Up: Internals   [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

file-type.lisp.

Special Variable: *precedence*

Table of operator precedence.

Package

infix-math/data.

Source

file-type.lisp.

Special Variable: *right-associative*
Package

infix-math/data.

Source

file-type.lisp.

Special Variable: *unary*
Package

infix-math/data.

Source

file-type.lisp.


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

5.2.2 Symbol macros

Symbol Macro: e
Package

infix-math/symbols.

Source

file-type.lisp.

Symbol Macro: i
Package

infix-math/symbols.

Source

file-type.lisp.


5.2.3 Macros

Macro: binary-operator (new old)
Package

infix-math/symbols.

Source

file-type.lisp.

Macro: binary-operators (&body body)
Package

infix-math/symbols.

Source

file-type.lisp.

Macro: nodef (place operator)
Package

infix-math/infix-math.

Source

file-type.lisp.

Macro: unary-operator (new old)
Package

infix-math/symbols.

Source

file-type.lisp.

Macro: unary-operators (&body body)
Package

infix-math/symbols.

Source

file-type.lisp.


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

5.2.4 Ordinary functions

Function: ash- (i c)
Package

infix-math/symbols.

Source

file-type.lisp.

Function: dotted-operator? (sym)
Package

infix-math/data.

Source

file-type.lisp.

Function: eliminate-common-subexpressions (form &optional env)
Package

infix-math/infix-math.

Source

file-type.lisp.

Function: expand-expression (exprs)
Package

infix-math/infix-math.

Source

file-type.lisp.

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

file-type.lisp.

Function: looks-like-operator? (sym)

Does SYM start and end with an operator char?

Package

infix-math/data.

Source

file-type.lisp.

Function: make-node (tree operator)
Package

infix-math/infix-math.

Source

file-type.lisp.

Function: operator-char? (c)
Package

infix-math/data.

Source

file-type.lisp.

Function: parse-coefficient (str)
Package

infix-math/infix-math.

Source

file-type.lisp.

Function: parse-expression (expression)
Package

infix-math/infix-math.

Source

file-type.lisp.

Function: precedence< (op1 op2)
Package

infix-math/infix-math.

Source

file-type.lisp.

Function: precedence= (op1 op2)
Package

infix-math/infix-math.

Source

file-type.lisp.

Function: save-operator (&key name from right-associative)
Package

infix-math/data.

Source

file-type.lisp.

Function: save-unary-operator (name)
Package

infix-math/data.

Source

file-type.lisp.

Function: shunting-yard (expression)
Package

infix-math/infix-math.

Source

file-type.lisp.

Function: valid? (expression)
Package

infix-math/infix-math.

Source

file-type.lisp.


5.2.5 Types

Type: operator ()
Package

infix-math/data.

Source

file-type.lisp.


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

$
$: Public macros

%
%: Public compiler macros
%: Public ordinary functions

&
&: Public compiler macros
&: Public ordinary functions

(
(setf precedence): Public ordinary functions
(setf right-associative?): Public ordinary functions
(setf unary?): Public ordinary functions

<
<<: Public compiler macros
<<: Public ordinary functions

>
>>: Public compiler macros
>>: Public ordinary functions

^
^: Public compiler macros
^: Public ordinary functions

×
×: Public compiler macros
×: Public ordinary functions

÷
÷: Public compiler macros
÷: Public ordinary functions

: Public compiler macros
: Public ordinary functions

A
ash-: Private ordinary functions

B
binary-operator: Private macros
binary-operators: Private macros

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

D
declare-binary-operator: Public macros
declare-unary-operator: Public macros
dotted-operator?: Private ordinary functions

E
eliminate-common-subexpressions: Private ordinary functions
expand-expression: Private ordinary functions
expand-fancy-symbols: Private ordinary functions

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

L
looks-like-operator?: Private ordinary functions

M
Macro, $: Public macros
Macro, binary-operator: Private macros
Macro, binary-operators: Private macros
Macro, declare-binary-operator: Public macros
Macro, declare-unary-operator: Public macros
Macro, nodef: Private macros
Macro, unary: Public macros
Macro, unary-operator: Private macros
Macro, unary-operators: Private macros
make-node: Private ordinary functions

N
nodef: Private macros

O
operator-char?: Private ordinary functions
operator?: Public ordinary functions
over: Public compiler macros
over: Public ordinary functions

P
parse-coefficient: Private ordinary functions
parse-expression: Private ordinary functions
precedence: Public ordinary functions
precedence<: Private ordinary functions
precedence=: Private ordinary functions

R
right-associative?: Public ordinary functions

S
save-operator: Private ordinary functions
save-unary-operator: Private ordinary functions
shunting-yard: Private ordinary functions

T
trim-dotted-operator: Public ordinary functions

U
unary: Public macros
unary-operator: Private macros
unary-operators: Private macros
unary?: Public ordinary functions

V
valid?: Private ordinary functions

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

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

A.4 Data types

Jump to:   F   I   O   P   S   T  
Index Entry  Section

F
File, file-type.lisp: The infix-math/infix-math/file-type․lisp file
File, file-type.lisp: The infix-math/symbols/file-type․lisp file
File, file-type.lisp: The infix-math/data/file-type․lisp file
File, infix-math.asd: The infix-math/infix-math․asd file
file-type.lisp: The infix-math/infix-math/file-type․lisp file
file-type.lisp: The infix-math/symbols/file-type․lisp file
file-type.lisp: The infix-math/data/file-type․lisp file

I
infix-math: The infix-math system
infix-math.asd: The infix-math/infix-math․asd file
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: Private 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: Public 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: Private types
Type, precedence: Public types

Jump to:   F   I   O   P   S   T