forked from philhofer/distill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.scm
97 lines (85 loc) · 2.84 KB
/
contract.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
(define-syntax conform
(syntax-rules ()
((_ contract value)
(if (contract value)
value
(error "value doesn't conform to contract:" value (quote contract))))))
(define false/c (lambda (arg) (not arg)))
(define true/c (lambda (arg) arg))
(define any/c (lambda (arg) #t))
(define (perhaps otherwise)
(lambda (in)
(or (not in) (otherwise in))))
;; or/c takes contract parameters and returns a contract
;; that passes when any of its input contracts pass
(: or/c (#!rest (* -> boolean) -> (* -> boolean)))
(define or/c disjoin)
(: cmp/c ((* 'a -> boolean) 'a -> (* -> boolean)))
(define (cmp/c cmp val)
(lambda (arg)
(cmp arg val)))
(define (eq?/c val) (cmp/c eq? val))
(define (eqv?/c val) (cmp/c eqv? val))
(define (equal?/c val) (cmp/c equal? val))
(define (string=?/c val) (cmp/c string=? val))
(define (=/c val) (cmp/c = val))
;; and/c takes contract parameters and returns
;; a contract that passes only when *all* of its
;; input contracts pass
(: and/c (#!rest (* -> boolean) -> (* -> boolean)))
(define and/c conjoin)
;; list-of takes a contract parameter and
;; returns a contract that passes when it
;; is given a list where every element passes the
;; input contract
(: list-of ((* -> boolean) -> (* -> boolean)))
(define (list-of c?)
(lambda (arg)
(and (list? arg)
(let loop ((lst arg))
(or (null? lst)
(and (c? (car lst))
(loop (cdr lst))))))))
(define (pair-of car/c cdr/c)
(lambda (arg)
(and (pair? arg)
(car/c (car arg))
(cdr/c (cdr arg)))))
;; vector-of is like list-of, but for vectors
(: vector-of ((* -> boolean) -> (* -> boolean)))
(define (vector-of c?)
(lambda (arg)
(and (vector? arg)
(let ((len (vector-length arg)))
(let loop ((i 0))
(or (fx>= i len)
(and (c? (vector-ref arg i))
(loop (fx+ i 1)))))))))
;; vector/c takes input contracts and matches a vector
;; where each vector element matches its corresponding
;; contract argument
;;
;; i.e. (vector/c integer? symbol? string?)
;; matches #(1 'foo "foo")
(: vector/c (#!rest (* -> boolean) -> (* -> boolean)))
(define (vector/c . args)
(lambda (arg)
(and (vector? arg)
(let loop ((i 0)
(lst args))
(if (null? lst)
(fx= i (vector-length arg))
(and (fx< i (vector-length arg))
((car lst) (vector-ref arg i))
(loop (fx+ i 1) (cdr lst))))))))
(: list/c (#!rest (* -> boolean) -> (* -> boolean)))
(define (list/c . args)
(lambda (arg)
(and (list? arg)
(let loop ((in arg)
(ok args))
(if (null? in)
(null? ok)
(and (not (null? ok))
((car ok) (car in))
(loop (cdr in) (cdr ok))))))))