File: Internals.hs

package info (click to toggle)
haskell-arrows 0.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 152 kB
  • ctags: 3
  • sloc: haskell: 664; makefile: 60; sh: 22
file content (256 lines) | stat: -rw-r--r-- 9,148 bytes parent folder | download | duplicates (3)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
{-# OPTIONS_GHC -fglasgow-exts #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Control.Arrow.Internals
-- Copyright   :  (c) Ross Paterson 2003
-- License     :  BSD-style (see the LICENSE file in the distribution)
--
-- Maintainer  :  ross@soi.city.ac.uk
-- Stability   :  experimental
-- Portability :  non-portable (multi-parameter type classes)
--
-- Manipulation of composite arrow types, beyond the basic lifting and
-- encapsulation provided with each arrow transformer.
--
-- The signatures are designed to be compatible with the proposed notation
-- for arrows, cf. <http://www.haskell.org/arrows/>.

-- #hide
module Control.Arrow.Internals (
		ArrowAddState(..),
		ArrowAddReader(..),
		ArrowAddWriter(..),
		ArrowAddError(..),
		ArrowAddStream(..),
	) where

import Control.Arrow
import Control.Arrow.Operations
import Data.Stream

-- | Adding a 'Control.Arrow.Transformer.State.StateArrow' to an
-- arrow type, but not necessarily as the outer arrow transformer.
--
-- Typically a composite arrow type is built by applying a series
-- of arrow transformer to a base arrow (usually either a function
-- arrow or a 'Kleisli' arrow.  One can add a transformer to the
-- top of this stack using the 'Control.Arrow.Transformer.lift'
-- method of the 'Control.Arrow.Transformer.ArrowTransformer' class,
-- or remove a state transformer from the top of the stack using the
-- 'Control.Arrow.Transformer.State.runState' encapsulation operator.
-- The methods of this class add and remove state transformers anywhere
-- in the stack.  In the instance
--
-- >	instance Arrow a => ArrowAddState s (ArrowState s a) a
--
-- they are equivalent to 'Control.Arrow.Transformer.lift' and
-- 'Control.Arrow.Transformer.State.runState' respectively.
-- Instances are lifted through other transformers with
--
-- >	instance ArrowAddState s a a' =>
-- >		ArrowAddState s (FooArrow a) (FooArrow a')

class (ArrowState s a, Arrow a') => ArrowAddState s a a' | a -> a' where

	-- | Lift a computation from an arrow to one with an added state.
	--
	-- Typical usage in arrow notation:
	--
	-- >	proc p -> ...
	-- >		(|liftState cmd|)

	liftState :: a' e b -> a e b

	-- | Elimination of a state transformer from a computation,
	-- exposing the initial and final states.
	--
	-- Typical usage in arrow notation:
	--
	-- >	proc p -> do
	-- >		...
	-- >		(result, final_state) <- (|elimState cmd|) init_state

	elimState :: a e b -> a' (e,s) (b,s)

-- | Adding a 'Control.Arrow.Transformer.Reader.ReaderArrow' to an
-- arrow type, but not necessarily as the outer arrow transformer.
--
-- Typically a composite arrow type is built by applying a series
-- of arrow transformer to a base arrow (usually either a function
-- arrow or a 'Kleisli' arrow.  One can add a transformer to the
-- top of this stack using the 'Control.Arrow.Transformer.lift'
-- method of the 'Control.Arrow.Transformer.ArrowTransformer' class,
-- or remove a state transformer from the top of the stack using the
-- 'Control.Arrow.Transformer.Reader.runReader' encapsulation operator.
-- The methods of this class add and remove state transformers anywhere
-- in the stack.  In the instance
--
-- >	instance Arrow a => ArrowAddReader r (ArrowReader r a) a
--
-- they are equivalent to 'Control.Arrow.Transformer.lift' and
-- 'Control.Arrow.Transformer.Reader.runReader' respectively.
-- Instances are lifted through other transformers with
--
-- >	instance ArrowAddReader r a a' =>
-- >		ArrowAddReader r (FooArrow a) (FooArrow a')

class (ArrowReader r a, Arrow a') => ArrowAddReader r a a' | a -> a' where

	-- | Lift a computation from an arrow to one with an added environment.
	--
	-- Typical usage in arrow notation:
	--
	-- >	proc p -> ...
	-- >		(|liftReader cmd|)

	liftReader :: a' e b -> a e b

	-- | Elimination of a state reader from a computation,
	-- taking a value for the state.
	--
	-- Typical usage in arrow notation:
	--
	-- >	proc p -> ...
	-- >		(|elimReader cmd|) env

	elimReader :: a e b -> a' (e,r) b

-- | Adding a 'Control.Arrow.Transformer.Writer.WriterArrow' to an
-- arrow type, but not necessarily as the outer arrow transformer.
--
-- Typically a composite arrow type is built by applying a series
-- of arrow transformer to a base arrow (usually either a function
-- arrow or a 'Kleisli' arrow.  One can add a transformer to the
-- top of this stack using the 'Control.Arrow.Transformer.lift'
-- method of the 'Control.Arrow.Transformer.ArrowTransformer' class,
-- or remove a state transformer from the top of the stack using the
-- 'Control.Arrow.Transformer.Writer.runWriter' encapsulation operator.
-- The methods of this class add and remove state transformers anywhere
-- in the stack.  In the instance
--
-- >	instance Arrow a => ArrowAddWriter w (ArrowWriter w a) a
--
-- they are equivalent to 'Control.Arrow.Transformer.lift' and
-- 'Control.Arrow.Transformer.Writer.runWriter' respectively.
-- Instances are lifted through other transformers with
--
-- >	instance ArrowAddWriter w a a' =>
-- >		ArrowAddWriter w (FooArrow a) (FooArrow a')

class (ArrowWriter w a, Arrow a') => ArrowAddWriter w a a' | a -> a' where

	-- | Lift a computation from an arrow to one with added output.
	--
	-- Typical usage in arrow notation:
	--
	-- >	proc p -> ...
	-- >		(|liftWriter cmd|)

	liftWriter :: a' e b -> a e b

	-- | Elimination of an output writer from a computation,
	-- providing the accumulated output.
	--
	-- Typical usage in arrow notation:
	--
	-- >	proc p -> do
	-- >		...
	-- >		(result, output) <- (|elimWriter cmd|)

	elimWriter :: a e b -> a' e (b,w)

-- | Adding a 'Control.Arrow.Transformer.Error.ErrorArrow' to an
-- arrow type, but not necessarily as the outer arrow transformer.
--
-- Typically a composite arrow type is built by applying a series
-- of arrow transformer to a base arrow (usually either a function
-- arrow or a 'Kleisli' arrow.  One can add a transformer to the
-- top of this stack using the 'Control.Arrow.Transformer.lift'
-- method of the 'Control.Arrow.Transformer.ArrowTransformer' class,
-- or remove a state transformer from the top of the stack using the
-- 'Control.Arrow.Transformer.Error.runError' encapsulation operator.
-- The methods of this class add and remove state transformers anywhere
-- in the stack.  In the instance
--
-- >	instance Arrow a => ArrowAddError ex (ArrowError ex a) a
--
-- they are equivalent to 'Control.Arrow.Transformer.lift' and
-- 'Control.Arrow.Transformer.Error.runError' respectively.
-- Instances are lifted through other transformers with
--
-- >	instance ArrowAddError ex a a' =>
-- >		ArrowAddError ex (FooArrow a) (FooArrow a')
--
-- This could be combined with 'Control.Arrow.Transformer.Error.handle',
-- since the resulting arrow is always the arrow of the handler.
-- Separating them has the advantage of consistency with the other arrows,
-- and might give more helpful type error messages.

class (ArrowError ex a, Arrow a') => ArrowAddError ex a a' | a -> a' where

	-- | Lift a computation from an arrow to one with error handling.
	--
	-- Typical usage in arrow notation:
	--
	-- >	proc p -> ...
	-- >		(|liftError cmd|)

	liftError :: a' e b -> a e b

	-- | Elimination of errors from a computation,
	-- by completely handling any errors.
	--
	-- Typical usage in arrow notation:
	--
	-- >	proc p -> ...
	-- >		body `elimError` \ex -> handler

	elimError :: a e b -> a' (e,ex) b -> a' e b

-- | Adding a 'Control.Arrow.Transformer.Stream.StreamArrow' to an
-- arrow type, but not necessarily as the outer arrow transformer.
--
-- Typically a composite arrow type is built by applying a series
-- of arrow transformer to a base arrow (usually either a function
-- arrow or a 'Kleisli' arrow.  One can add a transformer to the
-- top of this stack using the 'Control.Arrow.Transformer.lift'
-- method of the 'Control.Arrow.Transformer.ArrowTransformer' class,
-- or remove a state transformer from the top of the stack using the
-- 'Control.Arrow.Transformer.Stream.runStream' encapsulation operator.
-- The methods of this class add and remove state transformers anywhere
-- in the stack.  In the instance
--
-- >	instance Arrow a => ArrowAddStream (ArrowStream a) a
--
-- they are equivalent to 'Control.Arrow.Transformer.lift' and
-- 'Control.Arrow.Transformer.Stream.runStream' respectively.
-- Instances are lifted through other transformers with
--
-- >	instance ArrowAddStream a a' =>
-- >		ArrowAddStream (FooArrow a) (FooArrow a')

class (ArrowCircuit a, Arrow a') => ArrowAddStream a a' | a -> a' where

	-- | Lift a computation from an arrow to a stream processing one.
	--
	-- Typical usage in arrow notation:
	--
	-- >	proc p -> ...
	-- >		(|liftStream cmd|)

	liftStream :: a' e b -> a e b

	-- | Run a stream processor on a stream of inputs,
	-- obtaining a stream of outputs.
	--
	-- Typical usage in arrow notation:
	--
	-- >	proc p -> do
	-- >		...
	-- >		ys <- (|elimStream (\x -> ...)|) xs
	--
	-- Here @xs@ refers to the input stream and @x@ to individual
	-- elements of that stream.  @ys@ is bound to the output stream.

	elimStream :: a (e,b) c -> a' (e,Stream b) (Stream c)