5 ms·
Can OCaml's type system express a type parametrized on a value, eg. like template<auto f> struct my_type{}; using t1 = my_type<123>; constexpr t1 val1;
by jcelerier 5y ago
Can OCaml's type system express a type parametrized on a value, eg. like
template<auto f> struct my_type{};
using t1 = my_type<123>;
constexpr t1 val1;
using t2 = my_type<val1>;
?
- rowanG077 5y agoI haven't used ocaml but I think something is possible with GADTs. Beside the example you have given shows C++ templating prowess and not its type system. In general I wouldn't call a type system that doesn't support ADTs expressive.
- jcelerier 5y agoBut GADTs are expressible in the C++ type system (thanks to templates which are just the C++ implementation of the parametrizable types typesystem-level property)
- yawaramin 5y agoNo, but on the other hand it doesn’t monomorphize generics and make the compiles dog slow and the binaries bloated :-)
- andrepd 5y agoWell, but it does make the code slower
- yawaramin 5y agoI knew someone would say that :-) ‘Slower’ is super subjective. What is the actual performance requirement? And remember that the OP thread is talking about an extremely high-performance RPC system. Sure, it may not be as fast as C++, but again, what is the actual performance requirement. You don’t go around just throwing ‘Eh, not as fast as C++’ at everything you come across :-)
- rowanG077 5y agoHow is slower subjective? Performance of two pieces of code can be objectively measured. There is nothing subjective about it.
- yawaramin 5y agoWho cares? What use is measuring the objective performance difference between two solutions if no one cares about the difference? That’s why I’m talking about the actual performance requirement.
- krapht 5y agoThis is a pretty obnoxious way to tell me you don't work on real-time systems without explicitly saying you don't work on real-time systems.
- yawaramin 5y agoI didn't realize we were exclusively talking about real-time systems? Do you think people you argue with on HN are so dumb that they would go around claiming high-level GC languages are suitable for real-time systems?
- detaro 5y agoAsking about the actual requirement sounds very much like what you do in real-time systems, vs just being happy with "faster"?
- jolux 5y agoI feel like if your concern is real-time systems, you should probably say you don’t think it’s suitable for real-time systems, instead of talking about performance differences that no one really disagrees but which don’t matter in practice for most applications anyways.
- rowanG077 5y agoThat's an incredible arrogant and close minded way to think about software. There are plenty of domains where every extra % of performance matters. Video games, HFT, robotics etc.
- paulddraper 5y agoNo, the type system parameterizes on types.
- adamwk 5y agoPeople are downvoting but the GP's point does not really have anything to do with type systems. I think this example is closer to macro expansion than a typesystem.
- jcelerier 5y agoHow is paramerization of types on values not a typesystem-level property ? There's definitely nothing akin to macro expansion involved since you can do type-level computations that way
- throwaway17_17 5y agoI’m pretty sure the implication is that since template instances are made specific at compile time and are done so in a way that is very analogous to AST based macro expansion. Specifically, C++ templates are a programming language which runs during the compilation of C++ code that acts by producing valid C++ AST components.
- jcelerier 5y agoI have never seen a definition of type system which cares about things being done at compile-time or runtime, or that there even is a compile-time or run-time
- stonemetal12 5y agoNot exactly types, but with functors you can parameterize modules on values. As far as I can tell it ends up being very similar.
- gopiandcode 5y agoIt feels somewhat disingenuous to include a turing-complete meta-programming language (i.e macros) in your definition of the C++ type system. Because vanilla OCaml doesn't have any mechanism for expressing compile-time computations, the same level of transformations aren't possible, but I wouldn't call this a limitation of the type-system, rather the meta-programming capabilities. Although, you can somewhat approximate it with modules and functors: module type M = sig type t val t: t end let my_type (type a) (v: a) : (module M) = let module M = struct type t = a let t = v end in (module M : M) module T1 = (val my_type 123) type t1 = T1.t let val1 : t1 = T1.t module T2 = (val my_type val1) type t2 = T2.t
- jcelerier 5y ago> It feels somewhat disingenuous to include a turing-complete meta-programming language (i.e macros) in your definition of the C++ type system. But this is literally what a type system is about ? TS definitions don't care about compile-time and run-time ; what you call "macros" are type-level functions which are a byproduct of the expressivity of C++'s TS. Remember that expressivity of a type system is just the cardinality of the type set you can express with it. Type systems where you can parametrize on nothing are less expressive than type systems that can parametrize on types, which are less expressive than TS which can parametrize on meta-types, which are less expressive than TS which can parametrize on meta-types and integers (C++98, Haskell afaik), which are less expressive than TS which can parametrize on meta-types and arbitrary compile-time values (C++20, soon Rust I believe ?), which I'd guess are less expressive than full-blown dependent types but I'd need to check the exact definition
- LAC-Tech 5y agoI am guessing it could using PPX https://ocamlverse.github.io/content/metaprogramming.html https://ocamlverse.github.io/content/metaprogramming.html In C++ the system for generics is the same system for metaprogramming. In OCaml they are two different systems.