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.
Try CPL directly in your browser: https://msakai.github.io/cpl/
No installation required! Works on Chrome, Firefox, Safari, and Edge.
De-Compress the archive and enter its top directory. Then type:
$ cabal configure
$ cabal build
$ cabal installIf 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 cplTo build the WebAssembly version yourself:
-
Install GHC WebAssembly cross-compiler (GHC 9.10.3 or later):
- Download from: https://downloads.haskell.org/~ghc/
-
Build using the provided script:
$ ./scripts/build-wasm.sh
-
Test locally:
$ cd wasm $ python3 -m http.server 8000Then open http://localhost:8000 in your browser.
The WASM build creates three files in the wasm/ directory:
cpl.wasm- The compiled interpreterindex.html- Web interfacecpl-terminal.js- Terminal controller
See chapter 5 of [1]
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.
This program is licensed under the BSD-style license. (See the file COPYING.)
Copyright (C) 2004-2014 Masahiro Sakai [email protected]
Masahiro Sakai [email protected]
-
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
-
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.