Coq: modeling versus dependent records

I can not understand the difference between typeclasses and dependent records in Coq. The reference manual gives the syntax of typeclasses, but says nothing about what they really are and how you should use them. A little thinking and searching shows that typeclasses are essentially dependent records with a little syntactic sugar, which allows Coq to automatically output some implicit instances and parameters. It seems that the algorithm for class classes works better when there is more or less than one possible instance in any given context, but this is not a big problem, since we can always move all typeclass fields to its parameters, eliminating the ambiguity. Also adInstanceit is automatically added to the Hints database, which can often facilitate evidence, but can sometimes break it if the instances were too general and caused search query cycles or explosions. Are there any other issues I should be aware of? What is the heuristic for choosing between them? For instance. would I lose anything if I only used records and set their instances as implicit parameters, when is this possible?

+4
source share
1 answer

: Coq - ( , ). , "" , - , : .

, , :

Class monoid A := Monoid {
  op : A -> A -> A;
  id : A;
  opA : forall x y z, op x (op y z) = op (op x y) z;
  idL : forall x, op id x = x;
  idR : forall x, op x id = x
}.

Require Import Arith.

Instance nat_plus_monoid : monoid nat := {|
  op := plus;
  id := 0;
  opA := plus_assoc;
  idL := plus_O_n;
  idR := fun n => eq_sym (plus_n_O n)
|}.

class, , nat, ,

Definition times_3 (n : nat) := op n (op n n).

, , Class Instance Record Definition, :

Toplevel input, characters 38-39: Error: In environment n : nat The term "n" has type "nat" while it is expected to have type  "monoid ?11".

, , . , , , .

+8

Source: https://habr.com/ru/post/1584682/


All Articles