Exploring the Koka language by rebuilding GNU ls.
The goal is to follow the GNU coreutils philosophy, reimplemented piece by piece in Koka's effect-typed, functional style.
git clone https://github.com/cladam/koka-labs.git
cd koka-labs- macOS / Linux POSIX
- coreutils installed
- Koka the language installed:
curl -sSL https://github.com/koka-lang/koka/releases/latest/download/install.sh | sudo sh
# use the bash script to run kls
./exec-ls ls # build and run ls (debug)
./exec-ls ls /tmp # with arguments
./exec-ls ls -ar /tmp # with flags
###
# Build and produce a binary
koka src/ls.kk -o kls
chmod +x klsNote: Optimised builds (
koka -O1/-O2) currently segfault due to a Koka compiler bug inkk_cctx_copywhen stacking effect handlers. Use debug builds for now.
./kls
# See implemented flags with --helpmacOS Gatekeeper quarantines binaries downloaded from the internet. After downloading a release, run:
xattr -d com.apple.quarantine koka-lsI am testing kls with both integration and unit tests, see tests/test-ls.kk.
Integration tests use the bio effect to capture list-dir output into a string buffer instead of printing to stdout, making output assertions straightforward.
Run all tests with the helper script:
./test-lsThe testing framework is kunit, managed as a git submodule under lib/kunit/.
| Program | Description |
|---|---|
ls.kk |
List directory contents |
- Create a testing framework based on XUnit
- Create a CI workflow and publish binaries
- Default directory listing (
.when no args) - Hide dotfiles by default
- Alphabetical sort (default)
- Show basenames, not full paths
- Combined short flags (
-ar,-ra) -
--stops flag parsing - Multiple path arguments —
ls dir1 dir2 - Distinguish files vs directories in arguments
- Error handling (
ls: cannot access ...) - Exit codes (0 success, 1 minor problems, 2 serious trouble)
-
-a, --all— in directories, do not ignore names starting with. -
-A, --almost-all— like-a, but ignore.and.. -
-B, --ignore-backups— ignore entries ending with~ -
-d, --directory— list directories themselves, not their contents -
-H, --dereference-command-line— follow symlinks listed on the command line -
--dereference-command-line-symlink-to-dir— follow command-line symlinks that point to directories -
--hide=PATTERN— hide matching entries (overridden by-a/-A) -
-I, --ignore=PATTERN— do not list entries matching pattern -
-L, --dereference— show information for link target, not the link itself -
-R, --recursive— list subdirectories recursively
-
-l, --format=long— long format (type, mode, links, owner, group, size, timestamp, name) -
-g— long format, omit owner -
-o— long format, omit group (equivalent to-l -G) -
-G, --no-group— suppress group in long listing -
-n, --numeric-uid-gid— long format with numeric user and group IDs -
--author— with-l, print author of each file -
-i, --inode— print inode number of each file -
-s, --size— print allocated size of each file in blocks -
-h, --human-readable— print sizes like 1K 234M 2G (powers of 1024) -
--si— like-h, but use powers of 1000 -
--block-size=SIZE— scale sizes by SIZE when printing -
--full-time— long format with--time-style=full-iso -
--time-style=STYLE— time/date format with-l(full-iso, long-iso, iso, locale, +FORMAT) -
-Z, --context— print security context of each file -
-D, --dired— generate output for Emacs dired mode
-
-r, --reverse— reverse whatever the sorting method is -
-S, --sort=size— sort by file size, largest first -
-t, --sort=time— sort by modification time, newest first -
-U, --sort=none— do not sort; list entries in directory order -
-v, --sort=version— natural sort of version numbers within text -
-X, --sort=extension— sort alphabetically by file extension -
--sort=name— sort by name (default; explicit override) -
--sort=width— sort by printed width of file name -
-c, --time=ctime— use/sort by status change time -
-u, --time=atime— use/sort by access time -
--time=WORD— select timestamp (atime, ctime, mtime, birth) -
-f— do not sort; enable-aand-U -
--group-directories-first— group directories before files
-
-1— one file per line -
-C, --format=vertical— list in columns, sorted vertically (default for terminal) -
-F, --classify— append indicator (/dir,*exec,@symlink,=socket,|FIFO,>door) -
-p, --indicator-style=slash— append/to directories -
-m, --format=commas— comma-separated list, filling width -
-x, --format=across— like-C, but sorted across rather than down columns -
--format=WORD— select format (across, commas, long, single-column, verbose, vertical) -
--file-type, --indicator-style=file-type— like-F, but do not append* -
--indicator-style=WORD— none, slash, file-type, classify -
-k, --kibibytes— default to 1024-byte blocks for-sand per-directory totals -
--color[=WHEN]— colorize output (none, auto, always) -
--hyperlink[=WHEN]— hyperlink file names (none, auto, always) -
-T, --tabsize=COLS— assume tab stops at each COLS instead of 8 -
-w, --width=COLS— set output width (0 = no limit) -
--zero— end each line with NUL, not newline (implies-1,--color=none,-N)
-
-b, --escape— C-style backslash escapes for nongraphic characters -
-N, --literal— print entry names without quoting -
-q, --hide-control-chars— print?instead of nongraphic characters -
-Q, --quote-name— enclose entry names in double quotes -
--quoting-style=WORD— literal, shell, shell-always, shell-escape, shell-escape-always, c, escape, clocale, locale -
--show-control-chars— print nongraphic characters as-is (default for non-terminal)