Library nullable
Load grammar.
Computing Nullable
Inductive definition of nullable symbols
Inductive Nullable_sym (g:grammar) : sym -> Prop :=
| Nullable_In : forall nt exp, In (nt,exp) g ->
(forall x, In x exp -> Nullable_sym g x) ->
Nullable_sym g (NT nt).
Theorem Nullable_sym_spec : forall g s, Nullable_sym g s <-> Nullable g (s::nil).
Proof.
Admitted.
Computation step
NatSet allows to work on sets of natural numbers (represented as strictly
increasing lists). Coq's standard equality eq won't hold between sets containing
the same elements but built in different ways. Set equality is available as
NatSet.equal, but you should be able to complete this file without using it.
Now define all_NT_In_dec e l which determines if all items in e are non-terminals
that belong to l.
Lemma all_NT_In_dec : forall (e:list sym) (l:NatSet.t),
{ forall x, In x e -> exists y, x = NT y /\ NatSet.In y l } +
{ ~ forall x, In x e -> exists y, x = NT y /\ NatSet.In y l }.
Proof.
Admitted.
Fixpoint nullable_step (g:grammar) (s:NatSet.t) : NatSet.t :=
match g with
| nil => s
| (n,e)::g =>
if all_NT_In_dec e s then NatSet.add n (nullable_step g s) else nullable_step g s
end.
{ forall x, In x e -> exists y, x = NT y /\ NatSet.In y l } +
{ ~ forall x, In x e -> exists y, x = NT y /\ NatSet.In y l }.
Proof.
Admitted.
Fixpoint nullable_step (g:grammar) (s:NatSet.t) : NatSet.t :=
match g with
| nil => s
| (n,e)::g =>
if all_NT_In_dec e s then NatSet.add n (nullable_step g s) else nullable_step g s
end.
You will need to prove (at least) the correctness and completeness of nullable_step.
Formulating these two lemmas requires some care; the most immediate formulation won't
allow a direct proof by induction.
Iterate nullable_step some number of times, starting with NatSet.empty.
A possible bonus: improve the function to stop when a fixed point is reached.
Iterated computation
Fixpoint nullables g k : NatSet.t :=
match k with
| O => NatSet.empty
| S k => nullable_step g (nullables g k)
end.
Definition nullable_sym g s f :=
match s with
| T _ => False
| NT n => NatSet.In n (nullables g f)
end.
match k with
| O => NatSet.empty
| S k => nullable_step g (nullables g k)
end.
Definition nullable_sym g s f :=
match s with
| T _ => False
| NT n => NatSet.In n (nullables g f)
end.
Correctness of nullable_sym.
Complteness of nullable_sym.
Lemma Nullable_nullable : forall g s, Nullable_sym g s -> exists f, nullable_sym g s f.
Proof.
Admitted.
Proof.
Admitted.