mirror of
https://codeberg.org/hako/Rosenthal.git
synced 2026-11-29 22:58:06 +00:00
utils: Add a Racket-like contract system.
This commit is contained in:
4 files changed
+1007
No files matched your search
@@ -0,0 +1,86 @@
|
||||
;;; SPDX-License-Identifier: GPL-3.0-or-later
|
||||
;;; Copyright © 2026 Hilton Chain <hako@ultrarare.space>
|
||||
|
||||
(define-module (rosenthal utils contract)
|
||||
#:use-module (ice-9 match)
|
||||
#:use-module (srfi srfi-1)
|
||||
#:use-module (srfi srfi-26)
|
||||
#:use-module (guix colors)
|
||||
#:use-module (guix diagnostics)
|
||||
#:use-module (rosenthal utils contract impl)
|
||||
#:use-module (rosenthal utils contract blame)
|
||||
#:use-module (rosenthal utils contract combinators)
|
||||
#:export (contract
|
||||
define/contract))
|
||||
|
||||
#|
|
||||
|
||||
Helpers to attach contracts to values and functions.
|
||||
|
||||
See (rosenthal utils contract impl) for more information.
|
||||
|
||||
|#
|
||||
|
||||
(define (src->location-str src)
|
||||
(match src
|
||||
((_ _ _)
|
||||
(let ((location (source-properties->location src)))
|
||||
(if (supports-hyperlinks? (guix-warning-port))
|
||||
(location->hyperlink location)
|
||||
(location->string location))))
|
||||
(_ #f)))
|
||||
|
||||
(define* (apply-contract contract value positive negative name location
|
||||
#:key context-limit)
|
||||
(let* ((ctc (->contract contract))
|
||||
(ctc-name (contract-name ctc))
|
||||
(checker (contract-checker ctc))
|
||||
(blame
|
||||
(make-blame*
|
||||
#:source location
|
||||
#:value name
|
||||
#:build-name (lambda () ctc-name)
|
||||
#:positive positive
|
||||
#:negative #f
|
||||
#:original? #t
|
||||
#:context-limit context-limit)))
|
||||
(cond
|
||||
(checker
|
||||
(if (checker value)
|
||||
value
|
||||
(raise-blame-error
|
||||
blame #:missing-party negative
|
||||
`(expected: "~a" got: "~s") ctc-name value)))
|
||||
(else
|
||||
(((contract-enforcer ctc) blame) value negative)))))
|
||||
|
||||
(define-syntax contract
|
||||
(lambda (stx)
|
||||
(syntax-case stx ()
|
||||
((_ ctc value positive negative name location #:context-limit context-limit)
|
||||
#'(apply-contract ctc value positive negative name location #:context-limit context-limit))
|
||||
((_ ctc value positive negative name location)
|
||||
#'(contract ctc value positive negative name location #:context-limit #f))
|
||||
((_ ctc value positive negative name)
|
||||
(with-syntax ((location (src->location-str (syntax-source stx))))
|
||||
#'(contract ctc value positive negative name location)))
|
||||
((_ ctc value positive negative)
|
||||
#'(contract ctc value positive negative positive))
|
||||
((_ ctc value)
|
||||
#'(contract ctc value 'unknown-definition 'caller)))))
|
||||
|
||||
(define-syntax define/contract
|
||||
(lambda (stx)
|
||||
(syntax-case stx ()
|
||||
((_ (name . args) ctc body ...)
|
||||
(if (null? #'(body ...))
|
||||
(syntax-violation 'define/contract "missing body" stx)
|
||||
(with-syntax ((value #'(lambda* args body ...)))
|
||||
#'(define name
|
||||
(contract ctc value '(definition: name) 'caller 'name)))))
|
||||
((_ name ctc body ...)
|
||||
(if (null? #'(body ...))
|
||||
(syntax-violation 'define/contract "missing body" stx)
|
||||
(with-syntax ((value #'(begin body ...)))
|
||||
#'(define name
|
||||
(contract ctc value '(definition: name) 'caller 'name))))))))
|
||||
Reference in new issue
Block a user