Abstract Wikipedia/Function model

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search

Wikilambda is a multilingual catalog of functions that anyone can contribute to, where anyone can create, maintain, call, and use functions. Every function can have several implementations, e.g. in different programming languages or using different algorithms. It is a “Wikipedia of functions”, and a sister project run by the Wikimedia Foundation.

Wikilambda is a preliminary name and the final name will be chosen by the community.

This document covers the data model and the evaluation model of Wikilambda.

Throughout this model description, capitalized terms refer to terms defined in the glossary.

Reading the walkthrough of a previous prototype has been very helpful to get a better intuition of what is going on here before reading the following model.

Z1/ZObjects[edit]

Wikilambda is a wiki. As in all wikis, the content of Wikilambda is mainly stored in wiki pages. Wiki pages can be individually edited, and yet the project as a whole must retain a certain consistency. Also, wiki pages should be individually editable without having to understand all of the other pages.

We introduce ZObjects to represent the content of Wikilambda. Each wiki page of the main namespace of Wikilambda contains exactly one ZObject of type Z2/Persistent object. Other namespaces can contain other content, such as policy pages, user pages, discussion pages, etc. A ZObject can be serialized as a JSON object.

A ZObject consists of a list of Key/value pairs. Every value in a Key/value pair is a ZObject. Every Key can only appear once on each ZObject (but may reappear on an embedded ZObject).

ZObjects are basically abstract syntax trees. If there was a TLDR of the project it would probably be “something like LISP in JSON”. The goal is to provide an easy UX to allow the creation and manipulation of ZObjects through a wiki interface, and thus create a coding environment that can reach a large number of contributors and may become a Wikimedia project with an active community.

Every ZObject must have a key Z1K1/type with a value that evaluates to a Z4/Type (this will be explained soon). We use the notation ZID/label to refer to ZIDs in a more or less readable fashion where ‘ZID’ is a ZObject id or a key on such an object, and ‘label’ is the (English language) label attached to that language-neutral id or key.

Syntax[edit]

The canonical representation of a ZObject is a subset of JSON. A well-formed ZObject has the following syntax:

ZObject := String | List | Record
String  := "Character*" // to be specific, as in JSON / ECMA-404
List    := [(ZObject(,ZObject)*)]
Record  := { "Z1K1": ZObject(, "Key": ZObject)* }
Key     := ZNumberKNumber | KNumber

Bold characters are terminal symbols, cursive characters non-terminal symbols. () means optional, * means repeat 0..n times, | means Option. Whitespaces can be used as in JSON. That results in the subset of JSON without numbers, null, booleans, and with limited keys.

In order to be well-formed, the Z1K1 key must have a value that evaluates to a valid Z4/Type.

Serialization[edit]

A ZObject is serialized to its canonical JSON representation using as keys the abstract ZID keys (“Z1K1”, etc.), and for values either (1) the canonical JSON representation of another (transient) ZObject, (2) the ZID of a persistent ZObject (i.e. a string value with a leading capital letter of the latin alphabet and digits following), (3) a simple string value, which can alternatively be represented as a ZObject itself (Z6/string type), or (4) a JSON array as a representation for Z10/list objects.  An alternate more readable representation can be given by replacing the abstract keys and ZID’s with their labels in a given language, the “labelized” representation. The following table gives an example of a ZObject representing the positive integer 2. On the left we see the ZObject labelized in English, in the middle labelized in German, and on the right we see the ZObject using ZIDs.

1 {
2   "type": "positive integer",
3   "base 10 representation": "2"
4 }
{
  "Typ": "natürliche Zahl",
  "Dezimaldarstellung": "2"
}
{
  "Z1K1": "Z70",
  "Z70K1": "2"
}

As you can see, the labels don’t have to be in English, but can be in any of the more than 300 languages Wikilambda supports.

Also note that if the key is Z2K1/id or Z6K1/string value, then the value is always serialized as a simple string value.

Deserialization[edit]

The canonical JSON representation for a ZObject is converted into its computational equivalent essentially through standard JSON parsing - that is the JSON object becomes a ZObject with the same (abstract) keys. The four different allowed value formats are parsed as follows:

If the value is another JSON object, that in turn should follow the same deserialization rules, recursively.

For string values, if the value is a simple string that starts with a capital letter of the latin alphabet followed by a number then it is interpreted as a reference to a persistent ZObject, a Z9/Reference. In the above example, the value of Z1K1/type is given as the string "Z70", but it should be parsed as a reference ZObject which would itself be represented  as follows:

1 {
2   "type": "reference",
3   "reference id": "positive integer"
4 }
{
  "Z1K1": "Z9",
  "Z9K1": "Z70"
}

On the other hand if the value is a simple string that does not start with a capital letter of the latin alphabet followed by a number, the string is understood as a ZObject of the type Z6/String and the given value. I.e. the value of Z70K1/base 10 representation is given as the string "2", but it should be parsed as a ZObject that looks as follows:

1 {
2   "type": "string",
3   "string value": "2"
4 }
{
  "Z1K1": "Z6",
  "Z6K1": "2"
}

Note that the deserialization process should not be repeated as it could in these cases (the value of the Z1K1/type key would be interpreted as of type Z9/Reference, and the value of Z6K1/string value again as of type Z6/string) as that would lead to an infinite recursion; these are internally represented ZObjects, not serialized JSON at this point.  

