Skip to content

Commit 87368e8

Browse files
Port Type.Proxy code from purescript-proxy
1 parent 070b6eb commit 87368e8

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed

src/Type/Proxy.purs

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
-- | The `Proxy` type and values are for situations where type information is
2+
-- | required for an input to determine the type of an output, but where it is
3+
-- | not possible or convenient to provide a _value_ for the input.
4+
-- |
5+
-- | A hypothetical example: if you have a class that is used to handle the
6+
-- | result of an AJAX request, you may want to use this information to set the
7+
-- | expected content type of the request, so you might have a class something
8+
-- | like this:
9+
-- |
10+
-- | ``` purescript
11+
-- | class AjaxResponse a where
12+
-- | responseType :: a -> ResponseType
13+
-- | fromResponse :: Foreign -> a
14+
-- | ```
15+
-- |
16+
-- | The problem here is `responseType` requires a value of type `a`, but we
17+
-- | won't have a value of that type until the request has been completed. The
18+
-- | solution is to use a `Proxy` type instead:
19+
-- |
20+
-- | ``` purescript
21+
-- | class AjaxResponse a where
22+
-- | responseType :: Proxy a -> ResponseType
23+
-- | fromResponse :: Foreign -> a
24+
-- | ```
25+
-- |
26+
-- | We can now call `responseType (Proxy :: Proxy SomeContentType)` to produce
27+
-- | a `ResponseType` for `SomeContentType` without having to construct some
28+
-- | empty version of `SomeContentType` first. In situations like this where
29+
-- | the `Proxy` type can be statically determined, it is recommended to pull
30+
-- | out the definition to the top level and make a declaration like:
31+
-- |
32+
-- | ``` purescript
33+
-- | _SomeContentType :: Proxy SomeContentType
34+
-- | _SomeContentType = Proxy
35+
-- | ```
36+
-- |
37+
-- | That way the proxy value can be used as `responseType _SomeContentType`
38+
-- | for improved readability. However, this is not always possible, sometimes
39+
-- | the type required will be determined by a type variable. As PureScript has
40+
-- | scoped type variables, we can do things like this:
41+
-- |
42+
-- | ``` purescript
43+
-- | makeRequest :: URL -> ResponseType -> Aff _ Foreign
44+
-- | makeRequest = ...
45+
-- |
46+
-- | fetchData :: forall a. (AjaxResponse a) => URL -> Aff _ a
47+
-- | fetchData url = fromResponse <$> makeRequest url (responseType (Proxy :: Proxy a))
48+
-- | ```
49+
module Type.Proxy where
50+
51+
import Prelude
52+
53+
-- | Proxy type for all `kind`s.
54+
data Proxy :: forall k. k -> Type
55+
data Proxy a = Proxy
56+
57+
derive instance eqProxy :: Eq (Proxy a)
58+
59+
derive instance functorProxy :: Functor Proxy
60+
61+
derive instance ordProxy :: Ord (Proxy a)
62+
63+
instance applicativeProxy :: Applicative Proxy where
64+
pure _ = Proxy
65+
66+
instance applyProxy :: Apply Proxy where
67+
apply _ _ = Proxy
68+
69+
instance bindProxy :: Bind Proxy where
70+
bind _ _ = Proxy
71+
72+
instance booleanAlgebraProxy :: BooleanAlgebra (Proxy a)
73+
74+
instance boundedProxy :: Bounded (Proxy a) where
75+
bottom = Proxy
76+
top = Proxy
77+
78+
instance commutativeRingProxy :: CommutativeRing (Proxy a)
79+
80+
instance discardProxy :: Discard (Proxy a) where
81+
discard = bind
82+
83+
instance heytingAlgebraProxy :: HeytingAlgebra (Proxy a) where
84+
conj _ _ = Proxy
85+
disj _ _ = Proxy
86+
implies _ _ = Proxy
87+
ff = Proxy
88+
not _ = Proxy
89+
tt = Proxy
90+
91+
instance monadProxy :: Monad Proxy
92+
93+
instance ringProxy :: Ring (Proxy a) where
94+
sub _ _ = Proxy
95+
96+
instance semigroupProxy :: Semigroup (Proxy a) where
97+
append _ _ = Proxy
98+
99+
instance semiringProxy :: Semiring (Proxy a) where
100+
add _ _ = Proxy
101+
mul _ _ = Proxy
102+
one = Proxy
103+
zero = Proxy
104+
105+
instance showProxy :: Show (Proxy a) where
106+
show _ = "Proxy"
107+
108+
-- | Value proxy for kind `Type -> Type` types.
109+
-- | **Deprecated as of v0.14.0 PureScript release**: use `Proxy` instead.
110+
data Proxy2 :: (Type -> Type) -> Type
111+
data Proxy2 f = Proxy2
112+
113+
derive instance eqProxy2 :: Eq (Proxy2 a)
114+
115+
derive instance ordProxy2 :: Ord (Proxy2 a)
116+
117+
instance booleanAlgebraProxy2 :: BooleanAlgebra (Proxy2 a)
118+
119+
instance boundedProxy2 :: Bounded (Proxy2 a) where
120+
bottom = Proxy2
121+
top = Proxy2
122+
123+
instance commutativeRingProxy2 :: CommutativeRing (Proxy2 a)
124+
125+
instance discardProxy2 :: Discard (Proxy2 a) where
126+
discard = bind
127+
128+
instance heytingAlgebraProxy2 :: HeytingAlgebra (Proxy2 a) where
129+
conj _ _ = Proxy2
130+
disj _ _ = Proxy2
131+
implies _ _ = Proxy2
132+
ff = Proxy2
133+
not _ = Proxy2
134+
tt = Proxy2
135+
136+
instance ringProxy2 :: Ring (Proxy2 a) where
137+
sub _ _ = Proxy2
138+
139+
instance semigroupProxy2 :: Semigroup (Proxy2 a) where
140+
append _ _ = Proxy2
141+
142+
instance semiringProxy2 :: Semiring (Proxy2 a) where
143+
add _ _ = Proxy2
144+
mul _ _ = Proxy2
145+
one = Proxy2
146+
zero = Proxy2
147+
148+
instance showProxy2 :: Show (Proxy2 a) where
149+
show _ = "Proxy2"
150+
151+
-- | Value proxy for kind `Type -> Type -> Type` types.
152+
-- | **Deprecated as of v0.14.0 PureScript release**: use `Proxy` instead.
153+
data Proxy3 (a :: Type -> Type -> Type) = Proxy3
154+
155+
derive instance eqProxy3 :: Eq (Proxy3 a)
156+
157+
derive instance ordProxy3 :: Ord (Proxy3 a)
158+
159+
instance booleanAlgebraProxy3 :: BooleanAlgebra (Proxy3 a)
160+
161+
instance boundedProxy3 :: Bounded (Proxy3 a) where
162+
bottom = Proxy3
163+
top = Proxy3
164+
165+
instance commutativeRingProxy3 :: CommutativeRing (Proxy3 a)
166+
167+
instance discardProxy3 :: Discard (Proxy3 a) where
168+
discard = bind
169+
170+
instance heytingAlgebraProxy3 :: HeytingAlgebra (Proxy3 a) where
171+
conj _ _ = Proxy3
172+
disj _ _ = Proxy3
173+
implies _ _ = Proxy3
174+
ff = Proxy3
175+
not _ = Proxy3
176+
tt = Proxy3
177+
178+
instance ringProxy3 :: Ring (Proxy3 a) where
179+
sub _ _ = Proxy3
180+
181+
instance semigroupProxy3 :: Semigroup (Proxy3 a) where
182+
append _ _ = Proxy3
183+
184+
instance semiringProxy3 :: Semiring (Proxy3 a) where
185+
add _ _ = Proxy3
186+
mul _ _ = Proxy3
187+
one = Proxy3
188+
zero = Proxy3
189+
190+
instance showProxy3 :: Show (Proxy3 a) where
191+
show _ = "Proxy3"

0 commit comments

Comments
 (0)