Dicidable

class Divisible f => Decidable f where
lose :: (a -> Void) -> f a
choose :: (a -> Either b c) -> f b -> f c -> f a lost :: Decidable f => f Void
lost = lose id chosen :: Decidable f => f b -> f c -> f (Either b c)
chosen = choose id

Predicate 是个 Decidable

newtype Predicate a = Predicate { getPredicate :: a -> Bool }

instance Decidable Predicate where
lose f = Predicate $ \a -> absurd (f a)
choose f (Predicate g) (Predicate h) = Predicate $ either g h . f

Comparison 是个 Decidable

newtype Comparison a = Comparison { getComparison :: a -> a -> Ordering }

instance Decidable Comparison where
lose f = Comparison $ \a _ -> absurd (f a)
choose f (Comparison g) (Comparison h) = Comparison $ \a b -> case f a of
Left c -> case f b of
Left d -> g c d
Right{} -> LT
Right c -> case f b of
Left{} -> GT
Right d -> h c d

应用 Decidable

Prelude Data.Functor.Contravariant Data.Functor.Contravariant.Divisible> getPredicate (choose  (\x -> if x `mod` 2==1 then Left (x `div` 2) else Right (x `div` 2)) (Predicate (<2)) (Predicate (<3))) 5
False
Prelude Data.Functor.Contravariant Data.Functor.Contravariant.Divisible> getPredicate (choose (\x -> if x `mod` 2==1 then Left (x `div` 2) else Right (x `div` 2)) (Predicate (<2)) (Predicate (<3))) 4
True

参考链接

ZuriHac 2015 - Discrimination is Wrong: Improving Productivity```

Haskell语言学习笔记(63)Dicidable的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  3. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  4. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  5. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  6. Haskell语言学习笔记(72)Free Monad

    安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...

  7. Haskell语言学习笔记(44)Lens(2)

    自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...

  8. Haskell语言学习笔记(38)Lens(1)

    Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...

  9. Haskell语言学习笔记(92)HXT

    HXT The Haskell XML Toolbox (hxt) 是一个解析 XML 的库. $ cabal install hxt Installed hxt-9.3.1.16 Prelude&g ...

随机推荐

  1. python 函数参数的传递(参数带星号的说明) 元组传递 字典传递

    python中函数参数的传递是通过赋值来传递的.函数参数的使用又有俩个方面值得注意:1.函数参数是如何定义的 2.在调用函数的过程中参数是如何被解析 先看第一个问题,在python中函数参数的定义主要 ...

  2. codevs 1131 统计单词数

    #include<iostream> #include<string> using namespace std; int main() { string s, s0; getl ...

  3. ASP.NET中将数据作为XML数据发送 使用 Request.InputStream 接收

    将数据作为XML数据发送,例如:public void PostXml(string url, string xml){byte[] bytes = Encoding.UTF8.GetBytes(xm ...

  4. win xp 安装 VS2010 时要重启是因为没安装WINDOWS INSTALLER 4.5

    win xp 安装 VS2010 时要重启是因为没安装WINDOWS INSTALLER 4.5. 无意间看到VS2010安装列表中有一项是 WINDOWS INSTALLER 4.5 . 装这个玩意 ...

  5. AngularJS---核心特性

    步入正题.学习Angular,首先得了解.熟知.掌握它的四大核心特性. 一.MVC模式 Model(模型):是应用程序中用于处理应用程序数据逻辑的部分,通常模型对象负责在数据库中存取数据. View( ...

  6. <<APUE>> 编译方法

    /********************************************************************************第0种-最简单实用********** ...

  7. VBA注释临时

    Sub shishi() '按ABCDE为多选题定义答案; 'A.沙利度胺 B.异烟肼 C.利福平 'd.氯法齐明 E.氨苯砜 '46.各型麻风病的首选药物为(D) 'A.沙利度胺 B.异烟肼 C.利 ...

  8. 【Active入门-3】ActiveMQ学习-发布者与订阅者

    2015年4月28日 1个发布者,1个订阅者,topic 方式1: 先发布消息: 然后订阅消息: 方式2: 先订阅消息: 然后发布消息:订阅者如下: 结论1: 从上面可以看出,消息发布需要在线发布. ...

  9. 如何判断自己的浏览器支持的是javascript的那个版本

    <script language="javascript"> var jsversion = 1.0; </script> <script langu ...

  10. 小朋友学C语言(7)

    数组 一.数组简介 C 语言支持数组数据结构,它可以存储一个固定大小的相同类型元素的顺序集合.数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量. 数组的声明并不是声明一个个单独的变量,比 ...