Given these deserialization rules, if we need to write an actual string that starts with a capital letter of the latin alphabet and is followed by a number, we need to escape this. This uses the exception regarding Z6K1/string value above. Here is the string value "Z1" on the key Z11K2/text.

1 {
2   "type": "text",
3   "language": "English",
4   "text": {
5     "type": "string",
6     "string value": "Z1"
7   }
8 }
{
  "Z1K1": "Z11",
  "Z11K1": "Z251",
  "Z11K2": {
    "Z1K1": "Z6",
    "Z6K1": "Z1"
  }
}

Finally, note that JSON arrays are deserialized as Z10/List ZObjects. The full interpretation of JSON arrays is explained in the section on Z10/Lists below.

Normalization[edit]

For the processing of ZObjects by the evaluator, all ZObjects are turned into a normalized version of themselves. The normalized version is similar to the deserialized version, but we don't rely on any implicitness regarding whether to interpret a string value as a Z6/String or a Z9/Reference, but they are all expressed as explicit ZObjects.

This means the normalized representation of a ZObject is a tree where all leaves are either of the type Z6/String or Z9/Reference.

This also means that all Z10/Lists are represented as ZObjects, not as an array. Furthermore, all Z10/List object must have either both the (Z10)K1/head and (Z10)K2/tail keys, or neither.

The following displays the normalized view on the positive integer 2.

 1 {
 2   "type": {
 3     "type": "reference",
 4     "reference id": "positive integer"
 5    },
 6    "base 10 representation": {
 7      "type": "string",
 8      "string value": "2"
 9    }
10 }
{
  "Z1K1": {
    "Z1K1": "Z9",
    "Z9K1": "Z70"
   },
   "Z70K1": {
     "Z1K1": "Z6",
     "Z6K1": "2"
   }
}

Normalized vies are used only as inputs for the evaluation engine. They ensure that the input for evaluation is always uniformly and easy to process, and requires a minimal amount of special cases.

Persistent and transient[edit]

Every top-level ZObject stored in a Wikilambda wiki page is a Z2/Persistent object. ZObjects that are not stored on their own wiki page are called transient ZObjects.

Every persistent ZObject must have a Z2K1/ZID which is equivalent to the name of the wiki page where it is stored. Let’s assume that there is a ZObject for the positive integer 2 that we saw previously and that it is stored on the page Z382/two. This is how it could look like (note that the ZIDs are not necessarily the ones we will use in Wikilambda).

 1 {
 2   "type": "persistent object",
 3   "id": "Z382",
 4   "value": {
 5     "type": "positive integer",
 6     "base 10 representation": "2"
 7   },
 8   "label": {
 9     "type": "multilingual text",
10     "texts": [
11       {
12         "type": "text",
13         "language": "English",
14         "text": "two"
15       },
16       {
17         "type": "text",
18         "language": "German",
19         "text": "zwei"
20       }
21     ]
22   }
23 }
{
  "Z1K1": "Z2",
  "Z2K1": "Z382",
  "Z2K2": {
    "Z1K1": "Z70",
    "Z70K1": "2"
  },
  "Z2K3": {
    "Z1K1": "Z12",
    "Z12K1": [
      {
        "Z1K1": "Z11",
        "Z11K1": "Z251",
        "Z11K2": "two"
      },
      {
        "Z1K1": "Z11",
        "Z11K1": "Z254",
        "Z11K2": "zwei"
      }
    ]
  }
}

The Z2/Persistent object provides metadata for the ZObject embedded in the Z2K2/value. The Z2K3/label is a ZObject of the type Z12/multilingual text which has one Key, Z12K1/texts pointing to a Z10/list of Z11/monolingual text (remember that a Z10/list is represented as an array in the JSON representation). The label allows for the labelization.

There are further Z3/Keys on Z2/Persistent object that provide metadata, which are for documentation, presentation in the wiki, and to improve findability. They are all defined on Z2/Persistent object.

Z9/References[edit]

As we have seen on the positive number 2 is that the value of Z1K1/type is a ZObject of type Z9/Reference. If we extend that, it would look like this.

1 {
2   "type": {
3     "type": "reference",
4     "reference id": "positive integer"
5   },
6   "base 10 representation": "2"
7 }
{
  "Z1K1": {
    "Z1K1": "Z9",
    "Z9K1": "Z70"
  },
  "Z70K1": "2"
}

A Z9/Reference is a reference to the Z2K2/value of the ZObject with the given ID, and means that this Z2K2/value should be inserted here. To give an example, given the following array with a single element:

1 [
2   "two"
3 ]
[
  "Z382"
]

The element is a shortcut Z9/Reference, that would look like this in the expanded form (as explained in the Section on deserialization):

1 [
2   {
3     "type": "reference",
4     "reference id": "two"
5   }
6 ]
[
  {
    "Z1K1": "Z9",
    "Z9K1": "Z382"
  }
]

And since this is a reference, this is to be replaced with the Z2K2/value of the Z2/Persistent object with the ZID Z382 (as given above), i.e. it would look as follow:

