Crash in compiler with static[int] #3706

Closed
andreaferretti opened this Issue Jan 10, 2016 · 2 comments

Projects

None yet

2 participants

@andreaferretti
Contributor

The following makes the compiler crash:

type Modulo[M: static[int]] = distinct int

proc modulo(a: int, M: static[int]): Modulo[M] = Modulo[M](a %% M)

proc `+`[M: static[int]](a, b: Modulo[M]): Modulo[M] = (a.int + b.int).modulo(M)

proc `$`*[M: static[int]](a: Modulo[M]): string = $(a.int) & " mod " & $(M)

when isMainModule:
  let
    a = 3.modulo(7)
    b = 5.modulo(7)
  echo a + b

Error: internal error: getSize: tyStatic

@Araq Araq added the Static[T] label Jan 14, 2016
@andreaferretti
Contributor

By the way, my original aim would be to make this work over any Euclidean ring (that is, a structure with the usual addition and multiplication, plus division with remainder - this include integers, but also polynomials over the reals for instance).

If I try to make this generic, I get an error even earlier:

type Modulo[A; M: static[A]] = distinct A

proc `$`[A; M: static[A]](x: Modulo[A, M]): string =
  $(A(x)) & " mod " & $(M)

proc modulo[A](a: A, M: static[A]): Modulo[A, M] = Modulo[A, M](a %% M)

proc `+`[A; M: static[A]](x, y: Modulo[A, M]): Modulo[A, M] =
  (A(x) + A(y)).modulo(M)

when isMainModule:
  echo (3.modulo(7) + 5.modulo(7))
example.nim(3, 36) Error: cannot instantiate Modulo
got: (typedesc[A], M)
but expected: (A, M)
@Araq Araq added the Crash label May 31, 2016
@Araq Araq added the Showstopper label Jul 12, 2016
@Araq Araq added a commit that closed this issue Jul 12, 2016
@Araq Araq fixes #3706 f47165a
@Araq Araq closed this in f47165a Jul 12, 2016
@andreaferretti
Contributor

Great, thank you very much! Now I am trying to work out what is going on with the second example :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment