Skip to content

Instantly share code, notes, and snippets.

@ashiato45
Created July 29, 2018 08:54
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/55a2c1bbb87ad77edcb815e006521885 to your computer and use it in GitHub Desktop.
Save ashiato45/55a2c1bbb87ad77edcb815e006521885 to your computer and use it in GitHub Desktop.
open Core
open Sexplib
open Set
module Autom = functor (AlphSet: Set.S) (StateSet: Set.S) -> struct
type alph = AlphSet.Elt.t
type state = StateSet.Elt.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 IntSet = Set.Make(Int)
module StringSet = Set.Make(String)
module MyAutom = Autom (IntSet) (StringSet)
let () =
let alphs = [0; 1] |> IntSet.of_list in
let states = ["q0"; "q1"] |> StringSet.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"] |> StringSet.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