Skip to content

msakai/cpl

Repository files navigation

An implementation of "A Categorical Programming Language"

Build Status Hackage Hackage Deps License

This package is an implementation of "A Categorical Programming Language" (CPL for short)[1][2] written in Haskell.

Try CPL in your browser: WebAssembly Demo (no installation required!)

CPL is a functional programming language based on category theory. Data types are declared in a categorical manner by adjunctions. Data types that can be handled include the terminal object, the initial object, the binary product functor, the binary coproduct functor, the exponential functor, the natural number object, the functor for finite lists, and the functor for infinite lists. Each data type is declared with its basic operations or morphisms. Programs consist of these morphisms, and execution of programs is the reduction of elements (i.e. special morphisms) to their canonical form.

Install

Option 1: Use WebAssembly Version (No Installation)

Try CPL directly in your browser: https://msakai.github.io/cpl/

No installation required! Works on Chrome, Firefox, Safari, and Edge.

Option 2: Build from Source

Standard Build (Native)

De-Compress the archive and enter its top directory. Then type:

$ cabal configure
$ cabal build
$ cabal install

If you want to compile with readline or haskeline, add -fReadline or -fHaskeline respectively to the configure command.

Alternatively, you can use Stack:

$ stack build
$ stack exec cpl

WebAssembly Build

To build the WebAssembly version yourself:

  1. Install GHC WebAssembly cross-compiler (GHC 9.10.3 or later):

  2. Build using the provided script:

    $ ./scripts/build-wasm.sh
  3. Test locally:

    $ cd wasm
    $ python3 -m http.server 8000

    Then open http://localhost:8000 in your browser.

The WASM build creates three files in the wasm/ directory:

  • cpl.wasm - The compiled interpreter
  • index.html - Web interface
  • cpl-terminal.js - Terminal controller

Usage

See chapter 5 of [1]

Quick Start

Once you have CPL running (either in browser or terminal), try these commands:

cpl> edit
| right object 1 with !
| end object;
right object 1 defined
cpl> edit
| right object prod(a,b) with pair is
|   pi1: prod -> a
|   pi2: prod -> b
| end object;
right object prod(+,+) defined
cpl> edit
| right object exp(a,b) with curry is
|   eval: prod(exp,a) -> b
| end object;
right object exp(-,+) defined
cpl> edit
| left object nat with pr is
|   0: 1 -> nat
|   s: nat -> nat
| end object;
left object nat defined
cpl> show pair(pi2,eval)
pair(pi2,eval)
    : prod(exp(*a,*b),*a) -> prod(*a,*b)
cpl> let add=eval.prod(pr(curry(pi2), curry(s.eval)), I)
add : prod(nat,nat) -> nat  defined
cpl> simp add.pair(s.s.0, s.0)
s.s.s.0
    : 1 -> nat
cpl> help
  exit                        exit the interpreter
  quit                        ditto
  bye                         ditto
  edit                        enter editing mode
  simp [full] <exp>           evaluate expression
  show <exp>                  print type of expression
  show function <name>        print information of function
  show object <functor>       print information of functor
  load <filename>             load from file
  set trace [on|off]          enable/disable trace
  reset                       remove all definitions
cpl> exit

For more examples, see the samples/ directory.

License

This program is licensed under the BSD-style license. (See the file COPYING.)

Copyright (C) 2004-2014 Masahiro Sakai [email protected]

Author

Masahiro Sakai [email protected]

Bibliography

  1. Tatsuya Hagino, “A Categorical Programming Language”. Ph.D. Thesis, University of Edinburgh, 1987. available at http://web.sfc.keio.ac.jp/~hagino/index.html.en

  2. Tatsuya Hagino, “Categorical Functional Programming Language”. Computer Software, Vol 7, No.1. Advances in Software Science and Technology 4, 1992. ISBN 0-12-037104-9.

About

An interpreter of Hagino's Categorical Programming Language (CPL).

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 5