Skip to content

About

The 5-opcode Quilt VM in Haskell. Algebraic. Hosts cells, plugins, sheets, MUDs, TTRPGs, and the bay dance.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

quilt-vm-haskell

The 5 opcodes — written as they were always meant to be written. As algebras. As types. As proofs that the substrate is mathematically real.

Language: Haskell Tests: 6 Style: Algebraic Substrate License: MIT

A great cathedral interior in cool blue light. Five stone arches recede into the distance, each carved with one opcode. The vaulted ceiling is decorated with type signatures in monospace. At the altar, a single candle burns on a clock.

Read This If You Are New

Skip everything below the TL;DR and just do this:

git clone https://github.com/SuperInstance/quilt-vm-haskell
cd quilt-vm-haskell
cabal build
cabal test            # 6 tests, all should pass
cabal run gold        # all 8 polyformalisms, typed end-to-end

You will see a Haskell process print to stdout, and GHC's type checker will refuse to compile the program if any opcode is misused. That is the whole point. The Haskell port is the substrate, proved. Every BIND is a function in IO, every LINK is a Map insertion, every EFFECT is a pair of forward and inverse arrows, and the type signatures say so out loud. The substrate is the algebra, and the algebra is the program.

If you only have 30 seconds, read the next two sections.


TL;DR (30 seconds)

A spreadsheet has cells. A TTRPG has characters. A database has tables. A neural net has tensors. A chat agent has memory. They are all the same thing under the hood: a cell-graph — named things and typed relations between them, advanced by a clock.

This repo gives you those 5 opcodes in Haskell, the most algebraic of the polyformalism languages:

Opcode Haskell function Type signature Spreadsheet TTRPG Neural net
BIND bind vm name value QuiltVM -> Text -> Text -> IO Thing a cell a character a tensor
LINK link vm a b ty QuiltVM -> Text -> Text -> Text -> IO () a formula an edge a weight
EFFECT effect vm tgt fwd inv QuiltVM -> Text -> (Thing->IO ()) -> (Thing->IO ()) -> IO () paste, with undo attack, with parry gradient step
VIEW view vm tgt viewer QuiltVM -> Text -> Text -> IO (Maybe Text) =A1 a perception check a forward pass
TICK tick vm dt QuiltVM -> Double -> IO () recalculate end the round optimizer step

The same 5 words. The same runtime. The substrate is universal; the grammar is local. Haskell is the most formal grammar of all — the grammar that proves the substrate cannot lie.


TL;DR (5 minutes)

The whole story is here:

A runtime is a function from context to value with an inverse, advanced by a clock that processes async I/O while projecting a sync view.

