Haskell语言学习笔记(63)Dicidable
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的更多相关文章
- Haskell语言学习笔记(88)语言扩展(1)
ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...
- Haskell语言学习笔记(79)lambda演算
lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...
- Haskell语言学习笔记(69)Yesod
Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...
- Haskell语言学习笔记(20)IORef, STRef
IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...
- Haskell语言学习笔记(39)Category
Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...
- Haskell语言学习笔记(72)Free Monad
安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...
- Haskell语言学习笔记(44)Lens(2)
自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...
- Haskell语言学习笔记(38)Lens(1)
Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...
- Haskell语言学习笔记(92)HXT
HXT The Haskell XML Toolbox (hxt) 是一个解析 XML 的库. $ cabal install hxt Installed hxt-9.3.1.16 Prelude&g ...
随机推荐
- netty答题
1,介绍一下netty netty封装了Java原生的nio,是一个异步和数据驱动的网络编程框架, 与tcp: netty -> Java Runtime Socket (io.nio.nio2 ...
- Rockchip parameter文件详解
瑞芯微的安卓平台使用 parameter 文件来配置一些系统参数,比如定义串口号,固件版本,flash分区信息等. parameter 文件是由 BootLoader 解析的,大小不能超过 64 KB ...
- 码出高效,阿里巴巴JAVA开发手册1.4.0
码出高效,阿里巴巴JAVA开发手册1.4.0阅读笔记 一.编程规约(三) 代码格式// 关键词if与括号之间必须有一个空格,括号内的f与左括号,0与右括号不需要空格 if (flag == 0) { ...
- vue 之radio绑定v-model
示例: 单选radio <label ><input type="radio" value="0" v-model="branch& ...
- 手动安装Android Support Library(23.0.1)
在搭建React-Native开发环境的时候,使用Android Sdk Manager无法找到Android Support Library这一项. 所以google了一下,找到了解决办法. 访问A ...
- Linux版本使用的文件系统类型
1. cat /etc/fstab 2. df -T -h
- javascript节点操作appendChild()
cloneNode(a)方法接受一个布尔值参数,表示是否深拷贝 true:表示执行深拷贝,复制本节点以及整个子节点树. false:浅拷贝.只复制节点本身. 复制后返回的节点副本属于文档所有,但是并没 ...
- 让MySql支持Emoji表情(MySQL中4字节utf8字符保存方法)
手机端插入Emoji表情,保存到数据库时报错: Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x84' ...
- ElasticSearch 搜索原理
运行结果:返回5条数据 参考代码ESTestDocumentAPI.java package com.dajiangtai.djt_spider.elasticsearch; import java. ...
- border-radius bug 收集
border-radius我相信对于老一辈的前端们有着特殊的感情,在经历了没有圆角的蛮荒时代,到如今 CSS3 遍地开花,我们还是很幸福的. 然而即使到了三星大脸流行时代,border-radius在 ...