Instantly share code, notes, and snippets.

Embed
What would you like to do?
open Core
open Sexplib
module Autom = struct
type ('a, 'b) autom = 'a list * 'b list * ('b -> 'a -> 'b) * 'b * 'b list * ('b -> 'b -> bool)
let get_alphabets (x, _, _, _, _, _) = x
let get_states (_, x, _, _, _, _) = x
let get_trans (_, _, x, _, _, _) = x
let get_init (_, _, _, x, _, _) = x
let get_final (_, _, _, _, x, _) = x
let get_equal_states (_, _, _, _, _, x) = x
let run (autom_: ('a, 'b) autom) word_ =
let pos = List.fold_left ~init:(get_init autom_) ~f:(get_trans autom_) word_ in
List.mem ~equal:(get_equal_states autom_) (get_final autom_) pos
end
let () =
let alphs = [0; 1] in
let states = ["q0"; "q1"] in
let trans s_ a_ =
match (s_, a_) with
| ("q0", 0) -> "q1"
| ("q0", 1) -> "q0"
| ("q1", 0) -> "q0"
| ("q1", 1) -> "q1"
| otherwise -> assert false
in
let init = "q0" in
let final = ["q0"] in
let compare a b = compare_string a b = 0 in
let autom : (int, string) Autom.autom = (alphs, states, trans, init, final, compare) in
[0;1;0] |> Autom.run autom |> string_of_bool |> print_endline;
[0;1;0;1;0] |> Autom.run autom |> string_of_bool |> print_endline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment