diff --git a/README.md b/README.md index 9d2661e..3d2371f 100644 --- a/README.md +++ b/README.md @@ -45,3 +45,23 @@ Text in parentheses is comments. Letters are variables: stand for anything.
Try to use only the connective you define in its definition. TODO
+ + + + + + + + + + + + + + + + + + + + diff --git a/chapter1/functional-lecture01.md b/chapter1/functional-lecture01.md index 154d623..71700f9 100644 --- a/chapter1/functional-lecture01.md +++ b/chapter1/functional-lecture01.md @@ -173,8 +173,9 @@ about **polymorphism.*** Type definitions we have seen above are *global*: they its own set of operators. For example, +, *, / work for intigers, while +., *., /. work for floating point numbers. **Exception:** comparisons <, =, etc. work for all values other than functions. - - ## 4 Exercises + + +## 4 Exercises Exercises from *Think OCaml. How to Think Like a Computer Scientist* by Nicholas Monje and Allen Downey. diff --git a/chapter2/functional-lecture02-deriv1.md b/chapter2/functional-lecture02-deriv1.md index 8aee54c..4517c6d 100644 --- a/chapter2/functional-lecture02-deriv1.md +++ b/chapter2/functional-lecture02-deriv1.md @@ -1,4 +1,4 @@ -# Lecture 2: Algebra, Fig. 1 +# Lecture 2: Algebra Type inference example derivation diff --git a/dune b/dune index d033d40..0dcc01b 100644 --- a/dune +++ b/dune @@ -23,15 +23,25 @@ "\n") (echo "# Curious OCaml\n") (cat chapter1/README.md) + (echo "\n\n") (cat chapter2/README.md) + (echo "\n\n") (cat chapter3/README.md) + (echo "\n\n") (cat chapter4/README.md) + (echo "\n\n") (cat chapter5/README.md) + (echo "\n\n") (cat chapter6/README.md) + (echo "\n\n") (cat chapter7/README.md) + (echo "\n\n") (cat chapter8/README.md) + (echo "\n\n") (cat chapter9/README.md) + (echo "\n\n") (cat chapter10/README.md) + (echo "\n\n") (cat chapter11/README.md))))) (rule @@ -72,34 +82,54 @@ (echo "\n") (cat chapter1/functional-lecture01.md) + (echo "\n\n") (cat chapter2/functional-lecture02.md) - (echo "## Chapter 2: Derivation example\n") + (echo "\n\n## Chapter 2: Derivation example\n") (cat chapter2/functional-lecture02-deriv1.md) + (echo "\n\n") (cat chapter2/lecture02-exercises.md) + (echo "\n\n") (cat chapter3/functional-lecture03.md) + (echo "\n\n") (cat chapter3/lecture03-exercises.md) + (echo "\n\n") (cat chapter4/functional-lecture04.md) + (echo "\n\n") (cat chapter4/lecture04-exercises.md) + (echo "\n\n") (cat chapter5/functional-lecture05.md) + (echo "\n\n") (cat chapter5/lecture05-exercises.md) + (echo "\n\n") (cat chapter6/functional-lecture06.md) + (echo "\n\n") (cat chapter6/lecture06-exercises.md) + (echo "\n\n") (cat chapter7/functional-lecture07.md) + (echo "\n\n") (cat chapter7/lecture07-exercises.md) + (echo "\n\n") (cat chapter8/functional-lecture08.md) + (echo "\n\n") (cat chapter8/lecture08-exercises.md) + (echo "\n\n") (cat chapter9/functional-lecture09.md) + (echo "\n\n") (cat chapter9/lecture09-exercises.md) + (echo "\n\n") (cat chapter10/functional-lecture10.md) + (echo "\n\n") (cat chapter10/lecture10-exercises.md) + (echo "\n\n") (cat chapter11/functional-lecture11.md) + (echo "\n\n") (cat chapter11/lecture11-exercises.md) - (echo "# Exam: Exercises for review\n") + (echo "\n\n# Exam: Exercises for review\n") (echo "## Exam set 0\n") (cat exam/Exam1.md) - (echo "## Exam set 1\n") + (echo "\n\n## Exam set 1\n") (cat exam/exam-Feb2013-set1.md) - (echo "## Exam set 2\n") + (echo "\n\n## Exam set 2\n") (cat exam/exam-Feb2013-set2.md) - (echo "## Exam set 3\n") + (echo "\n\n## Exam set 3\n") (cat exam/exam-Feb2013-set3.md))))) diff --git a/old_lectures_as_book.md b/old_lectures_as_book.md index 1ce161c..d9b266f 100644 --- a/old_lectures_as_book.md +++ b/old_lectures_as_book.md @@ -187,8 +187,9 @@ about **polymorphism.*** Type definitions we have seen above are *global*: they its own set of operators. For example, +, *, / work for intigers, while +., *., /. work for floating point numbers. **Exception:** comparisons <, =, etc. work for all values other than functions. - - ## 4 Exercises + + +## 4 Exercises Exercises from *Think OCaml. How to Think Like a Computer Scientist* by Nicholas Monje and Allen Downey. @@ -253,6 +254,8 @@ Nicholas Monje and Allen Downey. If you need help, see [http://en.wikipedia.org/wiki/Euclidean\_algorithm](http://en.wikipedia.org/wiki/Euclidean_algorithm). + + # Lecture 2: Algebra Algebraic Data Types and some curious analogies @@ -761,8 +764,10 @@ predicate does not hold for any node. *This homework is due for the class **after** the Computation class, i.e. for (before) the Functions class.* + + ## Chapter 2: Derivation example -# Lecture 2: Algebra, Fig. 1 +# Lecture 2: Algebra Type inference example derivation @@ -850,6 +855,8 @@ $$ \frac{\frac{\begin{array}{ll} {\texttt{int}}} $$ + + Exercise 1. Due to Yaron Minsky. @@ -973,6 +980,8 @@ the $b^a$ notation is actually used to denote functions in set theory. Further reading: [http://bababadalgharaghtakamminarronnkonnbro.blogspot.com/2012/10/algebraic-type-systems-combinatorial.html](http://bababadalgharaghtakamminarronnkonnbro.blogspot.com/2012/10/algebraic-type-systems-combinatorial.html) + + # Lecture 3: Computation ‘‘Using, Understanding and Unraveling the OCaml Language'' Didier Rémy, @@ -1249,6 +1258,8 @@ returns a list of values in the nodes of the tree. simplification, and wrap it using a general `fixpoint` function that performs an operation until a *fixed point* is reached: given $f$ and $x$, it computes $f^n (x)$ such that $f^n (x) = f^{n + 1} (x)$. + + Functional Programming Computation @@ -1290,6 +1301,8 @@ quicksort. element, sorts the parts, and puts them together.* + + # Lecture 4: Functions. Programming in untyped $\lambda$-calculus. @@ -2080,6 +2093,8 @@ of a solution to the last exercise. done; !s 1. let repeatuntil p f s = let s = ref (f s) in while not (p !s) do s := f !s done; !s + + Functional Programming Functions @@ -2139,6 +2154,8 @@ of a solution to this exercise. done; !s 1. let repeatuntil p f s = let s = ref (f s) in while not (p !s) do s := f !s done; !s + + # Lecture 5: Polymorphism & ADTs Parametric types. Abstract Data Types. @@ -2744,6 +2761,8 @@ else if x>y then balance (color,a,y,ins b) else s in `ins` so that it never tests the color of nodes not on the search path. 1. \* Implement maps (i.e. write a module for the map signature) based on AVL trees. See `http://en.wikipedia.org/wiki/AVL_tree`. + + Functional Programming Type Inference @@ -2896,6 +2915,8 @@ another one using a map into the unit type. 1. *One of the remaining tests on grandchildren is also unnecessary. Rewrite* `*ins*` *so that it never tests the color of nodes not on the search path.* + + # Lecture 6: Folding and Backtracking Mapping and folding.Backtracking using lists. Constraint solving. @@ -3815,6 +3836,8 @@ findboard (initstate honey cellstoeat) sketchy. Questions? * We will not discuss a complete implementation example, but you can exploit ideas from the algorithm in your homework. + + * Recall how we generated all subsequences of a list. Find (i.e. generate) all: 1. permutations of a list; @@ -3855,6 +3878,8 @@ findboard (initstate honey cellstoeat) [http://www.mathsisfun.com/games/plinx-puzzle.html](http://www.mathsisfun.com/games/plinx-puzzle.html). It does not need to always return correct solutions but it should correctly solve the initial levels from the game. + + # Lecture 7: Laziness Lazy evaluation. Stream processing. @@ -4413,6 +4438,8 @@ Call-by-needIf the function argument is evaluated, that value is stored for docpos >-> grends 60 >-> iterate (printedoc printint printpos)let = prettyprint 20 testdoclet = prettyprint 30 testdoclet = prettyprint 60 testdoc + + Functional Programming Streams and lazy evaluation @@ -4574,6 +4601,8 @@ class="verbatim">g. -5.769180.217059-4.245171318957530.217059134806191-3.800670.238226-2.44599153327160.746229660007938-3.821830.259393-2.4459915332716-0.2909445693874851.088870.2170593.988722053181640.7250628389998681.046530.1958923.96755523217357-0.3332782114036250cm* + + # Lecture 8: Monads List comprehensions. Basic monads; transformers. Probabilistic @@ -5929,6 +5958,8 @@ assert false let killthreads () = Queue.clear jobsRemove pending work.end) ```ocaml # let test = Cooperative.killthreads (); let thread1 = TT.loop "A" 5 in let thread2 = TT.loop "B" 4 in Cooperative.access thread1; Cooperative.access thread2;;-- A(5)-- B(4)-- A(4)-- B(3)-- A(3)-- B(2)-- A(2)-- B(1)-- A(1)-- B(0)-- A(0)val test : unit = () ``` + + Exercise 1. Puzzle via Oleg Kiselyov. @@ -6112,6 +6143,8 @@ Implement the coarse-grained concurrency model. * One possibility is to introduce `suspend` of type unit monad, introduce a “dummy” monadic value `Suspend` (besides `Return` and `Sleep`), and define `bind suspend b` to do what `bind (return ()) b` would formerly do. + + # Lecture 9: Compiler Compilation. Runtime. Optimization. Parsing. @@ -7796,6 +7829,8 @@ let next ii w cp = let cw,ps = find w ii in let l = Array.length ps in if l = * Time: 2.4s – minimal speedup in our simple test case. + + Exercise 1. (Exercise 6.1 from *“Modern Compiler Implementation in ML”* by Andrew W. @@ -7895,6 +7930,8 @@ Write an XML parser tailored to the `shakespeare.xml` corpus provided with the phrase search example. Modify the phrase search engine to provide detailed information for each found location, e.g. which play and who speaks the phrase. + + # Lecture 10: FRP Zippers. Functional Reactive Programming. GUIs. @@ -8807,6 +8844,8 @@ to a new number.let calce, cancelcalc = eventflow calcNotifies display update. (button#coerce)) ) buttons; F.notifye calce (fun now -> result#setlabel (stringoffloat now)); window#show (); GMain.Main.main () * ![](Lec10calc_gtk.png) + + Functional Programming Zippers, Reactivity, GUIs @@ -8866,6 +8905,8 @@ None -> () | Some y -> emit y)val local : ('a -> 'b) -> ('a, *Implement an example that uses this compositionality-increasing capability.* + + The Expression Problem The Expression Problem @@ -9311,6 +9352,8 @@ open0.5emPluginBase.ParseMlet0.5emdigitofchar0.5emd0.5em=0.5emintofchar0.5emd0.5 * File `Plugin2.ml`: open0.5emPluginBase.ParseMlet0.5emmultiplication0.5emlang0.5em=0.5em0.5emperformMultiplication rule: $S \rightarrow (S \ast S)$.0.5em0.5em0.5em0.5emliteral0.5em"(";0.5emn10.5em<--0.5emlang;0.5emliteral0.5em"*";0.5emn20.5em<--0.5emlang;0.5emliteral0.5em")";0.5em0.5em0.5em0.5emreturn0.5em(n10.5em*0.5emn2)let0.5em()0.5em= PluginBase.(grammarrules0.5em:=0.5emmultiplication0.5em::0.5em!grammarrules) + + Functional ProgrammingŁukasz Stafiniak The Expression Problem @@ -9396,6 +9439,8 @@ built on top of *even lazy lists*? To additionally illustrate your answer: odd lazy lists.* + + # Exam: Exercises for review ## Exam set 0 Exercise 1. @@ -9466,6 +9511,8 @@ Exercise 7. Provide an algebraic specification and an implementation for first-in-first-out queues (lecture 5 exercise 9). + + ## Exam set 1 Functional ProgrammingFebruary 5th 2013 @@ -9594,6 +9641,8 @@ the Huffman code table for the frequency table `fs`.* **Exercise 15:** (Black.) Implement the Gaussian Elimination algorithm for solving linear equations and inverting square invertible matrices. + + ## Exam set 2 Functional ProgrammingFebruary 5th 2013 @@ -9684,6 +9733,8 @@ suggestions (in some sense of "best", e.g. occurring in most of documents containing the given words). + + ## Exam set 3 Functional ProgrammingFebruary 5th 2013 diff --git a/site/old_lectures_as_book.html b/site/old_lectures_as_book.html index 769daf9..19491e1 100644 --- a/site/old_lectures_as_book.html +++ b/site/old_lectures_as_book.html @@ -271,6 +271,11 @@

