Skip to main content

Concatenative programming integrated to Python

Project description

catty -- Concatenative programming integrated in Python

Concatenative programming is a method and coding style that emphasizes functional composition through implicit parameter passing and postfix notation. It is often understood as stack programming, mimicking the constraints of stack machines. While it carries some hipster cred, it is popular in development circles involving dedicated hardware. Its most popular incarnation is the FORTH language; the Factor language and platform provides a more modern programming system over its principles.

This project is an attempt at integrating a concatenative style and approach to the Python language and software ecosystem. Its present incarnation is somewhat clumsy as it parasites the Python syntax: a catty program is valid Python code. The catty semantics also attempt to leverage the Python built-ins and library as much as possible. Here is a taste:

from operator import add, mul
from catty import reduce
from catty.words import dup


result = reduce([5, 6, dup, mul, add])
print(result)  # Yields [41]

So this early take yields the answer to life, the universe and everything, but it's off by one. Should be fixed in next version.

The interpretation is performed in a Python code loop, so speed cannot be expected. However, the interpreter in its current form optimizes tail calls, enabling arbitrary recursive applications:

from operator import mul
from catty import reduce
from catty.words import dup, tuck, fork, apply, Word

factorial_ = Word(  # prod, n -- prod, n-1
    dup, 0, eq,
    [],
    [dup, tuck, mul, swap, 1, sub, apply.factorial_],
    fork
)
factorial = Word(  # n -- n!
    1, swap, factorial_
)
print(reduce([5, factorial]))  # Yields [120]

This project is meant for incremental exploration of interactions between concatenative and applicative programming styles. It will not shy away from changing completely as it progresses. Current plans involve digging further into tapping into namespace control through function definitions, replacing the interpretation loop with Python bytecode generation, and further strengthening integration so that concatenative and regular Python code can live side by side.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

catty-0.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file catty-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: catty-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for catty-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f2eb08e1fc05505b171e828223c8d6065c79be2e0098d27ee932c58e4f867612
MD5 d66ab9f8040f6c80240cd923fabebfc4
BLAKE2b-256 f76ea39ac4e88f4552cd12a0b3926149b190d76a923ad4ed27822de654bbf685

See more details on using hashes here.

Supported by

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