Is there a way that a compiler plugin can take the...
# compiler
r
Is there a way that a compiler plugin can take the resolved generic types from certain values, and replace them with different generics (assuming the replacement conforms to type bounds) before code analysis?
s
You can certainly modify psi before it gets analyzed ...or you can restart analysis after modification.
r
You can also just transform the IR tree so the generated functions and all their call sites are also accounted for. In IR you have all the info right before codegen including type information and bounds and where they come from at call sites
r
I should make clear what I'm trying to do, if it's possible. I have some classes representing numeric values in the type system, N0 through N20, and I'd like to add some more classes representing basic arithmetic on these values, Add<Num ,Num>, Sub<Num, Num>, Mul<Num, Num>, and Div<Num, Num>. Then when those show up at compile time, I'd like to transform them to be the result of the arithmetic instead of compositions of the operations. E.g. Add<N1, Mul<N2, N3>> would be transformed into N7
r
That is very interesting similar to what I'm doing for refined types
You want type level Nat and singleton literals but that is not a thing in Kotlin though you can perform those computations like meta does in type proofs with the type checker. If Kotlin had path dependent types this computations could be encoded like scala.refined does
@redrield I'm building these features in meta in case you are interested in helping. I'm using the constant evaluator to lift predicates as expressions at compile time and refine types. Also using nats and others for the predicates.
r
I've already got the numeric types, and I know they work because I've written some stuff based on kotlingrad that uses them to typecheck matrix math; I'm trying to do this to improve a typesafe units of measure library that I've already made. Right now the units library works with compositions of base units in the type system, but that can lead to situations where two types are dimensionally equivalent but addition between them won't typecheck. I'd like to try to refactor it so that the type parameter to the wrapper simply encodes the exponent for each base unit
r
We have so far union types, kinds, type classes and I'm currently trying to crack the nut of type refinements to have more precise models in Kotlin that what it can support today with the platform types
Is this work open source somewhere ? I'm interested in checking it out
r
Thanks!
s
Wow, that looks is quite interesting @raulraja do you have a link to constant evaluator in meta you mentioned earlier?
r
I tried the constant evaluator and discarded it because it can't support complex runtime symbols that are not literals. The signature using ktExpression fooled me and I'm now resorting to the script engine to validate the predicates with the std lib in the classpath so I can do things like startsWith('@')
Working on that today