classes 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ------------------------------------------------------------------------
  2. Notes after class discussion 2007-08-17 (Ulf, NAD)
  3. These are just some quick and dirty notes outlining the outcome of the
  4. discussion. The use cases motivating the choices done are not
  5. discussed.
  6. Class declarations:
  7. record class C Γ : Δ where
  8. ...
  9. data class C Γ : Δ where
  10. ...
  11. No restrictions on parameters or indices.
  12. Instance declarations:
  13. f : ...
  14. f = ...
  15. instance f
  16. or perhaps
  17. instance f : ...
  18. f = ...
  19. f and the instance declaration have to be in the same scope.
  20. The instance which f declares is not in scope (with respect to
  21. instance search, see below) in the type signature of f, but it is in
  22. scope in the body of f (perhaps).
  23. The type of f has to evaluate to the normal form Γ -> C Δ, where C
  24. is a class. Γ has to consist solely of:
  25. • variables which are "rigid" in Δ (arguments to type constructors),
  26. and/or
  27. • class type constructors applied to arbitrary arguments. Denote
  28. these by C' Δ'.
  29. The type of f gives rise to a rewrite rule:
  30. C Δ -> C' Δ' (With an empty RHS if C' Δ' is empty.)
  31. The collection of all instance rewrite rules always has to be
  32. terminating. For every new instance added the termination checker is
  33. run on the rewrite rules (treating data/record type constructors in
  34. Δ/Δ' as constructors). One could also imagine a more local variant,
  35. in which the "recursive calls" in the RHS always have to be strictly
  36. structurally smaller than the LHS arguments.
  37. We may also allow higher-order types, in which case the RHSs become
  38. higher-order and the positive occurrences in C' Δ' should be
  39. inspected by the termination checker.
  40. Superclass declarations:
  41. record class C₁ Γ : Δ -> Setⁿ where
  42. ...
  43. instance x : C₂ X
  44. ...
  45. C₂ has to be a class; C₂ X becomes a superclass of C₁ Γ Δ.
  46. Searching for instances:
  47. If ? : C Γ, where C is a class, then ordinary unification is not
  48. used. Instead a special instance search is performed.
  49. The instance search is performed in several "levels". First a match
  50. for C Γ is searched for. If several matches are found, an error is
  51. emitted (ambiguous instances). If exactly one match is found, that
  52. match is bound to the meta variable. And if no matches are found,
  53. the search continues with the superclasses of C Γ, in a
  54. breadth-first manner. (First all immediate superclasses, all at the
  55. same time. Then the next level, and so on.)
  56. The search at a specific level is done in the following way:
  57. ⑴ All instances in scope are collected. Locally bound arguments
  58. whose types are of the form outlined for instances above also
  59. count as instances.
  60. ⑵ The rewrite rules generated by the instances are applied to the
  61. goals at hand (for instance C Γ), yielding new goals. This is
  62. repeated until no rules apply any more. Due to the termination
  63. checking done we know that this will terminate (right?).
  64. ⑶ All successful matches (search trees whose leaves all correspond
  65. to empty RHSs) are collected.
  66. From a successful match a dictionary can be built by applying the
  67. instance functions in a way corresponding to the resulting search
  68. tree; all arguments to these functions are either given by the goal
  69. at hand (the rigid variables) or by the children in the search tree.
  70. ------------------------------------------------------------------------
  71. A class system
  72. --------------
  73. what can be a class?
  74. - any datatype/record
  75. what is an instance?
  76. - an element of a class (possibly parameterised)
  77. - parameterised by what?
  78. + other instances clearly
  79. + non-instances? yes, arguments to the classes
  80. some examples:
  81. class Eq (A : Set) : Set where
  82. eq : (_==_ : A -> A -> Bool) -> Eq A
  83. instance
  84. eqList : {A : Set} -> Eq A -> Eq (List A)
  85. eqList eqA = ..
  86. what are the restrictions on an instance declaration?
  87. - should probably be
  88. instance i : Δ -> Cs -> C ts
  89. where
  90. Cs are classes
  91. Δ can be inferred from C ts
  92. - instance search will proceed as follows:
  93. + given a goal C ss, unify with C ts to get ρ (deciding all of Δ)
  94. + the new goals are Cs ρ
  95. multiparameter type classes?
  96. - how difficult?
  97. - necessary? clearly useful (at least with inductive families)
  98. functional dependecies
  99. - probably not needed (we have associated types for free)
  100. zero parameter type classes are useful:
  101. class True : Set where
  102. instance tt : True
  103. A constructor can be declared an instance by writing instance before the
  104. name. The normal checks are applied to the type.
  105. _/_ : (n m : Nat) -> {m ≠ 0} -> Nat
  106. m / n = ..
  107. now x / 3 requires a proof of 3 ≠ 0 = True for which there is an instance
  108. (tt). This would work with an inductive definition of ≠ as well (but requires
  109. multiparameter classes):
  110. class _≠_ : Nat -> Nat -> Set where
  111. instance neqZS : {m : Nat} -> 0 ≠ suc m
  112. instance neqSZ : {n : Nat} -> suc n ≠ 0
  113. instance neqSS : {n m : Nat} -> n ≠ m -> suc n ≠ suc m
  114. How to do super classes?
  115. class Ord (A : Set){eqA : Eq A} : Set where
  116. ord : (compare : A -> A -> Ordering) -> Ord A {eqA}
  117. instance ordNat : Ord Nat
  118. ordNat = ord compareNat
  119. this doesn't really work...
  120. sort : {A : Set} -> {Ord A} -> List A -> List A
  121. there is no instance Eq A here. Ord A must contain Eq A
  122. class Ord (A : Set) : Set where
  123. ord : {Eq A} -> (compare : A -> A -> Ordering) -> Ord A
  124. how to get the Eq dictionary from the Ord dictionary (nothing recording the
  125. relationship here). One attempt:
  126. instance ordToEq : {A : Set} -> Ord A -> Eq A
  127. ordToEq (ord eq _) = eq
  128. How does this work with other instances (clearly overlapping)? Maybe there is
  129. a good reason it's treated specially in Haskell... It would be nice not to
  130. have to introduce more syntax.
  131. Finding instances
  132. -----------------
  133. Instances form a rewriting system (there is a paper about this stuff...)
  134. instance i : Δ -> Cs -> C ts
  135. corresponds to a rule
  136. ∀ Δ. C ts --> Cs
  137. vim: sts=2 sw=2 tw=80