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 1 commit
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
7 changes: 7 additions & 0 deletions lib/any.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module type Any = sig
type t
end

implicit module Any_Int = struct type t = int end
implicit module Any_String = struct type t = string end
implicit module Any_Pair { A : Any } {B : Any } = struct type t = A.t * B.t end
pxeger marked this conversation as resolved.
Show resolved Hide resolved
27 changes: 27 additions & 0 deletions lib/control.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open Any

(* Functor, Applicative and Monad module types *)
module type Functor = sig
type 'a t
pxeger marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -176,3 +178,28 @@ end = struct
let cons x ys = F.apply (F.fmap (fun x xs -> x :: xs) (f x)) ys in
List.fold_right cons t (F.return [])
end

implicit module Function {A : Any} : sig
include Functor with type 'b t = A.t -> 'b
include Applicative with type 'b t := 'b t
include Monad with type 'b t := 'b t
end = struct
type 'b t = A.t -> 'b

(* Functor *)
let fmap m f x = m (f x)

(* Applicative *)
let return x _ = x
let apply f g x = f x (g x)

(* Monad *)
let bind g f x = f (g x) x
end
(** (a -> b) is an instance of Monad b - it behaves like the reader monad *)

implicit module Pair {A : Any} : Functor with type 'b t = A.t * 'b = struct
type 'b t = A.t * 'b

let fmap m (a, b) = (a, m b)
end
4 changes: 1 addition & 3 deletions lib/data.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
module type Any = sig
type t
end
open Any

module type Show = sig
type t
Expand Down
2 changes: 1 addition & 1 deletion lib/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(library
(public_name imp)
(modules Control Data Num Scope Show)
(modules Any Control Data Num Scope Show)
(synopsis "Experimental library using modular implicits"))
14 changes: 14 additions & 0 deletions tests/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,17 @@ let () =
assert (Foldable.fold ( * ) (Some 5) 1 = 5);
assert (Foldable.fold ( * ) None 1234 = 1234);
assert (Traversable.traverse (fun x -> [x; x + 1]) (Some 3) = [Some 3; Some 4])

let () =
let open Imp.Control in
let open implicit Imp.Any in
let pair x y = (x, y) in
assert ((fmap (fun x -> x + 1) (int_of_string)) "3" = 4);
assert ((return 4) "3" = 4);
assert ((apply pair (fun x -> x * x)) 3 = (3, 9));
assert ((bind (fun x -> x * x) pair) 3 = (9, 3))

let () =
let open Imp.Control in
let open implicit Imp.Any in
assert (fmap (fun x -> x + 1) ("hello", 3) = ("hello", 4))