Skip to content

Instantly share code, notes, and snippets.

@daharon
Last active February 16, 2020 15:28
Show Gist options
  • Save daharon/d6713c1dac1ed343c3e1df5d47eaafe9 to your computer and use it in GitHub Desktop.
Save daharon/d6713c1dac1ed343c3e1df5d47eaafe9 to your computer and use it in GitHub Desktop.
Error: Unbound value show -- Fixed
(executable
(name recursive_variants)
(modules recursive_variants)
(libraries base core stdio)
(preprocess (pps ppx_deriving.show)))
(alias (name recursive_variants) (deps recursive_variants.exe))
$ dune build
File "recursive_variants.ml", line 68, characters 19-23:
68 | print_endline (show expression);
^^^^
Error: Unbound value show
open Base
open Stdio
(** An expression within this Boolean expression language. *)
type 'a expr =
| Base of 'a
| Const of bool
| And of 'a expr list
| Or of 'a expr list
| Not of 'a expr
[@@deriving show]
type mail_field = To | From | CC | Date | Subject [@@deriving show]
type mail_predicate = {
field: mail_field;
contains: string;
} [@@deriving show]
let and_ l =
if List.exists l ~f:(function Const false -> true | _ -> false)
then Const false
else
match List.filter l ~f:(function Const true -> false | _ -> true) with
| [] -> Const true
| [ x ] -> x
| l -> And l
let or_ l =
if List.exists l ~f:(function Const true -> true | _ -> false)
then Const true
else
match List.filter l ~f:(function Const false -> false | _ -> true) with
| [] -> Const false
| [ x ] -> x
| l -> Or l
let not_ = function
| Const b -> Const (not b)
| e -> Not e
let rec simplify = function
| Base _ | Const _ as x -> x
| And l -> and_ (List.map ~f:simplify l)
| Or l -> or_ (List.map ~f:simplify l)
| Not e -> not_ (simplify e)
let rec eval expr base_eval =
let eval' expr = eval expr base_eval in
match expr with
| Base base -> base_eval base
| Const b -> b
| And exprs -> List.for_all exprs ~f:eval'
| Or exprs -> List.exists exprs ~f:eval'
| Not expr -> not (eval' expr)
let () =
let _test field contains = Base { field; contains; } in
let expression = And [
Or [
_test To "doligez";
_test CC "doligez"
];
_test Subject "runtime";
] in
print_endline (show_expr pp_mail_predicate expression);
let expression = simplify (Not (
And [
Or [
Base "It's snowing";
Const true
];
Not (Not (Base "It's raining" ))
]
)) in
print_endline (show_expr Caml.Format.pp_print_string expression);
@daharon
Copy link
Author

daharon commented Feb 16, 2020

This is from working through the following: http://dev.realworldocaml.org/variants.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment