{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lambda notebook demo v. 1.0.2\n", "\n", "## Author: Kyle Rawlins\n", "\n", "This notebook provides a demo of the core capabilities of the lambda notebook, aimed at linguists who already have training in semantics (but not necessarily implemented semantics).\n", "\n", "Last updated Aug 2023. Version history:\n", "\n", " * 0.5: first version\n", " * 0.6: updated to work with refactored class hierarchy (Apr 2013)\n", " * 0.6.1: small fixes to adapt to changes in various places (Sep 2013)\n", " * 0.7: various fixes to work with alpha release (Jan 2014)\n", " * 0.9: substantial updates, merge content from LSA poster (Apr 2014)\n", " * 0.95: substantial updates for a series of demos in Apr-May 2014\n", " * 1.0: various changes / fixes, more stand-alone text (2017)\n", " * 1.0.1: small tweaks (Nov/Dec 2018)\n", " * 1.0.2: bugfixes\n", " \n", "To run through this demo incrementally, use shift-enter (runs and moves to next cell). If you run things out of order, you may encounter problems (missing variables etc.)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.065975Z", "iopub.status.busy": "2024-04-08T14:10:36.065650Z", "iopub.status.idle": "2024-04-08T14:10:36.069552Z", "shell.execute_reply": "2024-04-08T14:10:36.068978Z" } }, "outputs": [], "source": [ "from lamb.types import TypeMismatch, type_e, type_t, type_property\n", "from lamb.meta import TypedTerm, TypedExpr, LFun, CustomTerm" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.072215Z", "iopub.status.busy": "2024-04-08T14:10:36.072020Z", "iopub.status.idle": "2024-04-08T14:10:36.074763Z", "shell.execute_reply": "2024-04-08T14:10:36.074359Z" } }, "outputs": [], "source": [ "# There are several ways of display outputs, this one is the default:\n", "lamb.display.default(style=lamb.display.DerivStyle.BOXES)\n", "# you can also try:\n", "# lamb.display.default(style=lamb.display.DerivStyle.PROOF)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# First pitch\n", "\n", "Have you ever wanted to type something like this in, and have it actually do something?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.076984Z", "iopub.status.busy": "2024-04-08T14:10:36.076815Z", "iopub.status.idle": "2024-04-08T14:10:36.086404Z", "shell.execute_reply": "2024-04-08T14:10:36.086148Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO (core): Coerced guessed type for 'Student_t' into , to match argument 'x_e'\n", "INFO (core): Coerced guessed type for 'Danced_t' into , to match argument 'x_e'\n" ] }, { "data": { "text/markdown": [ "$[\\![\\text{\\textbf{every}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} f_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: \\lambda{} g_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: \\forall{} x_{e} \\: . \\: {f}({x}) \\rightarrow{} {g}({x})$
\n", "$[\\![\\text{\\textbf{student}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Student}({x})$
\n", "$[\\![\\text{\\textbf{danced}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Danced}({x})$" ], "text/plain": [ "⟦every⟧ = (λ f_: (λ g_: (Forall x_e: (f_(x_e) >> g_(x_e)))))\n", "⟦student⟧ = (λ x_e: Student_(x_e))\n", "⟦danced⟧ = (λ x_e: Danced_(x_e))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%lamb\n", "||every|| = λ f_ : λ g_ : Forall x_e : f(x) >> g(x)\n", "||student|| = L x_e : Student(x)\n", "||danced|| = L x_e : Danced(x)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.112933Z", "iopub.status.busy": "2024-04-08T14:10:36.112808Z", "iopub.status.idle": "2024-04-08T14:10:36.117417Z", "shell.execute_reply": "2024-04-08T14:10:36.117146Z" } }, "outputs": [ { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{[[every student] danced]}}]\\!]^{}_{t} \\:=\\: \\forall{} x_{e} \\: . \\: {Student}({x}) \\rightarrow{} {Danced}({x})$" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦[every student]⟧ * ⟦danced⟧ leads to: ⟦[[every student] danced]⟧ = (Forall x_e: (Student_(x_e) >> Danced_(x_e))) [by FA]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "r = ((every * student) * danced)\n", "r" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.118799Z", "iopub.status.busy": "2024-04-08T14:10:36.118703Z", "iopub.status.idle": "2024-04-08T14:10:36.121526Z", "shell.execute_reply": "2024-04-08T14:10:36.121233Z" } }, "outputs": [ { "data": { "text/html": [ "1 composition path:
$[\\![\\text{\\textbf{every}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} f_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: \\lambda{} g_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: \\forall{} x_{e} \\: . \\: {f}({x}) \\rightarrow{} {g}({x})$
*
$[\\![\\text{\\textbf{student}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Student}({x})$
[FA]
$[\\![\\text{\\textbf{[every student]}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},t\\right\\rangle{}}$
$\\lambda{} g_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: \\forall{} x_{e} \\: . \\: {Student}({x}) \\rightarrow{} {g}({x})$
*
$[\\![\\text{\\textbf{danced}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Danced}({x})$
[FA]
$[\\![\\text{\\textbf{[[every student] danced]}}]\\!]^{}_{t}$
$\\forall{} x_{e} \\: . \\: {Student}({x}) \\rightarrow{} {Danced}({x})$


" ], "text/plain": [ "1 composition path:
$[\\![\\text{\\textbf{every}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} f_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: \\lambda{} g_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: \\forall{} x_{e} \\: . \\: {f}({x}) \\rightarrow{} {g}({x})$
*
$[\\![\\text{\\textbf{student}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Student}({x})$
[FA]
$[\\![\\text{\\textbf{[every student]}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},t\\right\\rangle{}}$
$\\lambda{} g_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: \\forall{} x_{e} \\: . \\: {Student}({x}) \\rightarrow{} {g}({x})$
*
$[\\![\\text{\\textbf{danced}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Danced}({x})$
[FA]
$[\\![\\text{\\textbf{[[every student] danced]}}]\\!]^{}_{t}$
$\\forall{} x_{e} \\: . \\: {Student}({x}) \\rightarrow{} {Danced}({x})$


" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.tree()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Two problems in formal semantics #\n", "\n", "1. Type-driven computation could be a lot easier to visualize and check. (Q: could it be made too easy?)\n", "\n", "2. Grammar fragments as in Montague Grammar: good idea in principle, hard to use in practice.\n", "\n", " * A **fragment** is a *complete* formalization of *sublanguage* consisting of the *key relevant phenomena* for the problem at hand. (Potential problem-points italicized.)\n", "\n", "Solution: a system for developing interactive fragments: \"*IPython Lambda Notebook*\"\n", "\n", "* Creator can work interactively with analysis -- accelerate development, limit time spent on tedious details.\n", "* Reader can explore derivations in ways that are not typically possible in typical paper format.\n", "* Creator and reader can be certain that derivations work, verified by the system.\n", "* Bring closer together formal semantics and computational modeling.\n", "\n", "Inspired by:\n", "\n", " * Von Eijck and Unger (2013): implementation of compositional semantics in Haskell. No interface (beyond standard Haskell terminal); great if you like Haskell. Introduced the idea of a fragment in digital form.\n", " * UPenn Lambda calculator (Champollion, Tauberer, and Romero 2007): teaching oriented. (Now under development again.)\n", " * `nltk.sem`: implementation of the lambda calculus with a typed metalanguage, interface with theorem provers. No interactive interface.\n", " * Jealousy of R studio, Matlab, Mathematica, etc.\n", "\n", "### The role of formalism & fragments ###\n", "\n", "What does *formal* mean in semantics? What properties should a theory have?\n", "\n", " 1. Mathematically precise (lambda calculus, type theory, logic, model theory(?), ...)\n", " 2. Complete (covers \"all\" the relevant data).\n", " 3. Predictive (like any scientific theory).\n", " 4. Consistent, or at least compatible (with itself, analyses of other phenomena, some unifying conception of the grammar).\n", " \n", "The *method of fragments* (Partee 1979, Partee and Hendriks 1997) provides a structure for meeting these criteria.\n", "\n", " * Paper with a fragment provides a working system. (Probably.)\n", " * Explicit outer bound for empirical coverage.\n", " * Integration with a particular theory of grammar. (To some extent.)\n", " * Explicit answer to many detailed questions not necessarily dealt with in the text.\n", " \n", "**Claim**: fragments are a method of replicability, similar to a computational modeller providing their model.\n", "\n", " * To be clear, a fragment is neither necessary nor sufficient for having a good theory / analysis / paper...\n", "\n", "Additional benefit: useful internal check for researcher.\n", "\n", ">\"...But I feel strongly that it is important to try to [work with fully explicit fragments] periodically, because otherwise it is extremely easy to think that you have a solution to a problem when in fact you don't.\" (Partee 1979, p. 41)\n", "\n", "### The challenges of fragments\n", "\n", "Part 1 of the above quote:\n", "\n", ">\"It can be very frustrating to try to specify frameworks and fragments explicitly; this project has not been entirely rewarding. I would not recommend that one always work with the constraint of full explicitness.\" (Ibid.)\n", "\n", " * Fragments can be tedious and time-consuming to write (not to mention hard).\n", " * Fragments as traditionally written are in practice not easy for a reader to use.\n", " \n", " - Dense/unapproachable. With exactness can come a huge chunk of hard-to-digest formalism. E.g. Partee (1979), about 10% of the paper.\n", " - Monolithic/non-modular. For the specified sublanguage, everything specified. Outside the bounds of the sublanguage, nothing specified. How does the theory fit in with others?\n", " - Exact opposite of the modern method -- researchers typically hold most aspects of the grammar constant (implicitly) while changing a few key points. (*Portner and Partee intro*)\n", "\n", "**Summary:** In practice, the typical payoff for neither the reader nor the writer of a fragment exceeded the effort.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# A solution: digital fragments\n", "\n", "Von Eijck and Unger 2010: specify a fragment in digital form.\n", "\n", "* They use Haskell. Type system of Haskell extremely well-suited to natural language semantics.\n", "* (Provocative statement) Interface, learning curve of Haskell not well suited to semanticists (or most people)?\n", "\n", "### Benefits of digital fragments (in principle)\n", "\n", "* Interactive.\n", "* Easy to distribute, adapt, modify.\n", "* Possibility of modularity. (E.g. abstract a 'library' for compositional systems away from the analysis of a particular phenomenon.)\n", "* Bring closer together the CogSci idea of a 'computational model' to the project of natural language semantics.\n", "* Connections to computational semantics. (weak..)\n", "\n", "### What sorts of things might we want in a fragment / system for fragments?\n", "\n", "* Typed lambda calculus.\n", "* Logic / logical metalanguage.\n", "* Framework for semantic composition. (Broad...)\n", "* Model theory? (x)\n", "* Interface with theorem provers? (x)\n", "\n", "IPython Lambda Notebook aims to provide these tools in a usable, interactive, format.\n", "\n", "* Choose Python, rather than Haskell/Java. Easy learning curve, rapid prototyping, existence of IPython.\n", "\n", "**Layer 1**: interface using IPython Notebook.\n", "\n", "**Layer 2**: flexible typed metalanguage.\n", "\n", "**Layer 3**: composition system for object language, building on layer 2." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Layer 1: an interface using IPython/Jupyter Notebook (Perez and Granger 2007) ##\n", "\n", " * Client-server system where a specialized IPython \"kernel\" is running in the background. This kernel implements various tools for formal semantics (see parts 2-3).\n", " * Page broken down into cells in which can be entered python code, markdown code, raw text, other formats.\n", " * Jupyter: supports display of graphical representations of python objects.\n", " * Notebook format uses the \"MathJax\" framework to enable it to render most math-mode latex. Can have python objects automatically generate decent-looking formulas. Can use latex math mode in documentation as well (e.g. $\\lambda x \\in D_e : \\mathit{CAT}(x)$)\n", "\n", "This all basically worked off-the-shelf.\n", "\n", "* Bulk of interface work so far: rendering code for logical and compositional representations.\n", "* Future: interactive widgets, etc." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.123000Z", "iopub.status.busy": "2024-04-08T14:10:36.122901Z", "iopub.status.idle": "2024-04-08T14:10:36.125535Z", "shell.execute_reply": "2024-04-08T14:10:36.125316Z" } }, "outputs": [ { "data": { "text/latex": [ "$\\lambda{} p_{t} \\: . \\: \\lambda{} x_{e} \\: . \\: {P}({x}) \\wedge{} {p}$" ], "text/plain": [ "(λ p_t: (λ x_e: (P_(x_e) & p_t)))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pmw_test1 = %te L p_t : L x_e : P_(x) & p\n", "pmw_test1" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.126796Z", "iopub.status.busy": "2024-04-08T14:10:36.126702Z", "iopub.status.idle": "2024-04-08T14:10:36.128723Z", "shell.execute_reply": "2024-04-08T14:10:36.128494Z" } }, "outputs": [ { "data": { "text/plain": [ "'$\\\\lambda{} p_{t} \\\\: . \\\\: \\\\lambda{} x_{e} \\\\: . \\\\: {P}({x}) \\\\wedge{} {p}$'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pmw_test1._repr_latex_()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " \n", "\n", "## Part 2: a typed metalanguage ##" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **metalanguage** infrastructure is a set of classes that implement the building blocks of logical expressions, lambda terms, and various combinations combinations. This rests on an implementation of a **type system** that matches what semanticists tend to assume.\n", "\n", "Starting point (2012): a few implementations of things like predicate logic do exist, this is an intro AI exercise sometimes. I started with the [AIMA python](http://code.google.com/p/aima-python/) _Expr_ class, based on the standard Russell and Norvig AI text. But, had to scrap most of it. Another starting point would have been `nltk.sem` (I was unaware of its existence at the time.)\n", "\n", "Preface cell with `%%lamb` to enter metalanguage formulas directly. The following cell defines a variable `x` that has type e, and exports it to the notebook's environment." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.130121Z", "iopub.status.busy": "2024-04-08T14:10:36.130021Z", "iopub.status.idle": "2024-04-08T14:10:36.132269Z", "shell.execute_reply": "2024-04-08T14:10:36.132047Z" } }, "outputs": [ { "data": { "text/markdown": [ "${x}_{e}\\:=\\:{x}_{e}$" ], "text/plain": [ "x_e = x_e" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%lamb reset\n", "x = x_e # define x to have this type" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.133462Z", "iopub.status.busy": "2024-04-08T14:10:36.133375Z", "iopub.status.idle": "2024-04-08T14:10:36.135222Z", "shell.execute_reply": "2024-04-08T14:10:36.135016Z" } }, "outputs": [ { "data": { "text/latex": [ "$e$" ], "text/plain": [ "e" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.type" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This next cell defines some variables whose values are more complex object -- in fact, functions in the typed lambda calculus." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.136498Z", "iopub.status.busy": "2024-04-08T14:10:36.136407Z", "iopub.status.idle": "2024-04-08T14:10:36.140906Z", "shell.execute_reply": "2024-04-08T14:10:36.140748Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO (core): Coerced guessed type for 'P_t' into , to match argument 'x_e'\n", "INFO (core): Coerced guessed type for 'P_t' into , to match argument 'x_e'\n", "INFO (core): Coerced guessed type for 'Q_t' into , to match argument 'x_e'\n", "INFO (core): Coerced guessed type for 'Q_t' into , to match argument 'x_e'\n" ] }, { "data": { "text/markdown": [ "${test1}_{\\left\\langle{}t,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}\\:=\\:\\lambda{} p_{t} \\: . \\: \\lambda{} x_{e} \\: . \\: {p} \\wedge{} {P}({x})$
\n", "${test1b}_{\\left\\langle{}e,t\\right\\rangle{}}\\:=\\:\\lambda{} x_{e} \\: . \\: {P}({x}) \\wedge{} {Q}({x})$
\n", "${t2}_{t}\\:=\\:{Q}({x}_{e})$" ], "text/plain": [ "test1_> = (λ p_t: (λ x_e: (p_t & P_(x_e))))\n", "test1b_ = (λ x_e: (P_(x_e) & Q_(x_e)))\n", "t2_t = Q_(x_e)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%lamb\n", "test1 = L p_t : L x_e : P(x) & p # based on a Partee et al example\n", "test1b = L x_e : P(x) & Q(x)\n", "t2 = Q(x_e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These are now registered as variables in the python namespace and can be manipulated directly. A typed lambda calculus is fully implemented with all that that entails -- e.g. the value of `test1` includes the whole syntactic structure of the formula, its type, etc. and can be used in constructing new formulas. The following cells build a complex function-argument formula, and following that, does the reduction.\n", "\n", "(Notice that beta reduction works properly, i.e. bound $x$ in the function is renamed in order to avoid collision with the free `x` in the argument.)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.142167Z", "iopub.status.busy": "2024-04-08T14:10:36.142077Z", "iopub.status.idle": "2024-04-08T14:10:36.144034Z", "shell.execute_reply": "2024-04-08T14:10:36.143825Z" } }, "outputs": [ { "data": { "text/latex": [ "${[\\lambda{} p_{t} \\: . \\: \\lambda{} x_{e} \\: . \\: {p} \\wedge{} {P}({x})]}({Q}({x}_{e}))$" ], "text/plain": [ "(λ p_t: (λ x_e: (p_t & P_(x_e))))(Q_(x_e))" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test1(t2)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.145170Z", "iopub.status.busy": "2024-04-08T14:10:36.145086Z", "iopub.status.idle": "2024-04-08T14:10:36.147389Z", "shell.execute_reply": "2024-04-08T14:10:36.147188Z" } }, "outputs": [ { "data": { "text/latex": [ "$\\lambda{} x1_{e} \\: . \\: {Q}({x}_{e}) \\wedge{} {P}({x1})$" ], "text/plain": [ "(λ x1_e: (Q_(x_e) & P_(x1_e)))" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test1(t2).reduce()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.148517Z", "iopub.status.busy": "2024-04-08T14:10:36.148432Z", "iopub.status.idle": "2024-04-08T14:10:36.151236Z", "shell.execute_reply": "2024-04-08T14:10:36.151031Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO (core): Coerced guessed type for 'Cat_t' into , to match argument 'x_e'\n", "INFO (core): Coerced guessed type for 'Dog_t' into , to match argument 'x_e'\n" ] }, { "data": { "text/markdown": [ "${catf}_{\\left\\langle{}e,t\\right\\rangle{}}\\:=\\:\\lambda{} x_{e} \\: . \\: {Cat}({x})$
\n", "${dogf}_{\\left\\langle{}e,t\\right\\rangle{}}\\:=\\:\\lambda{} x_{e} \\: . \\: {Dog}({x})$" ], "text/plain": [ "catf_ = (λ x_e: Cat_(x_e))\n", "dogf_ = (λ x_e: Dog_(x_e))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%lamb\n", "catf = L x_e: Cat(x)\n", "dogf = λx: Dog(x_e)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.152360Z", "iopub.status.busy": "2024-04-08T14:10:36.152275Z", "iopub.status.idle": "2024-04-08T14:10:36.154181Z", "shell.execute_reply": "2024-04-08T14:10:36.153987Z" } }, "outputs": [ { "data": { "text/latex": [ "$t$" ], "text/plain": [ "t" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(catf(x)).type" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.155212Z", "iopub.status.busy": "2024-04-08T14:10:36.155142Z", "iopub.status.idle": "2024-04-08T14:10:36.157077Z", "shell.execute_reply": "2024-04-08T14:10:36.156873Z" } }, "outputs": [ { "data": { "text/latex": [ "$\\left\\langle{}e,t\\right\\rangle{}$" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "catf.type" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Type checking of course is a part of all this. If the types don't match, the computation will throw a `TypeMismatch` exception. The following cell uses python syntax to catch and print such errors." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.158243Z", "iopub.status.busy": "2024-04-08T14:10:36.158159Z", "iopub.status.idle": "2024-04-08T14:10:36.160159Z", "shell.execute_reply": "2024-04-08T14:10:36.159951Z" } }, "outputs": [ { "data": { "text/markdown": [ "**TypeMismatch**: `(λ p_t: (λ x_e: (p_t & P_(x_e))))/>` and `x_e` conflict (Function-argument expression: mismatched argument types `t` and `e`)" ], "text/plain": [ "TypeMismatch: `(λ p_t: (λ x_e: (p_t & P_(x_e))))/>` and `x_e` conflict (Function-argument expression: mismatched argument types `t` and `e`)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "with lamb.errors():\n", " display(test1(x)) # function is type > so will trigger a type mismatch." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A more complex expression:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.161307Z", "iopub.status.busy": "2024-04-08T14:10:36.161227Z", "iopub.status.idle": "2024-04-08T14:10:36.164114Z", "shell.execute_reply": "2024-04-08T14:10:36.163920Z" } }, "outputs": [ { "data": { "text/markdown": [ "${p2}_{t}\\:=\\:({p}_{t} \\wedge{} {Cat}({x}_{e})) \\rightarrow{} (\\exists{} y_{e} \\: . \\: {Dog}({y}))$" ], "text/plain": [ "p2_t = ((p_t & Cat_(x_e)) >> (Exists y_e: Dog_(y_e)))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%lamb\n", "p2 = (Cat_(x_e) & p_t) >> (Exists y: Dog_(y_e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is going on behind the scenes? The objects manipulated are recursively structured python objects of class TypedExpr.\n", "\n", "Class _TypedExpr_: parent class for typed expressions. Key subclasses:\n", "\n", "* BinaryOpExpr: parent class for things like conjunction.\n", "* TypedTerm: variables, constants of arbitrary type\n", "* BindingOp: operators that bind a single variable\n", " * LFun: lambda expression\n", "\n", "Many straightforward expressions can be parsed. Most expressions are created using a call to TypedExpr.factory, which is abbreviated as \"te\" in the following examples. The `%%lamb` magic is calling this behind the scenes.\n", "\n", "Three ways of instantiating a variable `x` of type `e`:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.165263Z", "iopub.status.busy": "2024-04-08T14:10:36.165189Z", "iopub.status.idle": "2024-04-08T14:10:36.167234Z", "shell.execute_reply": "2024-04-08T14:10:36.167043Z" } }, "outputs": [ { "data": { "text/markdown": [ "${x}_{e}\\:=\\:{x}_{e}$" ], "text/plain": [ "x_e = x_e" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%lamb \n", "x = x_e # use cell magic" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.168302Z", "iopub.status.busy": "2024-04-08T14:10:36.168229Z", "iopub.status.idle": "2024-04-08T14:10:36.170190Z", "shell.execute_reply": "2024-04-08T14:10:36.169994Z" } }, "outputs": [ { "data": { "text/latex": [ "${x}_{e}$" ], "text/plain": [ "x_e" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = te(\"x_e\") # use factory function to parse string\n", "x" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.171348Z", "iopub.status.busy": "2024-04-08T14:10:36.171270Z", "iopub.status.idle": "2024-04-08T14:10:36.173089Z", "shell.execute_reply": "2024-04-08T14:10:36.172901Z" } }, "outputs": [ { "data": { "text/latex": [ "${x}_{e}$" ], "text/plain": [ "x_e" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = meta.TypedTerm(\"x\", types.type_e) # use object constructer\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Various convenience python operators are overloaded, including functional calls. Here is an example repeated from earlier in two forms:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.174178Z", "iopub.status.busy": "2024-04-08T14:10:36.174114Z", "iopub.status.idle": "2024-04-08T14:10:36.176968Z", "shell.execute_reply": "2024-04-08T14:10:36.176762Z" } }, "outputs": [ { "data": { "text/markdown": [ "${p2}_{t}\\:=\\:({p}_{t} \\wedge{} {Cat}({x}_{e})) \\rightarrow{} (\\exists{} y_{e} \\: . \\: {Dog}({y}))$" ], "text/plain": [ "p2_t = ((p_t & Cat_(x_e)) >> (Exists y_e: Dog_(y_e)))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%lamb\n", "p2 = (Cat_(x_e) & p_t) >> (Exists y: Dog_(y_e))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.178035Z", "iopub.status.busy": "2024-04-08T14:10:36.177955Z", "iopub.status.idle": "2024-04-08T14:10:36.180394Z", "shell.execute_reply": "2024-04-08T14:10:36.180196Z" } }, "outputs": [ { "data": { "text/latex": [ "$({Cat}({x}_{e}) \\wedge{} {p}_{t}) \\rightarrow{} (\\exists{} y_{e} \\: . \\: {Dog}({y}))$" ], "text/plain": [ "((Cat_(x_e) ∧ p_t) → (Exists y_e: Dog_(y_e)))" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 = (te(\"Cat_(x)\") & te(\"p_t\")) >> te(\"(Exists y: Dog_(y_e))\")\n", "p2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's examine in detail what happens when a function and argument combine." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.181553Z", "iopub.status.busy": "2024-04-08T14:10:36.181473Z", "iopub.status.idle": "2024-04-08T14:10:36.183867Z", "shell.execute_reply": "2024-04-08T14:10:36.183662Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO (core): Coerced guessed type for 'Cat_t' into , to match argument 'x_e'\n" ] }, { "data": { "text/latex": [ "$\\lambda{} x_{e} \\: . \\: {Cat}({x})$" ], "text/plain": [ "(λ x_e: Cat_(x_e))" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "catf = meta.LFun(te(\"x_e\"), te(\"Cat(x_e)\"))\n", "catf" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.184946Z", "iopub.status.busy": "2024-04-08T14:10:36.184866Z", "iopub.status.idle": "2024-04-08T14:10:36.186857Z", "shell.execute_reply": "2024-04-08T14:10:36.186674Z" } }, "outputs": [ { "data": { "text/latex": [ "${[\\lambda{} x_{e} \\: . \\: {Cat}({x})]}({y}_{e})$" ], "text/plain": [ "(λ x_e: Cat_(x_e))(y_e)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "catf(te(\"y_e\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Building a function-argument expression builds a complex, unreduced expression. This can be explicitly reduced (note that the `reduce_all()` function would be used to apply reduction recursively):" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.187971Z", "iopub.status.busy": "2024-04-08T14:10:36.187889Z", "iopub.status.idle": "2024-04-08T14:10:36.189960Z", "shell.execute_reply": "2024-04-08T14:10:36.189743Z" } }, "outputs": [ { "data": { "text/latex": [ "${Cat}({y}_{e})$" ], "text/plain": [ "Cat_(y_e)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "catf(te(\"y_e\")).reduce()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.191037Z", "iopub.status.busy": "2024-04-08T14:10:36.190962Z", "iopub.status.idle": "2024-04-08T14:10:36.193107Z", "shell.execute_reply": "2024-04-08T14:10:36.192908Z" } }, "outputs": [ { "data": { "text/html": [ "
${[\\lambda{} x_{e} \\: . \\: {Cat}({x})]}({y}_{e})$
Reduction
1.
${Cat}({y}_{e})$
" ], "text/plain": [ " 1. (λ x_e: Cat_(x_e))(y_e) (Reduction)\n", " 2. Cat_(y_e)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(catf(te(\"y_e\")).reduce()).derivation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The metalanguage supports some basic type inference. Type inference happens already on combination of a function and argument into an unreduced expression, not on beta-reduction." ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.194293Z", "iopub.status.busy": "2024-04-08T14:10:36.194211Z", "iopub.status.idle": "2024-04-08T14:10:36.198012Z", "shell.execute_reply": "2024-04-08T14:10:36.197813Z" } }, "outputs": [ { "data": { "text/markdown": [ "${ttest}_{\\left\\langle{}X,t\\right\\rangle{}}\\:=\\:\\lambda{} x_{X} \\: . \\: {P}_{\\left\\langle{}X,t\\right\\rangle{}}({x})$" ], "text/plain": [ "ttest_ = (λ x_X: P_(x_X))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "${tvar}_{t}\\:=\\:{y}_{t}$" ], "text/plain": [ "tvar_t = y_t" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "${[\\lambda{} x_{t} \\: . \\: {P}_{\\left\\langle{}t,t\\right\\rangle{}}({x})]}({y}_{t})$" ], "text/plain": [ "(λ x_t: P_(x_t))(y_t)" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%lamb ttest = L x_X : P_(x) # type \n", "%lamb tvar = y_t\n", "ttest(tvar)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " \n", "\n", "## Part 3: composition systems for an object language ##\n", "\n", "On top of the metalanguage are '**composition systems**' for modeling (step-by-step) semantic composition in an object language such as English. This is the part of the lambda notebook that tracks and manipulates mappings between object language elements (words, trees, etc) and denotations in the metalanguage. \n", "\n", "A composition at its core consists of a set of composition rules; the following cell defines a simple composition system that will be familiar to anyone who has taken a basic course in compositional semantics. (This example is just a redefinition of the default composition system.)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.199163Z", "iopub.status.busy": "2024-04-08T14:10:36.199079Z", "iopub.status.idle": "2024-04-08T14:10:36.201331Z", "shell.execute_reply": "2024-04-08T14:10:36.201119Z" } }, "outputs": [ { "data": { "text/html": [ "Composition system 'demo system'
Operations: {
    Binary composition rule FA, built on python function 'lamb.lang.fa_fun'
    Binary composition rule PM, built on python function 'lamb.lang.pm_fun'
    Binary composition rule PA, built on python function 'lamb.lang.pa_fun'
}" ], "text/plain": [ "Composition system: demo system" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# none of this is strictly necessary, the built-in library already provides effectively this system.\n", "fa = lang.BinaryCompositionOp(\"FA\", lang.fa_fun, reduce=True)\n", "pm = lang.BinaryCompositionOp(\"PM\", lang.pm_fun, commutative=False, reduce=True)\n", "pa = lang.BinaryCompositionOp(\"PA\", lang.pa_fun, allow_none=True)\n", "demo_hk_system = lang.CompositionSystem(name=\"demo system\", rules=[fa, pm, pa])\n", "lang.set_system(demo_hk_system)\n", "demo_hk_system" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Expressing denotations is done in a `%%lamb` cell, and almost always begins with lexical items. The following cell defines several lexical items that will be familiar from introductory exercises in the Heim & Kratzer 1998 textbook \"Semantics in Generative Grammar\". These definitions produce items that are subclasses of the class `Composable`." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.202462Z", "iopub.status.busy": "2024-04-08T14:10:36.202384Z", "iopub.status.idle": "2024-04-08T14:10:36.207210Z", "shell.execute_reply": "2024-04-08T14:10:36.207007Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO (core): Coerced guessed type for 'Cat_t' into , to match argument 'x_e'\n", "INFO (core): Coerced guessed type for 'Gray_t' into , to match argument 'x_e'\n", "INFO (core): Coerced guessed type for 'In_t' into <(e,e),t>, to match argument '(y_e, x_e)'\n" ] }, { "data": { "text/markdown": [ "$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x})$
\n", "$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Gray}({x})$
\n", "$[\\![\\text{\\textbf{john}}]\\!]^{}_{e} \\:=\\: {John}_{e}$
\n", "$[\\![\\text{\\textbf{julius}}]\\!]^{}_{e} \\:=\\: {Julius}_{e}$
\n", "$[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
\n", "$[\\![\\text{\\textbf{texas}}]\\!]^{}_{e} \\:=\\: {Texas}_{e}$
\n", "$[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$" ], "text/plain": [ "⟦cat⟧ = (λ x_e: Cat_(x_e))\n", "⟦gray⟧ = (λ x_e: Gray_(x_e))\n", "⟦john⟧ = John_e\n", "⟦julius⟧ = Julius_e\n", "⟦inP⟧ = (λ x_e: (λ y_e: In_<(e,e),t>(y_e, x_e)))\n", "⟦texas⟧ = Texas_e\n", "⟦isV⟧ = (λ p_: p_)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%lamb\n", "||cat|| = L x_e: Cat(x)\n", "||gray|| = L x_e: Gray(x)\n", "||john|| = John_e\n", "||julius|| = Julius_e\n", "||inP|| = L x_e : L y_e : In(y, x) # `in` is a reserved word in python\n", "||texas|| = Texas_e\n", "||isV|| = L p_ : p # `is` is a reserved word in python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the purely type-driven mode, composition is triggered by using the '`*`' operator on a `Composable`. This searches over the available composition operations in the system to see if any results can be had. `inP` and `texas` above should be able to compose using the FA rule:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.208394Z", "iopub.status.busy": "2024-04-08T14:10:36.208317Z", "iopub.status.idle": "2024-04-08T14:10:36.210559Z", "shell.execute_reply": "2024-04-08T14:10:36.210350Z" } }, "outputs": [ { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦inP⟧ * ⟦texas⟧ leads to: ⟦[inP texas]⟧ = (λ y_e: In_<(e,e),t>(y_e, Texas_e)) [by FA]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "inP * texas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On the other hand `isV` is looking for a property, so we shouldn't expect succesful composition. Below this I have given a complete sentence and shown some introspection on that composition result." ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.211708Z", "iopub.status.busy": "2024-04-08T14:10:36.211621Z", "iopub.status.idle": "2024-04-08T14:10:36.213832Z", "shell.execute_reply": "2024-04-08T14:10:36.213622Z" } }, "outputs": [ { "data": { "text/markdown": [ "Composition of \"[julius isV]\" failed:
\n", "    **TypeMismatch**: $[\\![\\text{\\textbf{julius}}]\\!]^{}_{e} \\:=\\: {Julius}_{e}$ and $[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$ conflict (Function Application)
\n", "    **TypeMismatch**: $[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$ and $[\\![\\text{\\textbf{julius}}]\\!]^{}_{e} \\:=\\: {Julius}_{e}$ conflict (Function Application)
\n", "    **TypeMismatch**: $[\\![\\text{\\textbf{julius}}]\\!]^{}_{e} \\:=\\: {Julius}_{e}$ and $[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$ conflict (Predicate Modification)
\n", "    **TypeMismatch**: $[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$ and $[\\![\\text{\\textbf{julius}}]\\!]^{}_{e} \\:=\\: {Julius}_{e}$ conflict (Predicate Modification)
\n", "    Composition failure (PA requires a valid binder) on: $[\\![\\text{\\textbf{julius}}]\\!]^{}_{e} \\:=\\: {Julius}_{e}$ * $[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
\n", "    Composition failure (PA requires a valid binder) on: $[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$ * $[\\![\\text{\\textbf{julius}}]\\!]^{}_{e} \\:=\\: {Julius}_{e}$
\n" ], "text/plain": [ "Composition of \"[julius isV]\" failed:\n", " TypeMismatch: `⟦julius⟧ = Julius_e` and `⟦isV⟧ = (λ p_: p_)` conflict (Function Application)\n", " TypeMismatch: `⟦isV⟧ = (λ p_: p_)` and `⟦julius⟧ = Julius_e` conflict (Function Application)\n", " TypeMismatch: `⟦julius⟧ = Julius_e` and `⟦isV⟧ = (λ p_: p_)` conflict (Predicate Modification)\n", " TypeMismatch: `⟦isV⟧ = (λ p_: p_)` and `⟦julius⟧ = Julius_e` conflict (Predicate Modification)\n", " Composition failure (PA requires a valid binder) on: ⟦julius⟧ = Julius_e * ⟦isV⟧ = (λ p_: p_)\n", " Composition failure (PA requires a valid binder) on: ⟦isV⟧ = (λ p_: p_) * ⟦julius⟧ = Julius_e" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "julius * isV # will fail due to type mismatches" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.214965Z", "iopub.status.busy": "2024-04-08T14:10:36.214895Z", "iopub.status.idle": "2024-04-08T14:10:36.218924Z", "shell.execute_reply": "2024-04-08T14:10:36.218737Z" } }, "outputs": [ { "data": { "image/svg+xml": [ "[julius [isV [inP texas]]]julius[isV [inP texas]]isV[inP texas]inPtexas" ], "text/plain": [ "Tree('[julius [isV [inP texas]]]', [Tree('julius', []), Tree('[isV [inP texas]]', [Tree('isV', []), Tree('[inP texas]', [Tree('inP', []), Tree('texas', [])])])])" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{[julius [isV [inP texas]]]}}]\\!]^{}_{t} \\:=\\: {In}({Julius}_{e}, {Texas}_{e})$" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦julius⟧ * ⟦[isV [inP texas]]⟧ leads to: ⟦[julius [isV [inP texas]]]⟧ = In_<(e,e),t>(Julius_e, Texas_e) [by FA]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sentence1 = julius * (isV * (inP * texas))\n", "display(sentence1[0].source_tree())\n", "sentence1" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.220045Z", "iopub.status.busy": "2024-04-08T14:10:36.219961Z", "iopub.status.idle": "2024-04-08T14:10:36.221925Z", "shell.execute_reply": "2024-04-08T14:10:36.221734Z" } }, "outputs": [ { "data": { "text/markdown": [ "Full composition trace. 1 path:
\n", "    Step 1: $[\\![\\text{\\textbf{julius}}]\\!]^{}_{e} \\:=\\: {Julius}_{e}$
\n", "    Step 2: $[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
\n", "    Step 3: $[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
\n", "    Step 4: $[\\![\\text{\\textbf{texas}}]\\!]^{}_{e} \\:=\\: {Texas}_{e}$
\n", "    Step 5: $[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$ * $[\\![\\text{\\textbf{texas}}]\\!]^{}_{e}$ leads to: $[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$ [by FA]
\n", "    Step 6: $[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$ * $[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$ leads to: $[\\![\\text{\\textbf{[isV [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$ [by FA]
\n", "    Step 7: $[\\![\\text{\\textbf{julius}}]\\!]^{}_{e}$ * $[\\![\\text{\\textbf{[isV [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$ leads to: $[\\![\\text{\\textbf{[julius [isV [inP texas]]]}}]\\!]^{}_{t} \\:=\\: {In}({Julius}_{e}, {Texas}_{e})$ [by FA]
\n" ], "text/plain": [ "Full composition trace. 1 path:
\n", "    Step 1: $[\\![\\text{\\textbf{julius}}]\\!]^{}_{e} \\:=\\: {Julius}_{e}$
\n", "    Step 2: $[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
\n", "    Step 3: $[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
\n", "    Step 4: $[\\![\\text{\\textbf{texas}}]\\!]^{}_{e} \\:=\\: {Texas}_{e}$
\n", "    Step 5: $[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$ * $[\\![\\text{\\textbf{texas}}]\\!]^{}_{e}$ leads to: $[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$ [by FA]
\n", "    Step 6: $[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$ * $[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$ leads to: $[\\![\\text{\\textbf{[isV [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$ [by FA]
\n", "    Step 7: $[\\![\\text{\\textbf{julius}}]\\!]^{}_{e}$ * $[\\![\\text{\\textbf{[isV [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$ leads to: $[\\![\\text{\\textbf{[julius [isV [inP texas]]]}}]\\!]^{}_{t} \\:=\\: {In}({Julius}_{e}, {Texas}_{e})$ [by FA]
" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence1.trace()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Composition will find all possible paths (beware of combinatorial explosion). I have temporarily disabled the fact that standard PM is symmetric/commutative (because of conjunction), to illustrate a case with multiple composition paths:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.223047Z", "iopub.status.busy": "2024-04-08T14:10:36.222979Z", "iopub.status.idle": "2024-04-08T14:10:36.226847Z", "shell.execute_reply": "2024-04-08T14:10:36.226631Z" } }, "outputs": [ { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{[gray cat]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$   (2 equivalent paths lead here)" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦gray⟧ * ⟦cat⟧ leads to: ⟦[gray cat]⟧ = (λ x_e: (Cat_(x_e) & Gray_(x_e))) [by PM] (1 equivalent paths not shown: PM) (2 equivalent paths lead here)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "gray * cat" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.227914Z", "iopub.status.busy": "2024-04-08T14:10:36.227834Z", "iopub.status.idle": "2024-04-08T14:10:36.234003Z", "shell.execute_reply": "2024-04-08T14:10:36.233817Z" } }, "outputs": [ { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{[gray [cat [inP texas]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$   (4 equivalent paths lead here)" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦gray⟧ * ⟦[cat [inP texas]]⟧ leads to: ⟦[gray [cat [inP texas]]]⟧ = (λ x_e: ((Cat_(x_e) & Gray_(x_e)) & In_<(e,e),t>(x_e, Texas_e))) [by PM] (1 equivalent paths not shown: PM) (4 equivalent paths lead here)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "gray * (cat * (inP * texas))" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.235127Z", "iopub.status.busy": "2024-04-08T14:10:36.235041Z", "iopub.status.idle": "2024-04-08T14:10:36.242027Z", "shell.execute_reply": "2024-04-08T14:10:36.241848Z" } }, "outputs": [ { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{[isV [a [[gray cat] [inP texas]]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$   (4 equivalent paths lead here)" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦isV⟧ * ⟦[a [[gray cat] [inP texas]]]⟧ leads to: ⟦[isV [a [[gray cat] [inP texas]]]]⟧ = (λ x_e: ((Cat_(x_e) & Gray_(x_e)) & In_<(e,e),t>(x_e, Texas_e))) [by FA] (4 equivalent paths lead here)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "a = lang.Item(\"a\", isV.content) # identity function for copula as well\n", "isV * (a * (gray * cat * (inP * texas)))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.243095Z", "iopub.status.busy": "2024-04-08T14:10:36.243028Z", "iopub.status.idle": "2024-04-08T14:10:36.250847Z", "shell.execute_reply": "2024-04-08T14:10:36.250670Z" } }, "outputs": [ { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{[julius [isV [a [[gray cat] [inP texas]]]]]}}]\\!]^{}_{t} \\:=\\: {Cat}({Julius}_{e}) \\wedge{} {Gray}({Julius}_{e}) \\wedge{} {In}({Julius}_{e}, {Texas}_{e})$   (4 equivalent paths lead here)" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦julius⟧ * ⟦[isV [a [[gray cat] [inP texas]]]]⟧ leads to: ⟦[julius [isV [a [[gray cat] [inP texas]]]]]⟧ = ((Cat_(Julius_e) & Gray_(Julius_e)) & In_<(e,e),t>(Julius_e, Texas_e)) [by FA] (4 equivalent paths lead here)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "np = ((gray * cat) * (inP * texas))\n", "vp = (isV * (a * np))\n", "sentence2 = julius * vp\n", "sentence2" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.251981Z", "iopub.status.busy": "2024-04-08T14:10:36.251907Z", "iopub.status.idle": "2024-04-08T14:10:36.253900Z", "shell.execute_reply": "2024-04-08T14:10:36.253709Z" } }, "outputs": [ { "data": { "text/latex": [ "$[\\![\\text{\\textbf{[julius [isV [inP texas]]]}}]\\!]^{}_{t} \\:=\\: {In}({Julius}_{e}, {Texas}_{e})$" ], "text/plain": [ "⟦[julius [isV [inP texas]]]⟧ = In_<(e,e),t>(Julius_e, Texas_e)" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence1.results[0]" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.254976Z", "iopub.status.busy": "2024-04-08T14:10:36.254911Z", "iopub.status.idle": "2024-04-08T14:10:36.257277Z", "shell.execute_reply": "2024-04-08T14:10:36.257068Z" } }, "outputs": [ { "data": { "text/html": [ "
$[\\![\\text{\\textbf{julius}}]\\!]^{}_{e}$
${Julius}_{e}$
*
$[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
*
$[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
*
$[\\![\\text{\\textbf{texas}}]\\!]^{}_{e}$
${Texas}_{e}$
[FA]
$[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[isV [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[julius [isV [inP texas]]]}}]\\!]^{}_{t}$
${In}({Julius}_{e}, {Texas}_{e})$
" ], "text/plain": [ "
$[\\![\\text{\\textbf{julius}}]\\!]^{}_{e}$
${Julius}_{e}$
*
$[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
*
$[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
*
$[\\![\\text{\\textbf{texas}}]\\!]^{}_{e}$
${Texas}_{e}$
[FA]
$[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[isV [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[julius [isV [inP texas]]]}}]\\!]^{}_{t}$
${In}({Julius}_{e}, {Texas}_{e})$
" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence1.results[0].tree()" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.258369Z", "iopub.status.busy": "2024-04-08T14:10:36.258284Z", "iopub.status.idle": "2024-04-08T14:10:36.260969Z", "shell.execute_reply": "2024-04-08T14:10:36.260758Z" } }, "outputs": [ { "data": { "text/html": [ "
$[\\![\\text{\\textbf{julius}}]\\!]^{}_{e}$
${Julius}_{e}$
*
$[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
*
$[\\![\\text{\\textbf{a}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
*
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Gray}({x})$
*
$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x})$
[
PM
(or: PM)
]
$[\\![\\text{\\textbf{[gray cat]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$
*
$[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
*
$[\\![\\text{\\textbf{texas}}]\\!]^{}_{e}$
${Texas}_{e}$
[FA]
$[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$
[
PM
(or: PM)
]
$[\\![\\text{\\textbf{[[gray cat] [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[a [[gray cat] [inP texas]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[isV [a [[gray cat] [inP texas]]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[julius [isV [a [[gray cat] [inP texas]]]]]}}]\\!]^{}_{t}$
${Cat}({Julius}_{e}) \\wedge{} {Gray}({Julius}_{e}) \\wedge{} {In}({Julius}_{e}, {Texas}_{e})$
" ], "text/plain": [ "
$[\\![\\text{\\textbf{julius}}]\\!]^{}_{e}$
${Julius}_{e}$
*
$[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
*
$[\\![\\text{\\textbf{a}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
*
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Gray}({x})$
*
$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x})$
[
PM
(or: PM)
]
$[\\![\\text{\\textbf{[gray cat]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$
*
$[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
*
$[\\![\\text{\\textbf{texas}}]\\!]^{}_{e}$
${Texas}_{e}$
[FA]
$[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$
[
PM
(or: PM)
]
$[\\![\\text{\\textbf{[[gray cat] [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[a [[gray cat] [inP texas]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[isV [a [[gray cat] [inP texas]]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[julius [isV [a [[gray cat] [inP texas]]]]]}}]\\!]^{}_{t}$
${Cat}({Julius}_{e}) \\wedge{} {Gray}({Julius}_{e}) \\wedge{} {In}({Julius}_{e}, {Texas}_{e})$
" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence2.results[0].tree()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the infamous exercise examples from Heim and Kratzer (names different):\n", "\n", " (1) Julius is a gray cat in Texas fond of John.\n", " \n", "First let's get rid of all the extra readings, to keep this simple." ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.262117Z", "iopub.status.busy": "2024-04-08T14:10:36.262041Z", "iopub.status.idle": "2024-04-08T14:10:36.263539Z", "shell.execute_reply": "2024-04-08T14:10:36.263334Z" } }, "outputs": [], "source": [ "demo_hk_system.get_rule(\"PM\").commutative = True" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.264610Z", "iopub.status.busy": "2024-04-08T14:10:36.264534Z", "iopub.status.idle": "2024-04-08T14:10:36.275824Z", "shell.execute_reply": "2024-04-08T14:10:36.275667Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO (core): Coerced guessed type for 'Fond_t' into , to match argument 'y_e'\n", "INFO (core): Coerced guessed type for 'Fond_(y_e)' into , to match argument 'x_e'\n" ] }, { "data": { "image/svg+xml": [ "[julius [isV [a [[[gray cat] [inP texas]] [fond [of john]]]]]]julius[isV [a [[[gray cat] [inP texas]] [fond [of john]]]]]isV[a [[[gray cat] [inP texas]] [fond [of john]]]]a[[[gray cat] [inP texas]] [fond [of john]]][[gray cat] [inP texas]][gray cat]graycat[inP texas]inPtexas[fond [of john]]fond[of john]ofjohn" ], "text/plain": [ "Tree('[julius [isV [a [[[gray cat] [inP texas]] [fond [of john]]]]]]', [Tree('julius', []), Tree('[isV [a [[[gray cat] [inP texas]] [fond [of john]]]]]', [Tree('isV', []), Tree('[a [[[gray cat] [inP texas]] [fond [of john]]]]', [Tree('a', []), Tree('[[[gray cat] [inP texas]] [fond [of john]]]', [Tree('[[gray cat] [inP texas]]', [Tree('[gray cat]', [Tree('gray', []), Tree('cat', [])]), Tree('[inP texas]', [Tree('inP', []), Tree('texas', [])])]), Tree('[fond [of john]]', [Tree('fond', []), Tree('[of john]', [Tree('of', []), Tree('john', [])])])])])])])" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{[julius [isV [a [[[gray cat] [inP texas]] [fond [of john]]]]]]}}]\\!]^{}_{t} \\:=\\: {Cat}({Julius}_{e}) \\wedge{} {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({Julius}_{e})({John}_{e}) \\wedge{} {Gray}({Julius}_{e}) \\wedge{} {In}({Julius}_{e}, {Texas}_{e})$" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦julius⟧ * ⟦[isV [a [[[gray cat] [inP texas]] [fond [of john]]]]]⟧ leads to: ⟦[julius [isV [a [[[gray cat] [inP texas]] [fond [of john]]]]]]⟧ = (((Cat_(Julius_e) & Fond_>(Julius_e)(John_e)) & Gray_(Julius_e)) & In_<(e,e),t>(Julius_e, Texas_e)) [by FA]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fond = lang.Item(\"fond\", \"L x_e : L y_e : Fond(y)(x)\")\n", "ofP = lang.Item(\"of\", \"L x_e : x\")\n", "sentence3 = julius * (isV * (a * (((gray * cat) * (inP * texas)) * (fond * (ofP * john)))))\n", "display(sentence3[0].source_tree())\n", "sentence3" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.276915Z", "iopub.status.busy": "2024-04-08T14:10:36.276839Z", "iopub.status.idle": "2024-04-08T14:10:36.279882Z", "shell.execute_reply": "2024-04-08T14:10:36.279679Z" } }, "outputs": [ { "data": { "text/html": [ "1 composition path:
$[\\![\\text{\\textbf{julius}}]\\!]^{}_{e}$
${Julius}_{e}$
*
$[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
*
$[\\![\\text{\\textbf{a}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
*
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Gray}({x})$
*
$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x})$
[PM]
$[\\![\\text{\\textbf{[gray cat]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$
*
$[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
*
$[\\![\\text{\\textbf{texas}}]\\!]^{}_{e}$
${Texas}_{e}$
[FA]
$[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$
[PM]
$[\\![\\text{\\textbf{[[gray cat] [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
*
$[\\![\\text{\\textbf{fond}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({y})({x})$
*
$[\\![\\text{\\textbf{of}}]\\!]^{}_{\\left\\langle{}e,e\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {x}$
*
$[\\![\\text{\\textbf{john}}]\\!]^{}_{e}$
${John}_{e}$
[FA]
$[\\![\\text{\\textbf{[of john]}}]\\!]^{}_{e}$
${John}_{e}$
[FA]
$[\\![\\text{\\textbf{[fond [of john]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({y})({John}_{e})$
[PM]
$[\\![\\text{\\textbf{[[[gray cat] [inP texas]] [fond [of john]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({x})({John}_{e}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[a [[[gray cat] [inP texas]] [fond [of john]]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({x})({John}_{e}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[isV [a [[[gray cat] [inP texas]] [fond [of john]]]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({x})({John}_{e}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[julius [isV [a [[[gray cat] [inP texas]] [fond [of john]]]]]]}}]\\!]^{}_{t}$
${Cat}({Julius}_{e}) \\wedge{} {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({Julius}_{e})({John}_{e}) \\wedge{} {Gray}({Julius}_{e}) \\wedge{} {In}({Julius}_{e}, {Texas}_{e})$


" ], "text/plain": [ "1 composition path:
$[\\![\\text{\\textbf{julius}}]\\!]^{}_{e}$
${Julius}_{e}$
*
$[\\![\\text{\\textbf{isV}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
*
$[\\![\\text{\\textbf{a}}]\\!]^{}_{\\left\\langle{}\\left\\langle{}e,t\\right\\rangle{},\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} p_{\\left\\langle{}e,t\\right\\rangle{}} \\: . \\: {p}$
*
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Gray}({x})$
*
$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x})$
[PM]
$[\\![\\text{\\textbf{[gray cat]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$
*
$[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
*
$[\\![\\text{\\textbf{texas}}]\\!]^{}_{e}$
${Texas}_{e}$
[FA]
$[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$
[PM]
$[\\![\\text{\\textbf{[[gray cat] [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
*
$[\\![\\text{\\textbf{fond}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({y})({x})$
*
$[\\![\\text{\\textbf{of}}]\\!]^{}_{\\left\\langle{}e,e\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {x}$
*
$[\\![\\text{\\textbf{john}}]\\!]^{}_{e}$
${John}_{e}$
[FA]
$[\\![\\text{\\textbf{[of john]}}]\\!]^{}_{e}$
${John}_{e}$
[FA]
$[\\![\\text{\\textbf{[fond [of john]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({y})({John}_{e})$
[PM]
$[\\![\\text{\\textbf{[[[gray cat] [inP texas]] [fond [of john]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({x})({John}_{e}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[a [[[gray cat] [inP texas]] [fond [of john]]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({x})({John}_{e}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[isV [a [[[gray cat] [inP texas]] [fond [of john]]]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({x})({John}_{e}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
[FA]
$[\\![\\text{\\textbf{[julius [isV [a [[[gray cat] [inP texas]] [fond [of john]]]]]]}}]\\!]^{}_{t}$
${Cat}({Julius}_{e}) \\wedge{} {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({Julius}_{e})({John}_{e}) \\wedge{} {Gray}({Julius}_{e}) \\wedge{} {In}({Julius}_{e}, {Texas}_{e})$


" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence3.tree()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The _Composite_ class subclasses _nltk.Tree_, and so supports the things that class does. E.g. []-based paths:" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.281034Z", "iopub.status.busy": "2024-04-08T14:10:36.280945Z", "iopub.status.idle": "2024-04-08T14:10:36.283622Z", "shell.execute_reply": "2024-04-08T14:10:36.283425Z" } }, "outputs": [ { "data": { "text/html": [ "
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Gray}({x})$
*
$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x})$
[PM]
$[\\![\\text{\\textbf{[gray cat]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$
*
$[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
*
$[\\![\\text{\\textbf{texas}}]\\!]^{}_{e}$
${Texas}_{e}$
[FA]
$[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$
[PM]
$[\\![\\text{\\textbf{[[gray cat] [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
*
$[\\![\\text{\\textbf{fond}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({y})({x})$
*
$[\\![\\text{\\textbf{of}}]\\!]^{}_{\\left\\langle{}e,e\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {x}$
*
$[\\![\\text{\\textbf{john}}]\\!]^{}_{e}$
${John}_{e}$
[FA]
$[\\![\\text{\\textbf{[of john]}}]\\!]^{}_{e}$
${John}_{e}$
[FA]
$[\\![\\text{\\textbf{[fond [of john]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({y})({John}_{e})$
[PM]
$[\\![\\text{\\textbf{[[[gray cat] [inP texas]] [fond [of john]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({x})({John}_{e}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
" ], "text/plain": [ "
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Gray}({x})$
*
$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x})$
[PM]
$[\\![\\text{\\textbf{[gray cat]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$
*
$[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
*
$[\\![\\text{\\textbf{texas}}]\\!]^{}_{e}$
${Texas}_{e}$
[FA]
$[\\![\\text{\\textbf{[inP texas]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {Texas}_{e})$
[PM]
$[\\![\\text{\\textbf{[[gray cat] [inP texas]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
*
$[\\![\\text{\\textbf{fond}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({y})({x})$
*
$[\\![\\text{\\textbf{of}}]\\!]^{}_{\\left\\langle{}e,e\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {x}$
*
$[\\![\\text{\\textbf{john}}]\\!]^{}_{e}$
${John}_{e}$
[FA]
$[\\![\\text{\\textbf{[of john]}}]\\!]^{}_{e}$
${John}_{e}$
[FA]
$[\\![\\text{\\textbf{[fond [of john]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({y})({John}_{e})$
[PM]
$[\\![\\text{\\textbf{[[[gray cat] [inP texas]] [fond [of john]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Fond}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}({x})({John}_{e}) \\wedge{} {Gray}({x}) \\wedge{} {In}({x}, {Texas}_{e})$
" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "parse_tree3 = sentence3.results[0]\n", "parse_tree3[1][1][1].tree()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is support for traces and indexed pronouns, using the PA rule. (The implementation may not be what you expect.)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.284766Z", "iopub.status.busy": "2024-04-08T14:10:36.284697Z", "iopub.status.idle": "2024-04-08T14:10:36.287983Z", "shell.execute_reply": "2024-04-08T14:10:36.287781Z" } }, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/latex": [ "$[\\![\\text{\\textbf{t}}_{23}]\\!]^{}_{e} \\:=\\: {var23}_{e}$" ], "text/plain": [ "⟦t23⟧ = var23_e" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "" ], "text/latex": [ "$[\\![\\text{\\textbf{t}}_{5}]\\!]^{}_{e} \\:=\\: {var5}_{e}$" ], "text/plain": [ "⟦t5⟧ = var5_e" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "" ], "text/latex": [ "$[\\![\\text{\\textbf{23}}]\\!]^{}\\text{ [vacuous]}$" ], "text/plain": [ "⟦23⟧ = None" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "binder = lang.Binder(23)\n", "binder2 = lang.Binder(5)\n", "t = lang.Trace(23, types.type_e)\n", "t2 = lang.Trace(5)\n", "display(t, t2, binder)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.289091Z", "iopub.status.busy": "2024-04-08T14:10:36.289012Z", "iopub.status.idle": "2024-04-08T14:10:36.291125Z", "shell.execute_reply": "2024-04-08T14:10:36.290929Z" } }, "outputs": [ { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{[t23 gray]}}]\\!]^{}_{t} \\:=\\: {Gray}({var23}_{e})$" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦t23⟧ * ⟦gray⟧ leads to: ⟦[t23 gray]⟧ = Gray_(var23_e) [by FA]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "((t * gray))" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.292203Z", "iopub.status.busy": "2024-04-08T14:10:36.292122Z", "iopub.status.idle": "2024-04-08T14:10:36.296372Z", "shell.execute_reply": "2024-04-08T14:10:36.296190Z" } }, "outputs": [ { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{[23 [5 [t23 [inP t5]]]]}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} x1_{e} \\: . \\: \\lambda{} x_{e} \\: . \\: {In}({x1}, {x})$" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦23⟧ * ⟦[5 [t23 [inP t5]]]⟧ leads to: ⟦[23 [5 [t23 [inP t5]]]]⟧ = (λ x1_e: (λ x_e: In_<(e,e),t>(x1_e, x_e))) [by PA]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{[5 [23 [t23 [inP t5]]]]}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} x1_{e} \\: . \\: \\lambda{} x_{e} \\: . \\: {In}({x}, {x1})$" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦5⟧ * ⟦[23 [t23 [inP t5]]]⟧ leads to: ⟦[5 [23 [t23 [inP t5]]]]⟧ = (λ x1_e: (λ x_e: In_<(e,e),t>(x_e, x1_e))) [by PA]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "b1 = (binder * (binder2 * (t * (inP * t2))))\n", "b2 = (binder2 * (binder * (t * (inP * t2))))\n", "display(b1, b2)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.297489Z", "iopub.status.busy": "2024-04-08T14:10:36.297408Z", "iopub.status.idle": "2024-04-08T14:10:36.299427Z", "shell.execute_reply": "2024-04-08T14:10:36.299241Z" } }, "outputs": [ { "data": { "text/markdown": [ "Full composition trace. 1 path:
\n", "    Step 1: $[\\![\\text{\\textbf{23}}]\\!]^{}\\text{ [vacuous]}$
\n", "    Step 2: $[\\![\\text{\\textbf{5}}]\\!]^{}\\text{ [vacuous]}$
\n", "    Step 3: $[\\![\\text{\\textbf{t}}_{23}]\\!]^{}_{e} \\:=\\: {var23}_{e}$
\n", "    Step 4: $[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
\n", "    Step 5: $[\\![\\text{\\textbf{t}}_{5}]\\!]^{}_{e} \\:=\\: {var5}_{e}$
\n", "    Step 6: $[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$ * $[\\![\\text{\\textbf{t}}_{5}]\\!]^{}_{e}$ leads to: $[\\![\\text{\\textbf{[inP t5]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} y_{e} \\: . \\: {In}({y}, {var5}_{e})$ [by FA]
\n", "    Step 7: $[\\![\\text{\\textbf{t}}_{23}]\\!]^{}_{e}$ * $[\\![\\text{\\textbf{[inP t5]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$ leads to: $[\\![\\text{\\textbf{[t23 [inP t5]]}}]\\!]^{}_{t} \\:=\\: {In}({var23}_{e}, {var5}_{e})$ [by FA]
\n", "    Step 8: $[\\![\\text{\\textbf{5}}]\\!]^{}$ * $[\\![\\text{\\textbf{[t23 [inP t5]]}}]\\!]^{}_{t}$ leads to: $[\\![\\text{\\textbf{[5 [t23 [inP t5]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {In}({var23}_{e}, {x})$ [by PA]
\n", "    Step 9: $[\\![\\text{\\textbf{23}}]\\!]^{}$ * $[\\![\\text{\\textbf{[5 [t23 [inP t5]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$ leads to: $[\\![\\text{\\textbf{[23 [5 [t23 [inP t5]]]]}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} x1_{e} \\: . \\: \\lambda{} x_{e} \\: . \\: {In}({x1}, {x})$ [by PA]
\n" ], "text/plain": [ "Full composition trace. 1 path:
\n", "    Step 1: $[\\![\\text{\\textbf{23}}]\\!]^{}\\text{ [vacuous]}$
\n", "    Step 2: $[\\![\\text{\\textbf{5}}]\\!]^{}\\text{ [vacuous]}$
\n", "    Step 3: $[\\![\\text{\\textbf{t}}_{23}]\\!]^{}_{e} \\:=\\: {var23}_{e}$
\n", "    Step 4: $[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
\n", "    Step 5: $[\\![\\text{\\textbf{t}}_{5}]\\!]^{}_{e} \\:=\\: {var5}_{e}$
\n", "    Step 6: $[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$ * $[\\![\\text{\\textbf{t}}_{5}]\\!]^{}_{e}$ leads to: $[\\![\\text{\\textbf{[inP t5]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} y_{e} \\: . \\: {In}({y}, {var5}_{e})$ [by FA]
\n", "    Step 7: $[\\![\\text{\\textbf{t}}_{23}]\\!]^{}_{e}$ * $[\\![\\text{\\textbf{[inP t5]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$ leads to: $[\\![\\text{\\textbf{[t23 [inP t5]]}}]\\!]^{}_{t} \\:=\\: {In}({var23}_{e}, {var5}_{e})$ [by FA]
\n", "    Step 8: $[\\![\\text{\\textbf{5}}]\\!]^{}$ * $[\\![\\text{\\textbf{[t23 [inP t5]]}}]\\!]^{}_{t}$ leads to: $[\\![\\text{\\textbf{[5 [t23 [inP t5]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {In}({var23}_{e}, {x})$ [by PA]
\n", "    Step 9: $[\\![\\text{\\textbf{23}}]\\!]^{}$ * $[\\![\\text{\\textbf{[5 [t23 [inP t5]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$ leads to: $[\\![\\text{\\textbf{[23 [5 [t23 [inP t5]]]]}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}} \\:=\\: \\lambda{} x1_{e} \\: . \\: \\lambda{} x_{e} \\: . \\: {In}({x1}, {x})$ [by PA]
" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b1.trace()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.300520Z", "iopub.status.busy": "2024-04-08T14:10:36.300448Z", "iopub.status.idle": "2024-04-08T14:10:36.302922Z", "shell.execute_reply": "2024-04-08T14:10:36.302729Z" } }, "outputs": [ { "data": { "text/html": [ "
$[\\![\\text{\\textbf{23}}]\\!]^{}$
[idx: 23]
*
$[\\![\\text{\\textbf{5}}]\\!]^{}$
[idx: 5]
*
$[\\![\\text{\\textbf{t}}_{23}]\\!]^{}_{e}$
${var23}_{e}$
*
$[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
*
$[\\![\\text{\\textbf{t}}_{5}]\\!]^{}_{e}$
${var5}_{e}$
[FA]
$[\\![\\text{\\textbf{[inP t5]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {var5}_{e})$
[FA]
$[\\![\\text{\\textbf{[t23 [inP t5]]}}]\\!]^{}_{t}$
${In}({var23}_{e}, {var5}_{e})$
[PA]
$[\\![\\text{\\textbf{[5 [t23 [inP t5]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {In}({var23}_{e}, {x})$
[PA]
$[\\![\\text{\\textbf{[23 [5 [t23 [inP t5]]]]}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x1_{e} \\: . \\: \\lambda{} x_{e} \\: . \\: {In}({x1}, {x})$
" ], "text/plain": [ "
$[\\![\\text{\\textbf{23}}]\\!]^{}$
[idx: 23]
*
$[\\![\\text{\\textbf{5}}]\\!]^{}$
[idx: 5]
*
$[\\![\\text{\\textbf{t}}_{23}]\\!]^{}_{e}$
${var23}_{e}$
*
$[\\![\\text{\\textbf{inP}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: \\lambda{} y_{e} \\: . \\: {In}({y}, {x})$
*
$[\\![\\text{\\textbf{t}}_{5}]\\!]^{}_{e}$
${var5}_{e}$
[FA]
$[\\![\\text{\\textbf{[inP t5]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} y_{e} \\: . \\: {In}({y}, {var5}_{e})$
[FA]
$[\\![\\text{\\textbf{[t23 [inP t5]]}}]\\!]^{}_{t}$
${In}({var23}_{e}, {var5}_{e})$
[PA]
$[\\![\\text{\\textbf{[5 [t23 [inP t5]]]}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {In}({var23}_{e}, {x})$
[PA]
$[\\![\\text{\\textbf{[23 [5 [t23 [inP t5]]]]}}]\\!]^{}_{\\left\\langle{}e,\\left\\langle{}e,t\\right\\rangle{}\\right\\rangle{}}$
$\\lambda{} x1_{e} \\: . \\: \\lambda{} x_{e} \\: . \\: {In}({x1}, {x})$
" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b1.results[0].tree()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Composition in tree structures\n", "\n", "Some in-progress work: implementing tree-based computation, and top-down/deferred computation\n", "\n", "* using nltk Tree objects.\n", "* system for deferred / uncertain types -- basic inference over unknown types\n", "* arbitrary order of composition expansion. (Of course, some orders will be far less efficient!)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.304050Z", "iopub.status.busy": "2024-04-08T14:10:36.303977Z", "iopub.status.idle": "2024-04-08T14:10:36.305448Z", "shell.execute_reply": "2024-04-08T14:10:36.305265Z" } }, "outputs": [], "source": [ "lang.set_system(lang.hk_system)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.306521Z", "iopub.status.busy": "2024-04-08T14:10:36.306442Z", "iopub.status.idle": "2024-04-08T14:10:36.308933Z", "shell.execute_reply": "2024-04-08T14:10:36.308719Z" } }, "outputs": [ { "data": { "text/markdown": [ "$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Gray}({x})$
\n", "$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x})$" ], "text/plain": [ "⟦gray⟧ = (λ x_e: Gray_(x_e))\n", "⟦cat⟧ = (λ x_e: Cat_(x_e))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%lamb\n", "||gray|| = L x_e : Gray_(x)\n", "||cat|| = L x_e : Cat_(x)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.310041Z", "iopub.status.busy": "2024-04-08T14:10:36.309961Z", "iopub.status.idle": "2024-04-08T14:10:36.312218Z", "shell.execute_reply": "2024-04-08T14:10:36.312019Z" } }, "outputs": [ { "data": { "image/svg+xml": [ "SNPVP" ], "text/plain": [ "Tree('S', ['NP', 'VP'])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t2 = Tree(\"S\", [\"NP\", \"VP\"])\n", "t2" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.313338Z", "iopub.status.busy": "2024-04-08T14:10:36.313267Z", "iopub.status.idle": "2024-04-08T14:10:36.317626Z", "shell.execute_reply": "2024-04-08T14:10:36.317429Z" } }, "outputs": [ { "data": { "text/html": [ "3 composition paths:
\n", "Path [0]:
\n", "
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\forall{}X}$
*
$[\\![\\text{\\textbf{VP}}]\\!]^{}_{\\forall{}X}$
[FA/left]
$[\\![\\text{\\textbf{S}}]\\!]^{}_{X'}$
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}X,X'\\right\\rangle{}}([\\![\\text{\\textbf{VP}}]\\!]^{}_{X})$


Path [1]:
\n", "
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\forall{}X}$
*
$[\\![\\text{\\textbf{VP}}]\\!]^{}_{\\forall{}X}$
[FA/right]
$[\\![\\text{\\textbf{S}}]\\!]^{}_{X'}$
$[\\![\\text{\\textbf{VP}}]\\!]^{}_{\\left\\langle{}X,X'\\right\\rangle{}}([\\![\\text{\\textbf{NP}}]\\!]^{}_{X})$


Path [2]:
\n", "
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\forall{}X}$
*
$[\\![\\text{\\textbf{VP}}]\\!]^{}_{\\forall{}X}$
[PM]
$[\\![\\text{\\textbf{S}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: [\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}({x}) \\wedge{} [\\![\\text{\\textbf{VP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}({x})$


" ], "text/plain": [ "3 composition paths:
\n", "Path [0]:
\n", "
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\forall{}X}$
*
$[\\![\\text{\\textbf{VP}}]\\!]^{}_{\\forall{}X}$
[FA/left]
$[\\![\\text{\\textbf{S}}]\\!]^{}_{X'}$
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}X,X'\\right\\rangle{}}([\\![\\text{\\textbf{VP}}]\\!]^{}_{X})$


Path [1]:
\n", "
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\forall{}X}$
*
$[\\![\\text{\\textbf{VP}}]\\!]^{}_{\\forall{}X}$
[FA/right]
$[\\![\\text{\\textbf{S}}]\\!]^{}_{X'}$
$[\\![\\text{\\textbf{VP}}]\\!]^{}_{\\left\\langle{}X,X'\\right\\rangle{}}([\\![\\text{\\textbf{NP}}]\\!]^{}_{X})$


Path [2]:
\n", "
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\forall{}X}$
*
$[\\![\\text{\\textbf{VP}}]\\!]^{}_{\\forall{}X}$
[PM]
$[\\![\\text{\\textbf{S}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: [\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}({x}) \\wedge{} [\\![\\text{\\textbf{VP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}({x})$


" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t2 = Tree(\"S\", [\"NP\", \"VP\"])\n", "r2 = lang.compose(t2)\n", "r2.tree()\n", "r2.paths()" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.318700Z", "iopub.status.busy": "2024-04-08T14:10:36.318622Z", "iopub.status.idle": "2024-04-08T14:10:36.321102Z", "shell.execute_reply": "2024-04-08T14:10:36.320902Z" } }, "outputs": [ { "data": { "image/svg+xml": [ "NPgrayNcat" ], "text/plain": [ "Tree('NP', ['gray', Tree('N', ['cat'])])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Tree = lamb.utils.get_tree_class()\n", "t = Tree(\"NP\", [\"gray\", Tree(\"N\", [\"cat\"])])\n", "t" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.322209Z", "iopub.status.busy": "2024-04-08T14:10:36.322127Z", "iopub.status.idle": "2024-04-08T14:10:36.326262Z", "shell.execute_reply": "2024-04-08T14:10:36.326057Z" } }, "outputs": [ { "data": { "text/markdown": [ "3 composition paths. Results:
\n", "    [0]: $[\\![\\text{\\textbf{NP}}]\\!]^{}_{X'} \\:=\\: [\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}X,X'\\right\\rangle{}}([\\![\\text{\\textbf{N}}]\\!]^{}_{X})$
\n", "    [1]: $[\\![\\text{\\textbf{NP}}]\\!]^{}_{X'} \\:=\\: [\\![\\text{\\textbf{N}}]\\!]^{}_{\\left\\langle{}X,X'\\right\\rangle{}}([\\![\\text{\\textbf{gray}}]\\!]^{}_{X})$
\n", "    [2]: $[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: [\\![\\text{\\textbf{N}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}({x}) \\wedge{} [\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}({x})$" ], "text/plain": [ "3 composition paths. Results:\n", " [0]: ⟦gray⟧ * ⟦N⟧ leads to: ⟦NP⟧ = ⟦gray⟧(⟦N⟧) [by FA/left]\n", " [1]: ⟦gray⟧ * ⟦N⟧ leads to: ⟦NP⟧ = ⟦N⟧(⟦gray⟧) [by FA/right]\n", " [2]: ⟦gray⟧ * ⟦N⟧ leads to: ⟦NP⟧ = (λ x_e: (⟦N⟧(x_e) & ⟦gray⟧(x_e))) [by PM]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t2 = lang.CompositionTree.tree_factory(t)\n", "r = lang.compose(t2)\n", "r" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.327403Z", "iopub.status.busy": "2024-04-08T14:10:36.327317Z", "iopub.status.idle": "2024-04-08T14:10:36.329466Z", "shell.execute_reply": "2024-04-08T14:10:36.329273Z" } }, "outputs": [ { "data": { "text/html": [ "
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\forall{}X}$
*
$[\\![\\text{\\textbf{N}}]\\!]^{}_{\\forall{}X}$
$[\\![\\text{\\textbf{NP}}]\\!]$
[path 0]:
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{X'} \\:=\\: [\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}X,X'\\right\\rangle{}}([\\![\\text{\\textbf{N}}]\\!]^{}_{X})$
[path 1]:
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{X'} \\:=\\: [\\![\\text{\\textbf{N}}]\\!]^{}_{\\left\\langle{}X,X'\\right\\rangle{}}([\\![\\text{\\textbf{gray}}]\\!]^{}_{X})$
[path 2]:
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: [\\![\\text{\\textbf{N}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}({x}) \\wedge{} [\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}({x})$
" ], "text/plain": [ "
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\forall{}X}$
*
$[\\![\\text{\\textbf{N}}]\\!]^{}_{\\forall{}X}$
$[\\![\\text{\\textbf{NP}}]\\!]$
[path 0]:
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{X'} \\:=\\: [\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}X,X'\\right\\rangle{}}([\\![\\text{\\textbf{N}}]\\!]^{}_{X})$
[path 1]:
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{X'} \\:=\\: [\\![\\text{\\textbf{N}}]\\!]^{}_{\\left\\langle{}X,X'\\right\\rangle{}}([\\![\\text{\\textbf{gray}}]\\!]^{}_{X})$
[path 2]:
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: [\\![\\text{\\textbf{N}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}({x}) \\wedge{} [\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}({x})$
" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.tree()" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.330566Z", "iopub.status.busy": "2024-04-08T14:10:36.330485Z", "iopub.status.idle": "2024-04-08T14:10:36.335107Z", "shell.execute_reply": "2024-04-08T14:10:36.334908Z" } }, "outputs": [ { "data": { "text/markdown": [ "1 composition path. Result:
\n", "    [0]: $[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$" ], "text/plain": [ "1 composition path. Result:\n", " [0]: ⟦gray⟧ * ⟦N⟧ leads to: ⟦NP⟧ = (λ x_e: (Cat_(x_e) & Gray_(x_e))) [by PM]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "r = lang.get_system().expand_all(t2)\n", "r" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.336218Z", "iopub.status.busy": "2024-04-08T14:10:36.336136Z", "iopub.status.idle": "2024-04-08T14:10:36.338440Z", "shell.execute_reply": "2024-04-08T14:10:36.338223Z" } }, "outputs": [ { "data": { "text/html": [ "
$[\\![\\text{\\textbf{gray}}]\\!]$
[path 0]:
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Gray}({x})$
*
$[\\![\\text{\\textbf{cat}}]\\!]$
[path 0]:
$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x})$
$[\\![\\text{\\textbf{N}}]\\!]$
[path 0]:
$[\\![\\text{\\textbf{N}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x})$
$[\\![\\text{\\textbf{NP}}]\\!]$
[path 0]:
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$
" ], "text/plain": [ "
$[\\![\\text{\\textbf{gray}}]\\!]$
[path 0]:
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Gray}({x})$
*
$[\\![\\text{\\textbf{cat}}]\\!]$
[path 0]:
$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x})$
$[\\![\\text{\\textbf{N}}]\\!]$
[path 0]:
$[\\![\\text{\\textbf{N}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x})$
$[\\![\\text{\\textbf{NP}}]\\!]$
[path 0]:
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}} \\:=\\: \\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$
" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.tree()" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "execution": { "iopub.execute_input": "2024-04-08T14:10:36.339580Z", "iopub.status.busy": "2024-04-08T14:10:36.339498Z", "iopub.status.idle": "2024-04-08T14:10:36.341666Z", "shell.execute_reply": "2024-04-08T14:10:36.341487Z" } }, "outputs": [ { "data": { "text/html": [ "1 composition path:
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
[Lexicon]
$\\lambda{} x_{e} \\: . \\: {Gray}({x})$
*
$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
[Lexicon]
$\\lambda{} x_{e} \\: . \\: {Cat}({x})$
[NN]
$[\\![\\text{\\textbf{N}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x})$
[PM]
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$


" ], "text/plain": [ "1 composition path:
$[\\![\\text{\\textbf{gray}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
[Lexicon]
$\\lambda{} x_{e} \\: . \\: {Gray}({x})$
*
$[\\![\\text{\\textbf{cat}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
[Lexicon]
$\\lambda{} x_{e} \\: . \\: {Cat}({x})$
[NN]
$[\\![\\text{\\textbf{N}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x})$
[PM]
$[\\![\\text{\\textbf{NP}}]\\!]^{}_{\\left\\langle{}e,t\\right\\rangle{}}$
$\\lambda{} x_{e} \\: . \\: {Cat}({x}) \\wedge{} {Gray}({x})$


" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.paths()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " \n", "\n", "## Some future projects, non-exhaustive ##\n", "\n", "* complete fragment of Heim and Kratzer\n", "* In general: more fragments!\n", "* extend fragment coverage. Some interesting targets where interactivity would be useful to understanding:\n", " * Compositional hamblin semantics (partial)\n", " * Compositional DRT (partial)\n", " * QR\n", "* underlying model theory.\n", "* various improvements to the graphics -- trees (d3? graphviz?), interactive widgets, ...\n", "* full latex output (trees in tikz-qtree and so on).\n", "\n", "Longer term:\n", "\n", "* integration with SymPy (?)\n", "* deeper integration with nltk.\n", "* parsing that makes less use of python `eval`, and is generally less ad-hoc.\n", " * this is an issue where in principle, a language like Haskell is a better choice than python. But I think the usability / robustness of python and its libraries has the edge here overall, not to mention ipython notebook...\n", "* toy spatial language system\n", "* side-by-side comparison of e.g. multiple analyses of presupposition projection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Lambda Notebook (Python 3)", "language": "python", "name": "lambda-notebook" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.0" } }, "nbformat": 4, "nbformat_minor": 4 }