Curious OCaml

+
  • 4 Exercises
  • + +
  • Lecture 2: +Algebra +
  • +
  • Lecture +2: Algebra
  • +
  • Lecture 3: Computation +
  • +
  • Lecture 5: Polymorphism & +ADTs +
  • 11 Homework
  • + +
  • Lecture 6: Folding and +Backtracking +
  • 8 Constraint-based puzzles
  • + +
  • Lecture 7: +Laziness +
  • + +
  • Lecture 8: +Monads +
  • +
  • Lecture 9: +Compiler +
  • +
  • Lecture 10: +FRP +
  • +

    Functional Programming

    Computation

    Exercise 1: By “traverse a tree” below we mean: write a function that takes a tree and @@ -2763,8 +2806,9 @@

    10 In-class Work and Homework

    !s
    done; !s
  • let repeatuntil p f s = let s = ref (f s) in while not (p !s) do s -:= f !s done; !s Functional Programming
  • +:= f !s done; !s +

    Functional Programming

    Functions

    Exercise 1: Define (implement) and test on a couple of examples functions corresponding to / computing:

    @@ -2829,8 +2873,10 @@

    10 In-class Work and Homework

    !s
    done; !s
  • let repeatuntil p f s = let s = ref (f s) in while not (p !s) do s -:= f !s done; !s # Lecture 5: Polymorphism & ADTs
  • +:= f !s done; !s +

    Lecture 5: Polymorphism & +ADTs

    Parametric types. Abstract Data Types.

    Example: maps using red-black trees.

    If you see any error on the slides, let me know!

    @@ -3527,9 +3573,9 @@

    11 Homework

  • * Implement maps (i.e. write a module for the map signature) based on AVL trees. See -http://en.wikipedia.org/wiki/AVL_tree. Functional -Programming

  • +http://en.wikipedia.org/wiki/AVL_tree.

    +

    Functional Programming

    Type Inference

    Abstract Data Types

    Exercise 1: Derive the equations and solve them to @@ -3711,10 +3757,11 @@

    11 Homework

    to *balance* appropriately.
  • One of the remaining tests on grandchildren is also unnecessary. Rewrite *ins* so that it never tests the color of -nodes not on the search path. # Lecture 6: Folding and -Backtracking
  • +nodes not on the search path. +

    Lecture 6: Folding and +Backtracking

    Mapping and folding.Backtracking using lists. Constraint solving.

    Martin Odersky ‘‘Functional Programming Fundamentals’’ Lectures 2, 5 and 6

    @@ -4735,10 +4782,11 @@

    7.5 Optimizations for Honey findboard (initstate honey cellstoeat)

    8 Constraint-based puzzles

    +

    Lecture 7: Laziness

    Lazy evaluation. Stream processing.

    M. Douglas McIlroy ‘‘Power Series, Power Serious’’

    Oleg Kiselyov, Simon Peyton-Jones, Amr Sabry ‘‘Lazy v. Yield: @@ -5492,8 +5540,9 @@

    8.2 Example: pretty-printing

    (printedoc printint printpos)let = gen testdoc >-> docpos >-> grends 60 >-> iterate (printedoc printint printpos)let = prettyprint 20 testdoclet = prettyprint 30 testdoclet = prettyprint 60 -testdoc Functional Programming

    +testdoc

    +

    Functional Programming

    Streams and lazy evaluation

    Exercise 1: My first impulse was to define lazy list functions as here:

    @@ -5698,8 +5747,8 @@

    8.2 Example: pretty-printing

    -

    -5.769180.217059-4.245171318957530.217059134806191-3.800670.238226-2.44599153327160.746229660007938-3.821830.259393-2.4459915332716-0.2909445693874851.088870.2170593.988722053181640.7250628389998681.046530.1958923.96755523217357-0.3332782114036250cm* -# Lecture 8: Monads

    +

    -5.769180.217059-4.245171318957530.217059134806191-3.800670.238226-2.44599153327160.746229660007938-3.821830.259393-2.4459915332716-0.2909445693874851.088870.2170593.988722053181640.7250628389998681.046530.1958923.96755523217357-0.3332782114036250cm*

    +

    Lecture 8: Monads

    List comprehensions. Basic monads; transformers. Probabilistic Programming.Lightweight cooperative threads.

    Some examples from Tomasz Wierzbicki. Jeff Newbern ‘‘All About @@ -7480,8 +7529,9 @@

    11 Lightweight cooperative monad, introduce a “dummy” monadic value Suspend (besides Return and Sleep), and define bind suspend b to do what bind (return ()) b -would formerly do. # Lecture 9: Compiler +would formerly do. +

    Lecture 9: Compiler

    Compilation. Runtime. Optimization. Parsing.

    Andrew W. Appel ‘‘Modern Compiler Implementation in ML’‘E. Chailloux, P. Manoury, B. Pagano ‘‘Developing Applications with @@ -10171,7 +10221,8 @@

    Exam set 2

    +

    Functional ProgrammingFebruary 5th 2013

    Exam set 2

    Exercise 1: (Blue.) What is the type of the subexpression f as part of the expression below assuming