Skip to content

Instantly share code, notes, and snippets.

@ashiato45
Created July 29, 2018 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashiato45/c3aae0e602d5132a5a9f04b65747dcb6 to your computer and use it in GitHub Desktop.
Save ashiato45/c3aae0e602d5132a5a9f04b65747dcb6 to your computer and use it in GitHub Desktop.
open Core
open Sexplib
open Set
module Autom = functor (Alph: Set.Elt) (State: Set.Elt) -> struct
module AlphSet = Set.Make(Alph)
module StateSet = Set.Make(State)
type alph = Alph.t
type state = State.t
type autom = AlphSet.t * StateSet.t * (state -> alph -> state) * state * StateSet.t
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 run (autom_: autom) word_ =
let pos = List.fold_left ~init:(get_init autom_) ~f:(get_trans autom_) word_ in
StateSet.mem (get_final autom_) pos
end
module MyAutom = Autom (Int) (String)
let () =
let alphs = [0; 1] |> MyAutom.AlphSet.of_list in
let states = ["q0"; "q1"] |> MyAutom.StateSet.of_list 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"] |> MyAutom.StateSet.of_list in
let autom : MyAutom.autom = (alphs, states, trans, init, final) in
[0;1;0] |> MyAutom.run autom |> string_of_bool |> print_endline;
[0;1;0;1;0] |> MyAutom.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