Skip to content

Commit 6ceeac0

Browse files
committed
Do some more module reorganization to inline Eq, Show, Semigroup, Monoid
1 parent 4ad15fa commit 6ceeac0

File tree

11 files changed

+197
-199
lines changed

11 files changed

+197
-199
lines changed

hackett-demo/hackett/demo/web-server.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
(module untyped racket/base
2626
(require (prefix-in hackett: (combine-in hackett (submod ".." shared)))
2727
(only-in hackett : -> Integer String IO Unit)
28-
(only-in hackett/private/prim [type-#%app #%app] typed-out)
29-
(only-in hackett/private/prim/io io)
28+
hackett/private/prim/type-provide
29+
(only-in hackett/private/prim/type io)
3030
racket/string
3131
racket/promise
3232
net/url-structs

hackett-lib/hackett/data/list.rkt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
hackett/private/prim
66
hackett/private/provide)
77

8-
(provide (data List) head tail head! tail! zip-with)
8+
(provide (data List) head tail head! tail! take zip-with)
99

1010
(defn head : (∀ [a] {(List a) -> (Maybe a)})
1111
[[{x :: _}] (just x)]
@@ -21,13 +21,14 @@
2121
(defn tail! : (∀ [a] {(List a) -> (List a)})
2222
[[xs] (from-maybe (error! "tail!: empty list") (tail xs))])
2323

24+
(defn take : (∀ [a] {Integer -> (List a) -> (List a)})
25+
[[n {x :: xs}]
26+
(if {n == 0}
27+
nil
28+
{x :: (take {n - 1} xs)})]
29+
[[_ nil]
30+
nil])
31+
2432
(defn zip-with : (∀ [a b c] {{a -> b -> c} -> (List a) -> (List b) -> (List c)})
2533
[[f {x :: xs} {y :: ys}] {(f x y) :: (zip-with f xs ys)}]
2634
[[_ _ _ ] nil])
27-
28-
(defn sequence : (∀ [f a] (Applicative f) => {(List (f a)) -> (f (List a))})
29-
[[{y :: ys}] {:: <$> y <*> (sequence ys)}]
30-
[[nil ] (pure nil)])
31-
32-
(defn traverse : (∀ [f a b] (Applicative f) => {{a -> (f b)} -> (List a) -> (f (List b))})
33-
[[f xs] (sequence (map f xs))])

hackett-lib/hackett/monoid.rkt

Lines changed: 0 additions & 18 deletions
This file was deleted.

hackett-lib/hackett/prelude.rkt

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@
77

88
hackett/data/list
99
hackett/data/maybe
10-
hackett/monoid
11-
hackett/semigroup
1210

1311
hackett/private/prim
1412
hackett/private/provide)
1513

1614
(provide (all-from-out hackett/data/list)
1715
(all-from-out hackett/data/maybe)
18-
(all-from-out hackett/monoid)
19-
(all-from-out hackett/semigroup)
2016

2117
(data Unit) (data Bool) (data Tuple) (data Maybe) (data List)
2218
not or and if fst snd
2319

2420
id compose const flip
2521

22+
(class Eq) (class Show) (class Semigroup) (class Monoid)
23+
2624
(class Functor) (rename-out [map <$>]) <&> <$ $> ignore
2725
(class Applicative) sequence traverse
2826
(class Monad) =<< >>= do ap
@@ -43,60 +41,3 @@
4341
(defn println : {String -> (IO Unit)}
4442
[[str] (do (print str)
4543
(print "\n"))])
46-
47-
;; ---------------------------------------------------------------------------------------------------
48-
;; Show
49-
50-
(class (Show a)
51-
[show : {a -> String}])
52-
53-
(instance (Show Unit)
54-
[show (λ [unit] "unit")])
55-
56-
(instance (Show Bool)
57-
[show (λ* [[true ] "true"]
58-
[[false] "false"])])
59-
60-
(instance (Show Integer)
61-
[show show/Integer])
62-
63-
(instance (Show String)
64-
[show (λ [str] {"\"" ++ str ++ "\""})])
65-
66-
(instance (∀ [a] (Show a) => (Show (Maybe a)))
67-
[show (λ* [[(just x)] {"(just " ++ (show x) ++ ")"}]
68-
[[nothing ] "nothing"])])
69-
70-
(instance (∀ [a b] (Show a) (Show b) => (Show (Tuple a b)))
71-
[show (λ [(tuple a b)] {"(tuple " ++ (show a) ++ " " ++ (show b) ++ ")"})])
72-
73-
(instance (∀ [a] (Show a) => (Show (List a)))
74-
[show (λ* [[{y :: ys}] {"{" ++ (show y) ++ " :: " ++ (show ys) ++ "}"}]
75-
[[nil ] "nil"])])
76-
77-
;; ---------------------------------------------------------------------------------------------------
78-
;; Eq
79-
80-
(class (Eq a)
81-
[== : {a -> a -> Bool}])
82-
83-
(instance (Eq Unit)
84-
[== (λ [unit unit] true)])
85-
86-
(instance (Eq Bool)
87-
[== (λ* [[true y] y]
88-
[[false y] (not y)])])
89-
90-
(instance (Eq Integer)
91-
[== equal?/Integer])
92-
93-
(instance (Eq String)
94-
[== equal?/String])
95-
96-
(instance (∀ [a] (Eq a) => (Eq (Maybe a)))
97-
[== (λ* [[(just a) (just b)] (== a b)]
98-
[[nothing nothing ] true]
99-
[[_ _ ] false])])
100-
101-
(instance (∀ [a b] (Eq a) (Eq b) => (Eq (Tuple a b)))
102-
[== (λ [(tuple a b) (tuple c d)] (and (== a c) (== b d)))])

hackett-lib/hackett/private/prim.rkt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
#lang racket/base
22

3-
(require hackett/private/prim/base
4-
hackett/private/prim/io
5-
hackett/private/prim/op)
3+
(require hackett/private/util/require
4+
5+
(for-syntax racket/base)
6+
(postfix-in - (combine-in racket/base racket/promise))
7+
syntax/parse/define
8+
(only-in hackett/private/base with-dictionary-elaboration)
9+
(only-in hackett/private/kernel [#%app @%app])
10+
11+
hackett/private/prim/base
12+
hackett/private/prim/op
13+
hackett/private/prim/type)
614

715
(provide (all-from-out hackett/private/prim/base)
8-
(all-from-out hackett/private/prim/io)
9-
(all-from-out hackett/private/prim/op))
16+
(all-from-out hackett/private/prim/op)
17+
(all-from-out hackett/private/prim/type)
18+
19+
main)
20+
21+
(define-syntax-parser main
22+
[(_ e:expr)
23+
#'(module+ main
24+
(void- (with-dictionary-elaboration (force- (@%app unsafe-run-io! e)))))])

hackett-lib/hackett/private/prim/base.rkt

Lines changed: 109 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,23 @@
88
(except-in hackett/private/adt data)
99
(except-in hackett/private/class class)
1010
hackett/private/provide
11+
hackett/private/prim/op
12+
hackett/private/prim/type
1113
syntax/parse/define)
1214

13-
(provide (data Unit) (data Bool) (data Tuple) (data Maybe) (data List)
14-
not or and if fst snd
15+
(provide not or and if fst snd unsafe-run-io!
1516

1617
id compose const flip
1718

19+
(class Eq) (class Show)
20+
(class Semigroup) (class Monoid)
21+
1822
(class Functor) (rename-out [map <$>]) <&> <$ $> ignore
1923
(class Applicative) sequence traverse
2024
(class Monad) =<< >>= do ap)
2125

2226
;; ---------------------------------------------------------------------------------------------------
23-
;; datatypes
24-
25-
(data Unit unit)
26-
(data Bool true false)
27-
(data (Tuple a b) (tuple a b))
28-
(data (Maybe a) (just a) nothing)
29-
(data (List a)
30-
{a :: (List a)} #:fixity right
31-
nil)
27+
;; basic operations
3228

3329
(defn not : {Bool -> Bool}
3430
[[true ] false]
@@ -52,6 +48,9 @@
5248
(defn snd : (∀ [a b] {(Tuple a b) -> b})
5349
[[(tuple _ x)] x])
5450

51+
(defn unsafe-run-io! : (∀ [a] {(IO a) -> a})
52+
[[(io f)] (snd (f real-world))])
53+
5554
;; ---------------------------------------------------------------------------------------------------
5655
;; function combinators
5756

@@ -67,6 +66,86 @@
6766
(defn flip : (∀ [a b c] {{a -> b -> c} -> b -> a -> c})
6867
[[f x y] (f y x)])
6968

69+
;; ---------------------------------------------------------------------------------------------------
70+
;; Show
71+
72+
(class (Show a)
73+
[show : {a -> String}])
74+
75+
(instance (Show Unit)
76+
[show (λ [unit] "unit")])
77+
78+
(instance (Show Bool)
79+
[show (λ* [[true ] "true"]
80+
[[false] "false"])])
81+
82+
(instance (Show Integer)
83+
[show show/Integer])
84+
85+
(instance (Show String)
86+
[show (λ [str] {"\"" ++ str ++ "\""})])
87+
88+
(instance (∀ [a] (Show a) => (Show (Maybe a)))
89+
[show (λ* [[(just x)] {"(just " ++ (show x) ++ ")"}]
90+
[[nothing ] "nothing"])])
91+
92+
(instance (∀ [a b] (Show a) (Show b) => (Show (Tuple a b)))
93+
[show (λ [(tuple a b)] {"(tuple " ++ (show a) ++ " " ++ (show b) ++ ")"})])
94+
95+
(instance (∀ [a] (Show a) => (Show (List a)))
96+
[show (λ* [[{y :: ys}] {"{" ++ (show y) ++ " :: " ++ (show ys) ++ "}"}]
97+
[[nil ] "nil"])])
98+
99+
;; ---------------------------------------------------------------------------------------------------
100+
;; Eq
101+
102+
(class (Eq a)
103+
[== : {a -> a -> Bool}])
104+
105+
(instance (Eq Unit)
106+
[== (λ [unit unit] true)])
107+
108+
(instance (Eq Bool)
109+
[== (λ* [[true y] y]
110+
[[false y] (not y)])])
111+
112+
(instance (Eq Integer)
113+
[== equal?/Integer])
114+
115+
(instance (Eq String)
116+
[== equal?/String])
117+
118+
(instance (∀ [a] (Eq a) => (Eq (Maybe a)))
119+
[== (λ* [[(just a) (just b)] (== a b)]
120+
[[nothing nothing ] true]
121+
[[_ _ ] false])])
122+
123+
(instance (∀ [a b] (Eq a) (Eq b) => (Eq (Tuple a b)))
124+
[== (λ [(tuple a b) (tuple c d)] (and (== a c) (== b d)))])
125+
126+
;; ---------------------------------------------------------------------------------------------------
127+
;; Semigroup / Monoid
128+
129+
(class (Semigroup a)
130+
[++ : {a -> a -> a}
131+
#:fixity right])
132+
133+
(instance (Semigroup String)
134+
[++ append/String])
135+
136+
(instance (∀ [a] (Semigroup (List a)))
137+
[++ (λ* [[{z :: zs} ys] {z :: {zs ++ ys}}]
138+
[[nil ys] ys])])
139+
140+
(class (Semigroup a) => (Monoid a)
141+
[mempty : a])
142+
143+
(instance (Monoid String)
144+
[mempty ""])
145+
146+
(instance (∀ [a] (Monoid (List a)))
147+
[mempty nil])
148+
70149
;; ---------------------------------------------------------------------------------------------------
71150
;; Functor
72151

@@ -93,6 +172,12 @@
93172
[map (λ* [[f {y :: ys}] {(f y) :: (map f ys)}]
94173
[[_ nil ] nil])])
95174

175+
(instance (Functor IO)
176+
[map (λ [f (io mx)]
177+
(io (λ [rw]
178+
(case (mx rw)
179+
[(tuple rw* a) (tuple rw* (f a))]))))])
180+
96181
;; ---------------------------------------------------------------------------------------------------
97182
;; Applicative
98183

@@ -116,6 +201,10 @@
116201
[pure (λ [x] {x :: nil})]
117202
[<*> ap])
118203

204+
(instance (Applicative IO)
205+
[pure (λ [x] (io (λ [rw] (tuple rw x))))]
206+
[<*> ap])
207+
119208
;; ---------------------------------------------------------------------------------------------------
120209
;; Monad
121210

@@ -152,3 +241,12 @@
152241
[join (λ* [[{{z :: zs} :: yss}] {z :: (join {zs :: yss})}]
153242
[[{nil :: yss}] (join yss)]
154243
[[nil ] nil])])
244+
245+
(instance (Monad IO)
246+
[join (λ [(io outer)]
247+
(io (λ [rw]
248+
(case (outer rw)
249+
[(tuple rw* m-inner)
250+
(case m-inner
251+
[(io inner)
252+
(inner rw*)])]))))])

hackett-lib/hackett/private/prim/io.rkt

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)