Library LangAlg.expr
This module defines the full signature of language algebra we
consider here, and its finite complete axiomatization. We also define
here some normalisation functions, and list some of their properties.
Set Implicit Arguments.
Require Export tools language.
Delimit Scope expr_scope with expr.
Open Scope expr_scope.
Section s.
𝐄 X is the type of expressions with variables ranging over the
type X. They are built out of the constants 0 and 1, the
concatenation (also called sequential product) ⋅, the intersection
∩, the union + and the mirror image, denoted by the postfix
operator °.
Inductive 𝐄 : Set :=
| 𝐄_one : 𝐄
| 𝐄_zero : 𝐄
| 𝐄_var : X → 𝐄
| 𝐄_seq : 𝐄 → 𝐄 → 𝐄
| 𝐄_inter : 𝐄 → 𝐄 → 𝐄
| 𝐄_plus : 𝐄 → 𝐄 → 𝐄
| 𝐄_conv : 𝐄 → 𝐄.
Notation "x ⋅ y" := (𝐄_seq x y) (at level 40) : expr_scope.
Notation "x + y" := (𝐄_plus x y) (left associativity, at level 50) : expr_scope.
Notation "x ∩ y" := (𝐄_inter x y) (at level 45) : expr_scope.
Notation "x °" := (𝐄_conv x) (at level 25) : expr_scope.
Notation " 1 " := 𝐄_one : expr_scope.
Notation " 0 " := 𝐄_zero : expr_scope.
| 𝐄_one : 𝐄
| 𝐄_zero : 𝐄
| 𝐄_var : X → 𝐄
| 𝐄_seq : 𝐄 → 𝐄 → 𝐄
| 𝐄_inter : 𝐄 → 𝐄 → 𝐄
| 𝐄_plus : 𝐄 → 𝐄 → 𝐄
| 𝐄_conv : 𝐄 → 𝐄.
Notation "x ⋅ y" := (𝐄_seq x y) (at level 40) : expr_scope.
Notation "x + y" := (𝐄_plus x y) (left associativity, at level 50) : expr_scope.
Notation "x ∩ y" := (𝐄_inter x y) (at level 45) : expr_scope.
Notation "x °" := (𝐄_conv x) (at level 25) : expr_scope.
Notation " 1 " := 𝐄_one : expr_scope.
Notation " 0 " := 𝐄_zero : expr_scope.
The size of an expression is the number of nodes in its syntax
tree.
Global Instance size_𝐄 : Size 𝐄 :=
fix 𝐄_size (e: 𝐄) : nat :=
match e with
| 0 | 1 | 𝐄_var _ ⇒ 1%nat
| e + f | e ∩ f | e ⋅ f ⇒ S (𝐄_size e + 𝐄_size f)
| e° ⇒ S (𝐄_size e)
end.
fix 𝐄_size (e: 𝐄) : nat :=
match e with
| 0 | 1 | 𝐄_var _ ⇒ 1%nat
| e + f | e ∩ f | e ⋅ f ⇒ S (𝐄_size e + 𝐄_size f)
| e° ⇒ S (𝐄_size e)
end.
If the set of variables X is decidable, then so is the set of
expressions. Note that we are here considering syntactic equality,
as no semantic or axiomatic equivalence relation has been defined
for expressions.
The following are the axioms of the algebra of languages over
this signature.
⟨𝐄,⋅,1⟩ is a monoid.
⟨𝐄,+,0⟩ is a commutative idempotent monoid.
| ax_plus_com e f : ax (e+f) (f+e)
| ax_plus_idem e : ax (e+e) e
| ax_plus_ass e f g : ax (e+(f+g)) ((e+f)+g)
| ax_plus_0 e : ax (e+0) e
| ax_plus_idem e : ax (e+e) e
| ax_plus_ass e f g : ax (e+(f+g)) ((e+f)+g)
| ax_plus_0 e : ax (e+0) e
⟨𝐄,⋅,+,1,0⟩ is an idempotent semiring.
| ax_seq_0 e : ax (e⋅0) 0
| ax_0_seq e : ax (0⋅e) 0
| ax_plus_seq e f g: ax ((e + f)⋅g) (e⋅g + f⋅g)
| ax_seq_plus e f g: ax (e⋅(f + g)) (e⋅f + e⋅g)
| ax_0_seq e : ax (0⋅e) 0
| ax_plus_seq e f g: ax ((e + f)⋅g) (e⋅g + f⋅g)
| ax_seq_plus e f g: ax (e⋅(f + g)) (e⋅f + e⋅g)
⟨𝐄,∩⟩ is a commutative and idempotent semigroup.
| ax_inter_assoc e f g : ax (e∩(f ∩ g)) ((e∩f)∩g)
| ax_inter_comm e f : ax (e∩f) (f∩e)
| ax_inter_idem e : ax (e ∩ e) e
| ax_inter_comm e f : ax (e∩f) (f∩e)
| ax_inter_idem e : ax (e ∩ e) e
⟨𝐄,+,∩⟩ forms a distributive lattice, and 0 is absorbing for
∩.
| ax_plus_inter e f g: ax ((e + f)∩g) (e∩g + f∩g)
| ax_inter_plus e f : ax ((e∩f)+e) e
| ax_inter_0 e : ax (e∩0) 0
| ax_inter_plus e f : ax ((e∩f)+e) e
| ax_inter_0 e : ax (e∩0) 0
° is an involution that flips concatenations and commutes with
every other operation.
| ax_conv_conv e : ax (e°°) e
| ax_conv_1 : ax (1°) 1
| ax_conv_0 : ax (0°) 0
| ax_conv_plus e f: ax ((e + f)°) (e° + f°)
| ax_conv_seq e f: ax ((e ⋅ f)°) (f° ⋅ e°)
| ax_conv_inter e f: ax ((e∩f)°) (e° ∩ f°)
| ax_conv_1 : ax (1°) 1
| ax_conv_0 : ax (0°) 0
| ax_conv_plus e f: ax ((e + f)°) (e° + f°)
| ax_conv_seq e f: ax ((e ⋅ f)°) (f° ⋅ e°)
| ax_conv_inter e f: ax ((e∩f)°) (e° ∩ f°)
The four laws that follow are the most interesting ones. They
concern subunits, terms that are smaller than 1. With our
signature, any term can be projected to a subunit using the
operation 1 ∩ _ .
- For subunits, intersection and concatenation coincide.
- Mirror image is the identity on subunits.
- Subunits commute with every term.
- This last law is less intuitive, but with the previous ones, it allows one to extract from any union-free expressions a single global subunit.
We use these axioms to generate an axiomatic equivalence
relation and an axiomatic order relations.
Inductive 𝐄_eq : Equiv 𝐄 :=
| eq_refl e : e ≡ e
| eq_trans f e g : e ≡ f → f ≡ g → e ≡ g
| eq_sym e f : e ≡ f → f ≡ e
| eq_plus e f g h : e ≡ g → f ≡ h → (e + f) ≡ (g + h)
| eq_seq e f g h : e ≡ g → f ≡ h → (e ⋅ f) ≡ (g ⋅ h)
| eq_inter e f g h : e ≡ g → f ≡ h → (e ∩ f) ≡ (g ∩ h)
| eq_conv e f : e ≡ f → (e°) ≡ (f°)
| eq_ax e f : ax e f → e ≡ f.
Global Instance 𝐄_Equiv : Equiv 𝐄 := 𝐄_eq.
| eq_refl e : e ≡ e
| eq_trans f e g : e ≡ f → f ≡ g → e ≡ g
| eq_sym e f : e ≡ f → f ≡ e
| eq_plus e f g h : e ≡ g → f ≡ h → (e + f) ≡ (g + h)
| eq_seq e f g h : e ≡ g → f ≡ h → (e ⋅ f) ≡ (g ⋅ h)
| eq_inter e f g h : e ≡ g → f ≡ h → (e ∩ f) ≡ (g ∩ h)
| eq_conv e f : e ≡ f → (e°) ≡ (f°)
| eq_ax e f : ax e f → e ≡ f.
Global Instance 𝐄_Equiv : Equiv 𝐄 := 𝐄_eq.
For the order to match the equivalence relation, we need to add
this axiom, stating that 0 is a minimal element.
Inductive 𝐄_inf : Smaller 𝐄 :=
| inf_refl e : e ≤ e
| inf_trans f e g : e ≤ f → f ≤ g → e ≤ g
| inf_plus e f g h : e ≤ g → f ≤ h → (e + f) ≤ (g + h)
| inf_seq e f g h : e ≤ g → f ≤ h → (e ⋅ f) ≤ (g ⋅ h)
| inf_inter e f g h : e ≤ g → f ≤ h → (e ∩ f) ≤ (g ∩ h)
| inf_conv e f : e ≤ f → (e°) ≤ (f°)
| inf_ax e f : ax e f ∨ ax f e → e ≤ f
| inf_zero e : 0 ≤ e.
Global Instance 𝐄_Smaller : Smaller 𝐄 := 𝐄_inf.
Hint Constructors 𝐄_eq ax 𝐄_inf.
| inf_refl e : e ≤ e
| inf_trans f e g : e ≤ f → f ≤ g → e ≤ g
| inf_plus e f g h : e ≤ g → f ≤ h → (e + f) ≤ (g + h)
| inf_seq e f g h : e ≤ g → f ≤ h → (e ⋅ f) ≤ (g ⋅ h)
| inf_inter e f g h : e ≤ g → f ≤ h → (e ∩ f) ≤ (g ∩ h)
| inf_conv e f : e ≤ f → (e°) ≤ (f°)
| inf_ax e f : ax e f ∨ ax f e → e ≤ f
| inf_zero e : 0 ≤ e.
Global Instance 𝐄_Smaller : Smaller 𝐄 := 𝐄_inf.
Hint Constructors 𝐄_eq ax 𝐄_inf.
Some elementary properties of this algebra
Global Instance equiv_Equivalence : Equivalence equiv.
Global Instance smaller_PreOrder : PreOrder smaller.
Global Instance inter_equiv :
Proper (equiv ==> equiv ==> equiv) 𝐄_inter.
Global Instance plus_equiv :
Proper (equiv ==> equiv ==> equiv) 𝐄_plus.
Global Instance seq_equiv :
Proper (equiv ==> equiv ==> equiv) 𝐄_seq.
Global Instance conv_equiv :
Proper (equiv ==> equiv) 𝐄_conv.
Global Instance inter_smaller :
Proper (smaller ==> smaller ==> smaller) 𝐄_inter.
Global Instance plus_smaller :
Proper (smaller ==> smaller ==> smaller) 𝐄_plus.
Global Instance seq_smaller :
Proper (smaller ==> smaller ==> smaller) 𝐄_seq.
Global Instance conv_smaller :
Proper (smaller ==> smaller) 𝐄_conv.
Global Instance smaller_PreOrder : PreOrder smaller.
Global Instance inter_equiv :
Proper (equiv ==> equiv ==> equiv) 𝐄_inter.
Global Instance plus_equiv :
Proper (equiv ==> equiv ==> equiv) 𝐄_plus.
Global Instance seq_equiv :
Proper (equiv ==> equiv ==> equiv) 𝐄_seq.
Global Instance conv_equiv :
Proper (equiv ==> equiv) 𝐄_conv.
Global Instance inter_smaller :
Proper (smaller ==> smaller ==> smaller) 𝐄_inter.
Global Instance plus_smaller :
Proper (smaller ==> smaller ==> smaller) 𝐄_plus.
Global Instance seq_smaller :
Proper (smaller ==> smaller ==> smaller) 𝐄_seq.
Global Instance conv_smaller :
Proper (smaller ==> smaller) 𝐄_conv.
From the axioms, we can infer the following simple laws.
Lemma equiv_0_inter e : (0∩e) ≡ 0.
Lemma inter_plus e f g : (e∩(f + g)) ≡ (e∩f + e∩g).
Lemma inf_ax_inter_l e f : e ∩ f ≤ e.
Lemma inf_ax_inter_r e f : e ∩ f ≤ f.
Lemma inter_plus e f g : (e∩(f + g)) ≡ (e∩f + e∩g).
Lemma inf_ax_inter_l e f : e ∩ f ≤ e.
Lemma inf_ax_inter_r e f : e ∩ f ≤ f.
This allows us to prove that the order we defined is indeed
antisymmetric with respect to ≡.
We now strengthen our earlier lemma, by proving the following
reduction from ordering to equivalence.
Mirror is actually more than monotone, it is bijective.
We establish few properties of subunits.
Lemma inter_1_abs e f : ((1 ∩ e)⋅(1 ∩ f)) ≡ (1 ∩ (e ∩ f)).
Lemma inter_onel e : (1 ∩ e + (1 ∩ e)⋅e) ≡ ((1 ∩ e)⋅e).
Lemma inter_oner e : (1 ∩ e + e⋅(1 ∩ e)) ≡ (e⋅(1 ∩ e)).
Lemma inter_onel e : (1 ∩ e + (1 ∩ e)⋅e) ≡ ((1 ∩ e)⋅e).
Lemma inter_oner e : (1 ∩ e + e⋅(1 ∩ e)) ≡ (e⋅(1 ∩ e)).
Using these, we get a second reduction from ordering to
equivalence, relying on intersection rather than union.
A product is larger than 1 if and only if both its arguments are.
This operator satisfies some simple distributivity properties.
Lemma sum_app l m : ⋃ (l++m) ≡ ⋃ l + ⋃ m.
Lemma seq_distr l m : ⋃ l ⋅ ⋃ m ≡ ⋃ (bimap (fun e f : 𝐄 ⇒ e ⋅ f) l m).
Lemma inter_distr l m : ⋃ l ∩ ⋃ m ≡ ⋃ bimap (fun e f : 𝐄 ⇒ e ∩ f) l m.
Lemma seq_distr l m : ⋃ l ⋅ ⋃ m ≡ ⋃ (bimap (fun e f : 𝐄 ⇒ e ⋅ f) l m).
Lemma inter_distr l m : ⋃ l ∩ ⋃ m ≡ ⋃ bimap (fun e f : 𝐄 ⇒ e ∩ f) l m.
Normalisation of expressions
Fixpoint is_term (e : 𝐄) : bool :=
match e with
| 1 | 𝐄_var _ | (𝐄_var _)° ⇒ true
| e ⋅ f | e ∩ f ⇒ is_term e && is_term f
| _° | _ + _ | 0 ⇒ false
end.
match e with
| 1 | 𝐄_var _ | (𝐄_var _)° ⇒ true
| e ⋅ f | e ∩ f ⇒ is_term e && is_term f
| _° | _ + _ | 0 ⇒ false
end.
Fixpoint comb e (dir : bool) :=
match e with
| 0 ⇒ []
| 1 ⇒ [1]
| e + f ⇒ comb e dir ++ comb f dir
| e ∩ f ⇒ bimap (fun e f ⇒ e ∩ f) (comb e dir) (comb f dir)
| e ° ⇒ comb e (negb dir)
| 𝐄_var a ⇒ if dir then [𝐄_var a] else [𝐄_var a°]
| e ⋅ f ⇒
if dir
then bimap (fun e f ⇒ e ⋅ f) (comb e dir) (comb f dir)
else bimap (fun e f ⇒ e ⋅ f) (comb f dir) (comb e dir)
end.
match e with
| 0 ⇒ []
| 1 ⇒ [1]
| e + f ⇒ comb e dir ++ comb f dir
| e ∩ f ⇒ bimap (fun e f ⇒ e ∩ f) (comb e dir) (comb f dir)
| e ° ⇒ comb e (negb dir)
| 𝐄_var a ⇒ if dir then [𝐄_var a] else [𝐄_var a°]
| e ⋅ f ⇒
if dir
then bimap (fun e f ⇒ e ⋅ f) (comb e dir) (comb f dir)
else bimap (fun e f ⇒ e ⋅ f) (comb f dir) (comb e dir)
end.
Lemma comb_equiv e : e ≡ ⋃ comb e true.
Close Scope expr_scope.
End s.
Infix " ⋅ " := 𝐄_seq (at level 40) : expr_scope.
Infix " + " := 𝐄_plus (left associativity, at level 50) : expr_scope.
Infix " ∩ " := 𝐄_inter (at level 45) : expr_scope.
Notation "x °" := (𝐄_conv x) (at level 25) : expr_scope.
Notation " 1 " := 𝐄_one : expr_scope.
Notation " 0 " := 𝐄_zero : expr_scope.
Notation "⋃ l " := (sum l) (at level 30).
Section language.
Close Scope expr_scope.
End s.
Infix " ⋅ " := 𝐄_seq (at level 40) : expr_scope.
Infix " + " := 𝐄_plus (left associativity, at level 50) : expr_scope.
Infix " ∩ " := 𝐄_inter (at level 45) : expr_scope.
Notation "x °" := (𝐄_conv x) (at level 25) : expr_scope.
Notation " 1 " := 𝐄_one : expr_scope.
Notation " 0 " := 𝐄_zero : expr_scope.
Notation "⋃ l " := (sum l) (at level 30).
Section language.
Context { X : Set }.
We interpret expressions as languages in the obvious way:
Global Instance to_lang_𝐄 {Σ}: semantics 𝐄 language X Σ :=
fix to_lang_𝐄 σ e:=
match e with
| 1 ⇒ 1%lang
| 0 ⇒ 0%lang
| 𝐄_var a ⇒ (σ a)
| e + f ⇒ ((to_lang_𝐄 σ e) + (to_lang_𝐄 σ f))%lang
| e ⋅ f ⇒ ((to_lang_𝐄 σ e) ⋅ (to_lang_𝐄 σ f))%lang
| e ∩ f ⇒ ((to_lang_𝐄 σ e) ∩ (to_lang_𝐄 σ f))%lang
| e° ⇒ (to_lang_𝐄 σ e)°%lang
end.
fix to_lang_𝐄 σ e:=
match e with
| 1 ⇒ 1%lang
| 0 ⇒ 0%lang
| 𝐄_var a ⇒ (σ a)
| e + f ⇒ ((to_lang_𝐄 σ e) + (to_lang_𝐄 σ f))%lang
| e ⋅ f ⇒ ((to_lang_𝐄 σ e) ⋅ (to_lang_𝐄 σ f))%lang
| e ∩ f ⇒ ((to_lang_𝐄 σ e) ∩ (to_lang_𝐄 σ f))%lang
| e° ⇒ (to_lang_𝐄 σ e)°%lang
end.
This interpretation is sound in the sense that axiomatically
equivalent expressions are semantically equivalent. Differently put,
the axioms we imposed hold in every algebra of languages.
This extends to ordering as well.
A word is in the language of a sum if and only if it is in the
language of one of its components.