Skip to main content

Distributional Compositional Python

Project description

snake equation

Distributional Compositional Python

readthedocs Build Status codecov PyPI version arXiv:2005.02975

DisCoPy is a tool box for computing with monoidal categories.

Features

Diagrams & Recipes

Diagrams are the core data structure of DisCoPy, they are generated by the following grammar:

diagram ::= Box(name, dom=type, cod=type)
    | diagram @ diagram
    | diagram >> diagram
    | Id(type)

type ::= Ty(name) | type.l | type.r | type @ type | Ty()

String diagrams (also known as tensor networks or Penrose notation) are a graphical calculus for computing with monoidal categories. For example, if we take ingredients as types and cooking steps as boxes then a diagram is a recipe:

from discopy import Ty, Box, Id, Swap

egg, white, yolk = Ty('egg'), Ty('white'), Ty('yolk')
crack = Box('crack', egg, white @ yolk)
merge = lambda x: Box('merge', x @ x, x)

crack_two_eggs = crack @ crack\
    >> Id(white) @ Swap(yolk, white) @ Id(yolk)\
    >> merge(white) @ merge(yolk)
crack_two_eggs.draw(path='docs/_static/imgs/crack-eggs.png')

crack two eggs

Snakes & Sentences

Wires can be bended using two special kinds of boxes: cups and caps, which satisfy the snake equations, also called triangle identities.

from discopy import Cup, Cap

x = Ty('x')
left_snake = Id(x) @ Cap(x.r, x) >> Cup(x, x.r) @ Id(x)
right_snake =  Cap(x, x.l) @ Id(x) >> Id(x) @ Cup(x.l, x)
assert left_snake.normal_form() == Id(x) == right_snake.normal_form()

snake equations, with types

In particular, DisCoPy can draw the grammatical structure of natural language sentences encoded as reductions in a pregroup grammar (see Lambek, From Word To Sentence (2008) for an introduction).

from discopy import pregroup, Word

s, n = Ty('s'), Ty('n')
Alice, Bob = Word('Alice', n), Word('Bob', n)
loves = Word('loves', n.r @ s @ n.l)

sentence = Alice @ loves @ Bob >> Cup(n, n.r) @ Id(s) @ Cup(n.l, n)
pregroup.draw(sentence, path='docs/_static/imgs/alice-loves-bob.png')

Alice loves Bob

Functors & Rewrites

Monoidal functors compute the meaning of a diagram, given an interpretation for each wire and for each box. In particular, tensor functors evaluate a diagram as a tensor network using numpy. Applied to pregroup diagrams, DisCoPy implements the distributional compositional (DisCo) models of Clark, Coecke, Sadrzadeh (2008).

from discopy import TensorFunctor

F = TensorFunctor(
    ob={s: 1, n: 2},
    ar={Alice: [1, 0], loves: [[0, 1], [1, 0]], Bob: [0, 1]})

assert F(sentence) == 1

Free functors (i.e. from diagrams to diagrams) can fill each box with a complex diagram. The result can then be simplified using diagram.normalize() to remove the snakes.

from discopy import Functor

def wiring(word):
    if word.cod == n:  # word is a noun
        return word
    if word.cod == n.r @ s @ n.l:  # word is a transitive verb
        return Cap(n.r, n) @ Cap(n, n.l)\
            >> Id(n.r) @ Box(word.name, n @ n, s) @ Id(n.l)

W = Functor(ob={s: s, n: n}, ar=wiring)


rewrite_steps = W(sentence).normalize()
sentence.to_gif(*rewrite_steps, path='autonomisation.gif', timestep=1000)

autonomisation

Getting Started

pip install discopy

Documentation

The tool paper is now available on arXiv:2005.02975, it was presented at ACT2020.

The documentation is hosted at readthedocs.io, you can also checkout the notebooks for a demo!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

discopy-0.3.4.tar.gz (71.6 kB view details)

Uploaded Source

File details

Details for the file discopy-0.3.4.tar.gz.

File metadata

  • Download URL: discopy-0.3.4.tar.gz
  • Upload date:
  • Size: 71.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for discopy-0.3.4.tar.gz
Algorithm Hash digest
SHA256 1511c5caa53cb93790dba5fd4e11bb178e70497e393d7bda07d1f176a6e3a076
MD5 9271a5c69e93d989328244cc59477e8e
BLAKE2b-256 076485ffa9fb07ea587c072d97e04640273853159e8e8798c2ac237bb385f354

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page