Skip to content

Commit

Permalink
v0.0.2: the widish shell
Browse files Browse the repository at this point in the history
  • Loading branch information
colltoaction committed Jun 30, 2024
1 parent 8081442 commit ef1e3ed
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 76 deletions.
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ Programming is hard, but it shouldn't be _that_ hard.

So far widis have mainly shaped the user interface. Widis are also [graphical programming](https://graphicallinearalgebra.net/2015/04/26/adding-part-1-and-mr-fibonacci/) tools and one can work with them in purely mathematical terms. Start with [examples/mascarpone](examples/mascarpone) then take a look at current work in a functional library at [src](src).

# Working with the CLI
Open terminal and run `widip` to start an interactive session. The program `bin/yaml/shell.yaml` prompts for one command per line, so when we hit `↵ Enter` it is evaluated. When hitting `⌁ Ctrl+D` the environment exits.

```yaml
--- !bin/yaml/shell.yaml
!print Hello world!
Hello world!
```

<!-- <img src="examples/hello-world.jpg" width="300"> -->


[UNIX shell]: https://en.wikipedia.org/wiki/Unix_shell
[chatbot]: https://en.wikipedia.org/wiki/chatbot
Expand Down
31 changes: 30 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
# Examples
# Shell examples

## Hello world!

```
$ python -m widip examples/hello-world.yaml
Hello world!
```

![](hello-world.jpg)

## Script

```
$ python -m widip examples/shell.yaml
73
23
? !grep grep: !wc -c
? !tail -2
```

![IMG](shell.jpg)


# Working with the CLI
Open terminal and run `widip` to start an interactive session. The program `bin/yaml/shell.yaml` prompts for one command per line, so when we hit `↵ Enter` it is evaluated. When hitting `⌁ Ctrl+D` the environment exits.

```yaml
--- !bin/yaml/shell.yaml
!echo Hello world!
Hello world!
```

# Other examples

## React
The first example in https://react.dev/ in diagrammatic style.

Expand Down
Binary file modified examples/hello-world.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/hello-world.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
!print Hello world!
!echo Hello world!
Binary file added examples/shell.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/shell.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
!cat examples/shell.yaml:
? !wc -c
? !grep grep: !wc -c
? !tail -2
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages = ["."]

[project]
name = "widip"
version = "0.0.1"
version = "0.0.2"
description = "Widip is an interactive environment for computing with wiring diagrams in modern systems"
dependencies = [
"discopy>=1.1.7", "pyyaml>=6.0.1", "watchdog>=4.0.1",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="widip",
version="0.0.1",
version="0.0.2",
author="Martin Coll",
author_email="[email protected]",
description="Widip is an interactive environment for computing with wiring diagrams in modern systems",
Expand Down
5 changes: 3 additions & 2 deletions widip/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import sys
import warnings

from .watch import watch_main, shell_main, stream_main
from .watch import watch_main, shell_main
from .widish import widish_main


warnings.filterwarnings("ignore")
match sys.argv:
case [_]:
watch_main()
shell_main("bin/yaml/shell.yaml")
case [_, file_name]: stream_main(open(file_name))
case [_, file_name]: widish_main(file_name)
106 changes: 106 additions & 0 deletions widip/widish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from multiprocessing import Pipe
from pathlib import Path
from subprocess import PIPE, Popen
import sys

from discopy.frobenius import Category, Functor, Ty, Box
from discopy.frobenius import Hypergraph as H, Id, Spider
from discopy import python

from .files import stream_diagram



class IORun(python.Function):
@classmethod
def spiders(cls, n_legs_in: int, n_legs_out: int, typ: Ty):
def step(*processes: IOProcess):
"""
A many-to-many pipe.
"""
assert typ == Ty("io")
assert len(processes) == n_legs_in
return tuple(
IOSpiderProcess(*processes)
for _ in range(n_legs_out))
return IORun(
inside=step,
dom=Ty(*("io" for _ in range(n_legs_in))),
cod=Ty(*("io" for _ in range(n_legs_out))))


def command_io_f(diagram):
"""
close input parameters (constants)
drop outputs matching input parameters
all boxes are io->[io]"""
def command_io(b):
return Id().tensor(*(
Id("io") @ Spider(0, 1, t) >>
Box(b.name,
Ty("io") @ t,
Ty("io")) >>
H.spiders(1, len(b.cod), Ty("io")).to_diagram()
for t in b.dom))
f = Functor(
lambda x: Ty("io"),
lambda b: command_io(b),)
diagram = f(diagram)
return (
# H.spiders(0, len(diagram.dom), Ty("io")).to_diagram() >>
diagram >>
H.spiders(len(diagram.cod), 1, Ty("io")).to_diagram())


class IOProcess:
"""mutual with IOSpiderProcess"""
def __init__(self, box, *spider_processes):
assert box.dom[0] == Ty("io")
assert box.cod == Ty("io")
self.spider_processes = spider_processes
self.popen_args = [box.name, *(t.name for t in box.dom[1:])]
self.pipe_in, self.pipe_out = Pipe(duplex=False)

def communicate(self, input):
# print("process:", self.spider_processes)
# i = self.pipe_in.recv()
o = ""
for p in self.spider_processes:
o += p.communicate(input)
# print(o, self.popen_args)
popen = Popen(self.popen_args, stdin=PIPE, stdout=PIPE, text=True)
(o, _) = popen.communicate(o)
# print(o, self.popen_args)
return o


class IOSpiderProcess:
"""
takes many processes and creates n copies of their joined outputs
mutual with IOSpider"""
def __init__(self, *processes: IOProcess):
self.processes = processes
self.pipe_in, self.pipe_out = Pipe(duplex=False)

def communicate(self, input):
# print("spiderprocess:", [p.popen for p in self.processes])
w = ""
for p in self.processes:
o = p.communicate(input)
w += o
return w

shell_f = Functor(
lambda x: Ty("io"),
lambda b: lambda *p: IOProcess(b, *p),
cod=Category(Ty, IORun))


def widish_main(file_name):
path = Path(file_name)
diagram = stream_diagram(path.open())
diagram = command_io_f(diagram)
diagram.draw(path=path.with_suffix(".jpg"))
process: IOSpiderProcess = shell_f(diagram)()
r = process.communicate("")
sys.stdout.write(r)
17 changes: 0 additions & 17 deletions widish/__main__.py

This file was deleted.

16 changes: 0 additions & 16 deletions widish/examples/README.md

This file was deleted.

Binary file removed widish/examples/shell.jpg
Binary file not shown.
3 changes: 0 additions & 3 deletions widish/examples/shell.yaml

This file was deleted.

23 changes: 0 additions & 23 deletions widish/shell.py

This file was deleted.

0 comments on commit ef1e3ed

Please sign in to comment.