1 [
2   {
3     "type": "positive integer",
4     "base 10 representation": "2"
5   }
6 ]
[
  {
    "Z1K1": "Z70",
    "Z70K1": "2"
  }
]

Note that if a Z8/Function has an argument type of Z2/Persistent object, then, instead of the Z2K2/value, the Z2/Persistent object itself is being substituted in.

Z4/Types[edit]

Types are ZObjects of type Z4/Type. ZObjects of a type are called instances of that type. So Z382/two we saw above was an instance of the type Z70/positive integer.

A Type provides us with the means to check the validity of a ZObject of that type. A Type usually declares the keys available for its instances and a Function that is used to validate the Instances.

Here is (a simplified) type for positive integers.

 1 {
 2   "type": "persistent object",
 3   "id": "Z70",
 4   "value": {
 5     "type": "type",
 6     "identity": "positive integer",
 7     "keys": [
 8       {
 9         "type": "key",
10         "value type": "string",
11         "key id": "Z70K1",
12         "label": {
13           "type": "multilingual text",
14           "texts": [
15             {
16               "type": "text",
17               "language": "English",
18               "text": "base 10 representation"
19             },
20             {
21               "type": "text",
22               "language": "German",
23               "text": "Dezimaldarstellung"
24             }
25           ]
26         },
27         "default": "value required"
28       }
29     ],
30     "validator": "validate positive integer"
31   },
32   "label": {
33     "type": "multilingual text",
34     "texts": [
35       {
36         "type": "text",
37         "language": "English",
38         "text": "positive integer"
39       },
40       {
41         "type": "text",
42         "language": "German",
43         "text": "natürliche Zahl"
44       }
45     ]
46   }
47 }
{
  "Z1K1": "Z2",
  "Z2K1": "Z70",
  "Z2K2": {
    "Z1K1": "Z4",
    "Z4K1": "Z70",
    "Z4K2": [
      {
        "Z1K1": "Z3",
        "Z3K1": "Z6",
        "Z3K2": "Z70K1",
        "Z3K3": {
          "Z1K1": "Z12",
          "Z12K1": [
            {
              "Z1K1": "Z11",
              "Z11K1": "Z251",
              "Z11K2": "base 10 representation"
            },
            {
              "Z1K1": "Z11",
              "Z11K1": "Z254",
              "Z11K2": "Dezimaldarstellung"
            }
          ]
        },
        "Z3K4": "Z25"
      }
    ],
    "Z4K3": "Z559"
  },
  "Z2K3": {
    "Z1K1": "Z12",
    "Z12K1": [
      {
        "Z1K1": "Z11",
        "Z11K1": "Z251",
        "Z11K2": "positive integer"
      },
      {
        "Z1K1": "Z11",
        "Z11K1": "Z254",
        "Z11K2": "natürliche Zahl"
      }
    ]
  }
}

To make the core of the Type easier visible, let’s just look at the Z4/Type and remove the labels:

 1 {
 2   "type": "type",
 3   "identity": "positive integer",
 4   "keys": [
 5     {
 6       "type": "key",
 7       "value type": "string",
 8       "keyid": "Z70K1",
 9       "default": "value required"
10     }
11   ],
12   "validator": "validate positive integer"
13 }
{
  "Z1K1": "Z4",
  "Z4K1": "Z70",
  "Z4K2": [
    {
      "Z1K1": "Z3",
      "Z3K1": "Z6",
      "Z3K2": "Z70K1",
      "Z3K4": "Z25"
    }
  ],
  "Z4K3": "Z559"
}

Type Z70/positive integer defines in Z4K2/keys the new Z3/Key Z70K1/base 10 representation, which we had used above in the instance representing the number 2.

Z4K3/validator points to a Z8/Function that takes an instance as its argument and returns a Z10/List of Z5/errors. If the Z10/List is empty then the instance has passed the validation. In the given case, the Z8/Function would do the following checks:

  • There is one and only one Key, Z70K1/base 10 representation, on the instance, besides the metadata.
  • The value of the base 10 representation has the type Z6/String.
  • The base 10 representation contains only digits.
  • The base 10 representation does not start with a 0, unless it has a length of 1.
  • The base 10 representation is below 232 (or whatever else the limit is for this Type).

Note that all these checks are done by Z8/Functions that are provided by contributors, and that all Types can be defined and modified by contributors. There is nothing hardcoded regarding the number type that we use here.

An instance might use keys that are not defined on the Type. It is up to the validator function to allow that or not. For example, instances of Z7/Function call often use keys not defined on Z7/Function call, as can be seen in the Section on Z7/Function calls. Most validators are expected to require that all keys are defined, though.

But a few things are hardcoded, such as the behavior of Z7/function call. More about this later.

Z3/Keys[edit]

All keys must have a K followed by a natural number, and may be preceded by a ZID. If they are preceded by a ZID they are called Global Keys, if they are not they are called Local Keys. For example, the following two representations are equivalent.

1 {
2   "Z1K1": "Z7",
3   "Z7K1": "Z144",
4   "Z144K1": "Z382",
5   "Z144K2": "Z382"
6 }
{
  "Z1K1": "Z7",
  "Z7K1": "Z144",
  "K1": "Z382",
  "K2": "Z382"
}

