Proving Composition Law for Maybe Applicative

前端 未结 4 1813
旧巷少年郎
旧巷少年郎 2021-01-12 21:19

So, I wanted to manually prove the Composition law for Maybe applicative which is:

u <*> (v <*> w) = pure (.) <*> u <*> v <*> w         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-12 21:44

    A useful tool to learn when proving stuff about Haskell code is Agda: Here is a short proof stating what you want to prove:

    data Maybe (A : Set) : Set where
      Just : (a : A) -> Maybe A
      Nothing : Maybe A
    
    _<*>_ : {A B : Set} -> Maybe (A -> B) -> Maybe A -> Maybe B
    Just f <*> Just a = Just (f a)
    Just f <*> Nothing = Nothing
    Nothing <*> a = Nothing
    
    pure : {A : Set} -> (a : A) -> Maybe A
    pure a = Just a
    
    data _≡_ {A : Set} (x : A) : A → Set where
      refl : x ≡ x
    
    _∘_ : {A B C : Set} ->
          (B -> C) -> (A -> B) -> A -> C
    _∘_ f g = λ z → f (g z)
    
    maybeAppComp : {A B C : Set} -> (u : Maybe (B -> A)) -> (v : Maybe (C -> B)) -> (w : Maybe C)
                -> (u <*> (v <*> w)) ≡ (((pure _∘_ <*> u) <*> v) <*> w)
    maybeAppComp (Just f) (Just g) (Just w) = refl
    maybeAppComp (Just f) (Just g) Nothing = refl
    maybeAppComp (Just f) Nothing (Just w) = refl
    maybeAppComp (Just f) Nothing Nothing = refl
    maybeAppComp Nothing (Just g) (Just w) = refl
    maybeAppComp Nothing (Just a) Nothing = refl
    maybeAppComp Nothing Nothing (Just w) = refl
    maybeAppComp Nothing Nothing Nothing = refl
    

    This illustrates a couple of points others have pointed out:

    • Which definitions you use are important for the proof, and should be made explicit. In my case I did not want to use Agda's libraries.
    • Case analysis is key to making these kinds of proofs.
    • In fact, the proof becomes trivial once case analysis is done. The Agda compire/proof system is able to unify the proof for you.

提交回复
热议问题