Bool 和 Maybe () 是同构类型 (ignoring problems involving bottom),两者之间的以下映射证明了这一点:
b2m :: Bool -> Maybe ()
b2m True = Just ()
b2m False = Nothing
m2b :: Maybe () -> Bool
m2b (Just ()) = True
m2b Nothing = False
很容易验证b2m . m2b和m2b . b2m都等价于id:
m2b . b2m $ True == m2b (b2m True) == m2b (Just ()) == True == id True
m2b . b2m $ Fals == m2b (b2m False) == m2b Nothing == False == id False
b2m . m2b (Just ()) == b2m (m2b (Just ())) == b2m True == Just () == id (Just ())
b2m . m2b Nothing == b2m (m2b Nothing) == b2m False == Nothing == id Nothing
在您的问题中,您没有一个态射。您拥有类型为 Bool -> Maybe () 的 4 个不同函数的构建块,如下所示:
f1,f2,f3,f4 :: Bool -> Maybe ()
f1 True = Just ()
f1 False = Nothing
f2 True = Nothing
f2 False = Just ()
f3 True = Just ()
f3 False = Just ()
f4 True = Nothing
f4 False = Nothing
同样,Maybe () -> Bool 类型有 4 个不同的函数:
f5,f6,f7,f8 :: Maybe () -> Bool
f5 (Just ()) = True
f5 Nothing = False
f6 (Just ()) = False
f6 Nothing = True
f7 (Just ()) = True
f7 Nothing = True
f8 (Just ()) = False
f8 Nothing = False
有些函数对形成同构,但有些则不然。此答案的顶部显示f1 和f5 可以,但例如f3 和f8 不可以。
f3 . f8 $ (Just ()) == f3 (f8 Just ()) == f3 False == Just () == id (Just ())
f3 . f8 $ Nothing == f3 (f8 Nothing) == f3 False == Just () != id Nothing