Global Keys are named arguments whereas Local Keys are positional arguments. The rule of thumb is to use Global Keys whenever possible. The main use case for Local Keys is when a Z8/Function or Z4/Type is being created on the fly, and thus cannot have Global Keys because the created Z8/Function or Z4/Type itself is not persistent. One example is given by the call to Z79/curry right in the Section on Z20/Tests below.

A Global Key is always defined on the ZObject the ZID part of its ID refers to.

Z8/Functions[edit]

In the definition of Z70/positive integer we saw a first reference to a Z8/Function, Z559/validate positive integer. Here, we will use a much simpler function, Z144/add. Z144/add is a Z8/Function which takes two Z70/positive integers and returns a Z70/positive integer.

Note that we leave the value of Z1K1/Type empty for the moment. We will get back to this in a later section. We only show the value.

 1 {
 2  "type": { ... },
 3  "arguments": [
 4    {
 5      "type": "argument declaration",
 6      "argument type": "positive integer",
 7      "key id": "Z144K1",
 8      "label": { ... }
 9    },
10    {
11      "type": "argument declaration",
12      "argument type": "positive integer",
13      "key id": "Z144K2",
14      "label": { ... }
15    }
16  ],
17  "return type": "positive integer",
18  "tests": [
19    "add one and zero",
20    "add two and two"
21  ],
22  "identity": "Z144"
23 }
{
 "Z1K1": { ... },
 "K1": [
   {
     "Z1K1": "Z17",
     "Z17K1": "Z70",
     "Z17K2": "Z144K1",
     "Z17K3": { ... }
   },
   {
     "Z1K1": "Z17",
     "Z17K1": "Z70",
     "Z17K2": "Z144K2",
     "Z17K3": { ... }
   }
 ],
 "K2": "Z70",
 "K3": [
   "Z1441",
   "Z1442"
 ],
 "K5": "Z144"
}

To remain concise, we removed the Z17K2/labels from the Z17/Argument declarations, which are identified using Z17K2/keys IDs. But just like the Z3/Keys on Z4/Types, they have labels in all supported languages. The Keys are Global when the Z8/Function is persistent, and Local when transient.

The Function is specified through the (omitted) documentation, but also through the K3/tests and the K1/type declarations on the arguments and the K2/return type. Furthermore, since a Function can have several Z14/Implementations (see below), the Implementations confirm each other.

Z8/Functions are not allowed to have state-changing side effects.

Z7/Function calls[edit]

The following ZObject represents a function call. In the second row, we see a more compact representation of the function call, that uses a syntax that is more familiar for function calls.

1 {
2   "type": "function call",
3   "function": "add",
4   "left": "two",
5   "right": "two"
6 }
{
  "Z1K1": "Z7",
  "Z7K1": "Z144",
  "Z144K1": "Z382",
  "Z144K2": "Z382"
}
add(two, two) Z144(Z382, Z382)}

Using literals instead of persistent ZObjects for the arguments, this would look as follows. Note that we are creating the literals using the Z70/positive integer as a constructor. All Z4/Types can be called like this, providing a value for each of their keys. This is not a Z7/Function call, but a notation for the object of the given Z4/Type.

 1 {
 2   "type": "function call",
 3   "function": "add",
 4   "left": {
 5     "type": "positive integer",
 6     "base 10 representation": "2"
 7   },
 8   "right": {
 9     "type": "positive integer",
10     "base 10 representation": "2"
11   }
12 }
{
  "Z1K1": "Z7",
  "Z7K1": "Z144",
  "Z144K1": {
    "Z1K1": "Z70",
    "Z70K1": "2"
  },
  "Z144K2": {
    "Z1K1": "Z70",
    "Z70K1": "2"
  }
}
add(positive_integer("2"), positive_integer("2")) Z144(Z70("2"), Z70("2"))

When this Z7/Function call gets evaluated, it results as expected in the number four.

1 {
2   "type": "positive integer",
3   "base 10 representation": "4"
4 }
{
  "Z1K1": "Z70",
  "Z70K1": "4"
}

Evaluation is performed repeatedly on the evaluation result until a fixpoint is reached.

A Z8/Function has an optional K4/implementation key that points to the Z14/Implementation to be used. This is usually filled by the evaluation engine as part of the evaluation step, but if a specific implementation is given, the evaluator will honor it.

Z14/Implementations[edit]

Every Z8/Function can have a number of different Z14/Implementations. There are three main types of Z14/Implementations: builtins, Z16/code, or through composition of other Z8/Functions. Let us take the Z144/add Function and look at five different Z14/Implementations.

Builtin implementation[edit]

A builtin implementation tells the evaluator, i.e. the runtime, to return an appropriate evaluation result. Builtins are hardcoded into the evaluator. Z14K4/builtin refers to the hard-coded builtin-ID (which usually should be ZID of the Z2/Persistent object, but does not have to).

1 {
2   "type": "implementation",
3   "implements": "add",
4   "builtin": "Z1443"
5 }
{
  "Z1K1": "Z14",
  "Z14K1": "Z144",
  "Z14K4": "Z1443"
}

Builtins do not have to be declared as ZObjects in order to be used. An evaluator would be aware of all its own builtins and could use them at will. It is still useful to declare a builtin in order to call them directly.

Z16/Code[edit]

