Skip to content

Commit 3c194a9

Browse files
Jesse Bouwmanjbouwman
Jesse Bouwman
authored andcommitted
Convert standard library files
1 parent 8040947 commit 3c194a9

18 files changed

+1057
-1158
lines changed

coalton.asd

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
:serial t
3939
:components ((:file "set-float-traps")
4040
(:file "utils")
41-
(:file "types")
41+
(:coalton-file "types")
4242
(:file "primitive-types")
4343
(:file "classes")
4444
(:file "hash")
@@ -59,17 +59,17 @@
5959
(:file "elementary")
6060
(:file "dyadic")
6161
(:file "dual")))
62-
(:file "randomaccess")
63-
(:file "cell")
62+
(:coalton-file "randomaccess")
63+
(:coalton-file "cell")
6464
(:file "tuple")
6565
(:file "iterator")
66-
(:file "optional")
66+
(:coalton-file "optional")
6767
(:file "result")
6868
(:file "lisparray")
69-
(:file "list")
69+
(:coalton-file "list")
7070
(:file "vector")
71-
(:file "char")
72-
(:file "string")
71+
(:coalton-file "char")
72+
(:coalton-file "string")
7373
(:file "slice")
7474
(:file "hashtable")
7575
(:file "queue")
@@ -78,7 +78,7 @@
7878
(:file "ord-map")
7979
(:file "monad/free")
8080
(:file "seq")
81-
(:file "system")
81+
(:coalton-file "system")
8282
(:file "file")
8383
(:file "prelude")))
8484

library/cell.coal

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
(package coalton-library/cell
2+
(import
3+
coalton-library/builtin
4+
coalton-library/classes)
5+
(export
6+
Cell
7+
new
8+
read
9+
swap!
10+
write!
11+
update!
12+
update-swap!
13+
push!
14+
pop!
15+
increment!
16+
decrement!))
17+
18+
(lisp-toplevel
19+
(cl:declaim (cl:inline make-cell-internal))
20+
21+
(cl:defstruct cell-internal
22+
(inner (cl:error "") :type cl:t))
23+
24+
(cl:defmethod cl:print-object ((self cell-internal) stream)
25+
(cl:format stream "#.(CELL ~A)" (cell-internal-inner self))
26+
self)
27+
28+
#+sbcl
29+
(cl:declaim (sb-ext:freeze-type cell-internal)))
30+
31+
(repr :native cell-internal)
32+
(define-type (Cell :a)
33+
"Internally mutable cell")
34+
35+
(declare new (:a -> Cell :a))
36+
(define (new data)
37+
"Create a new mutable cell"
38+
(lisp (Cell :a) (data)
39+
(make-cell-internal :inner data)))
40+
41+
(declare read (Cell :a -> :a))
42+
(define (read cel)
43+
"Read the value of a mutable cell"
44+
(lisp :a (cel)
45+
(cell-internal-inner cel)))
46+
47+
(declare swap! (Cell :a -> :a -> :a))
48+
(define (swap! cel data)
49+
"Replace the value of a mutable cell with a new value, then return the old value"
50+
(lisp :a (data cel)
51+
(cl:let* ((old (cell-internal-inner cel)))
52+
(cl:setf (cell-internal-inner cel) data)
53+
old)))
54+
55+
(declare write! (Cell :a -> :a -> :a))
56+
(define (write! cel data)
57+
"Set the value of a mutable cell, returning the new value"
58+
(lisp :a (data cel)
59+
(cl:setf (cell-internal-inner cel) data)))
60+
61+
(declare update! ((:a -> :a) -> Cell :a -> :a))
62+
(define (update! f cel)
63+
"Apply F to the contents of CEL, storing and returning the result"
64+
(write! cel (f (read cel))))
65+
66+
(declare update-swap! ((:a -> :a) -> Cell :a -> :a))
67+
(define (update-swap! f cel)
68+
"Apply F to the contents of CEL, swapping the result for the old value"
69+
(swap! cel (f (read cel))))
70+
71+
;;; operators on cells of lists
72+
(declare push! (Cell (List :elt) -> :elt -> List :elt))
73+
(define (push! cel new-elt)
74+
"Push NEW-ELT onto the start of the list in CEL."
75+
(update! (Cons new-elt) cel))
76+
77+
(declare pop! (Cell (List :elt) -> Optional :elt))
78+
(define (pop! cel)
79+
"Remove and return the first element of the list in CEL."
80+
(match (read cel)
81+
((Cons fst rst)
82+
(write! cel rst)
83+
(Some fst))
84+
((Nil) None)))
85+
86+
;;; operators on cells of numbers
87+
(declare increment! (Num :counter => Cell :counter -> :counter))
88+
(define (increment! cel)
89+
"Add one to the contents of CEL, storing and returning the new value"
90+
(update! (+ 1) cel))
91+
92+
(declare decrement! (Num :counter => (Cell :counter) -> :counter))
93+
(define (decrement! cel)
94+
"Subtract one from the contents of CEL, storing and returning the new value"
95+
(update! (+ -1) cel))
96+
97+
;; i am very skeptical of these instances
98+
(define-instance (Eq :a => Eq (Cell :a))
99+
(define (== c1 c2)
100+
(== (read c1) (read c2))))
101+
102+
(define-instance (Ord :a => Ord (Cell :a))
103+
(define (<=> c1 c2)
104+
(match (<=> (read c1) (read c2))
105+
((LT) LT)
106+
((GT) GT)
107+
((EQ) EQ))))
108+
109+
(define-instance (Num :a => Num (Cell :a))
110+
(define (+ c1 c2)
111+
(new (+ (read c1) (read c2))))
112+
(define (- c1 c2)
113+
(new (- (read c1) (read c2))))
114+
(define (* c1 c2)
115+
(new (* (read c1) (read c2))))
116+
(define (fromInt i)
117+
(new (fromInt i))))
118+
119+
(define-instance (Semigroup :a => Semigroup (Cell :a))
120+
(define (<> a b)
121+
(new (<> (read a) (read b)))))
122+
123+
(define-instance (Functor Cell)
124+
(define (map f c)
125+
(new (f (read c)))))
126+
127+
(define-instance (Applicative Cell)
128+
(define pure new)
129+
(define (liftA2 f c1 c2)
130+
(new (f (read c1) (read c2)))))
131+
132+
(define-instance (Into :a (Cell :a))
133+
(define into new))
134+
135+
(define-instance (Into (Cell :a) :a)
136+
(define into read))
137+
138+
(define-instance (Default :a => Default (Cell :a))
139+
(define (default) (new (default))))

library/cell.lisp

-151
This file was deleted.

0 commit comments

Comments
 (0)