utils: contract: Memoize combinators and ->contract.

This commit is contained in:
Hilton Chain committed 2026-09-15 20:42:51 +08:00
1 parent 08b1980f22
commit b15af6f9ff
1 file changed
+112 -105
+112 -105
View File
@@ -8,6 +8,7 @@
#:use-module (srfi srfi-26) #:use-module (srfi srfi-26)
#:use-module (srfi srfi-34) #:use-module (srfi srfi-34)
#:use-module (srfi srfi-35) #:use-module (srfi srfi-35)
#:use-module (guix memoization)
#:use-module (rosenthal utils contract impl) #:use-module (rosenthal utils contract impl)
#:use-module (rosenthal utils contract blame) #:use-module (rosenthal utils contract blame)
#:export (any/c #:export (any/c
@@ -35,114 +36,120 @@ combinator names have a ‘/c’ suffix.
#:name "none/c" #:name "none/c"
#:checker (const #f))) #:checker (const #f)))
(define (or/c . ctcs) (define or/c
(let* ((contracts (map ->contract ctcs)) (memoize
(enforcers (map contract-enforcer contracts)) (lambda (. ctcs)
(name (compound-contract-name "or/c" contracts) )) (let* ((contracts (map ->contract ctcs))
(make-contract (enforcers (map contract-enforcer contracts))
#:name name (name (compound-contract-name "or/c" contracts) ))
#:enforcer (make-contract
(lambda (blame) #:name name
(define procs #:enforcer
(map (lambda (enforcer) (lambda (blame)
(enforcer blame)) (define procs
enforcers)) (map (lambda (enforcer)
(lambda (val neg-party) (enforcer blame))
(let loop ((procs procs)) enforcers))
(if (null? procs) (lambda (val neg-party)
(raise-blame-error blame #:missing-party neg-party (let loop ((procs procs))
'(expected: "~a" got: "~s") (if (null? procs)
name val) (raise-blame-error blame #:missing-party neg-party
(match procs '(expected: "~a" got: "~s")
((proc . rest) name val)
(guard (c ((contract-violation? c) (match procs
(loop rest))) ((proc . rest)
(proc val neg-party))))))))))) (guard (c ((contract-violation? c)
(loop rest)))
(proc val neg-party)))))))))))))
(define (listof/c ctc) (define listof/c
(let* ((contract (->contract ctc)) (mlambda (ctc)
(enforcer (contract-enforcer contract))) (let* ((contract (->contract ctc))
(make-contract (enforcer (contract-enforcer contract)))
#:name (compound-contract-name "listof/c" (list contract)) (make-contract
#:enforcer #:name (compound-contract-name "listof/c" (list contract))
(lambda (blame) #:enforcer
(define proc (lambda (blame)
(enforcer (blame-add-context blame "an element of"))) (define proc
(lambda (val neg-party) (enforcer (blame-add-context blame "an element of")))
(unless (list? val) (lambda (val neg-party)
(raise-blame-error blame #:missing-party neg-party (unless (list? val)
'(expected: "a list of ~a" got: "~s") (raise-blame-error blame #:missing-party neg-party
(contract-name contract) '(expected: "a list of ~a" got: "~s")
val)) (contract-name contract)
(map (lambda (element) val))
(proc element neg-party)) (map (lambda (element)
val)))))) (proc element neg-party))
val)))))))
;; TODO: Rewrite as a macro to support define*, multiple return values, and ;; TODO: Rewrite as a macro to support define*, multiple return values, and
;; ‘any’. ;; ‘any’.
(define (->/c . ctcs) (define ->/c
(let* ((contracts (map ->contract ctcs)) (memoize
(domain-contracts (drop-right contracts 1)) (lambda (. ctcs)
(range-contract (last contracts)) (let* ((contracts (map ->contract ctcs))
(contract-arity (length domain-contracts))) (domain-contracts (drop-right contracts 1))
(make-contract (range-contract (last contracts))
#:name (compound-contract-name "->/c" contracts) (contract-arity (length domain-contracts)))
#:enforcer (make-contract
(lambda (blame) #:name (compound-contract-name "->/c" contracts)
(define domain-blames #:enforcer
(map (lambda (index) (lambda (blame)
(let ((position (format #f "the #~a argument of" index))) (define domain-blames
(blame-add-context blame position #:swap? #t))) (map (lambda (index)
(iota contract-arity 1))) (let ((position (format #f "the #~a argument of" index)))
(define domain-enforcers (blame-add-context blame position #:swap? #t)))
(map contract-enforcer domain-contracts)) (iota contract-arity 1)))
(define range-proc (define domain-enforcers
(let ((range-blame (blame-add-context blame "the range of"))) (map contract-enforcer domain-contracts))
((contract-enforcer range-contract) range-blame))) (define range-proc
(lambda (val neg-party) (let ((range-blame (blame-add-context blame "the range of")))
(unless (procedure? val) ((contract-enforcer range-contract) range-blame)))
(raise-blame-error blame #:missing-party neg-party (lambda (val neg-party)
'(expected: "a procedure" got: "~s") val)) (unless (procedure? val)
(lambda args (raise-blame-error blame #:missing-party neg-party
;; XXX: Can't check procedure arity in Guile. '(expected: "a procedure" got: "~s") val))
(let ((arity (length args))) (lambda args
(unless (= arity contract-arity) ;; XXX: Can't check procedure arity in Guile.
(raise-blame-error (let ((arity (length args)))
(blame-swap blame) #:missing-party neg-party (unless (= arity contract-arity)
`("assuming arity of the contract is always correct" (raise-blame-error
expected: "~a argument(s)" got: "~a argument(s)") (blame-swap blame) #:missing-party neg-party
contract-arity arity))) `("assuming arity of the contract is always correct"
(define args* expected: "~a argument(s)" got: "~a argument(s)")
(map (lambda (arg blame enforcer) contract-arity arity)))
((enforcer blame) arg neg-party)) (define args*
args (map (lambda (arg blame enforcer)
domain-blames ((enforcer blame) arg neg-party))
domain-enforcers)) args
(range-proc (apply val args*) neg-party))))))) domain-blames
domain-enforcers))
(range-proc (apply val args*) neg-party)))))))))
(define (->contract x) (define ->contract
(match x (mlambda (x)
((? contract?) x) (match x
((? procedure?) ((? contract?) x)
(make-contract ((? procedure?)
#:name (format #f "~s" (or (procedure-name x) '???)) (make-contract
#:checker x)) #:name (format #f "~s" (or (procedure-name x) '???))
((or (? symbol?) #:checker x))
(? boolean?) ((or (? symbol?)
(? keyword?)) (? boolean?)
(make-contract (? keyword?))
#:name (format #f "~s" x) (make-contract
#:checker (cut eq? x <>))) #:name (format #f "~s" x)
((or (? string?) #:checker (cut eq? x <>)))
(? bytevector?) ((or (? string?)
(? char?) (? bytevector?)
+nan.0) (? char?)
(make-contract +nan.0)
#:name (format #f "~s" x) (make-contract
#:checker (cut equal? x <>))) #:name (format #f "~s" x)
((? number?) #:checker (cut equal? x <>)))
(make-contract ((? number?)
#:name (format #f "~s" x) (make-contract
#:checker (cut = x <>))) #:name (format #f "~s" x)
(_ (error (format #f "cannot use as contract: ~s" x))))) #:checker (cut = x <>)))
(_ (error (format #f "cannot use as contract: ~s" x))))))