Compiler Construction
Mayer Goldberg \ Ben-Gurion University October 20, 2018
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 1 / 115
Compiler Construction October 20, 2018 Compiler Construction - - PowerPoint PPT Presentation
Compiler Construction October 20, 2018 Compiler Construction October 20, 2018 1 / 115 Mayer Goldberg \ Ben-Gurion University Mayer Goldberg \ Ben-Gurion University Week 1 Goals Agenda Compiler Construction October 20, 2018 2 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 1 / 115
▶ Establishing common language & vocabulary ▶ Understanding the ”big picture”
▶ Some background in programming languages
▶ Abstraction ▶ Dynamic vs Static ▶ Functional vs Imperative languages
▶ Introduction to compiler construction ▶ Introduction to the ocaml programming language
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 2 / 115
▶ Abstraction is a way of moving from a particular to a generality ▶ Abstraction appears mathematics, logic, and in computer science ▶ Abstraction is a force-multiplier, and a great time-saver
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 3 / 115
▶ Abstraction in logic: going from propositions to quantifjed
▶ Abstraction in mathematics: going from expression to function ▶ Abstraction in programming
▶ Functional programming: Similar to abstraction in mathematics ▶ Expressions are abstracted into functions ▶ Functions are abstracted into higher order functions ▶ Collections of functions are abstracted into modules ▶ Modules are abstracted into functors ▶ Modules are abstracted into signatures ▶ Procedural programming ▶ Statements are abstracted into procedures Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 4 / 115
▶ Abstraction in programming (continued)
▶ Object-oriented programming ▶ Objects are abstracted into classes ▶ Classes are abstracted into generics & templates ▶ Classes are abstracted into packages ▶ Logic programming ▶ Similar to abstraction in logic ▶ Propositions are abstracted into predicates ▶ Text generation ▶ Text is abstracted into templates Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 5 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 6 / 115
▶ Dynamic & Static are used in many areas of computer science ▶ In programming languages theory, these terms have a very
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 7 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 8 / 115
▶ Programming languages where
▶ Types exist at compile-time only ▶ Terms exist at run-time only
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 9 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 10 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 11 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 12 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 13 / 115
▶ You’ve already been exposed to functional programming in your
▶ Functional languages are often described as languages lacking in
▶ Imperative languages have side efgects
▶ What can we say positively about functional programming?
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 14 / 115
▶ Computer science is the illegitimate child of mathematics and
▶ Electrical engineering gave us the hardware, the actual machine ▶ Nearly all ideas come from mathematics
▶ Digital electronics: Gates, fmip-fmops, latches, memory, etc ▶ Boolean functions that read their inputs from memory & write
▶ Reading & writing are synchronized using a clock (with a
▶ A fjnite-state machine
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 15 / 115
▶ We cannot design large software systems while thinking at the
▶ We need some theoretical foundations for programming &
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 16 / 115
▶ A language ▶ A set of ideas, notions, defjnitions, techniques, results, all
▶ Programming is based on a computable mathematics ▶ Theoretical computer science uses all of mathematics
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 17 / 115
▶ A language ▶ Notions such as functions, variables, expressions, evaluation,
▶ Operations such as arithmetic, Boolean, structural (e.g., on n
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 18 / 115
▶ Computers can only approximate real numbers ▶ Computers cannot implement infjnite tape (Turing machines) ▶ Mathematical objects are cheaper than objects created on a
▶ Functions are mappings; They take no time! ▶ Knowing that an object exists is often all we need! ▶ Bad things cannot happen: ▶ No exceptions, errors, incorrect results ▶ Nothing is lost, nothing is ”too big” or ”too much” Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 19 / 115
▶ Closer to mathematics ▶ Easier to reason about ▶ Easier to transform ▶ Easier to generate automatically
▶ Farther from mathematics ▶ Harder to reason about ▶ Harder to transform ▶ Harder to generate automatically
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 20 / 115
▶ You teach them a simplifjed version of printf:
▶ Only takes a single string argument ▶ Returns an int: the number of characters in the string
▶ Roughly: printf : string -> int ▶ But the logician objects: He already knows of a function from
▶ strlen : string -> int ▶ He wants to know the difgerence between printf and strlen Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 21 / 115
▶ You: ”Simple, printf also prints the string to the screen!” ▶ Logician: ”What does it mean to print??” ▶ You: ”Seriously?? The printf function prints its argument to
▶ Logician: ”But you said the domain of printf is string -> int,
▶ You: ”Yes, so?” ▶ Logician: ”Then where’s the screen??” ▶ You: ”In front of you!” ▶ Logician: ”Where’s the screen in the domain of the function
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 22 / 115
▶ You: ”It isn’t in the domain. You can think of the screen as a
▶ Logician: ”I have no idea what you mean: How can the screen
▶ You: ”But that’s the whole point of this printing being a side
▶ Logician: ”Well, then printf isn’t a function!” ▶ You: ”Ummm…” ▶ Logician (having a Eureka!-moment): ”I get it now! You got the
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 23 / 115
▶ The real type of printf is string × screen → int × screen ▶ The underlined parts of the type are implicit, i.e., they are not
▶ The implicit parts of the type form the environment ▶ The function call mentions only the explicit arguments ▶ Leaving out the implicit arguments in the function call creates
▶ In fact, nothing has changed: The screen in the domain has
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 24 / 115
▶ Introducing side efgects introduces discrete time ▶ Having introduced time, we must now introduce sequencing:
▶ The notion of sequencing is, like time, illusory:
▶ The screen object in the range of printf("Hello "); is the
▶ So the two printf expressions are nested, and this is why their
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 25 / 115
▶ Closer to the mathematical notions of function, variable,
▶ Nothing is implicit ▶ Easier to reason about ▶ Side efgects are not an explicit part of the language (although
▶ Ofgers many other advantages
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 26 / 115
▶ Farther away from the mathematical notions such as function,
▶ Hides information through the use of implicit arguments (for
▶ Harder to reason about: Contains notions such as side efgects,
▶ Abstraction is harder, prone to errors ▶ Side efgects create implicit, knotty inter-dependencies between
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 27 / 115
▶ Values ⇒ Expressions ⇒ Functions ▶ Higher-order functions ▶ Mathematical operators: mapping, folding, fjltering, partitioning,
▶ The interpreter evaluates expressions
▶ State ⇒ Change ⇒ Commands ⇒ Procedures ▶ Object-oriented programming ▶ Imperative ≡ Based upon commands (imperare means to
▶ The interpreter performs commands
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 28 / 115
▶ There are very few strictly functional languages, i.e., languages
▶ Most languages are quasi-functional: They don’t make it
▶ Most new imperative languages do include features from
▶ anonymous functions (”lambda”) ▶ higher-order functions ▶ modules/namespaces/functors Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 29 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 30 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 31 / 115
▶ L1: Language ▶ L2: Language ▶ PL: A program in language L ▶ Values: A set of values ▶ [
▶ An interpreter is a function IntL : PL → Values ▶ Interpreters map expressions to their values ▶ For example: In functional Scheme, we have
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 32 / 115
▶ An interpreter is a function
▶ Interpreters map the product of an expression and an
▶ The environments are implicit in the imperative language ▶ When the environment in the domain is not equal to the
▶ For example: In imperative Scheme, we have
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 33 / 115
▶ A compiler is a function CompL2 L1 : L1 → L2 ▶ Compilers translate programs from one language to another ▶ Let PL1 ∈ L1, then CompL2 L1[
▶ The correctness of the translation is established using
L1[
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 34 / 115
▶ We may chain any number of compilers together:
L1[
L2[
L1[
L3[
L2[
L1[
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 35 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 36 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 37 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 38 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 39 / 115
▶ Returns the same value (as the original code) under
▶ The interpreter takes less resources to evaluate the code.
▶ Computer memory ▶ Network traffjc ▶ Microprocessor registers
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 40 / 115
▶ Compilers to g-code, a language for CNCs, will try to minimize
▶ Compilers of graphs to visual presentations will try to minimize
▶ Compilers of document-layout languages will try to minimize
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 41 / 115
▶ Good code is hard to write ▶ Some programmers are incompetent ▶ Compilers are faster at detecting opportunities for optimizations ▶ Consistent output: Once debugged, compilers make no mistakes
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 42 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 43 / 115
▶ Code I
▶ less general than Code III ▶ less effjcient than Code II ▶ less maintainable than Code III
▶ Code II
▶ less general than Code I ▶ more effjcient than Code I, Code III ▶ less maintainable than Code I
▶ Code III
▶ more general than Code I, Code II ▶ less effjcient than Code II ▶ more maintainable than Code I, Code II Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 44 / 115
▶ Clearly Code III is better!
▶ It is more general ▶ It is more maintainable ▶ It is more fmexible ▶ Most compilers optimize away such ineffjciency
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 45 / 115
▶ Want to program in a more general, maintainable, fmexible way ▶ Compilation is a point-in-time where generality is traded for
▶ Compilers are opportunistic ▶ Compilers identify opportunities to trade generality for effjciency ▶ The resulting code is unreadable, unmaintainable,
▶ We [normally] don’t touch the compiled code: For
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 46 / 115
▶ Processors running programs written
▶ Interpreters written in L′, running
▶ Compilers written in L′, running on
▶ Programs written in L′, running on L
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 47 / 115
▶ Hardware-implementations of
▶ They have only 1 docking point:
provides lang Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 48 / 115
▶ Programs have at least 1 docking
▶ Mach Lang programs are
▶ Other programs are written in a
machine- language program
runs
program
src lang runs
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 49 / 115
▶ Interpreters come with 3 docking
▶ The language they provide ▶ The language [interpreter] on
▶ The [source] language in which
src lang
provides lang runs
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 50 / 115
▶ Compilers come with 4 docking
▶ The language they compile from ▶ The language they compile to ▶ The language in which they were
▶ The language [interpreter] on
compiling program
compiler
src lang dst lang runs
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 51 / 115
▶ Interpreters & compilers are often composed in complex ways ▶ Diagrams provide a simple, visual way to make sure that the
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 52 / 115
▶ The program must be written in the
▶ The two blocks join naturally
machine- language program
runs
processor
provides lang
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 53 / 115
▶ The program must have been
▶ The two blocks join naturally ▶ We are not saying anything about
program
src lang runs
processor
provides lang
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 54 / 115
▶ Interpreters are similar to processors
▶ They execute/evaluate programs
▶ Interpreters are programs too!
▶ Written in some source language ▶ Compiled into some target
▶ They run on an interpreter: ▶ a processor (hardware) ▶ a program (another interpreter)
program
src lang runs
processor
provides lang src lang
interpreter
provides lang runs
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 55 / 115
▶ Interpreters can execute/evaluate
▶ takes the program (on top) as
▶ outputs the program (on the
▶ It is the target program that is
compiling program
compiler
src lang dst lang runs
program
src lang runs
processor
provides lang src lang
interpreter
provides lang runs
program
src lang runs
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 56 / 115
▶ We may add additional details
▶ the processor/interpreter on
▶ We are still missing details
▶ the compiler that compiled the
▶ the compiled that compiled the
▶ …this can go on!
compiling program
compiler
src lang dst lang runs
program
src lang runs
processor
provides lang src lang
interpreter
provides lang runs
program
src lang runs
processor
provides lang
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 57 / 115
▶ The processor on which the compiler runs is difgerent from the
▶ It crosses the boundaries of architectures. ▶ Java compiler javac is an example of a cross-compiler:
▶ It runs on [e.g.,] x86 ▶ It generates Java-byte-code that runs on the JVM ▶ The JVM is an interpreter (java) running on [e.g.,] x86 Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 58 / 115
▶ Interpreters may be stacked up in a
▶ Towers of interpreters consume
▶ Unless there is a marked
▶ Virtual machines (VMs) can be
▶ IBM mainframe architecture
src lang
interpreter
provides lang runs
processor
provides lang src lang
interpreter
provides lang runs
src lang
interpreter
provides lang runs
program
src lang runs
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 59 / 115
▶ Using previously-written compilers and interpreters, of course! ▶ This process is known as bootstrapping, and it is a specifjc form
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 60 / 115
▶ c1 is an assembler acting as a cross-compiler ▶ c2 already runs on our PC, but it was created on an IBM
▶ All the efgort of writing an assembler (in Pascal) has to be
▶ Any updates, upgrades, bug fjxes, changes, require that we
▶ c3 is essentially c2 compiling itself
▶ With c3, we are free from our old environment: Pascal on IBM
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 61 / 115
▶ With c4 we’re diverging:
▶ c4 is a C compiler ▶ We don’t yet support many features
▶ c5 is a C compiler written in C! Notice that
▶ it is written in an older version of C (v. 0.1) ▶ it supports a newer version of C (v. 0.2)
▶ In writing c6, we fjnally get to use all the language features our
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 62 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 63 / 115
▶ Abstraction ▶ Dynamic vs Static ▶ Functional vs Imperative languages
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 64 / 115
▶ The language in which to write the compiler: ocaml ▶ The language we shall be compiling: Scheme ▶ The language we shall be compiling to: x86/64 assembly
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 65 / 115
▶ ML is a family of statically-typed, quasi-functional programming
▶ The main members of ML are
▶ SML (Standard ML) ▶ ocaml ▶ In Microsoftese, ocaml is called F#… Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 66 / 115
▶ is used all over the world ▶ is used in commercial and open source projects ▶ is powerful, effjcient, convenient, modern, elegant, and has a
▶ supports both functional and object-oriented programming
▶ The ocaml object system is very powerful!
▶ makes it very diffjcult to have run-time errors!
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 67 / 115
▶ Very rich language ▶ Great support for abstractions of various kinds ▶ Great library support: dbms, networking, web programming,
▶ Compiles effjciently, either to bytecode or native
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 68 / 115
▶ Pattern-matching, modules, object-orientation, and types make
▶ Easy to enforce an API
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 69 / 115
▶ https://ocaml.org/, or
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 70 / 115
▶ I run ocaml under GNU Emacs
▶ Create the fjle .ocamlinit in your home directory, and place it
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 71 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 72 / 115
▶ You are free to use ocaml under any editor/environment you like ▶ For example, for ocaml under Eclipse, try OcaIDE at
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 73 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 74 / 115
▶ Ocaml is a functional programming language ▶ Ocaml is interactive ▶ You enter expressions at the prompt, and get their values and
▶ Expressions are separated by ;;
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 75 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 76 / 115
▶ We shall learn about modules later on, as part of the module
▶ In the meantime,modules are ways of aggregating functions &
▶ Functionality in ocaml is managed via loading and using
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 77 / 115
▶ Directives are commands that start with # ▶ You can think of these as meta-commands ▶ Directives tell you about the system
▶ #list;; to list available modules ▶ #cd <string>;; to change to a directory ▶ #require <string>;; to specify that a module is required ▶ #show_module <module>;; to see the signature of the module ▶ #trace <function>;; to trace a function ▶ #untrace <function>;; to untrace a function
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 78 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 79 / 115
▶ The module Pervasives contains all the ”builtin” procedures in
▶ Try executing #show_module Pervasives;; and see what you
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 80 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 81 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 82 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 83 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 84 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 85 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 86 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 87 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 88 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 89 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 90 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 91 / 115
▶ lists of integers ▶ lists of strings ▶ lists of user-defjned data-types
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 92 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 93 / 115
▶ 'a is called alpha and is often written using α ▶ 'b is called beta and is often written using β ▶ 'c is called gamma and is often written using γ ▶ … etc.
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 94 / 115
▶ With non-empty lists, ocaml can fjgure out the type of the list
▶ With empty lists, ocaml is unable to fjgure out the type of the
▶ You may specify the type of α:
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 95 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 96 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 97 / 115
▶ Syntax for named functions ▶ Syntax for anonymous functions ▶ Syntax for functions with pattern-matching ▶ Syntax for recursive functions ▶ Syntax for mutually recursive functions
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 98 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 99 / 115
▶ You don’t ordinarily need parenthesis ▶ It’s not an error to have unneeded parenthesis ▶ Sometimes it’s really needed!
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 100 / 115
▶ Syntax for anonymous functions ▶ Syntax for functions with pattern-matching ▶ Syntax for recursive functions ▶ Syntax for mutually recursive functions
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 101 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 102 / 115
▶ Syntax for functions with pattern-matching ▶ Syntax for recursive functions ▶ Syntax for mutually recursive functions
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 103 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 104 / 115
▶ Syntax for recursive functions ▶ Syntax for mutually recursive functions
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 105 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 106 / 115
▶ Syntax for mutually recursive functions
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 107 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 108 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 109 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 110 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 111 / 115
▶ A function is a subset of the Cartesian product of a domain-set
▶ Suppose the domain is itself a Cartesian product:
▶ Then f ⊆ ((D1 × · · · × Dn) × R). ▶ The structure ((D1 × · · · × Dn) × R) is isomorphic to
▶ The structure D1 × (D2 · · · × (Dn × R) · · · ) is the domain of a
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 112 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 113 / 115
▶ In applications, Ocaml Curries arguments naturally. This means
▶ Parameters to named functions Curry naturally. For example:
▶ To avoid Currying, you may pass tuples: In C, f(x, y, z) is a
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 114 / 115
Mayer Goldberg \ Ben-Gurion University Compiler Construction October 20, 2018 115 / 115