Note: This section might need to be reworked, in order to let different programming languages have different implementations.

An implementation in Z16/Code represents a code snippet in a given programming language.

1 {
2   "type": "implementation",
3   "implements": "add",
4   "code": {
5     "type": "code",
6     "language": "javascript",
7     "source": "K0 = Z144K1 + Z144K2"
8   }
9 }
{
  "Z1K1": "Z14",
  "Z14K1": "Z144",
  "Z14K3": {
    "Z1K1": "Z16",
    "Z16K1": "Z701",
    "Z16K2": "K0 = Z144K1 + Z144K2"
  }
}
1 {
2   "type": "implementation",
3   "implements": "add",
4   "code": {
5     "type": "code",
6     "language": "python3",
7     "source": "K0 = Z144K1 + Z144K2"
8   }
9 }
{
  "Z1K1": "Z14",
  "Z14K1": "Z144",
  "Z14K3": {
    "Z1K1": "Z16",
    "Z16K1": "Z703",
    "Z16K2": "K0 = Z144K1 + Z144K2"
  }
}

In this case, the implementations are the same for JavaScript and Python, but that is obviously rarely the case.

The evaluator would know how to transform the given ZObjects representing the arguments into the supported programming languages (which is also the reason we limit the positive integer type to 32 bit - as the behavior for the two supported languages is equivalent in that range), how to execute the provided code snippet, and then how to transform the result back into a ZObject representing the result.

Eventually, the translation of ZObjects to the native values of the supported programming languages would be handled inside Wikilambda itself (which will require a new design document). Until then, we only support Z16/Code for arguments and return types that have hard-coded support by the evaluator.

Composition[edit]

The most portable (but often also the slowest) Z14/Implementation is achieved through composition of other Z8/Functions. We will look at two different implementations.

We show both the ZObject of the implementation, as well as an easier to read notation based on function call syntax (note that this syntax also replaces spaces in labels with underscores).

 1 {
 2  "type": "implementation",
 3  "implements": "add",
 4  "composition": {
 5    "type": "function call",
 6    "function": "lambda to integer",
 7    "arg": {
 8      "type": "function call",
 9      "function": "lambda add",
10      "left": {
11        "type": "function call",
12        "function": "integer to lambda",
13        "arg": {
14          "type": "argument reference",
15          "reference": "left"
16        }
17      },
18      "right": {
19        "type": "function call",
20        "function": "integer to lambda",
21        "arg": {
22          "type": "argument reference",
23          "reference": "right"
24        }
25      }
26    }
27  }
28 }
{
 "Z1K1": "Z14",
 "Z14K1": "Z144",
 "Z14K2": {
   "Z1K1": "Z7",
   "Z7K1": "Z71",
   "Z71K1": {
     "Z1K1": "Z7",
     "Z7K1": "Z77",
     "Z77K1": {
       "Z1K1": "Z7",
       "Z7K1": "Z72",
       "Z72K1": {
         "Z1K1": "Z18",
         "Z18K1": "Z144K1"
       }
     },
     "Z77K2": {
       "Z1K1": "Z7",
       "Z7K1": "Z72",
       "Z72K1": {
         "Z1K1": "Z18",
         "Z18K1": "Z144K2"
       }
     }
   }
 }
}

lambda_to_positive_integer(
  lambda_add(
    positive_integer_to_lambda(left),
    positive_integer_to_lambda(right)
  )
)

Z71(
  Z77(
    Z72(Z144K1),
    Z72(Z144K2)
  )
)

Wikilambda contains a full implementation of the Lambda calculus (in fact, that’s one of the inspirations for the name). This composition translates the Z70/positive integers into Church numerals using Z72/positive integer to lambda, then uses the addition as it is implemented in the Lambda calculus of Wikilambda (Z77/lambda add), and the result is translated to a Z70/positive integer from a Church numeral using Z71/lambda to positive integer. (In case you’re curious - Z77/lambda add is implemented the same way as Z1445/add recursive below, but using lambda-based functions as defined over Church numerals).

This relies entirely on the Lambda calculus. This is anything but fast - but it allows us to use a well-understood formalism and a very simple implementation of it in order to ensure that the other implementations of Z144/add are correct - admittedly, probably of less interest for addition, but we can imagine that there are Z8/Functions that have more obviously correct implementations and much cleverer faster implementations. Wikilambda can cross-test these implementations against each other and thus give us some sense of security regarding their correctness.

 1 {
 2  "type": "implementation",
 3  "implements": "add",
 4  "implementation": {
 5    "type": "function call",
 6    "function": "if",
 7    "condition": {
 8      "type": "function call",
 9      "function": "is zero",
10      "arg": {
11        "type": "argument reference",
12        "reference": "right"
13      }
14    },
15    "consequent": {
16      "type": "argument reference",
17      "reference": "left"
18    },
19    "alternative": {
20      "type": "function call",
21      "function": "add",
22      "left": {
23        "type": "function call",
24        "function": "successor",
25        "arg": {
26          "type": "argument reference",
27          "reference": "left"
28        }
29      },
30      "right": {
31        "type": "function call",
32        "function": "predecessor",
33        "arg": {
34          "type": "argument reference",
35          "reference": "right"
36        }
37      }
38    }
39  }
40 }
{
 "Z1K1": "Z14",
 "Z14K1": "Z144",
 "Z14K2": {
   "Z1K1": "Z7",
   "Z7K1": "Z31",
   "Z31K1": {
     "Z1K1": "Z7",
     "Z7K1": "Z145",
     "Z145K1": {
       "Z1K1": "Z18",
       "Z18K1": "Z144K2"
     }
   },
   "Z31K2": {
     "Z1K1": "Z18",
     "Z18K1": "Z144K1"
   },
   "Z31K3": {
     "Z1K1": "Z7",
     "Z7K1": "Z144",
     "Z144K1": {
       "Z1K1": "Z7",
       "Z7K1": "Z146",
       "Z146K1": {
         "Z1K1": "Z18",
         "Z18K1": "Z144K1"
       }
     },
     "Z144K2": {
       "Z1K1": "Z7",
       "Z7K1": "Z147",
       "Z147K1": {
         "Z1K1": "Z18",
         "Z18K1": "Z144K2"
       }
     }
   }
 }
}

