Divisible

class Contravariant f => Divisible f where
divide :: (a -> (b, c)) -> f b -> f c -> f a conquer :: f a divided :: Divisible f => f a -> f b -> f (a, b)
divided = divide id conquered :: Divisible f => f ()
conquered = conquer

Predicate 是个 Divisible

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

instance Divisible Predicate where
divide f (Predicate g) (Predicate h) = Predicate $ \a -> case f a of
(b, c) -> g b && h c
conquer = Predicate $ const True

Comparison 是个 Divisible

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

instance Divisible Comparison where
divide f (Comparison g) (Comparison h) = Comparison $ \a b -> case f a of
(a',a'') -> case f b of
(b',b'') -> g a' b' `mappend` h a'' b''
conquer = Comparison $ \_ _ -> EQ

应用 Divisible

Prelude Data.Functor.Contravariant Data.Functor.Contravariant.Divisible> getPredicate (divide (\x->(x,x)) (Predicate (>3)) (Predicate (<5))) 4
True
Prelude Data.Functor.Contravariant Data.Functor.Contravariant.Divisible> getPredicate conquer 2
True

参考链接

ZuriHac 2015 - Discrimination is Wrong: Improving Productivity

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

  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. weex-toolkit 使用

    weex-toolkit 的github地址:https://github.com/weexteam/weex-toolkit weex-toolkit: 初始化的项目是针对开发单个 Weex 页面而 ...

  2. Python中的类(classes)

    Python的类机制使用尽可能少的新语法和语义将类引入语言.python的类提供了面向对象程序设计语言所有的 标准特性:类继承机制允许有多个基类,一个派生类可以覆盖基类中的任何方法,一个方法可以使用相 ...

  3. bzoj1042硬币购物

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1042 dp预处理+容斥原理. 先预处理求出无限制的各面值的组成方案数 f (完全背包). 求s ...

  4. ERROR 1130 (HY000): Host '192.168.20.165' is not allowed to connect to this MySQL server

    问题 远程连接mysql时遇到如下问题: ERROR 1130 (HY000): Host '192.168.20.165' is not allowed to connect to this MyS ...

  5. Openfiler使用说明

    Openfiler使用说明 http://www.cnblogs.com/zb9222/p/6118074.html 一. Openfiler简介 Openfiler 能把标准x86/64架构的系统变 ...

  6. CSS Grid布局入门

    相信大家都比较熟悉flex布局了,最近有空研究了波grid布局,感觉虽然兼容性还不是太高,应用不是太普遍,但是功能非常强大.未来应该是grid+flex为主流,grid是二维布局,很灵活,适合整体构架 ...

  7. 2018-2019 Exp3 免杀原理与实践

    2018-2019 Exp3 免杀原理与实践 目录 一.实验内容说明及基础问题回答 二.实验过程 Task1 1.使用msf编码器生成后门程序及检测 Task1 2.msfvenom生成jar等文件 ...

  8. 致Python初学者:Anaconda入门使用指南

    http://python.jobbole.com/87522/ Anaconda使用总结 pasting

  9. Linux期中架构 全网备份案例

    server端脚本 #!/bin/bash #1 进行数据完整性验证 并生成结果 find /backup -type f -name "finger.txt"| xargs md ...

  10. [UE4]为什么会有类型检查

    类型检查 定义:对象的行为是否符合类型的行为 作用:帮助开发者找出潜在的错误. 类型转换 隐式类型转换:整数可以和浮点数运算 显式类型转换/强制类型转换