1

I am trying to represent a finite ring ℤ/nℤ with the characteristic being a type-level integer specified at runtime.

Would it be possible to have something similar to shapeless.Nat to represent a type-level integer, but without it needing to be a compile-time constant, i.e. only having to specify the value (and generate the corresponding type) at runtime? Could it be perhaps done with the help of macro paradise? What would I require to do so?

I would need to be able to retrieve the value from the type at the runtime as well, but I don't necessarily need to perform any arithmetic on the type level (i.e. add the type-level integers, increment, etc.)

Thank you.

PeterParameter
  • 524
  • 5
  • 16
  • 2
    I doubt it is possible, as you need macros are executed during compile time. What about generating some of the number classes (for example till 22) and use them at runtime. You can collect them in an array or that kind of thing. Else I think you will need to have a compiler included. – Gábor Bakos Feb 04 '14 at 18:16
  • Yes, I kind of suspected this would be the case. Thanks for your reply! – PeterParameter Feb 04 '14 at 21:14
  • These things don't have to be literals and known at compile time. See for example how you might solve this problem in Agda or Coq. The solution isn't limited to languages like that though, and you still can construct some sort of statically sized (but not known at compile time) mod ring in Haskell and I'm sure Scala. The main problem you'll run into is that working with it will be fairly painful. The key idea is basically existentials and skolemization to hide the runtime knowledge yet operate in a consistent type environment. The idiomatic equivalent to that is to use PDTs in Scala. – Mysterious Dan Feb 04 '14 at 21:59
  • 1
    The basic PDT pattern could be to have something like `class ModRing(i: Int) { type Element <: Numeric }` and then you can construct elements of a particular `Int`'s `ModRing` and operate within that context. Due to Scala not really having a story about equality or reduction, if I create two `ModRing` for the same `Int` value, their elements will not be interchangeable, but dealing with that gets a lot hairier. – Mysterious Dan Feb 04 '14 at 22:15
  • Ah, I see now. Well, yeah, it does get hairy, to a point where I'm not sure I want to go down that road. But thanks for the reply. – PeterParameter Feb 10 '14 at 09:00

0 Answers0