# The cmu-infix Reference Manual

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

# The cmu-infix Reference Manual

This is the cmu-infix Reference Manual, generated automatically by Declt version 2.4 "Will Decker" on Wed Jun 20 11:36:38 2018 GMT+0.

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

# CMU-INFIX

A library for writing infix mathematical notation in Common Lisp.

## Origin

This library was originally written by Mark Kantrowitz in 1993 with updates the following few years. The code in this repository was derived from the original `infix.cl` library provided by the CMU AI Repository. For posterity, a copy of this original file—otherwise unused by this library—can be found in `attic/infix.cl`.

With minimal changes to the core functionality, the library was modernized by Robert Smith to be in-line with contemporary Common Lisp usage.

## Example Use

This package uses `named-readtables` to manage the readtables. If you've loaded `CMU-INFIX` successfully, then you'll have this package loaded as well.

To use `CMU-INFIX`, simply use the readtable named `cmu-infix:syntax`:

``````(named-readtables:in-readtable cmu-infix:syntax)
``````

Once you have this, you can use the `#I` syntax for infix syntax. Here are some examples.

Example: Pythagorean Theorem

``````(defun hypot (a b)
"Compute the length of the hypotenuse of a right triangle
with sides A and B."
#I( sqrt(a^^2 + b^^2) ))
``````

Example: Power-of-Two Check

``````(defun power-of-two-p (n)
"Check if N is a power of 2."
#I( n != 0 and (n & (n - 1)) == 0 ))
``````

Example: Euclidean Algorithm

``````(defun euclid (a b)
"Compute the GCD of A and B using Euclid's algorithm."
(let (temp)
(loop :until #I( b == 0 ) :do
#I( temp := b,
b := a % b,
a := temp
))
a))
``````

Example: Matrix Multiplication

``````(defun matmul (A B)
"Compute C = A * B for matrices A and B."
(let* ((m (array-dimension A 0))
(n (array-dimension A 1))
(q (array-dimension B 1))
(C (make-array (list m q) :initial-element 0)))
(loop :for i :below m :do
(loop :for k :below q :do
(loop :for j :below n :do
#I( C[i, k] += A[i, j] * B[j, k] ))))
C))

;; Example:
(let ((A (make-array '(2 2) :initial-contents '((0 1) (1 0))))
(B (make-array '(2 1) :initial-contents '((2) (3)))))
#I( matmul(A, B) ))
``````

A full description of the supported operators is in the package documentation for `CMU-INFIX`:

``````(format t "~A" (documentation (find-package :cmu-infix) t))
``````

The library has been updated in the following ways:

• The package of this library has been renamed `CMU-INFIX` so as to not conflict with existing Quicklisp libraries.

• A system of the same name has been made so it is loadable by ASDF.

• The tests have been lifted and put into a separate system called `CMU-INFIX-TESTS`. You can run them by doing

``````(asdf:test-system :cmu-infix)
``````
• The library was modified to use `NAMED-READTABLES` to not eagerly pollute your readtable.

• Some out-of-date comments have been deleted.

## Contributing

After receiving permission from Mark Kantrowitz, Rigetti Computing has taken stewardship of the library. Questions and issues should be filed on GitHub here, and pull requests are welcome. The licensing terms are described in [`LICENSE.txt`](LICENSE.t

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

## 2 Systems

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

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

### 2.1 cmu-infix

Maintainer

Robert Smith <robert@rigetti.com>

Author

Mark Kantrowitz

Description

Mathematical infix notation for Common Lisp.

Dependency

Source

cmu-infix.asd (file)

Components

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 cmu-infix.asd

Location

/home/quickref/quicklisp/dists/quicklisp/software/cmu-infix-20180228-git/cmu-infix.asd

Systems

cmu-infix (system)

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

#### 3.1.2 cmu-infix/package.lisp

Parent

cmu-infix (system)

Location

package.lisp

Packages

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

#### 3.1.3 cmu-infix/cmu-infix.lisp

Dependency

package.lisp (file)

Parent

cmu-infix (system)

Location

cmu-infix.lisp

Exported Definitions

string->prefix (function)

Internal Definitions

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

## 4 Packages

Packages are listed by definition order.

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

### 4.1 cmu-infix

Package holding the readtable designator for mathematical infix notation.

The following two tables enumerate the supported operators along with their precedence.

Operators:

NOTE: == is equality, = is assignment (C-style).

\ quoting character: x\-y –> x-y
! lisp escape !(foo bar) –> (foo bar)
; comment
x = y assignment (setf x y)
x += y increment (incf x y)
x -= y decrement (decf x y)
x *= y multiply and store (setf x (* x y)) x /= y divide and store (setf x (/ x y)) x|y bitwise logical inclusive or (logior x y) x^y bitwise logical exclusive or (logxor x y) x&y bitwise logical and (logand x y) x<<y left shift (ash x y)
x>>y right shift (ash x (- y)) ~x ones complement (unary) (lognot x)
x and y conjunction (and x y)
x && y conjunction (and x y)
x or y disjunction (or x y)
x || y disjunction (or x y)
not x negation (not x)
x^^y exponentiation (expt x y)
x,y sequence (progn x y) (x,y) sequence (progn x y) also parenthesis (x+y)/z –> (/ (+ x y) z) f(x,y) functions (f x y)
a[i,j] array reference (aref a i j) x+y x*y arithmetic (+ x y) (* x y) x-y x/y arithmetic (- x y) (/ x y) -y value negation (- y)
x % y remainder (mod x y)
x<y x>y inequalities (< x y) (> x y) x <= y x >= y inequalities (<= x y) (>= x y) x == y equality (= x y)
x != y equality (not (= x y)) if p then q conditional (when p q)
if p then q else r conditional (if p q r)

Precedence:

The following precedence conventions are obeyed by the infix operators: [ ( !
^^
~
* / %
+ -
<< >>
< == > <= != >=
&
^
|
not
and
or
= += -= *= /=
,
if
then else
] )

Note that logical negation has lower precedence than numeric comparison so that "not a<b" becomes (not (< a b)), which is different from the C precedence conventions. You can change the precedence conventions by modifying the value of the variable *operator-ordering*.

Source

package.lisp (file)

Use List
• common-lisp
Exported Definitions

string->prefix (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

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

#### 5.1.1 Functions

Function: string->prefix STRING

Convert a string to a prefix s-expression using the infix reader. If the argument is not a string, just return it as is.

Package
Source

cmu-infix.lisp (file)

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

### 5.2 Internal definitions

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

#### 5.2.1 Special variables

Package
Source

cmu-infix.lisp (file)

Package
Source

cmu-infix.lisp (file)

Special Variable: *operator-ordering*

Ordered list of operators of equal precedence.

Package
Source

cmu-infix.lisp (file)

Special Variable: *peeked-token*
Package
Source

cmu-infix.lisp (file)

Special Variable: *right-associative-operators*
Package
Source

cmu-infix.lisp (file)

Special Variable: *token-infix-operator-table*
Package
Source

cmu-infix.lisp (file)

Special Variable: *token-operators*
Package
Source

cmu-infix.lisp (file)

Special Variable: *token-prefix-operator-table*
Package
Source

cmu-infix.lisp (file)

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

#### 5.2.2 Macros

Macro: define-character-tokenization CHAR FUNCTION
Package
Source

cmu-infix.lisp (file)

Macro: define-token-operator OPERATOR-NAME &key PREFIX INFIX
Package
Source

cmu-infix.lisp (file)

Macro: infix-error FORMAT-STRING &rest ARGS
Package
Source

cmu-infix.lisp (file)

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

#### 5.2.3 Functions

Function: apply-token-infix-operator TOKEN LEFT STREAM
Package
Source

cmu-infix.lisp (file)

Function: apply-token-prefix-operator TOKEN STREAM
Package
Source

cmu-infix.lisp (file)

Function: fancy-number-format-p LEFT OPERATOR STREAM
Package
Source

cmu-infix.lisp (file)

Function: gather-superiors PREVIOUS-OPERATOR STREAM

Gathers an expression whose operators all exceed the precedence of the operator to the left.

Package
Source

cmu-infix.lisp (file)

Function: get-first-token STREAM
Package
Source

cmu-infix.lisp (file)

Function: get-next-token STREAM LEFT
Package
Source

cmu-infix.lisp (file)

Function: get-token-infix-operator TOKEN
Package
Source

cmu-infix.lisp (file)

Function: get-token-prefix-operator TOKEN
Package
Source

cmu-infix.lisp (file)

Package
Source

cmu-infix.lisp (file)

Package
Source

cmu-infix.lisp (file)

Function: operator-lessp OP1 OP2
Package
Source

cmu-infix.lisp (file)

Function: operator-right-associative-p OPERATOR
Package
Source

cmu-infix.lisp (file)

Function: peek-token STREAM
Package
Source

cmu-infix.lisp (file)

Function: post-process-expression EXPRESSION
Package
Source

cmu-infix.lisp (file)

Package
Source

cmu-infix.lisp (file)

Package
Source

cmu-infix.lisp (file)

Package
Source

cmu-infix.lisp (file)

Function: same-operator-p X Y
Package
Source

cmu-infix.lisp (file)

Function: same-token-p X Y
Package
Source

cmu-infix.lisp (file)

Function: token-operator-p TOKEN
Package
Source

cmu-infix.lisp (file)

Function: valid-numberp STRING
Package
Source

cmu-infix.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   D   F   G   I   M   O   P   R   S   T   V
Jump to: A   D   F   G   I   M   O   P   R   S   T   V

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