Tagged Values
1 Overview
This package implements variants (tagged values) as the dual of Racket’s native multiple values, establishing a mathematical correspondence between programming constructs and set operations:
Product as Untagged Values
Racket’s values corresponds to Cartesian product (×), where (valuesv...) represents an element of a product set. The isomorphism A ≅ A × 1 justifies treating v as (valuesv).
Sum as Tagged Values
The variant corresponds to disjoint union (+), where (variant#:tagnv...) represents an element of a sum (coproduct) set. The isomorphism A ≅ A + 0 justifies treating (valuesv...) as (variant#:tag0v...).
2 API Reference
procedure
(apply/variant proc v ... lst [#:tag n]) → any
proc : procedure? v : any/c lst : list? n : natural? = 0
> (apply/variant + 1 2 (list 3)) 6
> (apply/variant + 1 2 (list 3) #:tag 0) 6
> (apply/variant + 1 2 (list 3) #:tag 1) application: procedure does not accept keyword arguments
procedure: +
arguments...:
1
2
3
#:tag 1
> (apply/variant (λ (a b #:tag [n 0]) (cons (vector a b) n)) (list 1 2) #:tag 1) '(#(1 2) . 1)
procedure
(call-with-variant generator receiver) → any
generator : (-> any) receiver : procedure?
> (call-with-variant (λ () (variant 'a 'b)) cons) '(a . b)
> (call-with-variant (λ () (variant 'a 'b #:tag 0)) cons) '(a . b)
> (call-with-variant (λ () (variant 'a 'b #:tag 1)) cons) application: procedure does not accept keyword arguments
procedure: cons
arguments...:
'a
'b
#:tag 1
> (call-with-variant (λ () (variant 'a 'b)) (λ (a b #:tag [n 0]) (cons (vector a b) n))) '(#(a b) . 0)
> (call-with-variant (λ () (variant 'a 'b #:tag 1)) (λ (a b #:tag [n 0]) (cons (vector a b) n))) '(#(a b) . 1)
syntax
(let*-variant ([kw-formals rhs-expr] ...) body ...+)
kw-formals = (arg ...) | (arg ...+ . rest-id) | rest-id arg = id | [id default-expr] | #:tag id | #:tag [id default-expr]
> (let*-variant ([v* (variant 1 2 3)]) v*) '(1 2 3)
> (let*-variant ([(v . v*) (variant 1 2 3)]) (cons v* v)) '((2 3) . 1)
> (let*-variant ([(v . v*) (variant 1 2 3 #:tag 0)]) (cons v* v)) '((2 3) . 1)
> (let*-variant ([(v . v*) (variant 1 2 3 #:tag 1)]) (cons v* v)) application: procedure does not accept keyword arguments
procedure: ...gs/variant/main.rkt:51:6
arguments...:
1
2
3
#:tag 1
> (let*-variant ([(#:tag n v . v*) (variant 1 2 3 #:tag 1)]) (cons (cons v* v) n)) '(((2 3) . 1) . 1)
> (let*-variant ([(#:tag [n 0] v . v*) (variant 1 2 3)]) (cons (cons v* v) n)) '(((2 3) . 1) . 0)
> (let*-variant ([(#:tag n v . v*) (variant 1 2 3)]) (cons (cons v* v) n)) application: required keyword argument not supplied
procedure: ...kgs/variant/main.rkt:51:6
required keyword: #:tag
arguments...:
1
2
3
> (let*-variant ([(#:tag n v . v*) (variant 1 2 3 #:tag 0)]) (cons (cons v* v) n)) application: required keyword argument not supplied
procedure: ...kgs/variant/main.rkt:51:6
required keyword: #:tag
arguments...:
1
2
3