Question: Before we write the parser, let's discuss a few more features of MicroCaml that go beyond the syntax. For one thing, MicrOCaml is dynamically typed.

 Before we write the parser, let's discuss a few more features

Before we write the parser, let's discuss a few more features of MicroCaml that go beyond the syntax. For one thing, MicrOCaml is dynamically typed. This isn't necessarily obvious from the syntax (even though there's no syntax for types): remember, because of type inference, you can write an entire OCaml program without writing down any types, but the language is still statically typed. So why is MicrOCaml dynamically typed? Well, because we haven't written a type checker. This may seem like a silly answer, but it's more or less correct: we could write a type checker for MicrOCaml and make it a statically typed language. Instead, we choose to allow all syntactically valid programs and allow type errors to arise at runtime this makes it dynamically typed. It also leads to some interesting results, as we'll now see. We've provided several example MicrOCaml programs in the examples folder. Take a look at these to get a sense of how to write code in the language. You may also want to write a couple of your own programs for testing. MicrOCaml may seem very simple, but it is actually incredibly powerful?. One somewhat surprising feature is that, even though there is no syntax for let rec, we can still write recursive functions. The program prog4. uml is an infinite loop. The program fact.uml contains a factorial function (as written, the program calculates 5!, but you can change this by editing the last line). You don't really need to understand how or why this works right now (we'll cover it in class in a few weeks), but you're welcome to take some time to puzzle it out if you want. As a hint, the fact that MicrOCaml is dynamically typed is important here. Bonus Task 2.5 (Written, 1 points). Why can't we write prog4.uml in regular OCaml? Hint: Think about what type f would have. We also have not written an interpreter for MicrOCaml. This is not a principled decision so much as a logistical one: you're going to write this interpreter on the next assignment, so we can't give you the code for it now :). Instead, we've done the next best thing and given you a compiler from MicrOCaml to another dynamically typed functional language, Python2. Once you've written the parser (following these instructions now will just raise the ImplementMe excep- tion), you can compile the compiler by running make, or if you don't have make: ocamlc -o microml types.ml parse.ml print.ml topy.ml main.ml Then run the compiler with ./microml .uml where .uml is a MicrOCaml program (one of the examples we gave you or one you write yourself). The program will produce .py, which will contain entirely unreadable, but working, Python code. You can run it with python3 .py if you have Python 3 installed, or copy and paste it into an online Python interpreter, such as https: //www.python.org/shell/. Before we write the parser, let's discuss a few more features of MicroCaml that go beyond the syntax. For one thing, MicrOCaml is dynamically typed. This isn't necessarily obvious from the syntax (even though there's no syntax for types): remember, because of type inference, you can write an entire OCaml program without writing down any types, but the language is still statically typed. So why is MicrOCaml dynamically typed? Well, because we haven't written a type checker. This may seem like a silly answer, but it's more or less correct: we could write a type checker for MicrOCaml and make it a statically typed language. Instead, we choose to allow all syntactically valid programs and allow type errors to arise at runtime this makes it dynamically typed. It also leads to some interesting results, as we'll now see. We've provided several example MicrOCaml programs in the examples folder. Take a look at these to get a sense of how to write code in the language. You may also want to write a couple of your own programs for testing. MicrOCaml may seem very simple, but it is actually incredibly powerful?. One somewhat surprising feature is that, even though there is no syntax for let rec, we can still write recursive functions. The program prog4. uml is an infinite loop. The program fact.uml contains a factorial function (as written, the program calculates 5!, but you can change this by editing the last line). You don't really need to understand how or why this works right now (we'll cover it in class in a few weeks), but you're welcome to take some time to puzzle it out if you want. As a hint, the fact that MicrOCaml is dynamically typed is important here. Bonus Task 2.5 (Written, 1 points). Why can't we write prog4.uml in regular OCaml? Hint: Think about what type f would have. We also have not written an interpreter for MicrOCaml. This is not a principled decision so much as a logistical one: you're going to write this interpreter on the next assignment, so we can't give you the code for it now :). Instead, we've done the next best thing and given you a compiler from MicrOCaml to another dynamically typed functional language, Python2. Once you've written the parser (following these instructions now will just raise the ImplementMe excep- tion), you can compile the compiler by running make, or if you don't have make: ocamlc -o microml types.ml parse.ml print.ml topy.ml main.ml Then run the compiler with ./microml .uml where .uml is a MicrOCaml program (one of the examples we gave you or one you write yourself). The program will produce .py, which will contain entirely unreadable, but working, Python code. You can run it with python3 .py if you have Python 3 installed, or copy and paste it into an online Python interpreter, such as https: //www.python.org/shell/

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!