|
| 1 | +# Custom operations (or: how to define a custom predicate): VERSION 2 |
| 2 | + |
| 3 | +# DO NOT USE THIS DOC |
| 4 | +# SAVING IN THE GITHUB JUST SO WE HAVE A COPY |
| 5 | +# WE ARE NOT USING THIS SPEC |
| 6 | +# DO NOT USE |
| 7 | + |
| 8 | +## The local variable requirement |
| 9 | + |
| 10 | +This spec differs from the main spec in that there are no anchored keys. However, there are still types `Origin` and `Key`. |
| 11 | + |
| 12 | +An `Origin` refers to an input POD; in-circuit, the `Origin` is the pod ID of the pod. |
| 13 | + |
| 14 | +A `Key` refers to a value within a POD. |
| 15 | + |
| 16 | +With the exception of the special statement `ValueFromPodKey`, a key always refers to a value within the POD _self. In other words, a statement (except for `ValueFromPodKey`) cannot refer to a value from a previous POD, only to a value in the "namespace" of the current POD. |
| 17 | + |
| 18 | +Roughly speaking, the statement |
| 19 | +``` |
| 20 | +ValueFromPodKey(local_key, origin_id, key) |
| 21 | +``` |
| 22 | +means that the value of `local_key` on the current POD (_self) is the same as the value of `key` on the POD `origin_id` -- in other words, it is basically the same as |
| 23 | +``` |
| 24 | +Equals(AnchoredKey(_SELF, local_key), AnchoredKey(origin_id, key)). |
| 25 | +``` |
| 26 | + |
| 27 | +I say "basically the same" because, in this spec, it is possible to refer to both keys and origin IDs by reference. |
| 28 | + |
| 29 | +## Referencing |
| 30 | + |
| 31 | +Recall that in the front-end, a `Key` is a string that functions as an identifier (like a variable name in other languages), and a `Value` is the value of that variable -- an `Integer`, `String`, or compound value. |
| 32 | + |
| 33 | +In the back-end, a `Key` is four field elements (computed as a hash of the front-end key); and a `Value` is again four field elements. Again, each `Key` has a unique `Value`. |
| 34 | + |
| 35 | +A `Reference` statement allows a key to be reinterpreted as a value; it is analogous to a pointer in C. |
| 36 | + |
| 37 | +The statement |
| 38 | +``` |
| 39 | +Reference(reference_key, key) |
| 40 | +``` |
| 41 | +means that `reference_key` is a key, whose associated value is the same as the key `key`. |
| 42 | + |
| 43 | +## ValueFromPodKey, precisely this time |
| 44 | + |
| 45 | +``` |
| 46 | +ValueFromPodKey(local_key: KeyOrLiteral::String, origin_id: KeyOrLiteral::OriginID, key: KeyOrLiteral::String). |
| 47 | +``` |
| 48 | + |
| 49 | +means that the _values_ of `local_key` and `key` are _keys_, the _value_ of `origin_id` is an _origin ID_, and the value assigned to the key `local_key` on the present POD is the same as the value assigned to the key `key` on the pod `origin_ID`. |
| 50 | + |
| 51 | +An example with literals: |
| 52 | +``` |
| 53 | +ValueFromPodKey("local_ssn", 0x4030, "ssn") |
| 54 | +``` |
| 55 | +means that the pod `0x4030` has a key called `ssn`, the local pod has a key `local_ssn`, and they have the same value. |
| 56 | + |
| 57 | +An example with keys, that expresses the same semantic meaning: |
| 58 | +``` |
| 59 | +ValueOf(local_varname, "local_ssn") |
| 60 | +ValueOf(remote_varname, "ssn") |
| 61 | +ValueOf(gov_id_pod_id, 0x4030) |
| 62 | +ValueFromPodKey(local_varname, gov_id_pod_id, remote_varname) |
| 63 | +``` |
| 64 | + |
| 65 | +## Summary of additional statements in this spec |
| 66 | + |
| 67 | +``` |
| 68 | +ValueFromPodKey(local_key: KeyOrLiteral::String, origin_id: KeyOrLiteral::OriginID, key: KeyOrLiteral::String). |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | +In addition to the built-in statements in the [main spec](./statements.md): |
| 73 | + |
| 74 | +There is one additional front-end type: `OriginID`. As the name suggests, it contains the "origin ID" of a POD. |
| 75 | + |
| 76 | +There are two additional built-in statements: |
| 77 | +``` |
| 78 | +Reference(reference_key: Key::String, key: Key) |
| 79 | +
|
| 80 | +ValueFromPodKey(local_key: KeyOrLiteral::String, origin_id: KeyOrLiteral::OriginID, key: KeyOrLiteral::String). |
| 81 | +``` |
| 82 | + |
| 83 | +``` |
| 84 | +Reference(reference_key, key) |
| 85 | +``` |
| 86 | +means that the *value* of `reference key` is the *key name* of `key`. |
| 87 | + |
| 88 | +``` |
| 89 | +ValueFromPodKey(local_key, origin_id, key) |
| 90 | +``` |
| 91 | +means that the key `local_key` in the local scope has the same value as the key `key` in the scope of the pod `origin_id`. |
| 92 | + |
| 93 | +## How to work with the local variable requirement |
| 94 | + |
| 95 | +To make a statement about an inherited value (a value introduced in an ancestor POD), the value must be copied to a local value: |
| 96 | + |
| 97 | +The statements below assert that "name" on pod1 and "friend" on pod2 are assigned the same value. |
| 98 | +``` |
| 99 | +ValueFromPodKey(name_from_pod1, pod1, "name") |
| 100 | +ValueFromPodKey(friend_from_pod2, pod2, "friend") |
| 101 | +Equal(name_from_pod1, friend_from_pod2) |
| 102 | +``` |
| 103 | + |
| 104 | +## How to inherit local variables from a previous POD |
| 105 | + |
| 106 | +In this design, an additional complication arises when you |
| 107 | +carry a value from one POD to another, |
| 108 | +and you want to keep track of the origin POD on which it originated. |
| 109 | + |
| 110 | +To allow this operation, we introduce an additional deduction rule |
| 111 | +``` |
| 112 | +InheritValueFromPodKey, |
| 113 | +``` |
| 114 | +which works as follows. |
| 115 | + |
| 116 | +Suppose "self" is the current POD and "parent_id" is the POD id of one of the input PODs to "self". |
| 117 | + |
| 118 | +Suppose "parent" has, among its public statements, the statement |
| 119 | +``` |
| 120 | +ValueFromPodKey(parent_name, origin, original_name) |
| 121 | +``` |
| 122 | +and "self" has the statement (public or private) |
| 123 | +``` |
| 124 | +ValueFromPodKey(self_name, parent_id, parent_name). |
| 125 | +``` |
| 126 | + |
| 127 | +Then ```InheritValueFromPodKey``` allows you to generate the following statement on "self": |
| 128 | +``` |
| 129 | +ValueFromPodKey(self_name, origin, original_name). |
| 130 | +``` |
0 commit comments