Guest User

Untitled

a guest
Jun 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. open Core
  2.  
  3. type ('a, 'b) dfa = {
  4. alphabet: 'a list;
  5. transition: ('b * 'a * 'b) list;
  6. initial: 'b;
  7. accept: 'b list;
  8. }
  9.  
  10. let alphabet = [0; 1;]
  11. let transition = [
  12. (0, 0, 1);
  13. (0, 1, 0);
  14. (1, 1, 1);
  15. (1, 0, 0);
  16. ]
  17. let initial = 0
  18. let accept = [0]
  19. let automata = { alphabet; transition; initial; accept; }
  20.  
  21.  
  22. let rec find transition state symbol =
  23. match transition with
  24. | [] -> None
  25. | (x, y, z) :: _ when x = state && y = symbol -> Some z
  26. | _::tl -> find tl state symbol
  27.  
  28. let run automata seq =
  29. let rec step state = function
  30. | [] ->
  31. if List.mem automata.accept state ~equal:(=) then
  32. Ok state
  33. else
  34. Or_error.error_string "not accepted"
  35. | symbol::tl ->
  36. match find automata.transition state symbol with
  37. | None -> Or_error.error_string "no transition"
  38. | Some next -> step next tl
  39. in
  40. step automata.initial seq
  41.  
  42. let () =
  43. match run automata [1; 0; 0; 1;] with
  44. | Ok state -> Printf.printf "accept %d\n" state
  45. | Error e -> Printf.printf "failed %s\n" (Error.to_string_hum e)
Add Comment
Please, Sign In to add comment