Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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
Tags more
Archives
Today
Total
관리 메뉴

HASKELL

Function compositions - difficult cases 본문

Basic Syntax

Function compositions - difficult cases

__main__ 2020. 11. 24. 13:35

Function composition, 여러 가지 복잡한 경우의 타입 정리

 

-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- I. `(.)(.)`
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

-- Haskell function composition, type of (.)(.) and how it's presented
-- https://stackoverflow.com/questions/16202289/haskell-function-composition-type-of-and-how-its-presented
-- https://stackoverflow.com/questions/39207345/how-to-understand-the-function-in-haskell

-- infix  expression: f . g
-- prefix expression: (.) f g               -- f ~ first, g ~ second function
-- (.)   :: (b -> c) -> (a -> b) -> a -> c  -- need two functions

-- Substitue the operator '(.)' with a function symbol 'f' (f :: x -> y)
-- But what is the type of '(.) f'?
-- (.) f :: ???

-- (b -> c) -> (a -> b) -> a -> c
-- first fx    second fx   arg  result
--  x -> y                                  -- apply the type of f (:: x -> y)
-- --------------------------------         -- We can get the type of '(.) f'
--             (a -> x) -> a -> y           -- b ~ x, c ~ y
-- So, result of `(.) f` is ...
-- (.) f :: (a -> x) -> a -> y

-- We assumed that '(.)' is 'f' above.
-- f   :: x        -> y
-- (.) :: (q -> r) -> (p -> q) -> p -> r
-- so,   x ~ q -> r
--       y ~ (p -> q) -> p -> r

-- (.) f  :: (a -> x)      -> a -> y
--           (a -> q -> r) -> a -> (p -> q) -> p -> r
-- (.)(.) :: (a -> b -> c) -> a -> (p -> b) -> p -> c
--           -------------         --------
--           binary fx             unary fx

--            ┌──────────────────────────────────────────────────────────────────────────┐
--            │                                                                          │
--       ┌─────────────────────────────────────────────────┐                             │
--       │    │                                            │                             │
--  ┌─────────────────────────┐                      ┌──────────────────────┐            │
--  │    │    │               │                      │     │                │            │
--  ↓    ↓    ↓               │                      ↓     │                │            │
-- (a -> b -> c)      ->      a          ->         (a1 -> b)        ->     a1     ->    c
-- ───────────              ───                     ───────                ──
--       ↓                    ↓                         ↓                   ↓
--       (f)                  (x)                       (g)                 (y)
--       ↓                    ↓                         ↓                   ↓
-- a function        a thing that works      a function of one        a thing that
-- of two arguments  as the first argument   argument that            works as the
-- that returns      of f                    returns a thing          argument of g
-- the same type                             suitable as the second
-- (.)(.) returns                            argument of f

-- λ> (.)(.) (+) 10 (*2) 20
--    50

-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- II. `(.) (.) (.)`
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

-- function application is left-associative
-- (.)(.)(.) == ((.)(.))(.)
--               ------ ---
--                      f
-- f ~ (l -> m -> n)
-- (.)(.)   :: (a -> b -> c) -> a -> (p -> b) -> p -> c
--             (l -> m -> n)                             ~ f
--             ----------------------------------------
--                              l -> (p -> m) -> p -> n  ~ result

-- f   :: l        -> m        -> n
-- (.) :: (b -> c) -> (a -> b) -> a -> c
-- l ~ b -> c
-- m ~ a -> b
-- n ~ a -> c
-- (.)(.) f :: l        -> (p -> m)      -> p -> n
--          :: (b -> c) -> (p -> a -> b) -> p -> a -> c

-- λ> (.)(.)(.) (*2) (+) 10 10
--    40

-- composition of composition
-- https://stackoverflow.com/questions/28657765/composition-of-compositions-in-haskell
-- lambda calculus
-- https://stackoverflow.com/questions/17585649/composing-function-composition-how-does-work

-- Desugar infix notation:
-- (.) (.) (.)
-- Eta-expand:
-- (\ a b -> (.) a b) (\ c d -> (.) c d) (\ e f -> (.) e f)
-- Inline the definition of (.):
-- (\ a b x -> a (b x)) (\ c d y -> c (d y)) (\ e f z -> e (f z))
-- Substitute a:
-- (\ b x -> (\ c d y -> c (d y)) (b x)) (\ e f z -> e (f z))
-- Substitute b:
-- (\ x -> (\ c d y -> c (d y)) ((\ e f z -> e (f z)) x))
-- Substitute e:
-- (\ x -> (\ c d y -> c (d y)) (\ f z -> x (f z)))
-- Substitute c:
-- (\ x -> (\ d y -> (\ f z -> x (f z)) (d y)))
-- Substitute f:
-- (\ x -> (\ d y -> (\ z -> x (d y z))))
-- Resugar lambda notation:
-- \ x d y z -> x (d y z)

-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- III. `(.) . (.)`  == `(.) (.) (.)`
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

-- Composition with binary function
-- https://riptutorial.com/haskell/example/16126/composition-with-binary-function
-- https://stackoverflow.com/questions/17585649/composing-function-composition-how-does-work

 

Comments