Coding Adventure: Karma as a System
Tags: devjournal, programming, computerscience, discuss
Published: 2023-10-06 05:13:05


![Computer rendering of a human head with spiraling gears](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61fy7fvep64i4dw8eda7.png)

<center>How can I use software to model Karma?</center>

---

## KaaS

Karma-as-a-System is a computer program that emulates the mechanisms of [**Karma**](https://en.wikipedia.org/wiki/Karma).

> _Wikipedia —_ Karma is a concept of action, work or deed, and its effect or consequences.[1] In Indian religions, the term more specifically refers to a principle of cause and effect, often descriptively called the principle of karma, wherein intent and actions of an individual (cause) influence the future of that individual (effect):[2] Good intent and good deeds contribute to good karma and happier rebirths, while bad intent and bad deeds contribute to bad karma and bad rebirths.

Let's design KaaS.  Imagine you're taking a cyber philosophy class - what are you submitting for this?

---

<center><b>My Design</b></center>

---

I'll start by transforming the given Wikipedia quote into a vocabulary about computer systems, to get a sense of what we'll be building.

1. **Karma describes _processes_ and their _outputs_.**
    _"Karma is a concept of action, work or deed, and its effect or consequences." — Wikipedia_

2. **A system's _state+outputs_ influence its future _state_.**
    _"Indian religions, the term more specifically refers to ... the principle of karma, wherein intent and actions of an individual (cause) influence the future of that individual (effect)" — Wikipedia_

3. **A system's _state+outputs_ influence its future _inputs_.**
    I've substituted "state" with "inputs" because a computer system's state is _always_ a function of its input. Runtime state is computed from user inputs, and compile-time state (or "hard-coded state") is determined by developer inputs.  Importantly, every state change comes from an associated input.

4. **_Qualities_ of a system's output influence the _qualities_ of its inputs.**
    _"Good intent and good deeds contribute to good karma ..., while bad intent and bad deeds contribute to bad karma..." — Wikipedia_

Putting this all together, let's define the system we want to build once and for all.

---

<center>Karma is the mechanism by which the qualities of a system's outputs may eventually influence the qualities of its inputs.</center>

---

Let's implement this system as a [Graph](https://en.wikipedia.org/wiki/Graph_(abstract_data_type)) of nodes and lines, where lines transport information ("state") between nodes.

```ts
// TypeScript

type Node = { /* ... */ };
type Line = [Node, Node];

type Graph = { nodes: Set<Node>, lines: Set<Line> }

/**
 * This function defines the mechanism
 * by which nodes send+receive information.
 *
 * Some might prefer for Node to have object-oriented
 * send() & receive() methods. I prefer functional style.
 */
function transmit([left, right]: Line) => void {
	// "right" receives information through "c"
	//    ^
	//    |  "left" emits information through "a" and "b"
	//    |        ^        ^
	//    |        |        |
	right.c = left.a + left.b;
};
```

To become karmic, nodes emitting information through lines will *eventually* have their outputs returned to them as inputs. This must abstract excellently across different kinds of information. Ex:

- After sending some object ***O*** with functional properties ***P***, one should expect to *eventually* receive some object(s) *qualitatively similar* to ***O***, or with properties functionally similar to ***P***.

- Increasing|decreasing the frequency of similar outputs will *eventually* affect the input frequency of *qualitatively similar* inputs.

Let us define some things.

### Eventually?
An event ***E*** happens 'eventually' if other events are permitted to happen first.

Let's model this in `transmit` as an asynchronous function.

```ts

async function transmit(...) { /* same as before */ }

```

### Qualitatively similar?
Personal experience strongly determines how you interpret this.

Simple examples:
- Red is qualitatively similar to maroon
- Oranges are qualitatively similar to clementines

Hard examples:
- An ant is qualitatively similar to a pebble (think: size)
- An ant is qualitatively **dis**similar to a pebble (think: alive/dead)

Controversial examples:
- Pornography is qualitatively similar to a cigarette
- The color gray is qualitatively similar to unseasoned food

Do you disagree with these?  Can you think of more?

---

In KaaS, we expect qualities (...whatever that means) to eventually return to the system that caused them. But clearly, qualities can be perspective-dependent!  What even _is_ a quality?  We still have not answered that question.

This is a challenging problem, because in order to proceed with our implementation of KaaS, we need a way to define:
1. What a quality is
2. What a perspective is
3. What a perceived quality is
4. ... and so on.

Well guess what?  Not a problem :) programming languages are _cool_, man, and I'd like to show you how.

---

Let's define a function to represent a system of inputs and outputs.

```ts
function system<I, O>(input: I) => O {
    // <scope>
    let output = input.foo + input.bar;
    return output as O;
    // </scope>
}
```

The **perspective** of the system is, literally, the scope of the function.

The **perceived qualities** of the system's inputs and outputs are, literally, whatever information it can access within that scope.

Feels like magic, right?  We've hardly even done any real coding, yet we have all the definitions we need to proceed with developing KaaS.

---

Sorry :( This is where I ran out of steam.  The purpose of this post isn't to actually design a system (spoiler alert!) - it's really just to make you think.

**Questions:**

As you've grown spiritually, did it evolve your perspectives on how systems interact?  Further, what did you notice about your new mental models in your professional life?

If you have a computer science brain, and are interested in spirituality, then it's important for you to recognize the mechanical overlap between the two disciplines.

:~)