in ,

Compiling a Functional Language Using C ++, Part 10 – Polymorphism, Hacker News

     [adjacent_group] In part 8 , we wrote some pretty interesting programs in our little language. We successfully expressed arithmetic and recursion. But there’s one thing that we cannot express in our language without further changes: an if (statement.)

Suppose we did not want to add a special [note:There is actually a slight difference between “mutual dependence”the way we defined it and “being in the same group”, andit lies in the symmetric property of an equivalence relation.We defined a function to depend on another function if it callsthat other function. Then, a recursive function depends on itself,but a non-recursive function does not, and therefore does notsatisfy the symmetric property. However, as far as we’re concerned,a function should be in a group with itself even if it’s not recursive. Thus, thereal equivalence relation we use is “in the same group as”, andconsists of “mutual dependence” extended with symmetry.]. (if / else) expression into our language. Thanks to lazy evaluation, we can express it using a function: [vertex.first] defn if cte={     case c of {         True -> {t}         False -> {e}     } } But an issue still remains: so far, our compiler remains monomorphic . That is, a particular function can only have one possible type for each one of its arguments. With our current setup, something like this would not work: [j,i] [vertex.first] (if (if True False True)) 3 This is because, for this to work, both of the following would need to hold (borrowing some of our notation from the typechecking post): [vertex_id] $$ text {if}: text {Bool} rightarrow text {Int} rightarrow text {Int} rightarrow text {Int} $$ $$ text {if}: text {Bool} rightarrow text {Bool} rightarrow text {Bool} rightarrow text {Bool} $$ But using our rules so far, such a thing is impossible, since there is no way for ( text {Int} ) to be unified with ( text {Bool} ). We need a more powerful set of rules to describe our program’s types. Hindley-Milner Type System One such powerful set of rules is the (Hindley-Milner type system) , which we have previously alluded to. In fact, the rules we came up with were already very close to Hindley-Milner, with the exception of two: generalization and (instantiation) . It’s been quite a while since the last time we worked on typechecking, so I’m going to present a table with these new rules, as well as all of the ones that we previously used. [note:The rules aren't quite the same as the ones we used earlier;note that (sigma) is used in place of (tau) in the first rule,for instance. These changes are slight, and we'll talk about how therules work together below.]

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

PMI Project Management Professional – PMP Practice Exams | 100% OFF

Internet Archive offers 1.4 million copyrighted books for free online, Ars Technica

Internet Archive offers 1.4 million copyrighted books for free online, Ars Technica