4 ms·
How is a Haskell type class dict different from a vtable? It's in fact the same.
by kccqzy 6y ago
How is a Haskell type class dict different from a vtable?
It's in fact the same.
- tome 6y agoHmm, not sure what you mean. In the body of a function there is one class dict available. On the other hand there is one vtable per object, typically.
- richardwhiuk 6y agoIn C++ and Rust, there's a single vtable per class (trait), not per object. (This is basically why Rust doesn't support multi-trait types (e.g. &dyn Debug + Clone)
- estebank 6y agoYou can accomplish the same behavior by having a marker trait: trait Foo: Debug + Clone {} Edit: No you can't, because `Clone` requires `Sized`, which means you can't make a trait object of `Foo`. https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=252fbe402fe80be54816667afc5c62f3 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- ImprobableTruth 6y agoI don't know of a single language that has a vtable per object. What would even be the point? Edit: Assuming we're talking about objects that can be grouped into some sort of 'classes'.
- gpderetta 6y agoIsn't python __dict__ pretty much a per-object vtable?
- tome 6y agoI see I wasn't clear enough. With object-style polymorphism, the vtable is looked up on the object, using some means or other. With typeclass-style polymorphism, the "vtable" is (effectively) looked up on the type. Perhaps gpderetta said it better: https://news.ycombinator.com/item?id=23537639 https://news.ycombinator.com/item?id=23537639
- eeereerews 6y agoOne way is that it can handle functions like return : Monad m => a -> m a where m occurs only in the outputs. There's no input arg you could hang a vtable off of.
- gpderetta 6y agoIn c++ vtables ptrs are attached to object instances (although all instances of the same type vtable), while AFAIK in Haskell "vtables ptrs" (better known as function dictionaries) are attached to object pointers (also known as fat pointers). I believe that rust impl traits behave similarly to Haskell. I think go interfaces also use fat pointers. The advantage is that "interfaces" can be attached to objects non intrusively (and outside of that object declaration, which is a huge win for interoperability), the disadvantage is that pointers are larger and there is no easy way to cross cast between interfaces. In c++ "fat pointers" (also sometimes known are virtual concepts) are often implemented manually, the most well known example is std::function. We are all waiting for compile time reflection to be able to implement virtual concepts generically (you can only approximate it up to a point currently).
- lalaithion 6y agoIf you have Obj f(Obj a, Obj b); in C++, you pass in Obj's vtable twice, and the returned value includes a vtable. If you have f :: Obj a => a -> a -> a in Haskell, you pass in one dictionary. If you have f :: (Obj a, Obj b) => a -> b -> a in Haskell, you pass in two dictionaries. If you have f :: (Obj a, Obj b, Obj c) => a -> b -> c you pass in three dictionaries, and the caller gets to decide what the concrete type of c is, NOT the function implementation. data DynObj = forall c. Obj c => DynObj c f :: (Obj a, Obj b) => a -> b -> DynObj In this case, you pass in two dictionaries and the returned value has a vtable. This essentially is what C++ does.