Scalaz(9)- typeclass:checking instance abiding the laws

  在前几篇关于Functor和Applilcative typeclass的讨论中我们自定义了一个类型Configure,Configure类型的定义是这样的:

 1 case class Configure[+A](get: A)
 2 object Configure {
 3     implicit val configFunctor = new Functor[Configure] {
 4         def map[A,B](ca: Configure[A])(f: A => B): Configure[B] = Configure(f(ca.get))
 5     }
 6     implicit val configApplicative = new Applicative[Configure] {
 7         def point[A](a: => A) = Configure(a)
 8         def ap[A,B](ca: => Configure[A])(cfab: => Configure[A => B]): Configure[B] = cfab map {fab => fab(ca.get)}
 9     }
10 }

通过定义了Configure类型的Functor和Applicative隐式实例(implicit instance),我们希望Configure类型既是一个Functor也是一个Applicative。那么怎么才能证明这个说法呢?我们只要证明Configure类型的实例能遵循它所代表的typeclass操作定律就行了。Scalaz为大部分typeclass提供了测试程序(scalacheck properties)。在scalaz/scalacheck-binding/src/main/scala/scalaz/scalacheck/scalazProperties.scala里我们可以发现有关functor scalacheck properties:

 1 object functor {
 2     def identity[F[_], X](implicit F: Functor[F], afx: Arbitrary[F[X]], ef: Equal[F[X]]) =
 3       forAll(F.functorLaw.identity[X] _)
 4 
 5     def composite[F[_], X, Y, Z](implicit F: Functor[F], af: Arbitrary[F[X]], axy: Arbitrary[(X => Y)],
 6                                    ayz: Arbitrary[(Y => Z)], ef: Equal[F[Z]]) =
 7       forAll(F.functorLaw.composite[X, Y, Z] _)
 8 
 9     def laws[F[_]](implicit F: Functor[F], af: Arbitrary[F[Int]], axy: Arbitrary[(Int => Int)],
10                    ef: Equal[F[Int]]) = new Properties("functor") {
11       include(invariantFunctor.laws[F])
12       property("identity") = identity[F, Int]
13       property("composite") = composite[F, Int, Int, Int]
14     }
15   }

可以看到:functor.laws[F[_]]主要测试了identity, composite及invariantFunctor的properties。在scalaz/Functor.scala文件中定义了这几条定律:

 1  trait FunctorLaw extends InvariantFunctorLaw {
 2     /** The identity function, lifted, is a no-op. */
 3     def identity[A](fa: F[A])(implicit FA: Equal[F[A]]): Boolean = FA.equal(map(fa)(x => x), fa)
 4 
 5     /**
 6      * A series of maps may be freely rewritten as a single map on a
 7      * composed function.
 8      */
 9     def composite[A, B, C](fa: F[A], f1: A => B, f2: B => C)(implicit FC: Equal[F[C]]): Boolean = FC.equal(map(map(fa)(f1))(f2), map(fa)(f2 compose f1))
10   }
11  。

我们在下面试着对那个Configure类型进行Functor实例和Applicative实例的测试:

 1 import scalaz._
 2 import Scalaz._
 3 import shapeless._
 4 import scalacheck.ScalazProperties._
 5 import scalacheck.ScalazArbitrary._
 6 import scalacheck.ScalaCheckBinding._
 7 import org.scalacheck.{Gen, Arbitrary}
 8 implicit def cofigEqual[A]: Equal[Configure[A]] = Equal.equalA
 9                                                   //> cofigEqual: [A#2921073]=> scalaz#31.Equal#41646[Exercises#29.ex1#59011.Confi
10                                                   //| gure#2921067[A#2921073]]
11 implicit def configArbi[A](implicit a: Arbitrary[A]): Arbitrary[Configure[A]] =
12    a map { b => Configure(b) }                    //> configArbi: [A#2921076](implicit a#2921242: org#15.scalacheck#121951.Arbitra
13                                                   //| ry#122597[A#2921076])org#15.scalacheck#121951.Arbitrary#122597[Exercises#29.
14                                                   //| ex1#59011.Configure#2921067[A#2921076]]

除了需要的import外还必须定义Configure类型的Equal实例以及任意测试数据产生器(test data generator)configArbi[A]。我们先测试Functor属性:

1 functor.laws[Configure].check                     //> 
2 + functor.invariantFunctor.identity: OK, passed 100 tests.
3                                                   //| 
4 + functor.invariantFunctor.composite: OK, passed 100 tests.
5                                                   //| 
6 + functor.identity: OK, passed 100 tests.
7                                                   //| 
8 + functor.composite: OK, passed 100 tests.

成功通过Functor定律测试。

