Type f in pointfree style?

Say I have features

g :: a -> b, h :: a -> c 

and

f :: b -> c -> d. 

Is it possible to write a function

 f' :: a -> a -> d 

specified

f' x y = f (g x) (h y) 

free-style dots ?.

You can write a function

f' a -> d, f' x = f (g x) (h x) 

in style without dots by setting

f' = (f <$> g) <*> h  

but I could not figure out how to make a more general case.

+4
source share
5 answers

We have:

k x y = (f (g x)) (h y)

and we want to write kin a dotless style.

The first argument passed in kis x. What do we need to do with x? Well, first we need to call gon it, and then f, and then do something fantastic to apply it to (h y).

k = fancy . f . g

What is that fancy? Well:

k x y = (fancy . f . g) x y
      = fancy (f (g x)) y
      = f (g x) (h y)

, fancy z y = z (h y). Eta-reduction, fancy z = z . h, fancy = (. h).

k = (. h) . f . g

                             β”Œβ”€β”€β”€β”           β”Œβ”€β”€β”€β”
                        x ───│ g │─── g x ───│   β”‚
                      /      β””β”€β”€β”€β”˜           β”‚   β”‚
               (x, y)                        β”‚ f │─── f (g x) (h y)
                      \      β”Œβ”€β”€β”€β”           β”‚   β”‚
                        y ───│ h │─── h y ───│   β”‚
                             β””β”€β”€β”€β”˜           β””β”€β”€β”€β”˜

                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      k

Control.Arrow:

k = curry ((g *** h) >>> uncurry f)
+22

-

f' x y = f (g x) (h y) 

f' = (. h) . f . g

f' = id (fix (const (flip ((.) . f . g) h))) 
f' = fix (const (flip ((.) . f . g) h)) 
f' = fix (const ((. h) . f . g)) 
f' = (. h) . f . g
+5

, , (. h) . f. g.

f', . ( , f'.)

f' (x, y) = f (g x) (h y)

fst snd :

f' t = f (g (fst t)) (h (snd t))

,

f' t = f ((g . fst) t) ((h . snd) t)

, , , :

f' = let g' = g . fst
         h' = h . snd
     in (f <$> g') <*> h'

: f' :: (a, a) -> d. , :

f' :: a -> a -> d
f' = let g' = g . fst
         h' = h . snd
     in curry $ (f <$> g') <*> h'

( , , Control.Arrow, Lynn.)

+3

" " (.),

(.) f g  =  (f . g)  =  (f .) g  =  (. g) f   -- the argument goes into the free slot
--       1           2           3

:

k x y =  (f (g x)) (h y)                      -- a (b c) === (a . b) c
      =  (f (g x) . h) y
      =  (. h)  (f (g x)) y
      =  (. h)  ((f . g)  x) y
      = ((. h) . (f . g)) x  y

, (.) , .

, , - , , :

k x y = (......) y
=>
k x   = (......)

Lather, , .


, ,

curry f x y = f (x,y)     

,

f (g x) (h y) = (f.g) x (h y)                      -- by B-combinator rule
              = (f.g.fst) (x,y) ((h.snd) (x,y))
              = (f.g.fst <*> h.snd) (x,y)          -- by S-combinator rule
              = curry (f.g.fst <*> h.snd) x y

, @chepner, .

So, you see, your (f.g <*> h) x 1 just becomes (f.g.fst <*> h.snd) (x,y). The same difference.


1 (because for functions (<$>) = (.))

+3
source

Control.compose

(g ~> h ~> id) f

Data.Function.Meld

f $* g $$ h *$ id

Data.Function.Tacit

lurryA @N2 (f <$> (g <$> _1) <*> (h <$> _2))
lurryA @N5 (_1 <*> (_2 <*> _4) <*> (_3 <*> _5)) f g h

Related Articles

+1
source

Source: https://habr.com/ru/post/1650033/


All Articles