package preface

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

Source file seq.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module S = Stdlib.Seq

type 'a t = 'a S.t

let pure x = S.return x
let cons x xs () = S.Cons (x, xs)

let rec append l r () =
  match l () with S.Nil -> r () | S.Cons (x, xs) -> S.Cons (x, append xs r)
;;

let rev seq =
  let rec aux acc seq =
    match seq () with S.Nil -> acc | S.Cons (x, xs) -> aux (cons x acc) xs
  in
  aux S.empty seq
;;

module Functor = Preface_make.Functor.Via_map (Stdlib.Seq)
module Invariant = Preface_make.Invariant.From_functor (Functor)

module Alternative = Preface_make.Alternative.Via_pure_and_apply (struct
  type nonrec 'a t = 'a t

  let pure x = pure x

  let apply fs xs =
    let rec aux a b () =
      match a () with S.Nil -> S.Nil | S.Cons (x, xs) -> merge xs b x b ()
    and merge a bs f b () =
      match b () with
      | S.Nil -> aux a bs ()
      | S.Cons (x, xs) -> S.Cons (f x, merge a bs f xs)
    in
    aux fs xs
  ;;

  let neutral = S.empty
  let combine l r = append l r
end)

module Applicative_traversable (A : Preface_specs.APPLICATIVE) =
  Preface_make.Traversable.Over_applicative
    (A)
    (struct
      type 'a t = 'a A.t
      type 'a iter = 'a S.t

      let traverse f seq =
        let open A.Infix in
        let rec traverse acc seq =
          match seq () with
          | S.Nil -> rev <$> acc
          | S.Cons (x, xs) -> traverse (A.lift2 cons (f x) acc) xs
        in

        traverse (A.pure S.empty) seq
      ;;
    end)

module Applicative =
  Preface_make.Traversable.Join_with_applicative
    (Alternative)
    (Applicative_traversable)

module Foldable = Preface_make.Foldable.Via_fold_map (struct
  type nonrec 'a t = 'a t

  let fold_map' neutral combine f seq =
    let rec aux acc seq () =
      match seq () with
      | S.Nil -> acc
      | S.Cons (x, xs) ->
        let result = combine acc (f x) in
        aux result xs ()
    in
    aux neutral seq ()
  ;;
end)

module Monad_plus = Preface_make.Monad_plus.Via_bind (struct
  type nonrec 'a t = 'a t

  let return = pure
  let bind = S.flat_map
  let neutral = S.empty
  let combine l r = append l r
end)

module Monad_traversable (M : Preface_specs.MONAD) =
  Preface_make.Traversable.Over_monad
    (M)
    (struct
      type 'a t = 'a M.t
      type 'a iter = 'a S.t

      let traverse f =
        let open M.Infix in
        let rec traverse acc seq =
          match seq () with
          | S.Nil -> acc >|= rev
          | S.Cons (x, xs) -> traverse (M.lift2 cons (f x) acc) xs
        in
        traverse (M.return S.empty)
      ;;
    end)

module Monad =
  Preface_make.Traversable.Join_with_monad (Monad_plus) (Monad_traversable)

module Selective =
  Preface_make.Selective.Over_applicative_via_select
    (Applicative)
    (Preface_make.Selective.Select_from_monad (Monad))

module Monoid (T : Preface_specs.Types.T0) =
Preface_make.Monoid.Via_combine_and_neutral (struct
  type nonrec t = T.t t

  let combine l r = append l r
  let neutral = S.empty
end)

let equal eq =
  let rec aux left right =
    match (left (), right ()) with
    | S.Nil, S.Nil -> true
    | S.Cons (a, axs), S.Cons (b, bxs) -> if eq a b then aux axs bxs else false
    | _ -> false
  in
  aux
;;

let pp f ppf seq =
  let rec aux ppf seq =
    match seq () with
    | S.Nil -> ()
    | S.Cons (x, xs) -> Format.fprintf ppf ";@ %a%a" f x aux xs
  in
  match seq () with
  | S.Nil -> ()
  | S.Cons (x, xs) -> Format.fprintf ppf "Seq@[[%a%a]@]" f x aux xs
;;
OCaml

Innovation. Community. Security.