再看看Applicative的scalacheck property:scalaz/scalacheck/scalazProperties.scala

 1  object applicative {
 2     def identity[F[_], X](implicit f: Applicative[F], afx: Arbitrary[F[X]], ef: Equal[F[X]]) =
 3       forAll(f.applicativeLaw.identityAp[X] _)
 4 
 5     def homomorphism[F[_], X, Y](implicit ap: Applicative[F], ax: Arbitrary[X], af: Arbitrary[X => Y], e: Equal[F[Y]]) =
 6       forAll(ap.applicativeLaw.homomorphism[X, Y] _)
 7 
 8     def interchange[F[_], X, Y](implicit ap: Applicative[F], ax: Arbitrary[X], afx: Arbitrary[F[X => Y]], e: Equal[F[Y]]) =
 9       forAll(ap.applicativeLaw.interchange[X, Y] _)
10 
11     def mapApConsistency[F[_], X, Y](implicit ap: Applicative[F], ax: Arbitrary[F[X]], afx: Arbitrary[X => Y], e: Equal[F[Y]]) =
12       forAll(ap.applicativeLaw.mapLikeDerived[X, Y] _)
13 
14     def laws[F[_]](implicit F: Applicative[F], af: Arbitrary[F[Int]],
15                    aff: Arbitrary[F[Int => Int]], e: Equal[F[Int]]) = new Properties("applicative") {
16       include(ScalazProperties.apply.laws[F])
17       property("identity") = applicative.identity[F, Int]
18       property("homomorphism") = applicative.homomorphism[F, Int, Int]
19       property("interchange") = applicative.interchange[F, Int, Int]
20       property("map consistent with ap") = applicative.mapApConsistency[F, Int, Int]
21     }
22   }

applicative.laws定义了4个测试Property再加上apply的测试property。这些定律(laws)在scalaz/Applicative.scala里定义了:

 1  trait ApplicativeLaw extends ApplyLaw {
 2     /** `point(identity)` is a no-op. */
 3     def identityAp[A](fa: F[A])(implicit FA: Equal[F[A]]): Boolean =
 4       FA.equal(ap(fa)(point((a: A) => a)), fa)
 5 
 6     /** `point` distributes over function applications. */
 7     def homomorphism[A, B](ab: A => B, a: A)(implicit FB: Equal[F[B]]): Boolean =
 8       FB.equal(ap(point(a))(point(ab)), point(ab(a)))
 9 
10     /** `point` is a left and right identity, F-wise. */
11     def interchange[A, B](f: F[A => B], a: A)(implicit FB: Equal[F[B]]): Boolean =
12       FB.equal(ap(point(a))(f), ap(f)(point((f: A => B) => f(a))))
13 
14     /** `map` is like the one derived from `point` and `ap`. */
15     def mapLikeDerived[A, B](f: A => B, fa: F[A])(implicit FB: Equal[F[B]]): Boolean =
16       FB.equal(map(fa)(f), ap(fa)(point(f)))
17   }

再测试一下Configure类型是否也遵循Applicative定律:

 1 pplicative.laws[Configure].check                 //> 
 2 + applicative.apply.functor.invariantFunctor.identity: OK, passed 100 tests
 3                                                   //| 
 4                                                   //|   .
 5                                                   //| 
 6 + applicative.apply.functor.invariantFunctor.composite: OK, passed 100 test
 7                                                   //| 
 8                                                   //|   s.
 9                                                   //| 
10 + applicative.apply.functor.identity: OK, passed 100 tests.
11                                                   //| 
12 + applicative.apply.functor.composite: OK, passed 100 tests.
13                                                   //| 
14 + applicative.apply.composition: OK, passed 100 tests.
15                                                   //| 
16 + applicative.identity: OK, passed 100 tests.
17                                                   //| 
18 + applicative.homomorphism: OK, passed 100 tests.
19                                                   //| 
20 + applicative.interchange: OK, passed 100 tests.
21                                                   //| 
22 + applicative.map consistent with ap: OK, passed 100 tests.

功通过了Applicative定律测试。现在我们可以说Configure类型既是Functor也是Applicative。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏DOTNET

.NET MongoDB Driver 2.2使用示例

说明:mongoDBService是对各种常用操作的封装 public class MongoDBService { #region 变量 ...

33110
来自专栏码云1024

DataOutputStream&DataInputStream

2878
来自专栏iOS开发笔记

Objective-C的内省(Introspection)

内省(Introspection)是面向对象语言和环境的一个强大特性,Objective-C和Cocoa在这个方面尤其的丰富。内省是对象揭示自...

1996
来自专栏个人分享

Hive metastore源码阅读(三)

  上次写了hive metastore的partition的生命周期,但是简略概括了下alter_partition的操作,这里补一下alter_partit...

1492
来自专栏WindCoder

个人常用语句笔记-Java

JDK1.7的新语法,这种try语句可以自动执行资源关闭过程,无需再在finally中显式关闭流。

781
来自专栏积累沉淀

干货--Hadoop自定义数据类型和自定义输入输出格式整合项目案例

正文开始前 ,先介绍几个概念 序列化 所谓序列化,是指将结构化对象转化为字节流,以便在网络上传输或写到磁盘进行永久存储。 反序列化 是指将字节流转回到结构化...

4996
来自专栏函数式编程语言及工具

Scalaz(36)- Free :实践-Free In Action - 实用体验

在上面几期讨论中我们连续介绍了Free Monad。因为FP是纯函数编程,也既是纯函数的组合集成,要求把纯代码和副作用代码可以分离开来。Free Monad...

1785
来自专栏积累沉淀

初识HtmlParser

1、概念 网页解析,即程序自动分析网页内容、获取信息,从而进一步处理信息。 htmlparser包提供方便、简洁的处理html文件的方法,它将html页面中...

2055
来自专栏算法修养

LeetCode 131 Palindrome Partitioning

思路是,先将所有的回文子串都找出来,记录下左右端点。 然后DFS这些子串就可以了。

791
来自专栏Jed的技术阶梯

Kafka 中使用 Avro 序列化框架(一):使用传统的 avro API 自定义序列化类和反序列化类

关于 avro 的 maven 工程的搭建以及 avro 的入门知识,可以参考: Apache Avro 入门

3103

扫码关注云+社区