```+    John believes Mary said he thinks she's cute.
+     |             |         |         |
+     |             |---------|---------|
+     |                       |
+     |-----------------------|
+```
+ +It will be convenient to +have a counterpart to the lift operation that combines a monadic +functor with a non-monadic argument: + +
```+    let g f v = ap (unit f) v;;
+    let g2 u a = ap u (unit a);;
+```
+ +As a first step, we'll bind "she" by "Mary": + +
```+believes (z said (g2 (g thinks (g cute she)) she) mary) john
+
+~~> believes (said (thinks (cute mary) he) mary) john
+```
+ +As usual, there is a trail of *g*'s leading from the pronoun up to the +*z*. Next, we build a trail from the other pronoun ("he") to its +binder ("John"). + +
```+believes
+  said
+    thinks (cute she) he
+    Mary
+  John
+
+believes
+  z said
+    (g2 ((g thinks) (g cute she))) he
+    Mary
+  John
+
+z believes
+  (g2 (g (z said)
+         (g (g2 ((g thinks) (g cute she)))
+            he))
+      Mary)
+  John
+```
+ +In the first interation, we build a chain of *g*'s and *g2*'s from the +pronoun to be bound ("she") out to the level of the first argument of +*said*. + +In the second iteration, we treat the entire structure as ordinary +functions and arguments, without "seeing" the monadic region. Once +again, we build a chain of *g*'s and *g2*'s from the currently +targeted pronoun ("he") out to the level of the first argument of +*believes*. (The new *g*'s and *g2*'s are the three leftmost). + +
```+z believes (g2 (g (z said) (g (g2 ((g thinks) (g cute she))) he)) mary) john
+
+~~> believes (said (thinks (cute mary) john) mary) john
+```
```+\tree ((((+) (1)) (((*) (((/) (6)) (2))) (4))))
+
+ ___________
+ |         |
+_|__    ___|___
+|  |    |     |
++  1  __|___  4
+      |    |
+      *  __|___
+         |    |
+        _|__  2
+        |  |
+        /  6
+```
+ +This computation should reduce to 13. But given a specific reduction +strategy, we can watch the order in which the computation proceeds. +Following on the lambda evaluator developed during the previous +homework, let's adopt the following reduction strategy: + + In order to reduce (head arg), do the following in order: + 1. Reduce head to h' + 2. Reduce arg to a'. + 3. If (h' a') is a redex, reduce it. + +There are many details left unspecified here, but this will be enough +for today. The order in which the computation unfolds will be + + 1. Reduce head (+ 1) to itself + 2. Reduce arg ((* ((/ 6) 2)) 3) + 1. Reduce head (* ((/ 6) 2)) + 1. Reduce head * + 2. Reduce arg ((/ 6) 2) + 1. Reduce head (/ 6) to itself + 2. Reduce arg 2 to itself + 3. Reduce ((/ 6) 2) to 3 + 3. Reduce (* 3) to itself + 2. Reduce arg 4 to itself + 3. Reduce ((* 3) 4) to 12 + 3. Reduce ((+ 1) 12) to 13 + +This reduction pattern follows the structure of the original +expression exactly, at each node moving first to the left branch, +processing the left branch, then moving to the right branch, and +finally processing the results of the two subcomputation. (This is +called depth-first postorder traversal of the tree.) + +It will be helpful to see how the types change as we make adjustments. + + type num = int + type contents = Num of num | Op of (num -> num -> num) + type tree = Leaf of contents | Branch of tree * tree + +Never mind that these types will allow us to construct silly +arithmetric trees such as `+ *` or `2 3`. Note that during the +reduction sequence, the result of reduction was in every case a +well-formed subtree. So the process of reduction could be animated by +replacing subtrees with the result of reduction on that subtree, till +the entire tree is replaced by a single integer (namely, 13). + +Now we replace the number 2 with 0: + +
```+\tree ((((+) (1)) (((*) (((/) (6)) (0))) (4))))
+
+ ___________
+ |         |
+_|__    ___|___
+|  |    |     |
++  1  __|___  4
+      |    |
+      *  __|___
+         |    |
+        _|__  0
+        |  |
+        /  6
+```
```+\tree ((((+) (1)) (((*) (((/) (6)) (2))) (4))))
+
+ ___________
+ |         |
+_|__    ___|___
+|  |    |     |
++  1  __|___  4
+      |    |
+      *  __|___
+         |    |
+        _|__  2
+        |  |
+        /  6
+```
+ +This computation should reduce to 13. But given a specific reduction +strategy, we can watch the order in which the computation proceeds. +Following on the lambda evaluator developed during the previous +homework, let's adopt the following reduction strategy: + + In order to reduce (head arg), do the following in order: + 1. Reduce head to h' + 2. Reduce arg to a'. + 3. If (h' a') is a redex, reduce it. + +There are many details left unspecified here, but this will be enough +for today. The order in which the computation unfolds will be + + 1. Reduce head (+ 1) to itself + 2. Reduce arg ((* (/ 6 2)) 3) + 1. Reduce head (* ((/ 6) 2)) + 1. Reduce head * + 2. Reduce arg ((/ 6) 2) + 1. Reduce head (/ 6) to itself + 2. Reduce arg 2 to itself + 3. Reduce ((/ 6) 2) to 3 + 3. Reduce (* 3) to itself + 2. Reduce arg 4 to itself + 3. Reduce ((* 3) 4) to 12 + 3. Reduce ((+ 1) 12) to 13 + +This reduction pattern follows the structure of the original +expression exactly, at each node moving first to the left branch, +processing the left branch, then moving to the right branch, and +finally processing the results of the two subcomputation. (This is +called depth-first postorder traversal of the tree.) + +It will be helpful to see how the types change as we make adjustments. + + type num = int + type contents = Num of num | Op of (num -> num -> num) + type tree = Leaf of contents | Branch of tree * tree + +Never mind that these types will allow us to construct silly +arithmetric trees such as `+ *` or `2 3`. Note that during the +reduction sequence, the result of reduction was in every case a +well-formed subtree. So the process of reduction could be animated by +replacing subtrees with the result of reduction on that subtree, till +the entire tree is replaced by a single integer (namely, 13). + +Now we replace the number 2 with 0: + +
```+\tree ((((+) (1)) (((*) (((/) (6)) (0))) (4))))
+
+ ___________
+ |         |
+_|__    ___|___
+|  |    |     |
++  1  __|___  4
+      |    |
+      *  __|___
+         |    |
+        _|__  0
+        |  |
+        /  6
+```
```+                 message: Division by zero occurred!
+                      ^
+ ___________          |
+ |         |          |
+_|__    ___|___       |
+|  |    |     |       |
++  1  __|___  4       |
+      |    |          |
+      *  __|___  -----|
+         |    |
+        _|__  0
+        |  |
+        /  6
+```
+ +We might want to reverse the direction of information flow, making +information available at the top of the computation available to the +subcomputations: + +
```+                    [Î»x]
+ ___________          |
+ |         |          |
+_|__    ___|___       |
+|  |    |     |       |
++  1  __|___  4       |
+      |    |          |
+      *  __|___       |
+         |    |       |
+        _|__  x  <----|
+        |  |
+        /  6
+```
```+\tree ((((map2 +) (â§1)) (((map2 *) (((map2 /) (â§6)) (x))) (â§4))))
+
+     __________________
+     |                |
+  ___|____        ____|_____
+  |      |        |        |
+map2 +  â§1    ____|_____  â§4
+              |        |
+            map2 *  ___|____
+                    |      |
+                 ___|____  x
+                 |      |
+               map2 /  â§6
+```
+ +It remains only to decide how the variable `x` will access the value input +at the top of the tree. Since the input value is supposed to be the +value put in place of the variable `x`. Like every leaf in the tree +in argument position, the code we want in order to represent the +variable will have the type of a boxed int, namely, `int -> int`. So +we have the following: + + x (i:int):int = i + +That is, variables in this system denote the indentity function! + +The result of evaluating this tree will be a boxed integer: a function +from any integer `x` to `(+ 1 (* (/ 6 x)) 4)`. + +Take a look at the definition of the reader monad again. The +midentity takes some object `a` and returns `\x.a`. In other words, +`â§a = Ka`, so `â§ = K`. Likewise, `map2` for this monad is the `S` +combinator. We've seen this before as a strategy for translating a +lambda abstract into a set of combinators. Here is a part of the +general scheme for translating a lambda abstract into Combinatory +Logic. The translation function `[.]` translates a lambda term into a +term in Combinatory Logic: + + [(MN)] = ([M] [N]) + [\a.a] = I + [\a.M] = K[M] (assuming a not free in M) + [\a.(MN)] = S[\a.M][\a.N] + +The reason we can make do with this subset of the full function is +that we're making the simplifying assumption that there is at most a +single lambda involved. So here you see the I (the translation of the +bound variable), the K and the S. + + +## Jacobson's Variable Free Semantics as a Reader Monad + +We've designed the discussion so far to make the following claim as +easy to show as possible: Jacobson's Variable Free Semantics +(e.g., Jacobson 1999, [Towards a +Variable-Free +Semantics](http://www.springerlink.com/content/j706674r4w217jj5/)) +is a reader monad. + +More specifically, it will turn out that Jacobson's geach combinator +*g* is exactly our `lift` operator, and her binding combinator *z* is +exactly our `bind` (though with the arguments reversed)! + +Jacobson's system contains two main combinators, *g* and *z*. She +calls *g* the Geach rule, and *z* performs binding. Here is a typical +computation. This implementation is based closely on email from Simon +Charlow, with beta reduction as performed by the on-line evaluator: + +
```+; Analysis of "Everyone_i thinks he_i left"
+let g = \f g x. f (g x) in
+let z = \f g x. f (g x) x in
+let he = \x. x in
+let everyone = \P. FORALL x (P x) in
+
+everyone (z thinks (g left he))
+
+~~>  FORALL x (thinks (left x) x)
+```
+ +Several things to notice: First, pronouns once again denote identity functions. +As Jeremy Kuhn has pointed out, this is related to the fact that in +the mapping from the lambda calculus into combinatory logic that we +discussed earlier in the course, bound variables translated to I, the +identity combinator (see additional comments below). We'll return to +the idea of pronouns as identity functions in later discussions. + +Second, *g* plays the role of transmitting a binding dependency for an +embedded constituent to a containing constituent. + +Third, one of the peculiar aspects of Jacobson's system is that +binding is accomplished not by applying *z* to the element that will +(in some pre-theoretic sense) bind the pronoun, here, *everyone*, but +rather by applying *z* instead to the predicate that will take +*everyone* as an argument, here, *thinks*. + +The basic recipe in Jacobson's system, then, is that you transmit the +dependence of a pronoun upwards through the tree using *g* until just +before you are about to combine with the binder, when you finish off +with *z*. (There are examples with longer chains of *g*'s below.) + +Jacobson's *g* combinator is exactly our `lift` operator: it takes a +functor and lifts it into the monad. +Furthermore, Jacobson's *z* combinator, which is what she uses to +create binding links, is essentially identical to our reader-monad +`bind`! + +
```+everyone (z thinks (g left he))
+
+~~> forall w (thinks (left w) w)
+
+everyone (z thinks (g (t bill) (g said (g left he))))
+
+~~> forall w (thinks (said (left w) bill) w)
+```
```+Extensional types              Intensional types       Examples
+-------------------------------------------------------------------
+
+S         t                    s->t                    John left
+DP        e                    s->e                    John
+VP        e->t                 (s->e)->s->t            left
+Vt        e->e->t              (s->e)->(s->e)->s->t    saw
+Vs        t->e->t              (s->t)->(s->e)->s->t    thought
+```
+ +This system is modeled on the way Montague arranged his grammar. +There are significant simplifications compared to Montague: for +instance, determiner phrases are thought of here as corresponding to +individuals rather than to generalized quantifiers. + +The main difference between the intensional types and the extensional +types is that in the intensional types, the arguments are functions +from worlds to extensions: intransitive verb phrases like "left" now +take so-called "individual concepts" as arguments (type s->e) rather than plain +individuals (type e), and attitude verbs like "think" now take +propositions (type s->t) rather than truth values (type t). +In addition, the result of each predicate is an intension. +This expresses the fact that the set of people who left in one world +may be different than the set of people who left in a different world. + +Normally, the dependence of the extension of a predicate to the world +of evaluation is hidden inside of an evaluation coordinate, or built +into the the lexical meaning function, but we've made it explicit here +in the way that the intensionality monad makes most natural. + +The intensional types are more complicated than the extensional +types. Wouldn't it be nice to make the complicated types available +for those expressions like attitude verbs that need to worry about +intensions, and keep the rest of the grammar as extensional as +possible? This desire is parallel to our earlier desire to limit the +concern about division by zero to the division function, and let the +other functions, like addition or multiplication, ignore +division-by-zero problems as much as possible. + +So here's what we do: + +In OCaml, we'll use integers to model possible worlds. Characters (characters in the computational sense, i.e., letters like `'a'` and `'b'`, not Kaplanian characters) will model individuals, and OCaml booleans will serve for truth values: + + type s = int;; + type e = char;; + type t = bool;; + + let ann = 'a';; + let bill = 'b';; + let cam = 'c';; + + let left1 (x:e) = true;; + let saw1 (x:e) (y:e) = y < x;; + + left1 ann;; (* true *) + saw1 bill ann;; (* true *) + saw1 ann bill;; (* false *) + +So here's our extensional system: everyone left, including Ann; +and Ann saw Bill (`saw1 bill ann`), but Bill didn't see Ann. (Note that the word +order we're using is VOS, verb-object-subject.) + +Now we add intensions. Because different people leave in different +worlds, the meaning of *leave* must depend on the world in which it is +being evaluated: + + let left (x:e) (w:s) = match (x, w) with ('c', 2) -> false | _ -> true;; + left ann 1;; (* true: Ann left in world 1 *) + left cam 2;; (* false: Cam didn't leave in world 2 *) + +This new definition says that everyone always left, except that +in world 2, Cam didn't leave. + +Note that although this general *left* is sensitive to world of +evaluation, it does not have the fully intensionalized type given in +the chart above, which was `(s->e)->s->t`. This is because +*left* does not exploit the additional resolving power provided by +making the subject an individual concept. In semantics jargon, we say +that *leave* is extensional with respect to its first argument. + +Therefore we will adopt the general strategy of defining predicates +in a way that they take arguments of the lowest type that will allow +us to make all the distinctions the predicate requires. When it comes +time to combine this predicate with monadic arguments, we'll have to +make use of various lifting predicates. + +Likewise, although *see* depends on the world of evaluation, it is +extensional in both of its syntactic arguments: + + let saw x y w = (w < 2) && (y < x);; + saw bill ann 1;; (* true: Ann saw Bill in world 1 *) + saw bill ann 2;; (* false: no one saw anyone in world 2 *) + +This (again, partially) intensionalized version of *see* coincides +with the `saw1` function we defined above for world 1; in world 2, no +one saw anyone. + +Just to keep things straight, let's review the facts: + +
```+     World 1: Everyone left.
+              Ann saw Bill, Ann saw Cam, Bill saw Cam, no one else saw anyone.
+     World 2: Ann left, Bill left, Cam didn't leave.
+              No one saw anyone.
+```
```+type 'a intension = s -> 'a;;
```+lift2' saw (unit bill) (unit ann) 1;;  (* true *)