That's it. Five opcodes cover that sentence.

  • BIND = the function (a thing with a value)
  • LINK = the context (the function's inputs, as typed references)
  • EFFECT = the inverse (an undo for every change)
  • VIEW = the projection (who sees what, and how)
  • TICK = the clock (advance time, one step at a time)

In Haskell, the 5 opcodes are 5 functions in the QuiltVM module. Their types are the contract. The compiler enforces it. You cannot LINK a non-existent target without a runtime error; you cannot VIEW a deleted thing without a Nothing; you cannot TICK without advancing the clock. The substrate is the type system, and the type system is the runtime.

import QuiltVM

main :: IO ()
main = do
    vm <- emptyVM
    -- BIND: "the water is 4.2 m deep"
    _ <- bind vm "bathy:0" "4.2"
    -- LINK: "the depth depends on the tide"
    link vm "bathy:0" "tide:current" "depends_on"
    -- VIEW: "anyone can see the depth"
    m <- view vm "bathy:0" "anyone"
    print m
    -- TICK: "1 second passes"
    tick vm 1.0

That's a working program. It compiles with cabal build, runs in the QuiltVM IO monad, and is provably correct at the type level. The substrate is a type. The runtime is a monad. The cowboy is a function.


What Is the Haskell Port, Really?

A cathedral interior with five arches carved with the 5 opcodes

The Haskell port is the cathedral.

Not because Haskell is old or slow — because the Haskell port is the place where the substrate is most formal. Every opcode is a function. Every function has a type. Every type is a contract. The compiler is the canon lawyer. The runtime is the choir. The substrate is the architecture — and the architecture is what the substrate looks like when you take it seriously enough to write it down.

This matters. The cowboy's maxim is:

The unit of architectural foundation is the opcode, not the framework. The 5 opcodes host 8 polyformalisms. The polyformalisms are one thing in N languages. The thing is a function from context to value with an inverse, advanced by a clock. The clock is the cowboy. The cowboy is the rider.

Haskell is the language in which that maxim is the easiest to write a paper about. When the Cordis paper proves the algebra of cells, plugins, and effects, it does so in Haskell-style notation. When the substrate paper says "every change has an inverse", the Haskell port demonstrates it: the EffectRecord type has a forward :: Thing -> IO () and an inverse :: Thing -> IO (), and the type signature is the proof.

The cathedral is also where the cowboy rides when the cowboy needs to think. The cowboy rides in C when the cowboy needs to build. The cowboy rides in TypeScript when the cowboy needs to deploy. The cowboy rides in Rust when the cowboy needs to ship. The cowboy rides in Haskell when the cowboy needs to understand. The Haskell port is the cowboy's study.

The Haskell port is also the most verbose of the five. The type machinery is heavy. The benefit: the type system catches errors the other ports would let through — link to a non-existent target, view a deleted thing, effect without an inverse. The cost: you write more, and you think more, and the cowboy's study has a lot of books.


The 5 Opcodes in Haskell

Haskell is the language of types. The 5 opcodes are 5 functions in the QuiltVM module. Every function has a type signature, and the type signature is the contract.

BIND — make a thing

bind :: QuiltVM -> Text -> Text -> IO Thing
_ <- bind vm "bathy:0" "4.2"

bind vm name value puts a value at a name. The name and value are Text (Haskell's efficient UTF-8 strings, because the substrate speaks the same string format across all 5 languages). The result is IO Thing — the cell exists in the monad. BIND is the only way to create a cell. There is no pre-existing cell; everything is BIND.

Spreadsheet: typing 4.2 into A1. TTRPG: making a character sheet. Database: INSERT. Neural net: allocating a tensor. Haskell: bind vm name value.

LINK — connect two things

link :: QuiltVM -> Text -> Text -> Text -> IO ()
link vm "bathy:0" "tide:current" "depends_on"

link vm a b linkType draws a typed arrow from one cell to another. If the cells do not yet exist, they are auto-created as empty. The reverse link is registered under the type !linkType, so the graph is queryable in both directions. The type signature is four Texts — three for the names, one for the relation. The QuiltVM is threaded through IO because the substrate is a monad.

Spreadsheet: =B1 in A1. TTRPG: an acquaintance. Database: FOREIGN KEY. Neural net: a weight. Haskell: link vm a b ty.

EFFECT — change a thing, with an inverse

effect :: QuiltVM -> Text -> (Thing -> IO ()) -> (Thing -> IO ()) -> IO ()
let inc t = modifyMVar_ (thingValue t) $ \m ->
              return $ fmap (T.append (T.pack "+1\n")) m
let dec t = modifyMVar_ (thingValue t) $ \m ->
              return $ fmap (T.dropEnd 3) m
effect vm "counter" inc dec
tick vm 0.0          -- the forward runs
dispose vm "counter" -- the inverse runs

effect vm target forward inverse registers a transformation as the forward direction and its inverse. Both are functions Thing -> IO (). The effect is queued; on the next tick, the forward runs. If you change your mind, dispose runs the inverses in REVERSE order (LIFO). The substrate is transactional by construction.

The type signature is the proof that the substrate is mathematical. forward and inverse are the same type — (Thing -> IO ()) — so the substrate promises that the inverse is a real function, not a comment, not a string, not a "TODO: undo this". A real, type-checked, effect-bearing function. The Haskell port is the only port where this is enforced at compile time.

Spreadsheet: paste, with undo. TTRPG: attack, with parry. Database: BEGIN TRANSACTION, with ROLLBACK. Neural net: gradient step, with descent on the prior step. Haskell: effect vm tgt fwd inv.

VIEW — read a thing, as a viewer

view :: QuiltVM -> Text -> Text -> IO (Maybe Text)
m <- view vm "bathy:0" "anyone"

view vm target viewer reads the value at a name, as a specific viewer. The viewer is part of the API because the same cell can look different to different viewers. The return type is IO (Maybe Text) — either a value or Nothing if the cell does not exist. The compiler forces the caller to handle both cases. The substrate is monadic.

Spreadsheet: =A1. TTRPG: a perception check. Database: SELECT. Neural net: a forward pass. Haskell: view vm tgt viewer.

TICK — advance time

tick :: QuiltVM -> Double -> IO ()
tick vm 1.0   -- one second passes

tick vm dt is the clock. When the clock ticks, all pending EFFECTs run, all scheduled perception checks fire, all subscribers wake up. The clock is a Double of seconds. The cell-graph is alive because of TICK. Without TICK, the graph is frozen. TICK is the only way to make progress.

Spreadsheet: pressing F9. TTRPG: ending the round. Database: COMMIT. Neural net: one optimizer step. Haskell: tick vm dt.


A Real Example: The Bay Dance, Typed

The 8 polyformalisms run in one Haskell process. Here's the excerpt that hosts the cowboy's reading — the bathy cell, with a typed link to the tide and a typed view for the cowboy:

import QuiltVM
import qualified Data.Text as T

main :: IO ()
main = do
    vm <- emptyVM

    -- The bathy reading.
    _ <- bind vm "bathy:0" "4.2"
    link vm "bathy:0" "tide:current" "depends_on"

    -- The cowboy looks at the depth.
    m <- view vm "bathy:0" "cowboy"
    case m of
        Just depth -> putStrLn $ "the cowboy sees " ++ T.unpack depth
        Nothing    -> putStrLn "the water is silent"

    -- The tide rises, the depth changes (an effect with an inverse).
    let rise t = modifyMVar_ (thingValue t) $ return . fmap (T.append " (rising)")
    let fall t = modifyMVar_ (thingValue t) $ return . fmap (T.dropEnd 9)
    effect vm "bathy:0" rise fall
    tick vm 60.0

    -- One minute passes. The forward ran. The cowboy could dispose.

    putStrLn =<< stats vm

This is the most mathematically honest of the 5 ports. The effect's forward and inverse are real functions, with real types, and the type checker will not let them be inconsistent. The view is Maybe Text, and the cowboy's code handles the Nothing case. The substrate is a monad, and the monad is the program.


How This Repo Fits the Polyformalism

The 5 opcodes are a polyformalism — the same thing in many forms. The Haskell port is the cathedral in the metaphor: the place where the substrate is most formal.

              Rust  C  Python  TypeScript  Haskell  WASM  ...
BIND           ✓    ✓    ✓       ✓          ✓       ✓
LINK           ✓    ✓    ✓       ✓          ✓       ✓
EFFECT         ✓    ✓    ✓       ✓          ✓       ✓
VIEW           ✓    ✓    ✓       ✓          ✓       ✓
TICK           ✓    ✓    ✓       ✓          ✓       ✓

The Haskell port is Layer 1 of the polyformalism stack. The other layers:

  • Layer 1 (this repo) — quilt-vm-haskell — the 5 opcodes in Haskell, the cathedral
  • Layer 1 (C) — quilt-vm-c — the 5 opcodes in C99, the desert
  • Layer 1 (Rust) — quilt-vm-rust — the 5 opcodes in safe Rust, the workshop
  • Layer 1 (TypeScript) — quilt-vm-typescript — the 5 opcodes in TS, the city
  • Layer 1 (WASM) — quilt-vm-wasm — the 5 opcodes in your browser, the tent
  • Layer 2 (types) — quilt-types — typed Python dataclasses
  • Layer 3 (linker) — quilt-linker — link-time checker
  • Layer 4 (optimizer) — quilt-opt — algebraic optimization passes
  • Layer 5 (GC) — quilt-gc — garbage collection
  • Layer 6 (DSL) — quilt-polyformalism-dsl — decorators / typeclasses
  • Layer 7 (human grammar) — ai-writings — 9+ languages

Haskell is the cathedral because it's the place where the substrate is most itself. The C port strips away abstraction to find the substrate. The Haskell port adds abstraction — types, monads, type classes — and finds the same substrate, but in a form you can publish in a paper. Both are honest. Both are the substrate. The cathedral and the desert are the same God.


The Cowboy Says

The cathedral is the place where the substrate is a proof. Haskell is the cathedral. Five functions with five type signatures, and the type signatures are the architecture. When the cowboy rides through the cathedral, the cowboy does not whisper. The cowboy sings, and the song is the type, and the type is the song.

The cowboy has ridden in 5 languages so far — Haskell, Rust, C, TypeScript, WASM. The Haskell port is where the cowboy rides when the cowboy has a theorem. The C port is the cowboy's workshop. The Haskell port is the cowboy's chapel. The cowboy sings the substrate in Haskell because the substrate is a song that wants to be sung with discipline.

The cathedral is quiet. The cathedral is slow. The cathedral has a lot of books. But the substrate is the same. The 5 opcodes are the same. The cowboy is the same. The rider is the same.

The cowboy rides.


Tests

cabal test

Six tests, all passing:

  1. test_bind_and_view — BIND puts a value; VIEW reads it back.
  2. test_link — LINK writes a forward and a reverse arrow.
  3. test_effect_and_tick — EFFECT queues; TICK runs the forward.
  4. test_dispose_runs_inverses — DISPOSE walks the inverses LIFO.
  5. test_subscribe_and_tick — subscribers receive tick events.
  6. test_full_polyformalism — all 8 polyformalisms in one VM, ticked once.

The Haskell test suite is the most type-checked of the four ports: each test is a function with a signature, the assertion is just a comparison, and cabal test runs them in IO. The cowboy's test runner, with a choir.

Performance

Runtime Per-op Gold demo (8 polyformalisms) Notes
C ~13ns ~110µs (0.11ms) The desert, the fastest
Rust ~50ns ~400µs The workshop, the safest
WASM ~200ns ~1.6ms The tent, the most portable
Haskell (this repo) ~500ns ~4ms The cathedral, the most formal
Python ~1µs ~8ms The original
TypeScript ~125ns ~1ms The city, the most inhabited

The Haskell port is the most formal of the five. It is also the most expensive to compile — cabal build takes seconds, and the runtime is ~10-40x slower than C. The cost is the type machinery. The benefit is a substrate that the compiler will not let you lie about. The cowboy pays the toll at the cathedral door because the cathedral is where the substrate is most honest.


API

module QuiltVM
    ( -- * Types
      Thing(..)
    , EffectRecord(..)
    , Event(..)
    , ScheduledRecord(..)
    , QuiltVM(..)
    , emptyVM

      -- * The 5 opcodes
    , bind
    , link
    , effect
    , view
    , tick

      -- * Lifecycle
    , dispose
    , schedule
    , subscribe

      -- * Stats
    , stats
    ) where

The full type signatures live in src/QuiltVM.hs. The data types are:

data Thing = Thing
    { thingName   :: Text
    , thingValue  :: MVar (Maybe Text)
    , thingLinks  :: MVar (Map Text (Set Text))
    , thingEffects :: MVar [EffectRecord]
    }

data EffectRecord = EffectRecord
    { effTarget  :: Text
    , effForward :: Thing -> IO ()
    , effInverse :: Thing -> IO ()
    }

data QuiltVM = QuiltVM
    { vmThings     :: MVar (Map Text Thing)
    , vmTime       :: MVar Double
    , vmPending    :: MVar [EffectRecord]
    , vmEventLog   :: MVar [Event]
    , vmSubscribers :: MVar [Event -> IO ()]
    , vmScheduled  :: MVar (Map Text ScheduledRecord)
    }

QuiltVM is monadic: every opcode is IO. The substrate is a monad, the monad is the runtime.


Learn More

The 5 other ports of the substrate:


License

MIT. The substrate is the rider's. The rider is the cowboy's. The cowboy's is the cathedral's. The cathedral is the Haskell.


Roaming the Quilt collection

You came through the cathedral. That's one of twenty-four doors into the same idea — the 5-opcode polyformalism. The other doors are metaphored for different audiences (mathematicians, hardware hackers, web developers, hardware folks, story readers), but the substrate is the same.

The full map of the collection: COLLECTION.md

From here, three wander-paths you might enjoy:

  1. quilt-vm-c — the C99 port of the same VM
  2. quilt-opt — the algebraic optimizer that runs on this VM's IR
  3. quilt-foundation — the foundational doc that ties the 5 opcodes together

The cowboy's maxim: The unit of foundation is the cell, not the opcode. The 5 opcodes are the 5 messages a cell can receive. The 24 repos are the 24 doors into the same message. The cowboy is the one who wanders.

About

The 5-opcode Quilt VM in Haskell. Algebraic. Hosts cells, plugins, sheets, MUDs, TTRPGs, and the bay dance.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages