forked from coalton-lang/coalton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuple.lisp
148 lines (125 loc) · 3.51 KB
/
tuple.lisp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
(coalton-library/utils:defstdlib-package #:coalton-library/tuple
(:use
#:coalton
#:coalton-library/builtin
#:coalton-library/classes)
(:export
#:fst
#:snd
#:Tuple3
#:Tuple4
#:Tuple5))
#+coalton-release
(cl:declaim #.coalton-impl:*coalton-optimize-library*)
(cl:in-package #:coalton-library/tuple)
(coalton-toplevel
;;
;; Tuple
;;
(declare fst ((Tuple :a :b) -> :a))
(define (fst t)
"Get the first element of a tuple."
(match t
((Tuple a _) a)))
(declare snd ((Tuple :a :b) -> :b))
(define (snd t)
"Get the second element of a tuple."
(match t
((Tuple _ b) b)))
(define-type (Tuple3 :a :b :c)
(Tuple3 :a :b :c))
(define-type (Tuple4 :a :b :c :d)
(Tuple4 :a :b :c :d))
(define-type (Tuple5 :a :b :c :d :e)
(Tuple5 :a :b :c :d :e))
;;
;; Tuple instances
;;
(define-instance ((Eq :a) (Eq :b) => (Eq (Tuple :a :b)))
(define (== a b)
(and (== (fst a) (fst b))
(== (snd a) (snd b)))))
(define-instance ((Ord :a) (Ord :b) => (Ord (Tuple :a :b)))
(define <=> undefined))
(define-instance (Into (Tuple :a :b) (Tuple :b :a))
(define (into t)
(match t
((Tuple a b) (Tuple b a)))))
(define-instance (Iso (Tuple :a :b) (Tuple :b :a)))
(define-instance ((Hash :a) (Hash :b) => (Hash (Tuple :a :b)))
(define (hash item)
(match item
((Tuple a b)
(combine-hashes
(hash a)
(hash b))))))
;;
;; Larger Tuple Instances
;;
(define-instance ((Eq :a) (Eq :b) (Eq :c) => (Eq (Tuple3 :a :b :c)))
(define (== a b)
(match (Tuple a b)
((Tuple (Tuple3 a1 b1 c1)
(Tuple3 a2 b2 c2))
(and (== a1 a2)
(== b1 b2)
(== c1 c2))))))
(define-instance ((Hash :a) (Hash :b) (Hash :c) => (Hash (Tuple3 :a :b :c)))
(define (hash item)
(match item
((Tuple3 a b c)
(combine-hashes
(hash a)
(combine-hashes
(hash b)
(hash c)))))))
(define-instance ((Eq :a) (Eq :b) (Eq :c) (Eq :d) => (Eq (Tuple4 :a :b :c :d)))
(define (== a b)
(match (Tuple a b)
((Tuple (Tuple4 a1 b1 c1 d1)
(Tuple4 a2 b2 c2 d2))
(and (== a1 a2)
(== b1 b2)
(== c1 c2)
(== d1 d2))))))
(define-instance ((Hash :a) (Hash :b) (Hash :c) (Hash :d) => (Hash (Tuple4 :a :b :c :d)))
(define (hash item)
(match item
((Tuple4 a b c d)
(combine-hashes
(hash a)
(combine-hashes
(hash b)
(combine-hashes
(hash c)
(hash d))))))))
(define-instance ((Eq :a) (Eq :b) (Eq :c) (Eq :d) (Eq :e) => (Eq (Tuple5 :a :b :c :d :e)))
(define (== a b)
(match (Tuple a b)
((Tuple (Tuple5 a1 b1 c1 d1 e1)
(Tuple5 a2 b2 c2 d2 e2))
(and (== a1 a2)
(== b1 b2)
(== c1 c2)
(== d1 d2)
(== e1 e2))))))
(define-instance ((Hash :a)
(Hash :b)
(Hash :c)
(Hash :d)
(Hash :e)
=> (Hash (Tuple5 :a :b :c :d :e)))
(define (hash item)
(match item
((Tuple5 a b c d e)
(combine-hashes
(hash a)
(combine-hashes
(hash b)
(combine-hashes
(hash c)
(combine-hashes
(hash d)
(hash e))))))))))
#+sb-package-locks
(sb-ext:lock-package "COALTON-LIBRARY/TUPLE")