package forcamla

  1. Overview
  2. Docs
Organize program execution via formulae

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.2.0.tar.gz
md5=55d48713129306960d43989b54742c16
sha512=6ee09f89a32d3dc9cc2f7cda0f713363ace88a9c28c1a6cbbe8d1893a805589ed511f5140deaddf2e9d77ce76de4ea676932289a1e10b2416169825287c28424

doc/README.html

forcamla

What is forcamla?

We start by learning constants in OCaml as ways to have names hold values for us to use later. For instance,

let x = 2
let z = x * x

simply defines an integer constant x that has the value 2 and we later use x again to make z have the value of x * x = 2 * 2 = 4. But often in programming constants are not enough because we want to change the values these names have after a sequence of instructions. Thus, OCaml has ref types for this, observe

let x = ref 2 (* Let x be a *variable* that contains 2 *)
x := !x + 1 (* Update x so it is now x + 1 = 2 + 1 = 3 *)

However, even this has it limitations. Suppose we have the following scenario

let x = ref 2
let y = ref 2
let z = ref (!x + !y) (* Make z a variable equal to 2 + 2 = 4 *)
x := 3

What happens to z? Does z change to reflect the fact that x changed? Or does z stay the same? Most languages (including OCaml) do not update z when x changes. This might defy your intuition. Even worse, this behavior may be inconvenient to deal with. This is where forcamla comes into play!

Formulae

forcamla introduces a formula type that behaves similarly to ref types but formula stay up-to-date when their terms change. Modifying the example above we have

open Formula (* To use formula *)

let x = t 2 (* Create an integer term called x *)
let y = t 2 (* Create an integer term called y *)
let z = x + y
x =: 3 (* Set x to 3, and z now is 5 *)

But it gets even better!

Event Listeners

Suppose you're making a game. You probably have player and maybe it has type hero with a bunch of fields including health.

open Formula

type hero =
{
  (* A bunuch of fields *)
  health: int formula
}

let player =
{
  (* Assign the fields *)
  health = t 3; (* Give health a value of something, say 3 in this case. *)
}

In most games, when the player.health is 0 that means "Game Over!". So, we first make a "Game Over!" function:

let game_over () = print_endline "Game Over!"

Then, with the power of forcamla we can create an event listener (see the MDN docs to understand what an event listener is) to execute game_over when the player health is 0. Observe

let () = when_satisfied (player.health =? 0) game_over

=? is used to check if an integer formula is equal to another (integer) formula. This reads as: "When player.health is 0 fire the function game_over". Therfore, if we run:

let () = player.health =: (player.health - 1) (* Nothing happens yet! player.health is 2 now. *)
let () = player.health =: (player.health - 1) (* Nothing happens yet! player health is 1 now. *)
let () = player.health =: (player.health - 1) (* Now something happens! player.health is 0 and "Game Over!" is printed to the screen! *)

Documentation

More examples and an API reference can be found here.