diff --git a/bin/dune b/bin/dune index 03b8761..40c9397 100644 --- a/bin/dune +++ b/bin/dune @@ -1,5 +1,5 @@ (executables (names main) - (libraries incr_dom hazelnut_lib) + (libraries incr_dom app_lib) (preprocess (pps js_of_ocaml-ppx ppx_jane))) diff --git a/bin/main.re b/bin/main.re index 346a778..f0cc5bd 100644 --- a/bin/main.re +++ b/bin/main.re @@ -1,7 +1,7 @@ open! Core; open! Incr_dom; open! Js_of_ocaml; -module App = Hazelnut_lib.App; +module App = App_lib.App; Start_app.start( (module App), diff --git a/hazelnut/dune b/hazelnut/dune new file mode 100644 index 0000000..5f2b05c --- /dev/null +++ b/hazelnut/dune @@ -0,0 +1,5 @@ +(library + (name hazelnut_lib) + (libraries core incr_dom) + (preprocess + (pps ppx_jane))) diff --git a/hazelnut/hazelnut.re b/hazelnut/hazelnut.re new file mode 100644 index 0000000..86c5ed2 --- /dev/null +++ b/hazelnut/hazelnut.re @@ -0,0 +1,34 @@ +[@deriving (sexp, compare)] +type htyp = + | Arrow(htyp, htyp) + | Num + | Hole; + +[@deriving (sexp, compare)] +type hexp = + | Var(string) + | Lam(string, hexp) + | Ap(hexp, hexp) + | Num(int) + | Plus(hexp, hexp) + | Asc(hexp, htyp) + | EHole + | NEHole(hexp); + +[@deriving (sexp, compare)] +type ztyp = + | Cursor(htyp) + | LArrow(ztyp, htyp) + | RArrow(htyp, ztyp); + +[@deriving (sexp, compare)] +type zexp = + | Cursor(hexp) + | Lam(string, zexp) + | LAp(zexp, hexp) + | RAp(hexp, zexp) + | LPlus(zexp, hexp) + | RPlus(hexp, zexp) + | LAsc(zexp, htyp) + | RAsc(hexp, ztyp) + | NEHole(zexp); diff --git a/hazelnut/hazelnut.rei b/hazelnut/hazelnut.rei new file mode 100644 index 0000000..86c5ed2 --- /dev/null +++ b/hazelnut/hazelnut.rei @@ -0,0 +1,34 @@ +[@deriving (sexp, compare)] +type htyp = + | Arrow(htyp, htyp) + | Num + | Hole; + +[@deriving (sexp, compare)] +type hexp = + | Var(string) + | Lam(string, hexp) + | Ap(hexp, hexp) + | Num(int) + | Plus(hexp, hexp) + | Asc(hexp, htyp) + | EHole + | NEHole(hexp); + +[@deriving (sexp, compare)] +type ztyp = + | Cursor(htyp) + | LArrow(ztyp, htyp) + | RArrow(htyp, ztyp); + +[@deriving (sexp, compare)] +type zexp = + | Cursor(hexp) + | Lam(string, zexp) + | LAp(zexp, hexp) + | RAp(hexp, zexp) + | LPlus(zexp, hexp) + | RPlus(hexp, zexp) + | LAsc(zexp, htyp) + | RAsc(hexp, ztyp) + | NEHole(zexp); diff --git a/lib/app.re b/lib/app.re index fc7b750..5521e98 100644 --- a/lib/app.re +++ b/lib/app.re @@ -1,25 +1,34 @@ open Core; open Incr_dom; +module Hazelnut = Hazelnut_lib.Hazelnut; + +let render_zexp: Hazelnut.zexp => string = + fun + | Cursor(_) => "Cursor" + | Lam(_, _) => "Lam" + | LAp(_, _) => "LAp" + | RAp(_, _) => "RAp" + | LPlus(_, _) => "LPlus" + | RPlus(_, _) => "RPlus" + | LAsc(_, _) => "LAsc" + | RAsc(_, _) => "RAsc" + | NEHole(_) => "NEHole"; module Model = { [@deriving (sexp, fields, compare)] - type t = {counter: int}; + type t = {e: Hazelnut.zexp}; - let set_default_input = (counter: int): t => {counter: counter}; + let set_default_expression = (e: Hazelnut.zexp): t => {e: e}; - let init = (): t => set_default_input(0); - let reset_counter = (_: t): t => set_default_input(0); - let incr_counter = (t: t): t => set_default_input(t.counter + 1); - let decr_counter = (t: t): t => set_default_input(t.counter - 1); + let init = (): t => set_default_expression(Cursor(EHole)); + let do_nothing = (e: t): t => e; let cutoff = (t1: t, t2: t): bool => compare(t1, t2) == 0; }; module Action = { [@deriving sexp] type t = - | Reset_counter - | Incr_counter - | Decr_counter; + | DoNothing; }; module State = { @@ -28,9 +37,7 @@ module State = { let apply_action = (model, action, _, ~schedule_action as _) => switch ((action: Action.t)) { - | Reset_counter => Model.reset_counter(model) - | Incr_counter => Model.incr_counter(model) - | Decr_counter => Model.decr_counter(model) + | DoNothing => Model.do_nothing(model) }; let on_startup = (~schedule_action as _, _) => Async_kernel.return(); @@ -52,16 +59,15 @@ let view = ); let buttons = { - let reset_button = button("Reset", Action.Reset_counter); - let incr_button = button("+", Action.Incr_counter); - let decr_button = button("-", Action.Decr_counter); + let do_nothing_button = button("Do Nothing", Action.DoNothing); - Node.div([decr_button, reset_button, incr_button]); + Node.div([do_nothing_button]); }; let%map counter = { - let%map counter = m >>| Model.counter; - Node.div([Node.textf("%d", counter)]); + let%map e = m >>| Model.e; + let e = render_zexp(e); + Node.div([Node.textf("%s", e)]); }; Node.body([counter, buttons]); diff --git a/lib/dune b/lib/dune index 5f2b05c..d4aeefe 100644 --- a/lib/dune +++ b/lib/dune @@ -1,5 +1,5 @@ (library - (name hazelnut_lib) - (libraries core incr_dom) + (name app_lib) + (libraries core hazelnut_lib incr_dom) (preprocess (pps ppx_jane))) diff --git a/test/dune b/test/dune index d7660e4..6c34aef 100644 --- a/test/dune +++ b/test/dune @@ -1,5 +1,5 @@ (library (name hazelnut_test) - (libraries async_kernel incr_dom_testing hazelnut_lib js_of_ocaml) + (libraries async_kernel incr_dom_testing app_lib js_of_ocaml) (preprocess (pps ppx_jane))) diff --git a/test/hazelnut_test.re b/test/hazelnut_test.re index bfae610..5ed0df9 100644 --- a/test/hazelnut_test.re +++ b/test/hazelnut_test.re @@ -1,6 +1,6 @@ open! Core; open! Incr_dom_testing; -module App = Hazelnut_lib.App; +module App = App_lib.App; let make_helpers = () => { let driver =