Pattern match Haskell Higher order function?

前端 未结 2 1064
遥遥无期
遥遥无期 2020-12-19 20:43

Imagine I have a custom type and two functions:

type MyType = Int -> Bool

f1 :: MyType -> Int
f3 :: MyType -> MyType -> MyType

2条回答
  •  旧巷少年郎
    2020-12-19 20:48

    Once a function has been applied its syntactic form is lost. There is now way, should I provide you 2 + 3 to distinguish what you get from just 5. It could have arisen from 2 + 3, or 3 + 2, or the mere constant 5.

    If you need to capture syntactic structure then you need to work with syntactic structure.

    data Exp = I Int | Plus Exp Exp
    
    justFive :: Exp
    justFive = I 5
    
    twoPlusThree :: Exp
    twoPlusThree = I 2 `Plus` I 3
    
    threePlusTwo :: Exp
    threePlusTwo = I 2 `Plus` I 3
    

    Here the data type Exp captures numeric expressions and we can pattern match upon them:

    isTwoPlusThree :: Exp -> Bool
    isTwoPlusThree (Plus (I 2) (I 3)) = True
    isTwoPlusThree _ = False
    

    But wait, why am I distinguishing between "constructors" which I can pattern match on and.... "other syntax" which I cannot?

    Essentially, constructors are inert. The behavior of Plus x y is... to do nothing at all, to merely remain as a box with two slots called "Plus _ _" and plug the two slots with the values represented by x and y.

    On the other hand, function application is the furthest thing from inert! When you apply an expression to a function that function (\x -> ...) replaces the xes within its body with the applied value. This dynamic reduction behavior means that there is no way to get a hold of "function applications". They vanish into thing air as soon as you look at them.

提交回复
热议问题