if(
  is_zero(right),
  left,
  add(
    successor(left),
    predecessor(right)
  )
)

Z31(
  Z145(Z144K2),
  Z144K1,
  Z144(
    Z146(Z144K1),
    Z147(Z144K2)
  )
)

This composition relies on a number of other Z8/Functions: Z145/is zero, Z146/successor, Z147/predecessor, Z31/if, and, most interestingly - itself. It is entirely OK for an Z14/Implementation to call its own Z8/Function recursively. Note though that the evaluator does not have to call the Z14/Implementation recursively - an evaluator is free to choose any implementation at each recursion step.

Evaluation order[edit]

The evaluation order is up to the evaluator. Since all Z8/Functions are not allowed to have side-effects, this will always lead to the same result. But an unwise evaluation strategy can lead to much more computation than necessary or even to the evaluator not terminating. Z1445/add recursive provides us with an example that might end up in an endless loop if we try a complete evaluation order:

For the call to Z31/if in Z1445/add recursive it would be unwise to first evaluate all three arguments and then to return either the second or the third argument. Depending on the first argument Z31K1/condition we will only need to return either Z31K2/consequent or Z31K3/alternative. There is never the case that we need to evaluate both the second and the third argument.

In fact we could even return the second or third argument unevaluated. Remember that the evaluator will evaluate each result again anyway until a fixpoint is reached. So Z31/if can be implemented lazily, drop the irrelevant branch, and return the relevant branch as an unevaluated ZObject.

A lazy evaluation strategy is in general recommended, but for example when the evaluator wants to use a Z16/Code based implementation, it might not be feasible, and then the evaluator might decide to first evaluate the arguments and then the outer call. In the end, there are opportunities to experiment with different evaluation strategies.

Z20/Tests[edit]

Z20/Tests are ZObjects that call a Z7/function call and then test the result using a Z8/Function. If the Z8/Function returns an Z54/true, the Z20/Test passes, otherwise it fails.

Tests are used to ensure that all Z14/Implementations behave as they should, and should be considered similar to unit tests. A Z8/Function should list all the Z20/Tests that need to pass for an Z14/Implementation to be compliant. Additionally, the different Z14/Implementations can be cross-tested against each other for consistency.

 1 {
 2  "type": "test",
 3  "call": {
 4    "type": "function call",
 5    "function": "add",
 6    "left": "two",
 7    "right": "two"
 8  },
 9  "check": {
10    "type": "function call",
11    "function": "curry right",
12    "function to curry": "equal positive integer",
13    "right": "four"
14  }
15 }
{
 "Z1K1": "Z20",
 "Z20K1": {
   "Z1K1": "Z7",
   "Z7K1": "Z144",
   "Z144K1": "Z382",
   "Z144K2": "Z382"
 },
 "Z20K2": {
   "Z1K1": "Z7",
   "Z7K1": "Z79",
   "Z79K1": "Z150",
   "Z79K2": "Z384"
 }
}

Z1442/Add two and two exhibits an interesting pattern: the Z20K2/check Function is created on the fly by currying the function Z150/equal positive integer. We would not want to write a Z8/Function called “equals four”, but instead use the Z150/equal positive integer function and fix (curry) one of its arguments with a constant value, Z384/four in this case. So when it runs, Z20K2/check will have the value of a dynamically created Z8/Function, which in turn will be used to validate the result of the function call in Z20K1/call.

Generic types[edit]

A generic type is realized by a Z7/Function call to a Z8/Function which takes some arguments and returns a Z4/Type.

For example, Z22/Pair can be parametrized with two Z4/Types, one for the first and one for the second element. So if we want to make a pair of Z70/Positive integers, we would call Z22/Pair(Z70/Positive Integer, Z70/Positive integer) and the result would be a Z4 which we can use for the Z1K1 field of a ZObject.

 1 {
 2  "type": {
 3    "type": "function call",
 4    "function": "pair",
 5    "first": "positive integer",
 6    "second": "positive integer"
 7  },
 8  "first": "one",
 9  "second": "two"
10 }
{
 "Z1K1": {
   "Z1K1": "Z7",
   "Z7K1": "Z22",
   "Z22K1": "Z70",
   "Z22K2": "Z70"
 },
 "K1": "Z381",
 "K2": "Z382"
}

