On this page:
iso?
make-iso
2.4.1 Isomorphism operations
iso-forward
iso-backward
iso-modify
iso-reverse
iso-compose
2.4.2 Library Isomorphisms
symbol<->string
vector<->list
list-reverse-iso
identity-iso
2.4.3 Isomorphism Generic Interface
gen:  iso
8.15.0.2

2.4 Isomorphisms🔗ℹ

 (require ocular-patdown/optics/isomorphism)
  package: ocular-patdown

An isomorphism is an optic whose focus is "equivalent" (isomorphic) to its target. An isomorphism can be used when two data types can be converted back and forth between each other. Isomorphisms are useful for treating one data type as another.

Examples:

All isomorphisms are lenses, but not all lenses are isomorphisms.

procedure

(iso? val)  boolean?

  val : any/c
Predicate for isomorphisms. Recognizes implementers of gen:iso.

Examples:

procedure

(make-iso forward backward)  iso?

  forward : (-> any/c any/c)
  backward : (-> any/c any/c)
Constructor for isomorphisms.

Example:
> (define my-symbol<->string (make-iso symbol->string string->symbol))

forward and backward must be inverses of each other:
(equal? (backward (forward target)) target)
(equal? (forward (backward focus)) focus)

2.4.1 Isomorphism operations🔗ℹ

procedure

(iso-forward iso target)  any/c

  iso : iso?
  target : any/c
Convert target to its equivalent focus.

Example:

procedure

(iso-backward iso focus)  any/c

  iso : iso?
  focus : any/c
Convert focus to its equivalent target.

Example:

procedure

(iso-modify iso target proc)  any/c

  iso : iso?
  target : any/c
  proc : (-> any/c any/c)
Modify target with proc as if it was its focus under iso. proc must accept and return a focus.

Example:

procedure

(iso-reverse iso)  iso?

  iso : iso?
Reverse/invert iso such that it works in the opposite direction.

Examples:

procedure

(iso-compose iso ...)  iso?

  iso : iso?
Like optic-compose, but for isomorphisms.

Examples:

2.4.2 Library Isomorphisms🔗ℹ

Isomorphism between symbols and strings.

Example:

Isomorphism between vectors and lists.

Example:
> (iso-modify vector<->list #(1 2 3) reverse)

'#(3 2 1)

Isomorphism between lists and reversed lists. Useful for folding over a traversal in reverse.

Examples:
> (iso-forward list-reverse-iso (list 1 2 3))

'(3 2 1)

> (iso-backward list-reverse-iso (list 3 2 1))

'(1 2 3)

> (traversal-foldl (optic-compose vector<->list list-reverse-iso list-traversal)
                   #("a" "b" "c")
                   string-append
                   "")

"abc"

Isomorphism between values and themselves. The identity of optic-compose.

2.4.3 Isomorphism Generic Interface🔗ℹ

syntax

gen:iso

A generic interface for isomorphisms.

Example:
> (struct make-iso [forward backward]
    #:methods gen:iso
    [(define (iso-forward iso target) ((make-iso-forward iso) target))
     (define (iso-backward iso focus) ((make-iso-backward iso) focus))])