Browse Source

Add automata

master
engboris 3 years ago
parent
commit
02fcbc2b1f
  1. 13
      ocaml/Circuits.ml
  2. 32
      ocaml/FSA.ml

13
ocaml/Circuits.ml

@ -57,7 +57,6 @@ let rec value circ concl =
let x = List.hd (get_inputs circ concl) in
let vx = value circ x in
1 - vx
| _ -> -1
let rec eval (circ : circuit) : int =
let (_, _, concl) =
@ -66,31 +65,31 @@ let rec eval (circ : circuit) : int =
(* _________ Examples _________ *)
let make_gate ins op id = (ins, op, id)
let make_input value id = ([], CIn value, id)
let make_gate ins op id : gate = (ins, op, id)
let make_input value id : gate = ([], CIn value, id)
let make_and_circ x y = [
let make_and_circ x y : circuit = [
make_input x 0;
make_input y 1;
make_gate [0;1] CAnd 2;
make_gate [2] COut 3
]
let make_or_circ x y = [
let make_or_circ x y : circuit = [
make_input x 0;
make_input y 1;
make_gate [0;1] COr 2;
make_gate [2] COut 3
]
let make_neg_circ x = [
let make_neg_circ x : circuit = [
make_input x 0;
make_gate [0] CNeg 1;
make_gate [1] COut 2
]
(* corresponds to the excluded middle x v ~x *)
let make_em_circ x = [
let make_em_circ x : circuit = [
make_input x 0;
make_gate [0] CShare 1;
make_gate [1] CNeg 2;

32
ocaml/FSA.ml

@ -0,0 +1,32 @@
(* ============================================
Finite State Automata
============================================ *)
type id = int
type state = id
type initial_state = state
type final_state = state
type fa_transition = state * char * state
type fautomata = initial_state * (fa_transition list) * final_state list
let accepts ((qi, ts, qf) : fautomata) (w : char list) =
let rec head q (read : char list) =
match read with
| [] -> List.mem q qf
| c::w ->
let (_, _, qnext) =
List.hd (List.filter (fun (qs, c', qt) -> qs=q && c=c') ts)
in head qnext w
in head qi w
(* _________ Examples _________ *)
let even_ones : fautomata = (
0,
[
(0, '1', 1);
(1, '1', 0)
],
[0]
)
Loading…
Cancel
Save