Coder Social home page Coder Social logo

hvm-core-serialization's Introduction

Encoding

This crate provides a set of functions to encode and decode an interaction net and its related structures.

Net

A net consists of a root tree, a list of trees pointing to each other(redexes), and a wiring.

So we basically encode:

  • The root tree using the Tree encoding
  • How many redexes there are, using VarLenNumber encoding
  • The redexes(which are Vec<(Tree, Tree)>) using the Tree encoding, and reading 2 times the number of redexes
  • The wiring using the Wiring encoding

Scalars

  • HVMRef, uses fixed 60-bits.
  • VarLenNumber: uses elias-gamma encoding.
  • Tag uses 3-bits for the variant and:
    • for NUM, has a VarLenNumber
    • for REF, has a HVMRef
    • for TUP/DUP, has a VarLenNumber for the label
    • for OP2 and OP1, has a fixed 1-bit to differentiate between OP1 and OP2, and 4-bits for the operator.
    • for the rest, has no data

Tree

Note: The tree encoding by itself does NOT handle the encoding of variables, they are handled by the Net encoding.

We encode the tree by following a pre-order traversal, and writing the tag of each node

So for example, the tree ((@foo *) [* #123]):

  • By doing a pre-order traversal, we get the structure:
    1. ((@foo *) [* #123]), has children, visit left, write CON (3-bits)
    2. (@foo *), has children, visit left, write CON (3-bits)
    3. @foo, has no children, write REF(@foo), backtrack to sibling (3-bits + 60-bits)
    4. *, has no children, write ERA, backtrack to sibling (3-bits)
    5. [* #123], has children, visit left, write DUP(0) (3-bits + 1-bit)
    6. *, has no children, write ERA, backtrack to sibling (3-bits)
    7. #123, has no children, write NUM(123) (3-bits + 15-bits)
    8. Done, with a total of 65 bits

Wiring

For encoding the whole net, we need to encode the wiring between all of the VAR nodes(free ports).

Remember that any VAR node can connect with any other VAR node in the net.

Suppose we have 6 ports, and we want to encode the following wiring:

1 2 3 4 5 6
-----------
a b b c a c
  • the 1st port connects with the 5th
  • the 2nd port connects with the 3rd
  • the 4th port connects with the 6th

We can encode this in the following way:

starting with an empty wiring:
---wiring--   | written bits
_ _ _ _ _ _   | []

we begin with the first empty port, which is "a":
a _ _ _ _ _   | []

we know it is connected with the 5th port which looking at the
available empty ports is the 4th port out of 5 possible available ports
so we know that we only have to encode 5 numbers, which can fit in 3 bits,
so we write 4 in 3 bits:
a _ _ _ a _   | [0b100]

we continue with the first empty port, which is "b":
a b _ _ a _   | [0b100]

again, we know that it connects to the "global" 3rd port,
but looking at the available empty ports, we see that it is the 1st available port
out of 3 possible available ports, so we write 0 in 2 bits:
a b b _ a _   | [0b100, 0b00]

we are now in the last connection, because it is the last one,
we know that it can only connect to the remaining one, so we don't
need to write any more bits to recover the original wiring:
a b b c a c   | [0b100, 0b00]

hvm-core-serialization's People

Contributors

eduhenke avatar

Stargazers

denuitt avatar Zicklag avatar

Watchers

Victor Taelin avatar Zicklag avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.