Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous additions #1

Merged
merged 36 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f37adb6
instantiate monoid for unit
pxeger Aug 3, 2023
ca68f49
Control: cleanup and code deduplication
pxeger Aug 3, 2023
eca1304
fix List applicative implementation
pxeger Aug 3, 2023
e4d65dd
make definition of Monad_plus consistent
pxeger Aug 3, 2023
c9ff356
add list monoid
pxeger Aug 3, 2023
65e8ca8
add tests for string monoid
pxeger Aug 3, 2023
d5e8a8a
fix typo
pxeger Aug 3, 2023
cdb9a41
explicitly annotate that we expect List to satisfy Functor etc.
pxeger Aug 3, 2023
8d7a25b
improve Monad factory comment
pxeger Aug 3, 2023
134a261
add `with type 'a t = 'a M.t` to Monad
pxeger Aug 3, 2023
0ca7c07
improve with type annotation on List
pxeger Aug 3, 2023
7dfe6cf
fix traverse implementation
pxeger Aug 3, 2023
d55660a
add tests for list instances
pxeger Aug 3, 2023
b7b3b1f
clarify the meaning of unqualified "t" in Option
pxeger Aug 3, 2023
aeec009
Update lib/control.ml
pxeger Aug 3, 2023
33cb03d
Update lib/control.ml
pxeger Aug 3, 2023
5e7cbab
use List.fold_left
pxeger Aug 4, 2023
2e99ffe
fix type errors
pxeger Aug 4, 2023
ba63044
add tests for option instances
pxeger Aug 4, 2023
ef7d9f4
add signature for Option module
pxeger Aug 4, 2023
65931a0
split Option and List tests
pxeger Aug 4, 2023
e11a836
add functor etc. instances for functions and pairs
pxeger Aug 4, 2023
3deca46
add (commented-out due to bug) tests for sequence
pxeger Aug 4, 2023
a1419e8
rename Any.t to Any.t_for_any to avoid potential overlapping instances
pxeger Aug 4, 2023
cc7f909
add Any instance for lists
pxeger Aug 4, 2023
d84aec9
remove duplicate declaration of Num
pxeger Aug 4, 2023
3d4a012
remove duplicate Show classes
pxeger Aug 4, 2023
067a7b0
add show instances for more sizes of tuples
pxeger Aug 4, 2023
4abf10c
strengthen typing of Ord.compare
pxeger Aug 7, 2023
24e1050
remove ambiguous instantiations of Monoid for numeric types
pxeger Aug 7, 2023
ab21e06
add type signatures for instances of Eq, Num, Enum, etc.
pxeger Aug 7, 2023
3701b83
add const and identity functors
pxeger Aug 8, 2023
5d5b2ff
add myself as author
pxeger Aug 8, 2023
2b0dd8f
instantiate Applicative for Const of a Monoid
pxeger Aug 9, 2023
05a844b
change type of translateCompare
pxeger Aug 9, 2023
e3c744a
mark Functor et al as covariant
pxeger Aug 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 12 additions & 32 deletions lib/control.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ let sequence {M : Monad} (ms : 'a M.t list) =
ms
(return [])

(* Create a monad using default functor and idiom implementation *)
(* Create a monad using default functor and applicative implementation *)
dvlasits marked this conversation as resolved.
Show resolved Hide resolved
module Monad(M : sig
type 'a t
val return : 'a -> 'a t
val bind : 'a t -> ('a -> 'b t) -> 'b t
end) = struct
end): Monad = struct
pxeger marked this conversation as resolved.
Show resolved Hide resolved
type 'a t = 'a M.t
(* Functor *)
let fmap f m = M.bind m (fun x -> M.return (f x))
(* Applicative *)
Expand All @@ -62,30 +63,9 @@ module Monad(M : sig
let bind = M.bind
end;;

(* Monad for option *)
implicit module MonadOption = Monad(struct
type 'a t = 'a option
let return x = Some x
let bind x f = match x with
| None -> None
| Some x -> f x
end);;

(* Monad for list *)
implicit module MonadList = Monad(struct
type 'a t = 'a list
let return x = [x]
let bind x f =
let rec aux acc = function
| x :: xs -> aux (x @ acc) xs
| [] -> acc
in
aux [] (List.rev_map f x)
end);;

module type Monad_plus = sig
include Monad
val mzero : unit -> 'a t
val mzero : 'a t
val mplus : 'a t -> 'a t -> 'a t
end

Expand All @@ -97,7 +77,7 @@ module Monad_plus = struct
if b then
M.return ()
else
M.mzero ()
M.mzero
end

module type Foldable = sig
Expand Down Expand Up @@ -128,13 +108,12 @@ implicit module Option = struct
| Some a -> Some (f a)

(* Applicative *)
let pure x = Some x
let return x = Some x
pxeger marked this conversation as resolved.
Show resolved Hide resolved
let apply f x = match f, x with
| Some f, Some x -> Some (f x)
| _, _ -> None

(* Monad *)
let return x = Some x
let bind x f = match x with
| None -> None
| Some x -> f x
Expand All @@ -150,6 +129,7 @@ implicit module Option = struct
| None -> acc
| Some x -> f x acc

(* Traversable *)
let traverse (type a) (type b) {F : Applicative} (f : a -> b F.t) : a option -> b t F.t = function
| None -> F.return None
| Some x -> F.fmap (fun x -> Some x) (f x)
Expand All @@ -161,18 +141,18 @@ implicit module List = struct
(* Functor *)
let fmap = List.map

(* Monad *)
(* Applicative *)
let return x = [x]
let apply fs xs =
List.concat (List.map (fun f -> List.map (fun x -> f x) xs) fs)

(* Monad *)
let bind x f =
let rec aux acc = function
| x :: xs -> aux (x @ acc) xs
| [] -> acc in
aux [] (List.rev_map f x)
pxeger marked this conversation as resolved.
Show resolved Hide resolved

(* Applicative *)
let pure x = [x]
let apply fs xs = bind fs (bind xs)

(* Monad_plus *)
let mzero = []
let mplus = (@)
Expand Down
16 changes: 16 additions & 0 deletions lib/data.ml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ implicit module String = struct
let append = (^)
end

module List (A : Any) = struct
type t = A.t list

(* Monoid *)
let empty = []
let append = (@)
end

implicit module Int32 = struct
type t = int32

Expand Down Expand Up @@ -369,3 +377,11 @@ implicit module Nativeint = struct
let empty = 0L
let append = Nativeint.add
end

implicit module Unit = struct
type t = unit

(* Monoid *)
let empty = ()
let append () () = ()
end
19 changes: 18 additions & 1 deletion tests/test.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

let () =
let open Imp.Num.Num in
let open implicit Imp.Num in
Expand Down Expand Up @@ -33,3 +32,21 @@ let () =

assert (show (4.5, ([9; 10; 11], "hello")) = "(4.5, ([9; 10; 11], \"hello\"))");
end

let () =
let open Imp.Data in
let e : unit = Monoid.empty () in
assert (Monoid.append e e = ())

let () =
let open Imp.Data in
let e : string = Monoid.empty () in
assert (e = "");
assert (Monoid.append "abc" "def" = "abcdef")

let () =
let open Imp.Data in
let implicit module IntList = List(struct type t = int end) in
let e : int list = Monoid.empty () in
assert (e = []);
assert (Monoid.append [1; 2] [3] = [1; 2; 3])