commit 0cb01daaa05ff88864c075a1e2728fdc6a21d5fe Author: sneeker Date: Mon Feb 11 13:34:00 2019 +0000 Define polymorphic lenses and compile the runtime library diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a959695 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.cmi +*.cmo +*.cma +*.byte +rewrite diff --git a/META b/META new file mode 100644 index 0000000..48cb191 --- /dev/null +++ b/META @@ -0,0 +1,3 @@ +version = "0.1.0" +description = "Record accessors and composable lenses" +archive(byte) = "fieldglass.cma" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fb8e9cc --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +OCAMLC ?= ocamlc + +.PHONY: all test clean +all: fieldglass.cma + +fieldglass.cmi: fieldglass.mli + $(OCAMLC) -c $< + +fieldglass.cmo: fieldglass.ml fieldglass.cmi + $(OCAMLC) -c $< + +fieldglass.cma: fieldglass.cmo + $(OCAMLC) -a $< -o $@ + +test: all + $(OCAMLC) fieldglass.cma runtime.ml -o runtime.byte + ./runtime.byte + +clean: + rm -f *.cmi *.cmo *.cma *.byte rewrite diff --git a/README.md b/README.md new file mode 100644 index 0000000..ba482fe --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# fieldglass + +Lenses for reading and updating immutable data in OCaml. + +Build with `make`; run the checks with `make test`. diff --git a/fieldglass.install b/fieldglass.install new file mode 100644 index 0000000..6fc7c99 --- /dev/null +++ b/fieldglass.install @@ -0,0 +1 @@ +lib: ["fieldglass.cmi" "fieldglass.cma" "fieldglass.mli" "META"] diff --git a/fieldglass.ml b/fieldglass.ml new file mode 100644 index 0000000..d42e44d --- /dev/null +++ b/fieldglass.ml @@ -0,0 +1,5 @@ +type ('s, 't, 'a, 'b) lens = ('s -> 'a) * ('b -> 's -> 't) +type ('s, 'a) lens' = ('s, 's, 'a, 'a) lens + +let lens ~view ~set = view, set +let view (get, _) = get diff --git a/fieldglass.mli b/fieldglass.mli new file mode 100644 index 0000000..b8055af --- /dev/null +++ b/fieldglass.mli @@ -0,0 +1,5 @@ +type ('s, 't, 'a, 'b) lens = ('s -> 'a) * ('b -> 's -> 't) +type ('s, 'a) lens' = ('s, 's, 'a, 'a) lens + +val lens : view:('s -> 'a) -> set:('b -> 's -> 't) -> ('s, 't, 'a, 'b) lens +val view : ('s, 't, 'a, 'b) lens -> 's -> 'a diff --git a/fieldglass.opam b/fieldglass.opam new file mode 100644 index 0000000..304bbcc --- /dev/null +++ b/fieldglass.opam @@ -0,0 +1,11 @@ +opam-version: "1.2" +name: "fieldglass" +version: "0.1.0" +maintainer: "rack" +authors: ["rack"] +homepage: "https://github.com/rackquiem/fieldglass" +bug-reports: "https://github.com/rackquiem/fieldglass/issues" +dev-repo: "git+https://github.com/rackquiem/fieldglass.git" +build: [make] +build-test: [make "test"] +available: [ocaml-version = "4.06.1"] diff --git a/runtime.ml b/runtime.ml new file mode 100644 index 0000000..14b0ade --- /dev/null +++ b/runtime.ml @@ -0,0 +1,6 @@ +open Fieldglass + +let sample : (string * bool, string) lens' = + lens ~view:fst ~set:(fun value (_, other) -> value, other) + +let () = assert (view sample ("colour", true) = "colour")