#!/usr/bin/env python # coding: utf-8 # # The IPython Lambda Notebook # # Welcome to the [IPython Lambda Notebook](http://lambdanotebook.com/)! This is a tool based on the [Jupyter Notebook](http://jupyter.org/), tailored for doing calculations of the sort involved in linguistic semantics. The focus is mainly on those calculations involving the typed lambda calculus that are typically involved in theories of *compositionality*. The documentation mostly assumes some basic knowledge of compositionality (though you can see [this tutorial](/notebooks/An%20introduction%20to%20compositionality%20via%20%22two%20plus%20two%20is%20four%22.ipynb) if you would like a review.) The overall aim of the lambda notebook is to provide facilities for writing '*digital fragments*' -- working implementations of a compositional system developed in the course of linguistics research. The Lambda Notebook is very much in an early state, what might be called a public alpha, but because it is based on Jupyter, it inherits many extremely powerful tools aimed at scientific computing. # # 1. [Basics](#1.-Basics) # 2. [Background of IPython notebook](#2.-Interface-and-background-on-Jupyter) # # Section 2 is recommended reading if you have not used IPython/Jupyter before (and especially if you haven't used Python). # # Quick links: [Quick reference.ipynb](/notebooks/documentation/Quick reference.ipynb) | [Metalanguage documentation.ipynb](/notebooks/documentation/Metalanguage documentation.ipynb) # ## 1. Basics # # Jupyter Notebooks are structured into cells. Each cell contains code or text (typically markdown formatted text). Code cells without a "magic" command on the first line, which would be indicated by `%%`, contain ordinary python code. The value of the last line of any python cell, as long as it is not `None`, is displayed when the cell is executed. Hit *shift+enter* on the following cell to see a basic example. # In[ ]: print("This is the output of a python print command (but not the output of the cell).") 20 + 22 # If the first line of the cell begins with `%%`, then the cell is interpreted differently, depending on the command following those symbols. This is called a _cell magic_, and the Lambda Notebook makes heavy use of cell magics to interpret e.g. typed lambda calculus expressions. # # Despite the alpha status, there are already many things you can do related to compositional semantics. The main cell magic is the `%%lamb` magic. If a cell starts with `%%lamb`, then the rest of the cell will be interpreted as a series of definitions in the Lambda Notebook Metalanguage (LNM). The metalanguage is documented in [Metalanguage documentation.ipynb](/notebooks/documentation/Metalanguage documentation.ipynb), and the quick-reference at [Quick reference.ipynb](/notebooks/documentation/Quick reference.ipynb) may also be quite useful. # # For example, here is how you can define some basic lexical entries in LNM, and use them (again, run using *shift-enter*): # In[ ]: get_ipython().run_cell_magic('lamb', '', '||every|| = lambda f_ : lambda g_ : Forall x_e : f(x) >> g(x)\n||cat|| = lambda x_e : Cat_(x_e)\n||dance|| = lambda x_e : Dance_(x_e)\n') # Lexical entries defined this way are available in python. The `*` operator performs semantic composition in the sense you may know from a Semantics 1 course, so the three above lexical entries can be combined as follows: # In[ ]: (every * cat) * dance # **Running code:** To (re-)run a cell you can hit the run button in the toolbar with the cell selected, or hit shift+enter while the cell is selected. Both of the above two cells are runnable. # # The `*` operator here is overloaded to do composition on lexical items (and their results). We can see this by probing the result of `*` in various ways. (The following cell is python code.) # # **Important catch in understanding how Jupyter runs code**: the output of a computation can be saved independently of the code generating the actual computation. Running the above `%%lamb` cell defines some lexical items, and as a result displays a piece of the lexicon -- but if you save the notebook and reload, the output will remain without the underlying lexical items. When you reload a notebook, be sure to run all necessary cells. Keep this principle in mind also when running cells out of order. # In[ ]: result = (every * cat) * dance result.tree() # The lambda notebook also supports several "line magics", which can be mixed in with python code inside a cell. This is indicated by a single `%` command at the beginning of a line. The `%lamb` magic can be used as a line magic. In addition, a single expression can be displayed using the `%te` line magic (standing for `TypedExpression`). Note that these magics will only display output if they are the last thing in a cell. The result of `%te` can be assigned to a python variable. # In[ ]: get_ipython().run_line_magic('lamb', '||dog|| = L x_e : Dog(x)') # In[ ]: get_ipython().run_line_magic('te', '(L f_ : L x_e : f(x))(L y_e : Dog(y))') # In[ ]: get_ipython().run_line_magic('te', 'reduce (L f_ : L x_e : f(x))(L y_e : Dog(y))') # In[ ]: x = get_ipython().run_line_magic('te', 'reduce (L f_ : L x_e : f(x))(L y_e : Dog(y))') x.type # The expression parser can be accessed in python mode via the function `te` by providing a string argument. The resulting python objects can be examined in many interesting ways; one example is below. # In[ ]: x = te("(L f_ : L x_e : f(x))(L y_e : Dog(y))") x.type # In[ ]: x.reduce_all().derivation.trace() # Please see the notebook [Lambda Notebook Demo.ipynb](http:///notebooks/Lambda%20Notebook%20Demo.ipynb) for many more examples of how to use these two modes. Two reasonably detailed examples of full, research-oriented fragments can be found in [fragments/Continuations and quantifier scope.ipynb](http:///notebooks/fragments/Continuations%20and%20quantifier%20scope.ipynb) and in [fragments/Variable free binding.ipynb](http:///notebooks/fragments/Variable%20free%20binding.ipynb). A good starting point for understanding and extending the lambda notebook is [tutorials/Type shifting.ipynb](http:///notebooks/tutorials/Type%20shifting.ipynb), which goes through the process of adding a variety of type-shifting operations to a compositinoal system. Many further examples of various things you might want to do are provided as well, and we welcome contributions. # # These documentation cells are in another mode where you can write ['markdown'-formatted](http://daringfireball.net/projects/markdown/) text; this mode can be chosen from the dropdown box in the toolbar. Also, the IPython notebook uses the MathJax library to support latex math mode; most equations can be written simply be wrapping them in dollar signs. For example: '$\lambda x : x \in D_e \:.\: x\text{ is a cat}$'. To see what the markdown looks like in this notebook, just double-click on any of the text cells (or hit enter with the cell selected). To re-render it, hit shift-enter. # ### The state of the lambda notebook # # Some things that are implemented: # # * lambda calculus, types, etc # * composition operations on a layer above the pure metalanguage, including the ability to add new ones # * detailed display of compositional derivations # * multiple composition paths # * type shifting, ability to add new type shifts # * composition operations on tree structures (partial). (The normal mode illustrated above operates bottom-up on lexical items, building tree structures in tandem, i.e. a species of strictly-mapped categorial grammar.) # * binding (partial) # * a basic implementation of presuppositions via partiality # # Some things that are prominently missing, ordered from least to most difficult: # # * More realistic implementations of presupposition projection. # * Transformations on syntactic representations, for e.g. QR. (You can do QR now, you just have to manually construct the LFs.) # * Theorem proving on the logical subset of the metalanguage. (Also, no model theory.) # * Integration with any parsing of natural language. (E.g., compare the web demo of boxer.) # # Contributions are welcome! The first two of these are preconditions for moving to beta and currently under development, the latter two are further away. # ## 2. Interface and background on Jupyter # # The Lambda Notebook is based heavily on the Jupyter Notebook and most of the interface you are seeing is Jupyter. Because the Jupyter Notebook is purely browser-based, its interface may be somewhat idiosyncratic compared to native apps that you are used to. We are working on a closer native wrapping at least for OS X, but in the meantime here are some things that may be helpful in understanding what is going on for users who are new to Jupyter. # # * IPython/Jupyter is an enhanced interface to python that supports many extremely useful things; one of the main targets is interfacing with the 'scipy stack', for doing scientific and mathematical computing of the sort often done in R and matlab. It also provides good support for distributed computing. # * Jupyter Notebook is a particular interface to IPython (and other languages) inspired by mathematica notebooks, and is centered around interleaving code with documentation. # * The Jupyter Notebook runs as a server, accessible only from your local computer (at least by default). You then select and edit notebooks via a web browser that contacts this server. Typically it runs at the URL [http://localhost:8888](http://localhost:8888), and you can access this from any browser as long as the server is running. # * The server is represented by a bunch of logging messages in a window, usually in a terminal/console. To shut it down, hit Ctrl-C twice. # * Every notebook corresponds to both a document and a 'kernel' which runs the code on the server. If you close a document by closing the browser window, the kernel will still be running and can be shut down (or re-accessed) at the notebook browser. Kernels store program state of any code executed in cells. # * The default location for files is the `notebooks` directory in the folder where you start the lambda notebook from. # * Because of all this complexity, there is currently no way to double click in your OS on a notebook and open it. Rather, you will need to copy the notebook into this Documents directory, run the notebook, and open the file using the notebook browser. # * Currently, lambda notebook .ipynb files will not work in vanilla Jupyter notebook unmodified; they require the lambda notebook kernel. However, in regular python contexts if the lambda notebook is in your python path (as long as it is python 3), you can import the lambda notebook by doing either `import lamb` to get at the framework programmatically, or `import lamb; import lamb.lnsetup; lamb.lnsetup.ipython_setup()` to get the IPython/Jupyter convenience environment and magics. The lambda notebook infrastructure can be loaded anywhere by using the above import commands. Two things won't generally work: the magics (which require Jupyter/Ipython) and the fancy equation display, which requires the browser in order to render MathJax. # # We welcome patches to reduce any and all of this complexity!