Consider a simple language which has natural numbers, vectors of natural numbers, variables, and some operations like +,- and nth. Naively, I would encode it in Coq like this:
Require Import Coq.Vectors.Vector.
Inductive NExpr: Type :=
| NVarValue: nat -> NExpr
| NConst: nat -> NExpr
| NPlus : NExpr -> NExpr -> NExpr
| NMinus: NExpr -> NExpr -> NExpr
| NNth : forall n, VExpr n -> NExpr -> NExpr
with
VExpr (n:nat): Type :=
| VVarValue: nat -> VExpr n
| VConst: Vector.t nat n -> VExpr n.
Of course, this does not work due to the known limitation producing the error: "Error: Parameters should be syntactically the same for each inductive type."
What would be a correct way to encode such language in Coq? . Of course, I should be able to write an eval function, evaluating these expressions along the lines of https://softwarefoundations.cis.upenn.edu/lf-current/Imp.html
When evaluating, the dimensionality of vectors is used as follows:
match e with
...
| @NNth v i => match Compare_dec.lt_dec (evalNexp st i) n with
| left p => Vnth (evalNexp st v) p
| right _ => 0
end
N.B. In this example, VExpr does not depend on NExpr, but in future it could, with the addition of constructors some of which may use NExpr. Also, I may need to add more types, for example, ZExpr for integers.
VExprwould depend onNExpr? Do I understand your evaluation snippet correctly, thenVExpris used to represent values andNExprrepresents abstract syntax? - nesrekaNVarValueandVVarValue, which both seem to model natural number constants. - eponierNVarValueandVVarValueare lookup functions which take a variable name (represented as a natural number) and return a natural number or a vector respectively. - krokodilNExprwould be1+2*3and the example ofVExprwould be[1,2,3]using more traditional syntax. An example ofVExprwhich depdens onNExprwould be if we add a new constructor toVExpr, sayVZeroElement: VExpr n -> NExpr -> VExpr nwhich would represent a hypothetical function which takes a vector and replaces an element with given index with 0. - krokodiln:natparameter from the second inductive? You could add is as an argument to the constructors where needed and pattern match on them. You can also move theforall n:nat, ...into aconstructor n : ...- nesreka