The result of the Z7/Function call is a dynamically created Z4/Type that ensures that the two elements of the Pair have the right Z4/Type. The result of that Z7/Function call looks like this.

 1 {
 2  "type": "type",
 3  "identity": {
 4    "type": "function call",
 5    "function": "pair",
 6    "first": "positive integer",
 7    "second": "positive integer"
 8  },
 9  "keys": [
10    {
11      "type": "key",
12      "id": "K1",
13      "value type": "positive integer",
14      "required": "true"
15    },
16    {
17      "type": "key",
18      "id": "K2",
19      "value type": "positive integer",
20      "required": "true"
21    }
22  ],
23  "validator": "validate pair"
24 }
{
 "Z1K1": "Z4",
 "Z4K1": {
   "Z1K1": "Z7",
   "Z7K1": "Z22",
   "Z22K1": "Z70",
   "Z22K2": "Z70"
 },
 "Z4K2": [
   {
     "Z1K1": "Z3",
     "Z1K2": "K1",
     "Z3K1": "Z70",
     "Z3K2": "Z54"
   },
   {
     "Z1K1": "Z3",
     "Z1K2": "K2",
     "Z3K1": "Z70",
     "Z3K2": "Z54"
   }
 ],
 "Z4K3": "Z702"
}

This also explains the Z4K1/identity field on Z4/Type: it describes how the Z4/Type was created, and allows us to access the arguments used for Type creation. Keeping this information declaratively is very helpful for validating a Function call statically, and for comparing types.

If we want a Z22/Pair that doesn’t restrict the Z4/Type of one or both of its elements, one could call the Z22/Pair function with Z1/ZObject as one or both arguments.

Z10/Lists[edit]

Here is a list of two strings.

1 [
2  "a",
3  "b"
4 ]
[
 "a",
 "b"
]

If we turn this into ZObjects, it looks as follows.

 1 {
 2  "type": {
 3    "type": "function call",
 4    "function": "list",
 5    "elementtype": "string"
 6  },
 7  "head": "a",
 8  "tail": {
 9    "type": {
10      "type": "function call",
11      "function": "list",
12      "elementtype": "string"
13    },
14    "head": "b",
15    "tail": "nil"
16  }
17 }
{
 "Z1K1": {
   "Z1K1": "Z7",
   "Z7K1": "Z10",
   "Z10K1": "Z6"
 },
 "K1": "a",
 "K2": {
   "Z1K1": {
     "Z1K1": "Z7",
     "Z7K1": "Z10",
     "Z10K1": "Z6"
   },
   "K1": "b",
   "K2": "Z13"
 }
}

Note that the deserialization of a JSON array literal has to first check whether all elements of the array have the same Z1K1/type. If yes, we create a Z10/List using that type as the argument. If not, we create a Z10 using Z1/ZObject as the argument.

Z8/Function types[edit]

Functions are using the same mechanism as generic types to be concrete Function types. I.e. Z8/Function is not the Z4/Type of a Function, but rather is a Function itself that creates a Z4/Type that can be used for a Function.

On Z144/add, we previously skipped the Z1K1/type field. Here is the field.

 1 {
 2  "type": {
 3    "type": "function call",
 4    "function": "function",
 5    "return type": "positive integer",
 6    "argument types": [
 7      "positive integer",
 8      "positive integer"
 9    ]
10  },
11  ... 
12 }
{
 "Z1K1": {
   "Z1K1": "Z7",
   "Z7K1": "Z8",
   "Z8K1": "Z70",
   "Z8K2": [
     "Z70",
     "Z70"
   ]
 },
 ... 
}

This will return the Z4/Type that also contains its return and argument types.

If you need a Z4/Type for a Z8/Function that works with any Z8K1/return type and Z8K2/argument types, then we would use Z1/ZObject for Z8K1/return type or the empty list as the Z8K2/argument type. This is useful, e.g., for a Z8/Function that tells you the number of arguments a Z8/Function has. Obviously, if you would already need to know what the number of arguments is in order to select the right function, this would be quite useless.

The validator on Z4/Function ensures that the types as given on the Z7/function call to Z8/Function are consistent with the types given on the Z4K2/key declaration.

Z5/Errors[edit]

A Z7/Function call could always result in a Z5/Error. This can be for more or less unrecoverable cases (i.e. division by zero or out of memory both are handled the same way). A Z5/Error is a generic type called with a Z15/Error type.

Normally, when a Z5/Error is being given as an argument to a Z7/Function call, the Z5/Error will be wrapped to enable tracing and passed through. The evaluator won’t even call the Z14/Implementation.

If a Z8/Function wants to catch a Z5/Error, it has to explicitly list all the Z15/Error types it wants to catch in its signature. If then a Z5/Error of the listed Z15/Error types shows up, the Z14/Implementation will still be called and can now handle the Z5/Error.

An Z5/Error is an instance of Z4/Type Z5/Error(Z15/Error type). The resulting type has further Z3/Keys, as specified by the Z15/Error type, in order to keep useful information about the Z5/Error.

Example: if you call the Z157/division function with a Z380/zero for the Z157K2/denominator, you would get back the the following object: Z5/error(Z442/division_by_zero)(Z384/four)

Non-functional Functions[edit]

