Skip to content

Instantly share code, notes, and snippets.

@dungpa
Last active May 13, 2017 18:25
Show Gist options
  • Save dungpa/46c48fdf25f73b34c7b428778d685613 to your computer and use it in GitHub Desktop.
Save dungpa/46c48fdf25f73b34c7b428778d685613 to your computer and use it in GitHub Desktop.
Create a simple FSM model
// A representation from http://stackoverflow.com/questions/5840292/automata-in-ocaml
type FiniteStateMachine<'State, 'Input> = {
Initial : 'State;
Final : 'State -> bool;
Transition : 'Input -> 'State -> 'State;
}
type IntegerKind =
| Odd
| Even
let odd = {
Initial = Even;
Final = (function Odd -> true | _ -> false);
Transition = (function
| "next" -> (function Even -> Odd | Odd -> Even)
| _ -> id);
}
let product a b = {
Initial = (a.Initial, b.Initial);
Final = (fun (x, y) -> a.Final x && b.Final y);
Transition = (fun c (x, y) -> a.Transition c x, b.Transition c y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment