Lesson 5 — Sibilance

Introduction


We must begin combining things we’ve learned in prior lessons, so that we can produce increasingly complex generators.

Once we begin stitching pieces together, you will discover new and interesting ways to create complex programs.  You might even be ready to make some programs of your own, but stick with us nonetheless!

In this lesson, we’re going to take what we learned in the last several lessons and combine them into a silly generator that takes in a tape and produces one of two tapes.

This exercise will tie together the last two lessons in which we looked at conditionals and cores with multiple arms.  Later programs will have many arms and may need to call one or another based on the inputs, which makes this structure (a conditional in a multi-arm core) fairly important.

Goal

Write a generator that checks an incoming tape for a specific ‘passphrase.’ If the passphrase is found, produce another desired tape; if not, produce a tape indicating the failure.

Rune List


=. tis·dot

SyntaxSummary
=.  wing  hoon  hoonChange the value of a leg of the subject

=.  changes the value of one leg of the subject. Technically =. creates a new subject, identical to the old subject except with the specified change.

Tisdot takes 3 children:

  1. the face from the subject you intend to modify,
  2. the new value for that face
  3. the following hoon which can use the face’s new value in evaluation, until that evaluation is complete
Documentation on urbit.org

Exercise

Try this in dojo:

=anything "anything"
anything
"anything"
=.  anything  "nothing"
anything
"nothing"

First, we add “anything” to our subject with face, ‘anything’,  immediately evaluate it and get ”anything”.

Next, we use =. to modify the wing with face ‘anything’, replacing it with our second child here, “nothing”. Then, we evaluate the third child, which is simple evaluating ‘anything’.

Our output is “nothing”, which we should have expected if we’re following along.

But note - if you attempt to call anything again:

anything
"anything"

We get our original value. That is because =. creates and mutates a temporary subject, used only for the evaluation of =.;.

Be sure to clear your dojo of this association afterward by using:

=anything


list

SyntaxSummary
(list <type>)Produces a type, which is a list of items having a specified type.

++list is a standard library function known as “mold generator”.
++list takes one argument, a type, and produces a mold of a list of that type.


Exercise

Try this in dojo:

(cass "SOMETHING")
"something"


Program Flow

As with our last generator, and from here on, you will need to begin parsing the flow of a program as you read it.  Not everything is going to operate linearly along the lines of the program.  Some calls will require us to move our focus down a few lines and then back up; conditionals will require us to think about which of two optional pieces of code will be executed.  Begin to practice this now.  We will walk you through it again this time.

|=  test-test=tape
|^
^-  tape
?:  =((checker test-test) &)
  (weld "One Two... One Two..." test-test)
"*horrific feedback whine*"
++  checker
  |=  to-check=tape
  =.  to-check  (cass to-check)
  ?:  =(to-check "mic check")
    &
  |
--

Note: This generator again unnecessarily uses an arm to perform a check which could be performed inline - the point is to demonstrate the capacities of using conditionals and arms together - this is not necessarily a well written generator.  If you’re feeling adventurous, rewrite this without the use of a core, doing the check inline and see if you can make it work (solution at bottom)

  • |=  test-test=tape
    |= creates a gate and requests one argument, a tape, called test-test

    • |^
      |^ creates a core, with one arm called $, that is computed immediately, and at least one other arm (checker, in this example).

      • ^-  tape
        ^- casts the output of |^'s core as a tape.

        • ?:  =((checker test-test) &)
          ?: creates a test with branching outcomes — if the test is true, it will branch to the second child; if false, branch to the third child.

          • (weld "one two...one two..." test-test)
            The second child of ?: resolves to a tape — a modified version of the input string
          • "*horrific feedback whine*"
            The third child of ?: — the "if-false" branch — simply evaluates to a tape.
      • ++  checker
        ++ indicates the start of a new arm named checker. Checker will evaluate to a gate, which expects an input tape and produces a pam(true) or bar(false).

        • =.  to-check  (cass to-check)
          =. creates a copy of the subject with a modification, for the evaluation of the hoon following it. In this case, it adds to the subject a face to-check with a value of a fully lowercase version of the input test.

          • ?:  =(to-check "mic check")
            ?: creates another test — if the incoming tape (as modified to be fully lowercase) is equal to "mic check," branch to the second child; if the test is false, branch to the third.

            • &
              pam is a "loobean" (boolean but in hoon) value meaning true.
            • |
              bar is a "loobean" (boolean but in hoon) value meaning false
      • --
        -- is a boundary which indicates the termination of a core, and is required as there are an indeterminate number of arms / children in a |^ core.

Flow

  1. Form a gate and take an input tape.
    |=  test-test=tape
  2. Step 2: Evaluate the buc arm of the gate
  3. Form a core, with one arm called $ (to be evaluated immediately) and at least one other arm, and cast it’s output as a tape.
    |^  ^-  tape
  4. Step 4: Create a branching test checking whether (checker test-test), a call to the one named arm of |^, is equal to &(true); branch based on the results
  5. Evaluate (checker test-test)
    Move to the arm checker and pass the test-test tape as the input (with a face of to-check) to the gate formed by the arm
    |=  to-check=tape
  6. Duplicate the subject with one modification, that of adding to the subject an all lowercase copy of our input.
    =.  to-check  (cass to-check)
  7. If to-check is equal to "mic check," return pam else return bar
    ?:  =(to-check "mic check")
      &
    |
  8. Step 8: If the result of (checker test-test) - steps 5,6 and 7 -  is &, output “One Two… One Two…” else output “*horrific feedback whine*”

Reading

homework

  • Write a generator that takes a @ud and uses the following code skeleton to call two arms that do some work and then returns a cell of those arms’ results.
    • In (at least) one arm, use multiple algebraic standard library functions.
    • In (at least) one arm, use a conditional expression.
    |=  input=@ud
    |^
    :-  (your-arm-one input)
    (your-arm-two input)
    ++  your-arm-one
    :: your arm code here
    ++  your-arm-two
    :: your arm code here
  • Write a program that takes a list of a given atomic type and prints a selected item from this list - you can use the +x:my-list notation.
    • Bonus Points - Write the above program, but with multiple arms to request different items from the list. Modify the gate's sample to let you select which arm to call. (There is an easier way to do this which we will see later!)
    • Note: The "x" described above cannot be a face - it must be a value. Experiment with the following code to get the hang of it:

      Exercise

      Try this in dojo (and try other numbers in lieu of 2):

      =a "this-test-tape"
      a
      "this-test-tape"
      +2:a
      't'
      &2:a
      'h'