compiler construction
play

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


  1. Imperative vs functional languages ( continued ) Example of non-mathematical ideas: Side efgects Imagine having to teach C programming to a logician string -> int that returns the number of characters in the string Compiler Construction October 20, 2018 21 / 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

  2. Imperative vs functional languages ( continued ) right?” October 20, 2018 Compiler Construction printf !” Side efgects: The Dialogue 22 / 115 the screen & also returns the number of characters it printed!” ▶ 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

  3. Imperative vs functional languages ( continued ) Side efgects: The Dialogue ( continued ) October 20, 2018 Compiler Construction domain of printf all wrong!” efgect : It is not expressed in the type!” not expressed in the domain of the function??” be a variable when it’s not passed as a parameter, and its type is global variable .” 23 / 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

  4. Imperative vs functional languages ( continued ) an illusion of change, as if the environment has changed: An October 20, 2018 Compiler Construction been mapped into another screen in the range. the function call Side efgects from a logical point of view 24 / 115 written explicitly in the original type given for printf ▶ 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 illusory notion of time has been created; We have the environment before the function call, and the environment after ▶ In fact, nothing has changed: The screen in the domain has Mayer Goldberg \ Ben-Gurion University

  5. Imperative vs functional languages ( continued ) Side efgects from a logical point of view ( continued ) October 20, 2018 Compiler Construction ”ordering” matters! screen object in the domain of printf("world!\n"); 25 / 115 } printf("world!\n"); printf("Hello "); { ▶ 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

  6. Imperative vs functional languages ( continued ) Functional programming languages computation they are used to implement it on a real computer!) Compiler Construction October 20, 2018 26 / 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

  7. Imperative vs functional languages ( continued ) Imperative programming languages variable, evaluation convenience) sequences , environment , etc., that require translation before we can reason about them various parts of a software system, making it harder to debug Compiler Construction October 20, 2018 27 / 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

  8. Imperative vs functional languages ( continued ) Abstraction in imperative programming languages October 20, 2018 Compiler Construction command in Latin) Abstraction in functional programming languages 28 / 115 etc ▶ 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

  9. Programming paradigms: A reality check in which side-efgects cannot be expressed impossible to use side-efgects, but they don’t make these easy or fun to use functional languages Compiler Construction October 20, 2018 29 / 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

  10. Imperative vs functional languages ( continued ) Question Languages based on the functional paradigm — Compiler Construction October 20, 2018 30 / 115 👏 do not allow us to perform side efgects 👏 do not support object-oriented programming 👏 are not suited to model and solve ”real” problems 👏 are slow and ineffjcient 👎 support high order functions Mayer Goldberg \ Ben-Gurion University

  11. Further Reading http://blog.jenkster.com/2015/12/what-is-functional-programming.html , or Compiler Construction October 20, 2018 31 / 115 🔘 Comparing programming paradigms 🔘 What is functional programming at Mayer Goldberg \ Ben-Gurion University

  12. Introduction to compiler construction Interpreters evaluate/perform October 20, 2018 Compiler Construction The functional picture (evaluate) 32 / 115 ▶ L 1 : Language ▶ L 2 : Language ▶ P L : A program in language L ▶ Values: A set of values ▶ [ [ · ] ] : Semantic brackets ▶ An interpreter is a function Int L : P L → Values ▶ Interpreters map expressions to their values ▶ For example: In functional Scheme, we have Int Scheme [ [(+ 3 5)] ] = 8 Mayer Goldberg \ Ben-Gurion University

  13. Introduction to compiler construction environment in the range, an illusion of change has been October 20, 2018 Compiler Construction Interpreters evaluate/perform created: ”Something changed in the environment” The imperative picture (perform) environment to the product of a value and an environment 33 / 115 ▶ An interpreter is a function Int L : L × Environment → Values × Environment ▶ 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 Int Scheme [ [ ⟨ ( defjne x 3) , { x �→ undefjned }⟩ ] ] = ⟨ # ⟨ void ⟩ , { x �→ 3 }⟩ Mayer Goldberg \ Ben-Gurion University

  14. Introduction to compiler construction interpreters for both the source and target language: October 20, 2018 Compiler Construction Compilers translate 34 / 115 L 1 : L 1 → L 2 ▶ A compiler is a function Comp L 2 ▶ Compilers translate programs from one language to another ▶ Let P L 1 ∈ L 1 , then Comp L 2 L 1 [ [ P L 1 ] ] ∈ L 2 ▶ The correctness of the translation is established using Int L 1 [ [ P L 1 ] ] = Int L 2 [ [ Comp L 2 L 1 [ [ P L 1 ] ]] ] Mayer Goldberg \ Ben-Gurion University

  15. Introduction to compiler construction Compilers translate ( continued ) October 20, 2018 Compiler Construction 35 / 115 ▶ We may chain any number of compilers together: Int L 1 [ [ P L 1 ] ] = Int L 2 [ [ Comp L 2 L 1 [ [ P L 1 ] ]] ] = Int L 3 [ [ Comp L 3 L 2 [ [ Comp L 2 L 1 [ [ P L 1 ] ]] ]] ] = Int L 4 [ [ Comp L 4 L 3 [ [ Comp L 3 L 2 [ [ Comp L 2 L 1 [ [ P L 1 ] ]] ]] ]] ] = . . . etc. Mayer Goldberg \ Ben-Gurion University

  16. Introduction to compiler construction Question Compiled code is generally faster than interpreted code. So why do we need interpreters? Why not stick with compiled code? Compiler Construction October 20, 2018 36 / 115 👏 It’s easier to program in interpreted languages 👏 It’s easier to debug interpreted code 👏 Interpreters are more fmexible than compilers 👏 The difgerence is pretty much a matter of personal taste 👎 Interpreters are the only way to reach values Mayer Goldberg \ Ben-Gurion University

  17. Introduction to compiler construction Question When we program directly in machine language, where’s the interpreter? underlying interpretation interpreter for the given micro-architecture Compiler Construction October 20, 2018 37 / 115 👏 The operating system is the interpreter 👏 The shell is the interpreter 👏 There is no interpreter 👏 The process of translating code to machine language is the 👎 The microprocessor is a hardware implementation of an Mayer Goldberg \ Ben-Gurion University

  18. Introduction to compiler construction Question If interpreters are a logical necessity, why do we need a compiler? Compiler Construction October 20, 2018 38 / 115 👏 For generality 👏 For ease 👏 For fmexibility 👏 For portability 👎 For optimizations Mayer Goldberg \ Ben-Gurion University

  19. Introduction to compiler construction Another way of looking at the question A compiler from language L to language L (itself!) is… …just an optimizer! Compiler Construction October 20, 2018 39 / 115 Mayer Goldberg \ Ben-Gurion University

  20. What is an optimization Resources include: October 20, 2018 Compiler Construction or any other resource consumed by the code A compiler optimization is a semantics-preserving transformation that 40 / 115 More effjcient code interpretation results in more effjcient code Semantics-preserving ( ≡ meaning-preserving) ▶ Returns the same value (as the original code) under ▶ The interpreter takes less resources to evaluate the code. ☞ Execution time ▶ Computer memory ▶ Network traffjc ▶ Microprocessor registers Mayer Goldberg \ Ben-Gurion University

  21. What is an optimization ( continued ) Special-purpose compilers may optimize for non-standard ”resources”: motion crossovers of edges ”widows”, ”orphans”, hyphenations, and other typographically problematic conditions Compiler Construction October 20, 2018 41 / 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

  22. What is an optimization ( continued ) Question Why do programmers write less-than-optimized code? What is the rationale for having compilers be in charge of optimizations? Are good programmers really that rare?? All these reasons are true, but irrelevant! intentionally write less-effjcient code? Compiler Construction October 20, 2018 42 / 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 ☞ Can you envision a reason why a programmer might Mayer Goldberg \ Ben-Gurion University

  23. What is an optimization ( continued ) Which code is better: Code I for (i = 0; i < 200; ++i) { A[i] = 0; } for (i = 0; i < 200; ++i) { B[i] = 0; } Code II for (i = 0; i < 200; ++i) { A[i] = 0; B[i] = 0; } Code III #define M 200 ... for (i = 0; i < M; ++i) { A[i] = 0; } for (i = 0; i < M; ++i) { B[i] = 0; } Compiler Construction October 20, 2018 43 / 115 Mayer Goldberg \ Ben-Gurion University

  24. What is an optimization ( continued ) Analysis October 20, 2018 Compiler Construction 44 / 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

  25. What is an optimization ( continued ) So which code is better? FACT: Optimizing the loops in Code III, converting it to Code II, is performed by most compilers today Conclusion People write less-than-optimal code because: Compiler Construction October 20, 2018 45 / 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

  26. What is an optimization ( continued ) How people use compilers October 20, 2018 Compiler Construction obtain effjcient code. unmaintainable, overly-specifjc, machine-dependent code in order to Without optimizing compilers, we would be forced to write 46 / 115 effjciency: ▶ 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, machine-specifjc, and fast ▶ We [normally] don’t touch the compiled code: For programmers, the quality of only the source code matters Mayer Goldberg \ Ben-Gurion University

  27. Composing & combining processors, interpreters, compilers, & programs October 20, 2018 Compiler Construction for you! We have 5 new blocks Remember Lego? languages!) 47 / 115 into equivalent programs written in programs written in language L in language L We can compose ▶ Processors running programs written ▶ Interpreters written in L ′ , running ▶ Compilers written in L ′ , running on L , compile programs written in L ′′ L ′′′ (we are talking about 4 ▶ Programs written in L ′ , running on L Mayer Goldberg \ Ben-Gurion University

  28. Composing & combing processors, interpreters, compilers, & programs A processor interpreters for some language The languages they provide What it looks like Compiler Construction October 20, 2018 48 / 115 provides ▶ Hardware-implementations of lang processor ▶ They have only 1 docking point: Mayer Goldberg \ Ben-Gurion University

  29. Composing & combing processors, interpreters, source language and compiled to October 20, 2018 Compiler Construction compilers, & programs additional docking point compiler. Such programs have an some other language using a What it looks like 49 / 115 binary. This is hardly ever done hand-written directly in hex or point: The language they run on A program these days ▶ Programs have at least 1 docking machine- ▶ Mach Lang programs are runs language on program ▶ Other programs are written in a program lang runs src on Mayer Goldberg \ Ben-Gurion University

  30. Composing & combing processors, interpreters, they were written October 20, 2018 Compiler Construction compilers, & programs What it looks like 50 / 115 which they run points: An interpreter ▶ Interpreters come with 3 docking provides lang ▶ The language they provide interpreter ▶ The language [interpreter] on lang runs src on ▶ The [source] language in which Mayer Goldberg \ Ben-Gurion University

  31. Composing & combing processors, interpreters, written October 20, 2018 Compiler Construction compilers, & programs What it looks like which they run 51 / 115 points: A compiler ▶ Compilers come with 4 docking compiling program ▶ The language they compile from compiler ▶ The language they compile to ▶ The language in which they were runs lang lang src dst on ▶ The language [interpreter] on Mayer Goldberg \ Ben-Gurion University

  32. Composing & combing processors, interpreters, compilers, & programs Why bother with these pics?? compositions are correct Compiler Construction October 20, 2018 52 / 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

  33. Composing & combing processors, interpreters, What it looks like October 20, 2018 Compiler Construction compilers, & programs 53 / 115 by the processor same machine-language interpreted running A machine language program machine- runs language on ▶ The program must be written in the program provides lang processor ▶ The two blocks join naturally Mayer Goldberg \ Ben-Gurion University

  34. Composing & combing processors, interpreters, the language the program was October 20, 2018 Compiler Construction compilers, & programs What it looks like written in (though we could!) 54 / 115 processor machine-language interpreted by the compiled into the same A compiled program running ▶ The program must have been program lang runs src on provides lang ▶ The two blocks join naturally processor ▶ We are not saying anything about Mayer Goldberg \ Ben-Gurion University

  35. Composing & combing processors, interpreters, language October 20, 2018 Compiler Construction compilers, & programs What it looks like 55 / 115 An interpreter running a compiled in a given language program (I) program ▶ Interpreters are similar to processors lang runs src on ▶ They execute/evaluate programs provides lang ▶ Interpreters are programs too! interpreter ▶ Written in some source language lang runs src on ▶ Compiled into some target provides lang ▶ They run on an interpreter: processor ▶ a processor (hardware) ▶ a program (another interpreter) Mayer Goldberg \ Ben-Gurion University

  36. Composing & combing processors, interpreters, source, and October 20, 2018 Compiler Construction compilers, & programs What it looks like executed right) as target 56 / 115 compiler An interpreter running a compiled another language. Note that the programs that were compiled from program (II) ▶ Interpreters can execute/evaluate program lang runs src on compiling program compiler program lang src runs lang dst lang src runs on on ▶ takes the program (on top) as provides lang interpreter lang runs src on ▶ outputs the program (on the provides lang processor ▶ It is the target program that is Mayer Goldberg \ Ben-Gurion University

  37. Composing & combing processors, interpreters, interpreter October 20, 2018 Compiler Construction compilers, & programs What it looks like compiler 57 / 115 An interpreter running a compiled which the compiler executes. program (III) ▶ We may add additional details program lang runs src on ▶ the processor/interpreter on compiling program compiler program lang src runs lang dst lang src runs on on ▶ We are still missing details provides provides lang lang ▶ the compiler that compiled the processor interpreter lang runs src on provides lang ▶ the compiled that compiled the processor ▶ …this can go on! Mayer Goldberg \ Ben-Gurion University

  38. Composing & combing processors, interpreters, compilers, & programs Cross-compilers one one which the [compiled] program runs Compiler Construction October 20, 2018 58 / 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

  39. Composing & combing processors, interpreters, tower! October 20, 2018 Compiler Construction compilers, & programs What it looks like actually does this! stacked up as towers of interpreters 59 / 115 slowdown, this is not really a Towers of interpreters height of the tower tower ▶ Interpreters may be stacked up in a program lang src runs on provides ▶ Towers of interpreters consume lang interpreter resources that are exponential to the lang runs src on provides lang ▶ Unless there is a marked interpreter lang runs src on provides lang interpreter ▶ Virtual machines (VMs) can be lang runs src on provides lang ▶ IBM mainframe architecture processor Mayer Goldberg \ Ben-Gurion University

  40. Composing & combing processors, interpreters, compilers, & programs How are compilers and interpreters made? of composing & combining compilers and interpreters… Compiler Construction October 20, 2018 60 / 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

  41. Bootstrapping (I) x86 asm October 20, 2018 Compiler Construction mainframe! detach from the mainframe! re-done from scratch in x86 assembly language if we are to mainframe x86 exe x86 asm x86 c 2 compiler c 3 x86 exe x86 asm x86 c 1 written in compiled by runs on compiles outputs c 1 61 / 115 pascal ibm pascalvs ibm 370 x86 asm x86 exe c 2 x86 asm ▶ c 1 is an assembler acting as a cross-compiler ▶ c 2 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 re-compile c 2 on the mainframe! ▶ c 3 is essentially c 2 compiling itself ▶ With c 3 , we are free from our old environment: Pascal on IBM Mayer Goldberg \ Ben-Gurion University

  42. Bootstrapping (II) C (v. 0.2) C (v. 0.1) compiler x86 C (v. 0.2) x86 exe c 6 c 5 x86 exe x86 C (v. 0.2) x86 exe compiler supports! Compiler Construction October 20, 2018 c 5 c 4 C (v. 0.1) x86 asm written in compiled by runs on x86 outputs c 3 compiles c 2 x86 x86 asm x86 exe c 4 x86 asm c 3 62 / 115 ▶ With c 4 we’re diverging: ▶ c 4 is a C compiler ▶ We don’t yet support many features ▶ c 5 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 c 6 , we fjnally get to use all the language features our Mayer Goldberg \ Ben-Gurion University

  43. Further Reading Introduction) Compiler Construction October 20, 2018 63 / 115 🕯 Modern compiler design (2nd edition), Page 1 (Section 1: 🔘 Self hosting 🔘 Bootstrapping 🔘 Interpreters Mayer Goldberg \ Ben-Gurion University

  44. Week 1 Goals Agenda Compiler Construction October 20, 2018 64 / 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

  45. Languages used in this course In this course, we shall be working with 3 languages: language Compiler Construction October 20, 2018 65 / 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

  46. Introduction to ocaml (1) languages Compiler Construction October 20, 2018 66 / 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

  47. What kind of language is ocaml Ocaml — rich toolset Compiler Construction October 20, 2018 67 / 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

  48. The advantages of learning ocaml system programming, etc Compiler Construction October 20, 2018 68 / 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 ☞ We’ll actually check and grade your work! 😊 Mayer Goldberg \ Ben-Gurion University

  49. Why we are using ocaml in the course programming sophisticated, abstract, clean, easy, elegant, re-usable, safe Compiler Construction October 20, 2018 69 / 115 ▶ Pattern-matching, modules, object-orientation, and types make ▶ Easy to enforce an API Mayer Goldberg \ Ben-Gurion University

  50. Getting, installing & using ocaml Compiler Construction October 20, 2018 70 / 115 ▶ https://ocaml.org/ , or Mayer Goldberg \ Ben-Gurion University

  51. Getting, installing & using ocaml ( https://www.gnu.org/software/emacs/ ) which is the best text editor. Period. in the line: #use "topfind";; This will load some basic tools you will want to use. Compiler Construction October 20, 2018 71 / 115 ▶ I run ocaml under GNU Emacs ▶ Create the fjle .ocamlinit in your home directory, and place it Mayer Goldberg \ Ben-Gurion University

  52. Getting, installing & using ocaml Compiler Construction October 20, 2018 72 / 115 Mayer Goldberg \ Ben-Gurion University

  53. Getting, installing & using ocaml http://www.algo-prog.info/ocaide/ , or Compiler Construction October 20, 2018 73 / 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

  54. Further Reading Whitington Yaron Minsky & Anil Madhavapeddy http://caml.inria.fr/pub/docs/manual-ocaml/ , or Compiler Construction October 20, 2018 74 / 115 🕯 OCaml from the Very Beginning, by John Whitington 🕯 More OCaml: Algorithms, Methods & Diversions, by John 🕯 Real World OCaml: Functional programming for the masses, by 🕯 Practical OCaml, by Joshua B. Smith 🔘 The online manual at Mayer Goldberg \ Ben-Gurion University

  55. Expressions & types Compiler Construction October 20, 2018 75 / 115 ▶ Ocaml is a functional programming language ▶ Ocaml is interactive ▶ You enter expressions at the prompt, and get their values and their types ▶ Expressions are separated by ;; Mayer Goldberg \ Ben-Gurion University

  56. Expressions & types An interaction at the ocaml prompt: # 3;; - : int = 3 # "asdf";; - : string = "asdf" # 'm';; - : char = 'm' # 3.1415;; - : float = 3.1415 # [1; 2; 3; 5; 8; 13];; - : int list = [1; 2; 3; 5; 8; 13] Compiler Construction October 20, 2018 76 / 115 Mayer Goldberg \ Ben-Gurion University

  57. What’s available?? Modules system variables, while controlling their visibility modules. Compiler Construction October 20, 2018 77 / 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

  58. What’s available?? Directives Useful directives Compiler Construction October 20, 2018 78 / 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

  59. What’s available?? What modules are available? Hashtbl.iter (fun k _v -> print_endline k) Toploop.directive_table;; This is nasty! We can defjne a function to do that: let directives () = Hashtbl.iter (fun k _v -> print_endline k) Toploop.directive_table;; and now we can just run directives();; to see the list of directives. Compiler Construction October 20, 2018 79 / 115 Mayer Goldberg \ Ben-Gurion University

  60. What’s available? exn -> 'a = "%raise_notrace" October 20, 2018 Compiler Construction ... exception Exit val failwith : string -> 'a val invalid_arg : string -> 'a external raise_notrace : Pervasives external raise : exn -> 'a = "%raise" sig module Pervasives : get! ocaml. 80 / 115 ▶ The module Pervasives contains all the ”builtin” procedures in ▶ Try executing #show_module Pervasives;; and see what you Mayer Goldberg \ Ben-Gurion University

  61. Integers in ocaml # 1 + 2;; - : int = 3 # 3 * 4;; - : int = 12 # 8 / 3;; - : int = 2 # 8 mod 3;; - : int = 2 Compiler Construction October 20, 2018 81 / 115 Mayer Goldberg \ Ben-Gurion University

  62. Read your error messages! cos(3);; October 20, 2018 Compiler Construction was expected of type float Error: This expression has type int but an expression ^^^ Characters 3-6: # 1.2 + 3.4;; # cos(3);; was expected of type int Error: This expression has type float but an expression ^^^ 1.2 + 3.4;; Characters 0-3: 82 / 115 Mayer Goldberg \ Ben-Gurion University

  63. Floating-point numbers in ocaml Operators take a . after them to denote fmoating-point ops: # 3.4 +. 4.5;; - : float = 7.9 # 3.2 *. 5.1;; - : float = 16.32 # cos(2.0);; - : float = -0.416146836547142407 Compiler Construction October 20, 2018 83 / 115 Mayer Goldberg \ Ben-Gurion University

  64. Booelans in ocaml - : bool = true October 20, 2018 Compiler Construction - : bool = true # 3 != 4;; - : bool = false # 3 = 4;; # 3 = 3;; # true;; - : bool = false # false || false;; - : bool = false # true && false;; - : bool = false # false;; - : bool = true 84 / 115 Mayer Goldberg \ Ben-Gurion University

  65. Read your error messages! if 4 = 4 then 123 else "moshe";; October 20, 2018 Compiler Construction was expected of type int Error: This expression has type string but an expression ^^^^^^^ Characters 23-30: In ocaml, unlike in Scheme, the then -clause and else -clause of # if 4 = 4 then 123 else "moshe";; - : string = "yosi" # if 3 = 4 then "moshe" else "yosi";; - : int = 123 # if 3 = 3 then 123 else 456;; if -expressions must be of the same type! 85 / 115 Mayer Goldberg \ Ben-Gurion University

  66. Bitwise Boolean functions over the integers # 5 land 3;; - : int = 1 # 8 lor 3;; - : int = 11 # 5 lxor 3;; - : int = 6 Compiler Construction October 20, 2018 86 / 115 Mayer Goldberg \ Ben-Gurion University

  67. Characters in ocaml # '\"';; October 20, 2018 Compiler Construction - : char = 'A' # '\065';; - : char = '"' - : char = '\\' # '*';; # '\\';; - : char = '\n' # '\n';; - : char = '\t' # '\t';; - : char = '*' 87 / 115 Mayer Goldberg \ Ben-Gurion University

  68. Strings in ocaml # "moshe!";; - : string = "moshe!" # "moshe\n";; - : string = "moshe\n" # "hello" ^ " " ^ "world";; - : string = "hello world" # "moshe".[3];; - : char = 'h' #show_module String;; will show you what string functions are available Compiler Construction October 20, 2018 88 / 115 Mayer Goldberg \ Ben-Gurion University

  69. Conversion & coercion of types # char_of_int 97;; - : char = 'a' # int_of_char 'A';; - : int = 65 Chars in ocaml are ASCII, not Unicode! # char_of_int 1488;; Exception: Invalid_argument "char_of_int". Compiler Construction October 20, 2018 89 / 115 ☞ There is Unicode support in ocaml (later!) Mayer Goldberg \ Ben-Gurion University

  70. Conversion & coercion of types # int_of_string "1234";; - : int = 1234 # int_of_string "12+34";; Exception: Failure "int_of_string". # string_of_int 496351;; - : string = "496351" # float_of_string "123.456";; - : float = 123.456 # string_of_float 3.1415927;; - : string = "3.1415927" Compiler Construction October 20, 2018 90 / 115 Mayer Goldberg \ Ben-Gurion University

  71. Tuples in ocaml # (3, 4, 5);; - : int * int * int = (3, 4, 5) # (3, "blind", "mice");; - : int * string * string = (3, "blind", "mice") # ();; - : unit = () Compiler Construction October 20, 2018 91 / 115 Ocaml supports ordered n -tuples. If e 1 : τ 1 , . . . e n : τ n , then ⟨ e 1 , . . . , e n ⟩ : ( τ 1 × · · · × τ n ) . The ordered 0 -tuple is possible too, and its type is unit : Mayer Goldberg \ Ben-Gurion University

  72. Lists in ocaml # [2; 3; 5; 8; 13];; October 20, 2018 Compiler Construction - : bool list = [true; false; false; false; true] # [true; false; false; false; true];; - : int list = [2; 3; 5; 8; 13] etc. Ocaml supports lists as a builtin data type. Lists are lists of some type : 92 / 115 For a type α , A list of type α list is either empty, or it contains something of type α , followed by an α list . ▶ lists of integers ▶ lists of strings ▶ lists of user-defjned data-types Mayer Goldberg \ Ben-Gurion University

  73. Lists in ocaml Elements in a list must all belong to the same type: # [true; 234; "moshe!"];; Characters 7-10: [true; 234; "moshe!"];; ^^^ Error: This expression has type int but an expression was expected of type bool This is difgerent from lists in dynamic languages (Scheme, Racket, Prolog, Python, etc). Compiler Construction October 20, 2018 93 / 115 Mayer Goldberg \ Ben-Gurion University

  74. Lists in ocaml The empty list has an interesting type: # [];; - : 'a list = [] In ocaml, Compiler Construction October 20, 2018 94 / 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. The expressions 'a , 'b , 'c , etc., are known as type variables Mayer Goldberg \ Ben-Gurion University

  75. Lists in ocaml # ([] : string list);; October 20, 2018 Compiler Construction - : int list list list = [] # ([] : int list list list);; - : string list = [] - : int list = [] # ([] : int list);; of [] . list. This is why you see the type variable unresolved in the type based on the type of the elements in the list. 95 / 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

  76. Read your error messages! Specifying the type is not the same as casting in C/C++/Java: # (2.345 : float);; - : float = 2.345 # (2 : float);; Characters 1-2: (2 : float);; ^ Error: This expression has type int but an expression was expected of type float generate corresponding data in another type. Compiler Construction October 20, 2018 96 / 115 There is no casting in ocaml, but there are procedures you can call to Mayer Goldberg \ Ben-Gurion University

  77. Lists in ocaml Working with lists: Adding an element to a list: # 3 :: [4; 5; 6];; - : int list = [3; 4; 5; 6] Appending elements to a list: # [2; 3] @ [5; 8; 13];; - : int list = [2; 3; 5; 8; 13] #show_module List;; will show you what more is available Compiler Construction October 20, 2018 97 / 115 Mayer Goldberg \ Ben-Gurion University

  78. Functions in ocaml Overview Compiler Construction October 20, 2018 98 / 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

  79. Functions in ocaml To defjne the function square , that takes an integer argument and October 20, 2018 Compiler Construction - : int = 0 # square 0;; - : int = 1156 # square(34);; - : int = 54756 # square 234;; - : int -> int = <fun> # square;; We can now use square as any builtin function: val square : int -> int = <fun> # let square n = n * n;; returns its square , we defjne: 99 / 115 Mayer Goldberg \ Ben-Gurion University

  80. Read your error messages! Error: This expression has type int -> int October 20, 2018 Compiler Construction - : int = 54756 # square (-234);; but an expression was expected of type int ^^^^^^ square -234;; Characters 0-6: # square -234;; - : int = 15129 # square ((((((123))))));; 100 / 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend