Skip to content

Commit

Permalink
Add example to README.md (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
toumix committed Jan 3, 2024
1 parent e1a90c7 commit 0768c4c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,37 @@ DisCoPy began as an implementation of [DisCoCat](https://en.wikipedia.org/wiki/D
- [PennyLane](https://pennylane.ai/) for automatic differentiation
* an implementation of formal grammars ([context-free](https://en.wikipedia.org/wiki/Context-free_grammar), [categorial](https://en.wikipedia.org/wiki/Categorial_grammar), [pregroup](https://en.wikipedia.org/wiki/Pregroup_grammar) or [dependency](https://en.wikipedia.org/wiki/Dependency_grammar)) with interfaces to [lambeq](https://cqcl.github.io/lambeq), [spaCy](https://spacy.io/) and [NLTK](https://www.nltk.org/)

## Example: Cooking

This example is inspired from Pawel Sobocinski's blog post [Crema di Mascarpone and Diagrammatic Reasoning](https://graphicallinearalgebra.net/2015/05/06/crema-di-mascarpone-rules-of-the-game-part-2-and-diagrammatic-reasoning/).

```python
from discopy.symmetric import Ty as Ingredient, Box as Step, Diagram as Recipe

egg, white, yolk = Ingredient("egg"), Ingredient("white"), Ingredient("yolk")
crack = Step("crack", egg, white @ yolk)
merge = lambda x: Step("merge", x @ x, x)

# DisCoPy allows string diagrams to be defined as Python functions

@Recipe.from_callable(egg @ egg, white @ yolk)
def crack_two_eggs(left_egg, right_egg):
left_white, left_yolk = crack(left_egg)
right_white, right_yolk = crack(right_egg)
return (merge(white)(left_white, right_white),
merge(yolk)(left_yolk, right_yolk))

# ... or in point-free style using parallel (@) and sequential (>>) composition

assert crack_two_eggs == crack @ crack\
>> white @ Recipe.swap(yolk, white) @ yolk\
>> merge(white) @ merge(yolk)

crack_two_eggs.draw()
```

![crack_two_eggs.draw()](test/src/imgs/crack-eggs.png)

## Architecture

Software dependencies between modules go top-to-bottom, left-to-right and [forgetful functors](https://en.wikipedia.org/wiki/Forgetful_functor) between categories go the other way.
Expand Down
4 changes: 2 additions & 2 deletions test/drawing/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

IMG_FOLDER, TIKZ_FOLDER, TOL = 'test/src/imgs/', 'test/src/tikz/', 10


def draw_and_compare(file, **params):
tol = params.pop('tol', TOL)
return utils.draw_and_compare(file, IMG_FOLDER, tol, **params)
Expand All @@ -19,7 +18,8 @@ def tikz_and_compare(file, **params):


@draw_and_compare(
'crack-eggs.png', figsize=(5, 6), fontsize=18, aspect='equal')
'crack-eggs.png',
figsize=(4, 4), aspect='auto', margins=(0.1, 0.025))
def test_draw_eggs():
def merge(x):
return Box('merge', x @ x, x)
Expand Down
Binary file modified test/src/imgs/crack-eggs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0768c4c

Please sign in to comment.