t18409.nim 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. discard """
  2. action: "compile"
  3. """
  4. # A vector space over a field F concept.
  5. type VectorSpace*[F] = concept x, y, type V
  6. vector_add(x, y) is V
  7. scalar_mul(x, F) is V
  8. dimension(V) is Natural
  9. # Real numbers (here floats) form a vector space.
  10. func vector_add*(v: float, w: float): float = v + w
  11. func scalar_mul*(v: float, s: float): float = v * s
  12. func dimension*(x: typedesc[float]): Natural = 1
  13. # 2-tuples of real numbers form a vector space.
  14. func vector_add*(v, w: (float, float)): (float, float) =
  15. (vector_add(v[0], w[0]), vector_add(v[1], w[1]))
  16. func scalar_mul*(v: (float, float), s: float): (float, float) =
  17. (scalar_mul(v[0], s), scalar_mul(v[1], s))
  18. func dimension*(x: typedesc[(float, float)]): Natural = 2
  19. # Check concept requirements.
  20. assert float is VectorSpace
  21. assert (float, float) is VectorSpace
  22. # Commutivity axiom for vector spaces over the same field.
  23. func axiom_commutivity*[F](u, v: VectorSpace[F]): bool =
  24. vector_add(u, v) == vector_add(v, u)
  25. # This is okay.
  26. assert axiom_commutivity(2.2, 3.3)
  27. # This is not.
  28. assert axiom_commutivity((2.2, 3.3), (4.4, 5.5))