Notations in Coq - coq

I want to use the notations to represent the predicate test as follows:
Variable A B : Type.
Inductive test : A -> B -> A -> B -> Prop :=
| test1 : forall a1 a2 b1 b2,
a1 \ b1 || a2 \ b2
where "c1 '\' st '||' c2 '\' st'" := (test c1 st c2 st')
.
However, the Coq has an error:
Why this notation cannot be accepted in Coq?

The notation is accepted, it's actually that Coq is incorrectly parsing your use of the notation within the definition of test1. To correctly parse this notation you need to adjust the parsing levels of its terms. You can do that with a reserved notation, since these where clauses for notation within an inductive don't support the syntax for configuring the notation:
Variable A B : Type.
Reserved Notation "c1 '\' st '||' c2 '\' st'" (at level 40, st at next level, c2 at next level, no associativity).
Inductive test : A -> B -> A -> B -> Prop :=
| test1 : forall a1 a2 b1 b2,
a1 \ b1 || a2 \ b2
where "c1 '\' st '||' c2 '\' st'" := (test c1 st c2 st')
.
I don't have a good intuition for what parsing levels work well (40 is somewhat arbitrary above), so the best advice I can give is to experiment and if it's parsed incorrectly somewhere then try adjusting the level.

Related

Proving General Associativity in Groups

For a project I'm coding group theory through Coq, obviously associatvity of 3 elements is a given, however I'm struggling to prove it holds for a string of length n. That is, (x1 * ... * xn) is always the same regardless of how many brackets are in there, or there placement.
The relevant group code is
Structure group :=
{
e : G;
Op : G -> G -> G;
Inv : G -> G;
Associativity : forall (x y z : G), Op x (Op y z) = Op (Op x y) z;
LeftInverse : forall (x : G), Op (Inv x) x = e;
LeftIdentity : forall (x : G), Op e x = x;
}.
It's not the proof itself I have the issue with but how to code it. I can see at the very least I'll need a further function that allows me to operate on strings rather than just elements, but i've no idea how to get started. Any pointers?
Operating on strings directly is certainly possible, but cumbersome. When reasoning about languages, it is much more convenient to use abstract syntax trees instead. For your statement, we only want to consider combinations of elements with some binary operation, so a binary tree suffices:
Inductive tree T :=
| Leaf : T -> tree T
| Node : tree T -> tree T -> tree T.
For concreteness, I'll only consider the natural numbers under addition, but this generalizes to any other monoid (and thus any other group). We can write a function that sums all the elements of a tree:
Fixpoint sum_tree t : nat :=
match t with
| Leaf n => n
| Node t1 t2 => sum_tree t1 + sum_tree t2
end.
We can also write a function that flattens the tree, collecting all of its elements in a list
Fixpoint elements {T} (t : tree T) : list T :=
match t with
| Leaf x => [x]
| Node t1 t2 => elements t1 ++ elements t2
end.
With these ingredients, we can formulate the statement that you were looking for: if two trees (that is, two ways of putting parenthesis in an expression) have the same sequences of elements, then they must add up to the same number.
Lemma eq_sum_tree t1 t2 :
elements t1 = elements t2 -> sum_tree t1 = sum_tree t2.
I'll leave the proof of this statement to you. ;)

What are the possible ways to define parallel composition in Coq apart from using list?

I know how to use the list to define it but expecting something other than this. Because I think it is not possible to prove associativity and commutative property if we define it using list.
Just guessing that you are asking about parallel composition of state machines. There are many ways to model them in Coq's logic, I don't know if lists are the best way...
If you define the steps as propositional relations you can compose two machines "in parallel" by creating a new inductive relation that can take a step either in one of the machines, or in the other.
This is sketch is not the most general way to write it, but hopefully it gets the idea across.
Suppose the machines can take steps between the states A, B, or C. Machine M1 behaves in one way, and M2 in another. I.e. M1 can step from A to B or C, from B to C, and from C to A. M2 can only step from A to C, and from C to A.
Inductive state : Set := A | B | C.
Inductive M1 : state -> state -> Prop :=
s1 : M1 A B | s2 : M1 A C | s3 : M1 B C | s4 : M1 C A.
Inductive M2 : state -> state -> Prop :=
ss1 : M2 A C | ss2 : M2 C A.
The we can create a new step relation for the combined system
Inductive M1M2 : (state*state) -> (state*state) -> Prop :=
| st1 s1 s1' s2: M1 s1 s1' -> M1M2 (s1, s2) (s1', s2) (* M1 takes a step *)
| st2 s1 s2 s2': M2 s2 s2' -> M1M2 (s1, s2) (s1, s2'). (* M2 takes a step *)

How to write agda equivalent code of this coq code?

I want to write the given coq code in agda.
Definition included (D1 D2:R -> Prop) : Prop := forall x:R, D1 x -> D2 x.
I have tried in this way ..
data included (D1 D2 : R -> Set) : Set where
forall x : R D1 x -> D2 x
I know the problem is in second line but how to fix it. Do we need to define constructor ?? And how will i do that??
please help.
data in Agda is the equivalent of Inductive in Coq: it introduces a new type defined inductively.
The equivalent of Definition in Agda is simply a defining equation for the thing you wish to define:
postulate R : Set
included : (_ _ : R → Set) → Set
included D1 D2 = ∀ x → D1 x → D2 x

How to search pattern in certain range of text and replace with another string?

$ NAME : corry
$$.Inc s d
$$.Oc s
$$.TO
G1 ty n1 EE EE M T1 T2 $$SRU
G2 n1 y OO OO M T3 T4 $$SRU
.EON
$ NAME : patrick
$$.Inc c d
$$.Oc c
$$.TO
G1 td n3 EE EE M T5 T6 $$SRU
G2 n3 y OO OO M T7 T8 $$SRU
.EON
$ NAME : danny
$$.Inc a b
$$.Oc b
$$.TO
#lc1 corry
#lc2 patrick
1 to n0 EE EE M S1 S2 $$SRU
G2 n0 y OO OO M S3 S4 $$SRU
.EON
$ NAME : sandy
$$.Inc m n
$$.Oc n
$$.TO
G1 te n1 EE EE M b1 b2 $$SRU
G2 n1 o OO OO M b3 b4 $$SRU
.EON
$ NAME : manager
$$.Inc o e
$$.Oc e
$$.TO
#lc3 danny
#lc4 sandy
G1o ty n1 EE EE M T1 T2 $$SRU
G2o n1 y OO OO M T3 T4 $$SRU
.EON
How to search for a certain pattern in a certain range? For example, I want to search G1o at range between the section from $ Name : manager until the end of End of name (.EON) and replace it with G1o.corry.n.
perl -pe 's/G1o/G1o.corry.n/ if /\$ NAME : manager/ .. /\.EON/' file
From perlop documentation:
In scalar context, ".." returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors. Each ".." operator maintains its own boolean state, even across calls to a subroutine that contains it. It is false as long as its left operand is false.
sed '/^\$ NAME : manager/,/\.EON/s/G1o/G1o.corry.n/'

Restricted inductive type definitions in Coq

I would like to somehow limit what kind of inputs constructors are allowed to take in an inductive definition. Say I want to say define binary numbers as follows:
Inductive bin : Type :=
| O : bin
| D : bin -> bin
| S : bin -> bin.
The idea here is that D doubles up a nonzero number by adding a zero at the end and S takes a number with zero as the last digit and turns the last digit into a one. This means that the following are legal numbers:
S 0
D (S 0)
D (D (S 0))
while the following would not be:
S (S 0)
D 0
Is there a way to enforce such restrictions in an inductive definition in a clean way?
You could define what it means for a bin to be legal with a predicate, and then give a name to the subset of bins that obey that predicate. Then you write functions with Program Definition and Program Fixpoint instead of Definition and Fixpoint. For recursive functions you'll also need a measure to prove the arguments of your functions decrease in size since the functions are not structurally recursive anymore.
Require Import Coq.Program.Program.
Fixpoint Legal (b1 : bin) : Prop :=
match b1 with
| O => True
| D O => False
| D b2 => Legal b2
| S (S _) => False
| S b2 => Legal b2
end.
Definition lbin : Type := {b1 : bin | Legal b1}.
Fixpoint to_un (b1 : bin) : nat :=
match b1 with
| O => 0
| D b2 => to_un b2 + to_un b2
| S b2 => Coq.Init.Datatypes.S (to_un b2)
end.
Program Definition zer (b1 : lbin) := O.
Program Fixpoint succ (b1 : lbin) {measure (to_un b1)} : lbin :=
But this simply-typed approach would probably be easier.
This could be done with inductive-recursive definitions - but unfortunately Coq doesn't support those.
From an object-oriented programming perspective, O, D and S are subtypes of bin, and their constructor types are then definable without resorting to logical predicates, but Coq doesn't support object-oriented programming natively either.
However, Coq does have typeclasses. So what I might do is make bin a typeclass, and make each of the constructors a separate inductive type, each of which has an instance of the bin typeclass. I'm not sure what the method(s) of the typeclass would be though.