package lilis

  1. Overview
  2. Docs

Small library to evaluate simple arithmetic expressions.

This library evaluates simple arithmetic expression over floats. Regular operators (+,-,*,/,^) and some regular functions (sin, cos, tan, asin, acos, atan, log, log10, exp, sqrt) are implemented. Arithmetic expressions can contain variables.

Here is an example of expression : 3*x+sin(2) .

type op2 =
  1. | Plus
  2. | Minus
  3. | Times
  4. | Div
  5. | Pow

Type of binary operators

type op1 =
  1. | Func of float -> float
  2. | MinusUn

Type of unary operators

type 'a t =
  1. | Float of float
  2. | Op2 of 'a t * op2 * 'a t
  3. | Op1 of op1 * 'a t
  4. | Var of 'a

Type of tree which represent an arithmetic expression

module Env : sig ... end

Variable environment.

exception Unknown_variable of string
val eval : Env.t -> string t -> float

Evaluate a tree in the given environment.

  • raises Unkown_variable

    if a variable is not defined in the environment.

val compress : Env.t -> string t -> string t

Compress a tree in the given environment, ie. evaluate everything that can be evaluated.

Some other functions

val eval_custom : ('a -> float) -> 'a t -> float

Evaluate a tree, the given function is used to evaluate variables.

val compress_custom : ('a -> float option) -> 'a t -> 'a t

Compress a tree using the given function, ie. evaluate everything that can be evaluated. A variable is untouched if the function returns None .

val bind : ('a -> 'b t) -> 'a t -> 'b t

Replace each variables by a subtree.

val bind_opt : ('a -> 'a t option) -> 'a t -> 'a t

Replace some variables by a subtree.

val fold : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b

Depth first left to right traversal of the tree.

val map : ('a -> 'b) -> 'a t -> 'b t

Change variables representation using the given function.

val iter : ?var:('a -> unit) -> ?float:(float -> unit) -> ?op1:(op1 -> unit) -> ?op2:(op2 -> unit) -> 'a t -> unit

Iteration on everything.

val vars : 'a t -> ('a -> unit) -> unit

Get the sequence of variables in the given tree. Use with sequence or containers.

val closure : ?env:Env.t -> string t -> (string * 'a) list -> ('a -> float) -> float

Compress the string in the optional env and return the resulting closure.

OCaml

Innovation. Community. Security.