Whereas no Z8/Function is allowed to have side effects, some Z8/Functions might not be entirely functional. I.e. they might return different values when being called with the same parameters. Typical such Z8/Functions are “return a random number”, “return the current time”, or “return a value from Wikidata”.

We also see that it would make sense not only to say whether a Z8/Function is functional or not, but in fact to specify a caching regime for each Z8/Function.

All these Z8/Functions need to be marked, and no Z8/Function that is fully functional may call a Function that is not. The evaluator has to make sure that this condition is being fulfilled, or else results might be stale or otherwise unexpected.

This will be handled in a later document.

Zx/Sum types[edit]

A particularly useful generic type is the Zx/Sum type, which takes a list of Z4/Types and returns a Z4/Type that can take exactly one instance of any of the given types.

This will also allow for non-required parameters in function calls.

This will be handled in a later document.

REPL[edit]

The REPL, or command line interface, takes an input Z6/string. It then runs a Z8/Function over that input string which parses and preprocesses the input and returns a ZObject. That ZObject gets validated by the validator of the Z4/Type of the ZObject. If valid, the result then evaluates until it reaches a fix point. Then the REPL calls a Z8/Function to linearize the resulting ZObject to a Z6/string. This string is then displayed or printed out to the user and the REPL asks for new input.

This follows the definition of REPL: Read, Evaluate, Print, Loop.

A good Wikilambda REPL should allow to:

  • set the parsing function
  • switch validation on or off
  • switch evaluation on or off
  • set the linearization function

This allows for many interesting patterns. For example, there could be parsers for many different syntaxes. Simple function call syntaxes as we have seen above, a Haskell-like syntax without many brackets, a syntax that is closer to mathematics using infix operators, translations from labels to ZIDs, or entirely domain specific syntaxes. The parser can also include a preprocessor. One example of preprocessing is to automatically add type coercions, thus making it easier to write Z7/Function calls, or specify the Z14/Implementation on a Z8/Function, and thus force the evaluator to use a specific Z14/Implementation.

Also the linearizer can be highly flexible towards a given use case. Parsers and linearizers will be Z8/Functions in Wikilambda, which will allow anyone to create new parsers and linearizers and use them in a wide range of interfaces, thus enabling interesting usage patterns.

Reference of central Z4/Types and their Z3/Keys[edit]

  • Z1/ZObject
    • Z1K1/type (Z4/Type)
  • Z2/Persistent object
    • Z2K1/id (Z6/string)
    • Z2K2/value (Z1/ZObject)
    • Z2K3/label (Z12/Multilingual text)
  • Z3K1/Key
    • Z3K1/value type (Z4/Type)
    • Z3K2/key id (Z6/String)
    • Z3K3/label (Z12/Multilingual text)
  • Z4/Type
    • Z4K1/identity (Z4/Type)
    • Z4K2/keys (Z10/List(Z3/Key))
    • Z4K3/validator (Z8/Function(...))
  • Z5/Error
    • Z5K1/error type
  • Z6/String
    • Z6K1/string value (Z6/String)
  • Z7/Function call
    • Z7K1/function (Z8/Function)
    • Others based on Z8/Function
  • Z8/Function (generic)
    • K1/arguments (Z10/List(Z17/Argument declaration))
    • K2/return type (Z4/Type)
    • K3/tests (Z10/List(Z20/Test))
    • K4/implementation (Z14/Implementation)
    • K5/identity (Z8/Function)
  • Z9/Reference
    • Z9K1/reference ID (Z6/String)
  • Z10/List (generic)
    • K1/head
    • K2/tail

Main differences to AbstractText[edit]

  • Only Z7/Function calls get evaluated. Everything else does not get evaluated.
  • Introduction of generic types
  • Z10/String and Z22/Pair are now generic types.
  • Z8/Function is a generic type, based on the types of the inputs and outputs.
  • Added a K5 to Z8s
  • Changed Z3K2 form validator to keyid, Z3K3 from required to label, added Z3K4-Z3K6
  • Removed Z4K4-Z4K7
  • Subtle change to Z4K1
  • Changed all keys on Z14/Implementation, got rid of Z19/builtin
  • Turn Z20/Test into top level ZObjects instead of subobjects of Z8/Function
  • By using autoquote on Z20/Test, got rid of Z21/Argumentlist
  • Turn Z14/Implementation into top level ZObjects instead of subobjects of Z8/Function
  • ZID of Pair was changed from Z2 to Z22 (in order to give space for Z2/Persistent object
  • Z5/Error and Z15/Exception have been folded together into Z5/Error
  • Z5/Error is now generic type.
  • Specified REPL behavior
  • Break out Z2/Persistent object from Z1
  • Added Z17K2/Keyid and Z17K3/Label to Z17
  • Builtins can now change their ZID

Some questions and Todos[edit]

  • Do we need “required/option” for keys anywhere in the beginning? - no
  • Replace defaults on Z3/Key with Zx/Sum? (Or at least make it consistent with Z17/argument declaration)
  • Could be left for later if we don’t need default on Z3 for now
  • Remove Z2K4 for now?
  • Break out Z16/Implementation by programming language?
  • If Errors can have their own fields, programming languages should too
  • Make it generic on programming language. That’s for later.