package preface

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file bifunctor.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module type LAWS = sig
  type ('a, 'b) t

  val bifunctor_1 : unit -> (('a, 'b) t, ('a, 'b) t) Law.t
  val bifunctor_2 : unit -> (('a, 'b) t, ('a, 'b) t) Law.t
  val bifunctor_3 : unit -> (('a, 'b) t, ('a, 'b) t) Law.t

  val bifunctor_4 :
    unit -> ('a -> 'b, ('c -> 'd) -> ('a, 'c) t -> ('b, 'd) t) Law.t

  val bifunctor_5 :
       unit
    -> ( 'a -> 'b
       , ('c -> 'a) -> ('d -> 'e) -> ('f -> 'd) -> ('c, 'f) t -> ('b, 'e) t )
       Law.t

  val bifunctor_6 :
    unit -> ('a -> 'b, ('c -> 'a) -> ('c, 'd) t -> ('b, 'd) t) Law.t

  val bifunctor_7 :
    unit -> ('a -> 'b, ('c -> 'a) -> ('d, 'c) t -> ('d, 'b) t) Law.t
end

module For (B : Preface_specs.BIFUNCTOR) :
  LAWS with type ('a, 'b) t := ('a, 'b) B.t = struct
  open Law
  open Preface_core.Fun.Infix

  let bifunctor_1 () =
    let lhs x = B.bimap (fun x -> x) (fun x -> x) x
    and rhs x = x in

    law ("bimap id id" =~ lhs) ("id" =~ rhs)
  ;;

  let bifunctor_2 () =
    let lhs x = B.map_fst (fun x -> x) x
    and rhs x = x in

    law ("map_fst id" =~ lhs) ("id" =~ rhs)
  ;;

  let bifunctor_3 () =
    let lhs x = B.map_snd (fun x -> x) x
    and rhs x = x in

    law ("map_snd id" =~ lhs) ("id" =~ rhs)
  ;;

  let bifunctor_4 () =
    let lhs f g x = B.bimap f g x
    and rhs f g x = (B.map_fst f % B.map_snd g) x in

    law ("bimap f g" =~ lhs) ("map_fst f % map_snd g" =~ rhs)
  ;;

  let bifunctor_5 () =
    let lhs f g h i x = B.bimap (f % g) (h % i) x
    and rhs f g h i x = (B.bimap f h % B.bimap g i) x in

    law ("bimap (f % g) (h % i)" =~ lhs) ("bimap f h % bimap g i" =~ rhs)
  ;;

  let bifunctor_6 () =
    let lhs f g x = B.map_fst (f % g) x
    and rhs f g x = (B.map_fst f % B.map_fst g) x in

    law ("map_fst (f % g)" =~ lhs) ("map_fst f % map_fst g" =~ rhs)
  ;;

  let bifunctor_7 () =
    let lhs f g x = B.map_snd (f % g) x
    and rhs f g x = (B.map_snd f % B.map_snd g) x in

    law ("map_snd (f % g)" =~ lhs) ("map_snd f % map_snd g" =~ rhs)
  ;;
end
OCaml

Innovation. Community. Security.