Comments by "" (@jboss1073) on "Haskell Is Faster Than C | Prime Reacts" video.
-
1
-
1
-
1
-
1
-
1
-
1
-
@AllanSavolainen "so if I have a tree in C made of struct nodes, it isn't typed?"
Correct, it is not typed. C does not have Computer Science Types, it has Computer Science Tags. Types are the elements of Type Theory in Computer Science, where Functions are Paths between the Types.
"Could you give a concrete example of something tree-like that C cannot do?"
Yes, C cannot inherently understand algebraic types such as a variant tree that might have different branches and leaves with varying structures. For example, in languages that support algebraic data types, like OCaml or Haskell, you can define a tree where each node can be either a Leaf with some value or a Branch that connects two subtrees, and the compiler will enforce those distinctions across the entire program. The compiler understands the structure and can infer or enforce the proper type usage without additional effort on your part.
In C, you would need to use structs and manually manage the interpretation of the tree. C doesn’t understand the tree as a singular type—it only sees the individual fields within the structs. You would have to implement all the logic yourself, handling pointers and manually verifying which type of node you're dealing with. There's no automatic pattern matching or type safety for these variant-like structures.
Hence, a concrete example of something tree-like that C cannot do is an algebraic tree where nodes can hold different types of data, such as a variant tree with different node types like Leaf Int, Leaf String, or Branch (Tree, Tree). In a language with algebraic data types, the compiler can enforce that only valid branches and leaves are combined in accordance with the defined type. It will also automatically handle different operations depending on the specific form of the node. C, on the other hand, has no concept of enforcing this; you would need to implement all validation, type checking, and interpretation manually, with no built-in language support for pattern matching or managing the tree in a type-safe manner.
So in C you can never truly get a Tree, which is a well-defined Type in Type Theory within Computer Science - it is defined abstractly solely by its operations. In a language that understands true types, the tree's behavior and structure are inherently tied to the rules of the type system. Operations like traversals, insertions, or transformations are governed by the formal type system, ensuring that the operations remain valid and consistent within the type's definition.
C, however, lacks the mechanisms to define such abstract types. It relies on tags (e.g., struct, enum, int, double), but those are just labels for data or groups of data; they don’t carry the semantic weight of a true type. A "tree" in C is just a collection of pointers and structs with no deeper understanding by the language of what makes a tree a tree. The compiler doesn’t know about the invariants or rules that are essential to the abstract concept of a tree; those are left entirely to the programmer to enforce manually. This is a fundamental difference between a language like C, which deals with memory layout and tagging, and languages with algebraic types that embody the abstract, rule-based nature of true types.
1
-
1