by Hirrolot · 4y ago · 0 comments
- Values dependent on values (algorithms)
- Values dependent on types (parametric polymorphism)
- Types dependent on types (type-level functions)
This is one dimension weaker than Calculus of Constructions (CoC) [1], which permits types dependent on values (so-called "dependent types"), but we don't need them until we want to prove some properties about our code (dependent types correspond to logical predicates). Some tasks handled by metaprogramming however may not be handled even by a type system that permits type-level functions; for this reason, probably we have to include some sort of simple syntax transformations, such as those found in Idris [2].
I expatiated in one of my posts [3] on why static languages duplicate their language concepts. This writing also includes Zig and Idris.
[1] https://gist.github.com/Hirrolot/89c60f821270059a09c14b940b4...
[2] http://docs.idris-lang.org/en/latest/tutorial/syntax.html
[3] https://hirrolot.github.io/posts/why-static-languages-suffer...
You mentioned the complexities of idris and the lack of adoption for a unified system. Are you hopeful that programming languages will begin to move in this direction, or do you think we will quickly arrive at a "good enough" halfway house (Rust seems like a good example of this)?
By the way, I don't think it's time to claim that Rust is a "good enough" house -- simply too little time passed since it was embraced by the community. The language didn't even include `async`/`.await` some four years ago. Macros 2.0 are being developed; a new trait solver too. If after some ten or twenty years we'll find it being a mess of legacy, maybe it is not the right way to design a language.
STRUCT struct_name = field_1, ..., field_N;
but it's just a syntactic sugar for CONST field_1 = 0, ..., field_N = N-1, struct_name = N;
just so that suitably sized vectors can be used as records: STRUCT POINT = PX, PY, PCOLOR; ! same as CONST PX = 0, PY = 1, PCOLOR = 2, POINT = 3;
VAR p[POINT]; ! same as VAR p[3];
p[PCOLOR] := 42; ! same as p[2] := 42;
It works reasonably well and actually allows for somewhat better meta-programming than e.g. C with its X-macros because you can literally loop over a struct's field, in a normal FOR loop.In most languages, a function taking a float and a function taking an int end up producing different compiler output (because they need to use different CPU registers). More to the point, this extends to generic/template functions with signatures like “void foo<T>(T arg)”, which then need to be monomorphized, that is, copied for each instantiation.
Languages with type erasure simply drop the type argument instead of instantiating it.