How can I implement this arrow? Thanks
Marc A. Ziegert
coeus at gmx.de
Tue Sep 16 07:38:45 EDT 2003
Am Dienstag 16 September 2003 04:57 schrieb Yu Di:
> Hi, I want to create an arrow which is essentially
>
> data MyArrow a b = MyArrow ((String, a) -> (String,b))
>
> i.e. there is an "information" asscioated with each piece of data
> (represented by the string), and I want
> to pass it around. But I have a problem about how to define "pure" and
> "first". At first, I declared
>
> pure f = MyArrow (\(s, x) -> (s, f x))
> first (MyArrow f) = MyArrow (\(s, (x, y)) -> let (s', z) = f (s, x) in (s',
> (z, y)))
>
> this seems to work, but then I begin to have problems with the
> "data-plumbing" pure arrows, e.g. in
>
> pure (\x -> (x, x)) >>> first someArrow >>>> pure (\(_, x) -> x)
>
> Ideally, this arrow will preserve whatever information I put there for the
> input, but because "first
> someArrow" will change the whole information associated with the pair of
> result, I can't find any
> way to let "pure (\(_, x)->x)" (which is an extremely generic function)
> retrieve the part of information for the second piece in the pair tuple.
what does the compiler say? or is it a runtime error?
how did you implement "(>>>) :: a b c -> a c d -> a b d"?
(MyArrow f1) >>> (MyArrow f2) = MyArrow (f2 . f1)
does this compile?:
(pure (\x -> (x, x)) :: MyArrow a (a,a)) >>> (first (someArrow :: MyArrow a b) :: MyArrow (a,a) (b,a)) >>> (pure (\(_, x) -> x) :: MyArrow (b,a) a)
pure and first seem to be correct.
but ... just as an (slow) alternative:
-- first :: a x fx -> a (x, y) (fx, y)
first (MyArrow f) = MyArrow $ (\((fs,fx),y)->(fs,(fx,y))) . (\(sx,y)->(f sx,y)) . (\(s,(x,y))->((s,x),y))
- marc
>
> Of course I can create specialized arrows for the tasks \x -> (x, x) and
> \(_, x) -> x which passes the information around, but this will become
> tedious as I will have to define specialized arrows for a lot of similar
> tasks one by one, and I won't be able to use the arrow pre-processor at all.
>
> So how can I implement this? Thanks very much!
>
> Di, Yu
> 9.15
>
> _________________________________________________________________
More information about the Haskell
mailing list