1
import SwiftCheck
2
import Bow
3
import BowGenerators
4

5
public class FunctorLaws<F: Functor & EquatableK & ArbitraryK> {
6 1
    public static func check() {
7 1
        InvariantLaws<F>.check()
8 1
        covariantIdentity()
9 1
        covariantComposition()
10 1
        void()
11 1
        fproduct()
12 1
        tupleLeft()
13 1
        tupleRight()
14
    }
15

16 1
    private static func covariantIdentity() {
17 1
        property("Identity is preserved under functor transformation") <~ forAll() { (fa: KindOf<F, Int>) in
18 1
            return F.map(fa.value, id) == id(fa.value)
19
        }
20
    }
21
    
22 1
    private static func covariantComposition() {
23 1
        property("Composition is preserved under functor transformation") <~ forAll() { (fa: KindOf<F, Int>, f: ArrowOf<Int, Int>, g: ArrowOf<Int, Int>) in
24 1
            return F.map(F.map(fa.value, f.getArrow), g.getArrow) ==
25 1
                F.map(fa.value, f.getArrow >>> g.getArrow)
26
        }
27
    }
28
    
29 1
    private static func void() {
30 1
        property("Void") <~ forAll() { (fa: KindOf<F, Int>, f: ArrowOf<Int, Int>) in
31 1
            return isEqual(F.void(fa.value), F.void(F.map(fa.value, f.getArrow)))
32
        }
33
    }
34
    
35 1
    private static func fproduct() {
36 1
        property("fproduct") <~ forAll { (fa: KindOf<F, Int>, f: ArrowOf<Int, Int>) in
37 1
            return F.map(F.fproduct(fa.value, f.getArrow), { x in x.1 }) ==
38 1
                F.map(fa.value, f.getArrow)
39
        }
40
    }
41
    
42 1
    private static func tupleLeft() {
43 1
        property("tuple left") <~ forAll { (fa: KindOf<F, Int>, b: Int) in
44 1
            return F.map(F.tupleLeft(fa.value, b), { x in x.0 }) ==
45 1
                F.as(fa.value, b)
46
        }
47
    }
48
    
49 1
    private static func tupleRight() {
50 1
        property("tuple right") <~ forAll { (fa: KindOf<F, Int>, b: Int) in
51 1
            return F.map(F.tupleRight(fa.value, b), { x in x.1 }) ==
52 1
                F.as(fa.value, b)
53
        }
54
    }
55
}

Read our documentation on viewing source code .

Loading