#   Author: Paul Fitzpatrick, paulfitz@csail.mit.edu
       #   Copyright (c) 2005 Paul Fitzpatrick
       #
       #   This file is part of CosmicOS.
       #
       #   CosmicOS is free software; you can redistribute it and / or modify
       #   it under the terms of the GNU General Public License as published by
       #   the Free Software Foundation; either version 2 of the License, or
       #   (at your option) any later version.
       #
       #   CosmicOS is distributed in the hope that it will be useful,
       #   but WITHOUT ANY WARRANTY; without even the implied warranty of
       #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       #   GNU General Public License for more details.
       #
       #   You should have received a copy of the GNU General Public License
       #   along with CosmicOS; if not, write to the Free Software
       #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
       #

       # MATH introduce numbers (in unary notation)
       # Here we count up, go through some primes, etc.
       # There is some syntax around the numbers, but that doesn't
       # need to be understood at this point.
       # Any 'words' written here are converted to arbitrary integers
       # in the actual message. Any word ending in -in-unary will be given
       # in unary rather than the binary code used in the main body
       # of the message.
[hear] (intro-in-unary 010);

[hear] (intro-in-unary 0110);

[hear] (intro-in-unary 01110);

[hear] (intro-in-unary 011110);

[hear] (intro-in-unary 0111110);

[hear] (intro-in-unary 01111110);

[hear] (intro-in-unary 011111110);

[hear] (intro-in-unary 0111111110);

[hear] (intro-in-unary 01111111110);

[hear] (intro-in-unary 011111111110);

[hear] (intro-in-unary 0111111111110);

[hear] (intro-in-unary 01111111111110);

[hear] (intro-in-unary 011111111111110);

[hear] (intro-in-unary 0111111111111110);

[hear] (intro-in-unary 01111111111111110);

[hear] (intro-in-unary 011111111111111110);

[hear] (intro-in-unary 0110);

[hear] (intro-in-unary 01110);

[hear] (intro-in-unary 0111110);

[hear] (intro-in-unary 011111110);

[hear] (intro-in-unary 0111111111110);

[hear] (intro-in-unary 011111111111110);

[hear] (intro-in-unary 010);

[hear] (intro-in-unary 011110);

[hear] (intro-in-unary 01111111110);

[hear] (intro-in-unary 011111111111111110);


       # MATH introduce equality for unary numbers
       # The intro operator does nothing essential, and could be
       # omitted - it just tags the first use of a new operator.
       # The = operator is introduced alongside a duplication of
       # unary numbers. The meaning will not quite by nailed down
       # until we see other relational operators.
[hear] (=-in-unary 010 010);

[hear] (=-in-unary 0110 0110);

[hear] (=-in-unary 01110 01110);

[hear] (=-in-unary 011110 011110);

[hear] (=-in-unary 0111110 0111110);

[hear] (=-in-unary 01111110 01111110);

[hear] (=-in-unary 011111110 011111110);

[hear] (=-in-unary 0111111110 0111111110);

[hear] (=-in-unary 010 010);

[hear] (=-in-unary 01111110 01111110);

[hear] (=-in-unary 0110 0110);


       # MATH now introduce other relational operators
       # After this lesson, it should be clear what contexts
       # < > and = are appropriate in.
       # drive the lesson home
[hear] (=-in-unary 010 010);

[hear] (>-in-unary 0110 010);

[hear] (>-in-unary 01110 010);

[hear] (>-in-unary 011110 010);

[hear] (<-in-unary 010 0110);

[hear] (=-in-unary 0110 0110);

[hear] (>-in-unary 01110 0110);

[hear] (>-in-unary 011110 0110);

[hear] (<-in-unary 010 01110);

[hear] (<-in-unary 0110 01110);

[hear] (=-in-unary 01110 01110);

[hear] (>-in-unary 011110 01110);

[hear] (<-in-unary 010 011110);

[hear] (<-in-unary 0110 011110);

[hear] (<-in-unary 01110 011110);

[hear] (=-in-unary 011110 011110);

[hear] (>-in-unary 010 00);

[hear] (>-in-unary 01111111110 0110);

[hear] (>-in-unary 01111110 00);

[hear] (>-in-unary 0110 00);

[hear] (>-in-unary 0111111110 0110);

[hear] (>-in-unary 011110 010);

[hear] (>-in-unary 0110 00);

[hear] (>-in-unary 011111111110 010);

[hear] (>-in-unary 01111110 010);

[hear] (>-in-unary 01111111110 010);

[hear] (>-in-unary 011111110 010);

[hear] (<-in-unary 00 010);

[hear] (<-in-unary 01110 011111111110);

[hear] (<-in-unary 011110 01111111110);

[hear] (<-in-unary 0110 011110);

[hear] (<-in-unary 010 011111110);

[hear] (<-in-unary 00 011111111110);

[hear] (<-in-unary 00 0110);

[hear] (<-in-unary 00 0110);

[hear] (<-in-unary 010 01110);

[hear] (<-in-unary 0110 0111110);

[hear] (<-in-unary 010 011110);

       # switch to binary labelling for commands
[hear] (= 010 010);

[hear] (> 0110 010);

[hear] (> 01110 010);

[hear] (> 011110 010);

[hear] (< 010 0110);

[hear] (= 0110 0110);

[hear] (> 01110 0110);

[hear] (> 011110 0110);

[hear] (< 010 01110);

[hear] (< 0110 01110);

[hear] (= 01110 01110);

[hear] (> 011110 01110);

[hear] (< 010 011110);

[hear] (< 0110 011110);

[hear] (< 01110 011110);

[hear] (= 011110 011110);

       # a few more random examples
[hear] (< 01110 011110);

[hear] (= 011110 011110);

[hear] (< 010 0111110);

[hear] (> 011110 00);

[hear] (> 0111110 011110);

[hear] (< 0110 01110);

[hear] (> 0110 010);

[hear] (> 0111110 010);

[hear] (= 01110 01110);

[hear] (= 01110 01110);

[hear] (> 010 00);


       # MATH introduce the NOT logical operator
[hear] (intro not);

[hear] (= 00 00);

[hear] (not / < 00 00);

[hear] (not / > 00 00);

[hear] (= 011110 011110);

[hear] (not / < 011110 011110);

[hear] (not / > 011110 011110);

[hear] (= 01111110 01111110);

[hear] (not / < 01111110 01111110);

[hear] (not / > 01111110 01111110);

[hear] (= 0110 0110);

[hear] (not / < 0110 0110);

[hear] (not / > 0110 0110);

[hear] (= 01110 01110);

[hear] (not / < 01110 01110);

[hear] (not / > 01110 01110);

[hear] (not / = 01110 011111111110);

[hear] (< 01110 011111111110);

[hear] (not / > 01110 011111111110);

[hear] (not / = 0111110 011111110);

[hear] (< 0111110 011111110);

[hear] (not / > 0111110 011111110);

[hear] (not / = 010 0110);

[hear] (< 010 0110);

[hear] (not / > 010 0110);

[hear] (not / = 00 0111110);

[hear] (< 00 0111110);

[hear] (not / > 00 0111110);

[hear] (not / = 0111111110 0111111111111110);

[hear] (< 0111111110 0111111111111110);

[hear] (not / > 0111111110 0111111111111110);

[hear] (not / = 0111111111110 01111110);

[hear] (> 0111111111110 01111110);

[hear] (not / < 0111111111110 01111110);

[hear] (not / = 01111111111110 0110);

[hear] (> 01111111111110 0110);

[hear] (not / < 01111111111110 0110);

[hear] (not / = 011111111110 011111110);

[hear] (> 011111111110 011111110);

[hear] (not / < 011111111110 011111110);

[hear] (not / = 011110 00);

[hear] (> 011110 00);

[hear] (not / < 011110 00);

[hear] (not / = 011111111111111110 01111111110);

[hear] (> 011111111111111110 01111111110);

[hear] (not / < 011111111111111110 01111111110);


       # MATH introduce addition
[hear] (intro +);

[hear] (= 0110 / + 00 0110);

[hear] (= 0111110 / + 011110 010);

[hear] (= 0110 / + 0110 00);

[hear] (= 011110 / + 00 011110);

[hear] (= 011110 / + 01110 010);

[hear] (= 01110 / + 010 0110);

[hear] (= 00 / + 00 00);

[hear] (= 011110 / + 011110 00);

[hear] (= 01110 / + 0110 010);

[hear] (= 011110 / + 011110 00);


       # MATH introduce subtraction
[hear] (intro -);

[hear] (= 00 / - 0110 0110);

[hear] (= 011110 / - 0111110 010);

[hear] (= 0110 / - 0110 00);

[hear] (= 00 / - 011110 011110);

[hear] (= 01110 / - 011110 010);

[hear] (= 010 / - 01110 0110);

[hear] (= 00 / - 00 00);

[hear] (= 011110 / - 011110 00);

[hear] (= 0110 / - 01110 010);

[hear] (= 011110 / - 011110 00);


       # MATH introduce multiplication
[hear] (intro *);

[hear] (= 00 / * 00 00);

[hear] (= 00 / * 00 010);

[hear] (= 00 / * 00 0110);

[hear] (= 00 / * 00 01110);

[hear] (= 00 / * 010 00);

[hear] (= 010 / * 010 010);

[hear] (= 0110 / * 010 0110);

[hear] (= 01110 / * 010 01110);

[hear] (= 00 / * 0110 00);

[hear] (= 0110 / * 0110 010);

[hear] (= 011110 / * 0110 0110);

[hear] (= 01111110 / * 0110 01110);

[hear] (= 00 / * 01110 00);

[hear] (= 01110 / * 01110 010);

[hear] (= 01111110 / * 01110 0110);

[hear] (= 01111111110 / * 01110 01110);

[hear] (= 00 / * 00 010);

[hear] (= 01110 / * 01110 010);

[hear] (= 00 / * 0110 00);

[hear] (= 00 / * 00 01110);

[hear] (= 01110 / * 01110 010);

[hear] (= 0110 / * 010 0110);

[hear] (= 00 / * 00 00);

[hear] (= 00 / * 01110 00);

[hear] (= 00 / * 0110 00);

[hear] (= 00 / * 01110 00);


       # MATH introduce a simple form of binary notation
       # After this lesson, in the higher-level version of the message,
       # will expand decimal to stand for the binary notation given.
       # It wouldn't be hard to accompany this lesson with a more
       # formal definition once functions are introduced (below)
       # so maybe the transition to binary should be delayed?
[hear] (= (:) 010);

[hear] (= (:.) 0110);

[hear] (= (:..) 011110);

[hear] (= (:...) 0111111110);

[hear] (= (:....) 011111111111111110);

[hear] (= (.) 00);

[hear] (= (:) 010);

[hear] (= (:.) 0110);

[hear] (= (::) 01110);

[hear] (= (:..) 011110);

[hear] (= (:.:) 0111110);

[hear] (= (::.) 01111110);

[hear] (= (:::) 011111110);

[hear] (= (:...) 0111111110);

[hear] (= (:..:) 01111111110);

[hear] (= (:.:.) 011111111110);

[hear] (= (:.::) 0111111111110);

[hear] (= (::..) 01111111111110);

[hear] (= (::.:) 011111111111110);

[hear] (= (:::.) 0111111111111110);

[hear] (= (::::) 01111111111111110);

[hear] (= (.) 00);

[hear] (= (:::) 011111110);

[hear] (= (::.:) 011111111111110);

[hear] (= (:.:) 0111110);

[hear] (= (:..:) 01111111110);

[hear] (= (.) 00);

[hear] (= (::) 01110);

[hear] (= (::::) 01111111111111110);

[hear] (= (::..) 01111111111110);

[hear] (= (:.:) 0111110);

[hear] (= (:.:) 0111110);

[hear] (= (:..:) 01111111110);

[hear] (= (:.) 0110);

[hear] (= (:) 010);

[hear] (= (::::) 01111111111111110);

[hear] (= (:.) 0110);

[hear] (= 0111110 / + 011110 010);

[hear] (= (:.:) / + (:..) (:));

[hear] (= 011111110 / + 01111110 010);

[hear] (= (:::) / + (::.) (:));

[hear] (= 0111110 / + 011110 010);

[hear] (= (:.:) / + (:..) (:));

[hear] (= 011110 / + 00 011110);

[hear] (= (:..) / + (.) (:..));

[hear] (= 01111111110 / + 011111110 0110);

[hear] (= (:..:) /
         + (:::) (:.));

[hear] (= 0111111111110 / + 011111110 011110);

[hear] (= (:.::) /
         + (:::) (:..));

[hear] (= 011111111110 / + 01110 011111110);

[hear] (= (:.:.) /
         + (::) (:::));

[hear] (= 01111110 / + 0111110 010);

[hear] (= (::.) / + (:.:) (:));

[hear] (= 011110 / * 011110 010);

[hear] (= (:..) / * (:..) (:));

[hear] (= 011110 / * 010 011110);

[hear] (= (:..) / * (:) (:..));

[hear] (= 011110 / * 010 011110);

[hear] (= (:..) / * (:) (:..));

[hear] (= 01111110 / * 0110 01110);

[hear] (= (::.) / * (:.) (::));

[hear] (= 01111110 / * 0110 01110);

[hear] (= (::.) / * (:.) (::));

[hear] (= 011110 / * 0110 0110);

[hear] (= (:..) / * (:.) (:.));

[hear] (= 01111111110 / * 01110 01110);

[hear] (= (:..:) /
         * (::) (::));

[hear] (= 011111111111111110 / * 011110 011110);

[hear] (= (:....) /
         * (:..) (:..));


       # MATH show local assignment
[hear] (assign 20 0 / = (20) 0);

[hear] (assign 20 1 / = (20) 1);

[hear] (assign 20 2 / = (20) 2);

[hear] (assign 21 0 / = (21) 0);

[hear] (assign 21 1 / = (21) 1);

[hear] (assign 21 2 / = (21) 2);

[hear] (assign 22 0 / = (22) 0);

[hear] (assign 22 1 / = (22) 1);

[hear] (assign 22 2 / = (22) 2);

[hear] (= 0 (assign 20 0 (20)));

[hear] (= 0 (assign 20 0 / 20));

[hear] (= 0 / assign 20 0 / 20);

[hear] (= 20 / assign 20 0 20);

[hear] (= 5 / assign 20 0 5);

[hear] (= 5 / assign 20 0 / assign 23 5 / 23);

[hear] (= 23 / assign 20 0 / assign 23 5 23);

[hear] (= 1 (assign 20 1 (20)));

[hear] (= 1 (assign 20 1 / 20));

[hear] (= 1 / assign 20 1 / 20);

[hear] (= 20 / assign 20 1 20);

[hear] (= 5 / assign 20 1 5);

[hear] (= 5 / assign 20 1 / assign 23 5 / 23);

[hear] (= 23 / assign 20 1 / assign 23 5 23);

[hear] (= 2 (assign 20 2 (20)));

[hear] (= 2 (assign 20 2 / 20));

[hear] (= 2 / assign 20 2 / 20);

[hear] (= 20 / assign 20 2 20);

[hear] (= 5 / assign 20 2 5);

[hear] (= 5 / assign 20 2 / assign 23 5 / 23);

[hear] (= 23 / assign 20 2 / assign 23 5 23);

[hear] (= 0 (assign 21 0 (21)));

[hear] (= 0 (assign 21 0 / 21));

[hear] (= 0 / assign 21 0 / 21);

[hear] (= 21 / assign 21 0 21);

[hear] (= 5 / assign 21 0 5);

[hear] (= 5 / assign 21 0 / assign 23 5 / 23);

[hear] (= 23 / assign 21 0 / assign 23 5 23);

[hear] (= 1 (assign 21 1 (21)));

[hear] (= 1 (assign 21 1 / 21));

[hear] (= 1 / assign 21 1 / 21);

[hear] (= 21 / assign 21 1 21);

[hear] (= 5 / assign 21 1 5);

[hear] (= 5 / assign 21 1 / assign 23 5 / 23);

[hear] (= 23 / assign 21 1 / assign 23 5 23);

[hear] (= 2 (assign 21 2 (21)));

[hear] (= 2 (assign 21 2 / 21));

[hear] (= 2 / assign 21 2 / 21);

[hear] (= 21 / assign 21 2 21);

[hear] (= 5 / assign 21 2 5);

[hear] (= 5 / assign 21 2 / assign 23 5 / 23);

[hear] (= 23 / assign 21 2 / assign 23 5 23);

[hear] (= 0 (assign 22 0 (22)));

[hear] (= 0 (assign 22 0 / 22));

[hear] (= 0 / assign 22 0 / 22);

[hear] (= 22 / assign 22 0 22);

[hear] (= 5 / assign 22 0 5);

[hear] (= 5 / assign 22 0 / assign 23 5 / 23);

[hear] (= 23 / assign 22 0 / assign 23 5 23);

[hear] (= 1 (assign 22 1 (22)));

[hear] (= 1 (assign 22 1 / 22));

[hear] (= 1 / assign 22 1 / 22);

[hear] (= 22 / assign 22 1 22);

[hear] (= 5 / assign 22 1 5);

[hear] (= 5 / assign 22 1 / assign 23 5 / 23);

[hear] (= 23 / assign 22 1 / assign 23 5 23);

[hear] (= 2 (assign 22 2 (22)));

[hear] (= 2 (assign 22 2 / 22));

[hear] (= 2 / assign 22 2 / 22);

[hear] (= 22 / assign 22 2 22);

[hear] (= 5 / assign 22 2 5);

[hear] (= 5 / assign 22 2 / assign 23 5 / 23);

[hear] (= 23 / assign 22 2 / assign 23 5 23);

       # Now for functions.
[hear] (assign 20 (? 28 5) / = 5 (20 2));

[hear] (assign 32 (? 24 5) / = 5 (32 3));

[hear] (assign 28 (? 29 6) / = 6 (28 2));

[hear] (assign 30 (? 32 6) / = 6 (30 3));

[hear] (assign 25 (? 38 (38)) / = 2 (25 2));

[hear] (assign 23 (? 30 (30)) / = 3 (23 3));

[hear] (assign 25 (? 33 (33)) / = 2 (25 2));

[hear] (assign 29 (? 21 (21)) / = 3 (29 3));

[hear] (assign 25 (? 32 / + (32) 1) / = 3 (25 2));

[hear] (assign 31 (? 38 / + (38) 1) / = 4 (31 3));

[hear] (assign 35 (? 33 / + (33) 1) / = 3 (35 2));

[hear] (assign 32 (? 26 / + (26) 1) / = 4 (32 3));

[hear] (assign y (? x / + (x) 6) / = (y 6) 12);

[hear] (= ((? x / + (x) 6) 6) 12);

[hear] (assign y (? x / + (x) 4) / = (y 0) 4);

[hear] (= ((? x / + (x) 4) 0) 4);

[hear] (assign y (? x / + (x) 12) / = (y 0) 12);

[hear] (= ((? x / + (x) 12) 0) 12);

[hear] (assign y (? x / + (x) 15) / = (y 2) 17);

[hear] (= ((? x / + (x) 15) 2) 17);

[hear] (assign z (? x / ? y / + 1 / * (x) (y)) /
         = (z 13 4) 53);

[hear] (assign z (? x / ? y / + 1 / * (x) (y)) /
         = ((z 13) 4) 53);

[hear] (= ((? x / ? y / + 1 / * (x) (y)) 13 4) 53);

[hear] (= (((? x / ? y / + 1 / * (x) (y)) 13) 4)
           53);

[hear] (assign z (? x / ? y / + 1 / * (x) (y)) /
         = (z 5 6) 31);

[hear] (assign z (? x / ? y / + 1 / * (x) (y)) /
         = ((z 5) 6) 31);

[hear] (= ((? x / ? y / + 1 / * (x) (y)) 5 6) 31);

[hear] (= (((? x / ? y / + 1 / * (x) (y)) 5) 6)
           31);

[hear] (assign z (? x / ? y / + 1 / * (x) (y)) /
         = (z 7 8) 57);

[hear] (assign z (? x / ? y / + 1 / * (x) (y)) /
         = ((z 7) 8) 57);

[hear] (= ((? x / ? y / + 1 / * (x) (y)) 7 8) 57);

[hear] (= (((? x / ? y / + 1 / * (x) (y)) 7) 8)
           57);

[hear] (assign z (? x / ? y / + 1 / * (x) (y)) /
         = (z 8 2) 17);

[hear] (assign z (? x / ? y / + 1 / * (x) (y)) /
         = ((z 8) 2) 17);

[hear] (= ((? x / ? y / + 1 / * (x) (y)) 8 2) 17);

[hear] (= (((? x / ? y / + 1 / * (x) (y)) 8) 2)
           17);

[hear] (assign
          w
          (? x /
           ? y /
           ? z /
           = (z) /
           + (x) (y)) /
         w 15 14 29);

[hear] (assign
          w
          (? x /
           ? y /
           ? z /
           = (z) /
           + (x) (y)) /
         w 5 8 13);

[hear] (assign
          w
          (? x /
           ? y /
           ? z /
           = (z) /
           + (x) (y)) /
         w 12 15 27);

[hear] (assign
          w
          (? x /
           ? y /
           ? z /
           = (z) /
           + (x) (y)) /
         w 14 14 28);

[hear] (assign
          w
          (? x /
           ? y /
           ? z /
           = (z) /
           + (x) (y)) /
         w 8 0 8);

[hear] (assign
          w
          (? x /
           ? y /
           ? z /
           = (z) /
           + (x) (y)) /
         w 15 9 24);

[hear] (assign
          w
          (? x /
           ? y /
           ? z /
           = (z) /
           + (x) (y)) /
         w 11 15 26);

[hear] (assign
          w
          (? x /
           ? y /
           ? z /
           = (z) /
           + (x) (y)) /
         w 5 7 12);


       # MATH demonstrate existence of memory
[hear] (define forty-something 42);

[hear] (= 42 (forty-something));

       # now introduce a function
[hear] (assign square (? x / * (x) (x)) /
         = 0 (square 0));

[hear] (assign square (? x / * (x) (x)) /
         = 16 (square 4));

[hear] (assign square (? x / * (x) (x)) /
         = 64 (square 8));

[hear] (assign square (? x / * (x) (x)) /
         = 9 (square 3));

       # show that functions can be remembered across statements
[hear] (define square / ? x / * (x) (x));

[hear] (= (square 5) 25);

[hear] (= (square 0) 0);

[hear] (= (square 1) 1);

[hear] (= (square 9) 81);

[hear] (define plusone / ? x / + (x) 1);

[hear] (= (plusone 7) 8);

[hear] (= (plusone 3) 4);

[hear] (= (plusone 3) 4);

[hear] (= (plusone 5) 6);

       # This could all be simplified or removed
       # once the handling of true / false stabilizes

       # MATH use equality for truth values
[hear] (= (= 0110 0110) (> 011110 0110));

[hear] (= (= 010 010) (> 01111110 011110));

[hear] (= (< 01110 011110) (= 0111110 0111110));

[hear] (= (= 01110 01110) (= 011110 011110));

[hear] (= (= 01110 01110) (= 00 00));

[hear] (= (< 01111110 0110) (< 011110 0110));

[hear] (= (< 011110 010) (> 00 00));

[hear] (= (> 00 0111110) (= 01110 0110));

[hear] (= (> 0110 01110) (> 011110 0111110));

[hear] (= (> 0110 01111110) (> 010 01111110));

[hear] (not / = (> 0110 01110) (< 010 011110));

[hear] (not / = (= 011110 01110) (< 010 0110));

[hear] (not / = (= 0111110 011110) (< 0110 011110));

[hear] (not / = (> 011110 01111110) (= 01110 01110));

[hear] (not / = (= 01110 010) (> 011110 010));

[hear] (not /
         = (< 01110 01111110) (< 01111110 0110));

[hear] (not / = (= 0110 0110) (> 0110 0111110));

[hear] (not /
         = (= 0111110 0111110) (< 01111110 0110));

[hear] (not / = (= 01110 01110) (< 011110 01110));

[hear] (not / = (> 0111110 0110) (< 0111110 011110));

[hear] (define true 1);

[hear] (define false 0);

[hear] (= (true) (= 010 010));

[hear] (= (true) (= 00 00));

[hear] (= (true) (> 011111110 0111110));

[hear] (= (true) (= 0111110 0111110));

[hear] (= (true) (= 011110 011110));

[hear] (= (< 0111110 01111110) (true));

[hear] (= (= 0111110 0111110) (true));

[hear] (= (> 011111110 0111110) (true));

[hear] (= (= 0110 0110) (true));

[hear] (= (< 01110 01111110) (true));

[hear] (= (false) (< 0111110 00));

[hear] (= (false) (= 0110 01110));

[hear] (= (false) (< 01111110 0110));

[hear] (= (false) (> 010 0110));

[hear] (= (false) (> 0110 0111110));

[hear] (= (= 011110 01110) (false));

[hear] (= (= 00 010) (false));

[hear] (= (< 01111110 01110) (false));

[hear] (= (= 01110 00) (false));

[hear] (= (= 011110 01110) (false));

[hear] (= (true) (true));

[hear] (= (false) (false));

[hear] (not / = (true) (false));

[hear] (not / = (false) (true));


       # MATH show mechanisms for branching
[hear] (intro if);

[hear] (= 28 / if (< 3 0) 24 28);

[hear] (= 27 / if (> 2 4) 29 27);

[hear] (= 29 / if (= 3 1) 20 29);

[hear] (= 21 / if (= 0 0) 21 26);

[hear] (= 29 / if (> 5 3) 29 23);

[hear] (= 26 / if (> 1 0) 26 22);

[hear] (= 21 / if (= 3 3) 21 27);

[hear] (= 23 / if (> 4 4) 25 23);

[hear] (define max /
         ? x /
         ? y /
         if (> (x) (y)) (x) (y));

[hear] (define min /
         ? x /
         ? y /
         if (< (x) (y)) (x) (y));

[hear] (= 0 / max 0 0);

[hear] (= 0 / min 0 0);

[hear] (= 1 / max 0 1);

[hear] (= 0 / min 0 1);

[hear] (= 2 / max 0 2);

[hear] (= 0 / min 0 2);

[hear] (= 1 / max 1 0);

[hear] (= 0 / min 1 0);

[hear] (= 1 / max 1 1);

[hear] (= 1 / min 1 1);

[hear] (= 2 / max 1 2);

[hear] (= 1 / min 1 2);

[hear] (= 2 / max 2 0);

[hear] (= 0 / min 2 0);

[hear] (= 2 / max 2 1);

[hear] (= 1 / min 2 1);

[hear] (= 2 / max 2 2);

[hear] (= 2 / min 2 2);

       # need to be careful about whether 'if' is eager or lazy
       # here we suggest that it is lazy
[hear] (define factorial /
         ? n /
         if (< (n) 1) 1 /
         * (n) /
         factorial /
         - (n) 1);

[hear] (= 1 / factorial 1);

[hear] (= 2 / factorial 2);

[hear] (= 6 / factorial 3);

[hear] (= 24 / factorial 4);

[hear] (= 120 / factorial 5);


       # MATH introduce the AND logical operator
[hear] (intro and);

[hear] (define and
          (? x /
           ? y /
           if (x) (if (y) (true) (false)) (false)));

[hear] (and (= 0110 0110) (> 011110 0110));

[hear] (and (= 010 010) (> 01111110 011110));

[hear] (and (< 01110 011110) (= 0111110 0111110));

[hear] (and (= 01110 01110) (= 011110 011110));

[hear] (and (= 01110 01110) (= 00 00));

[hear] (and (< 0111110 011111110) (> 0111110 01110));

[hear] (and (> 0111110 011110) (> 010 00));

[hear] (and (> 01110 00) (= 01110 01110));

[hear] (and (< 01110 011110) (< 01110 01111110));

[hear] (and (> 0111110 011110) (> 0111110 011110));

[hear] (not / and (> 01111110 011110) (< 01110 010));

[hear] (not / and (> 01110 010) (> 01110 01110));

[hear] (not / and (= 00 00) (= 0111110 011110));

[hear] (not /
         and (< 0110 011110) (> 011110 01111110));

[hear] (not / and (= 01110 01110) (= 01110 010));

[hear] (not /
         and (> 010 0111110) (< 01110 01111110));

[hear] (not / and (< 01111110 0110) (= 0110 0110));

[hear] (not /
         and (> 0110 0111110) (= 0111110 0111110));

[hear] (not / and (< 01111110 0110) (= 01110 01110));

[hear] (not / and (< 011110 01110) (> 0111110 0110));

[hear] (not / and (< 0111110 011110) (= 010 0110));

[hear] (not /
         and (< 01111110 011110) (= 0111110 010));

[hear] (not / and (> 0110 01111110) (= 010 0111110));

[hear] (not / and (< 01111110 01110) (= 0110 01110));

[hear] (not / and (< 01111110 011110) (> 00 010));

[hear] (not / and (= 01110 0111110) (< 011110 010));

[hear] (not / and (= 011110 010) (< 011110 0110));

[hear] (not / and (< 01111110 01110) (= 01110 00));

[hear] (not /
         and (< 011110 0110) (< 011110 01111110));

[hear] (not / and (> 011110 010) (< 0111110 0110));

[hear] (not / and (> 00 010) (> 011111110 0111110));

[hear] (not /
         and (< 01110 011110) (> 01110 01111110));

[hear] (not / and (> 010 0110) (> 01111110 011110));

[hear] (not / and (< 00 010) (= 011110 0111110));

[hear] (and (< 011110 01111110) (< 0111110 011111110));


       # MATH introduce the OR logical operator
[hear] (define or
          (? x /
           ? y /
           if (x) (true) (if (y) (true) (false))));

[hear] (intro or);

[hear] (or (= 0110 0110) (> 011110 0110));

[hear] (or (= 010 010) (> 01111110 011110));

[hear] (or (< 01110 011110) (= 0111110 0111110));

[hear] (or (= 01110 01110) (= 011110 011110));

[hear] (or (= 01110 01110) (= 00 00));

[hear] (or (< 0111110 011111110) (> 0111110 01110));

[hear] (or (> 0111110 011110) (> 010 00));

[hear] (or (> 01110 00) (= 01110 01110));

[hear] (or (< 01110 011110) (< 01110 01111110));

[hear] (or (> 0111110 011110) (> 0111110 011110));

[hear] (or (> 01111110 011110) (< 01110 010));

[hear] (or (> 01110 010) (> 01110 01110));

[hear] (or (= 00 00) (= 0111110 011110));

[hear] (or (< 0110 011110) (> 011110 01111110));

[hear] (or (= 01110 01110) (= 01110 010));

[hear] (or (> 010 0111110) (< 01110 01111110));

[hear] (or (< 01111110 0110) (= 0110 0110));

[hear] (or (> 0110 0111110) (= 0111110 0111110));

[hear] (or (< 01111110 0110) (= 01110 01110));

[hear] (or (< 011110 01110) (> 0111110 0110));

[hear] (not / or (< 0111110 011110) (= 010 0110));

[hear] (not /
         or (< 01111110 011110) (= 0111110 010));

[hear] (not / or (> 0110 01111110) (= 010 0111110));

[hear] (not / or (< 01111110 01110) (= 0110 01110));

[hear] (not / or (< 01111110 011110) (> 00 010));

[hear] (not / or (= 01110 0111110) (< 011110 010));

[hear] (not / or (= 011110 010) (< 011110 0110));

[hear] (not / or (< 01111110 01110) (= 01110 00));

[hear] (or (< 011110 0110) (< 011110 01111110));

[hear] (or (> 011110 010) (< 0111110 0110));

[hear] (or (> 00 010) (> 011111110 0111110));

[hear] (or (< 01110 011110) (> 01110 01111110));

[hear] (or (> 010 0110) (> 01111110 011110));

[hear] (or (< 00 010) (= 011110 0111110));

[hear] (or (< 011110 01111110) (< 0111110 011111110));

[hear] (define >=
          (? x /
           ? y /
           or (> (x) (y)) (= (x) (y))));

[hear] (define <=
          (? x /
           ? y /
           or (< (x) (y)) (= (x) (y))));

[hear] (>= 0 0);

[hear] (<= 0 0);

[hear] (not / >= 0 1);

[hear] (<= 0 1);

[hear] (not / >= 0 2);

[hear] (<= 0 2);

[hear] (>= 1 0);

[hear] (not / <= 1 0);

[hear] (>= 1 1);

[hear] (<= 1 1);

[hear] (not / >= 1 2);

[hear] (<= 1 2);

[hear] (>= 2 0);

[hear] (not / <= 2 0);

[hear] (>= 2 1);

[hear] (not / <= 2 1);

[hear] (>= 2 2);

[hear] (<= 2 2);


       # MATH illustrate pairs
[hear] (define cons (? x / ? y / ? f / f (x) (y)));

[hear] (define car
          (? pair /
           pair (? x / ? y / x)));

[hear] (define cdr
          (? pair /
           pair (? x / ? y / y)));

[hear] (assign x (cons 0 4) / = (car / x) 0);

[hear] (assign x (cons 0 4) / = (cdr / x) 4);

[hear] (assign x (cons 6 2) / = (car / x) 6);

[hear] (assign x (cons 6 2) / = (cdr / x) 2);

[hear] (assign x (cons 3 9) / = (car / x) 3);

[hear] (assign x (cons 3 9) / = (cdr / x) 9);

[hear] (assign x (cons 7 / cons 10 2) /
         = (car / x) 7);

[hear] (assign x (cons 7 / cons 10 2) /
         = (car / cdr / x) 10);

[hear] (assign x (cons 7 / cons 10 2) /
         = (cdr / cdr / x) 2);

[hear] (assign x (cons 1 / cons 15 17) /
         = (car / x) 1);

[hear] (assign x (cons 1 / cons 15 17) /
         = (car / cdr / x) 15);

[hear] (assign x (cons 1 / cons 15 17) /
         = (cdr / cdr / x) 17);

[hear] (assign x (cons 8 / cons 14 9) /
         = (car / x) 8);

[hear] (assign x (cons 8 / cons 14 9) /
         = (car / cdr / x) 14);

[hear] (assign x (cons 8 / cons 14 9) /
         = (cdr / cdr / x) 9);

[hear] (assign
          x
          (cons 3 /
           cons 0 /
           cons 2 /
           cons 4 1) /
         and (= 3 / car / x) /
         and (= 0 / car / cdr / x) /
         and (= 2 / car / cdr / cdr / x) /
         and (= 4 /
              car /
              cdr /
              cdr /
              cdr /
              x)
             (= 1 /
              cdr /
              cdr /
              cdr /
              cdr /
              x));


       # MATH introduce mutable objects, and side-effects
[hear] (intro make-cell);

[hear] (intro set!);

[hear] (intro get!);

[hear] (define demo-mut1 / make-cell 0);

[hear] (set! (demo-mut1) 15);

[hear] (= (get! / demo-mut1) 15);

[hear] (set! (demo-mut1) 5);

[hear] (set! (demo-mut1) 7);

[hear] (= (get! / demo-mut1) 7);

[hear] (define demo-mut2 / make-cell 11);

[hear] (= (get! / demo-mut2) 11);

[hear] (set! (demo-mut2) 22);

[hear] (= (get! / demo-mut2) 22);

[hear] (= (get! / demo-mut1) 7);

[hear] (= (+ (get! / demo-mut1) (get! / demo-mut2))
           29);

[hear] (if (= (get! / demo-mut1) 7)
          (set! (demo-mut1) 88)
          (set! (demo-mut1) 99));

[hear] (= (get! / demo-mut1) 88);

[hear] (if (= (get! / demo-mut1) 7)
          (set! (demo-mut1) 88)
          (set! (demo-mut1) 99));

[hear] (= (get! / demo-mut1) 99);


       # MATH illustrate lists and some list operators
       # to make list describable as a function, need to preceed lists
       # ... with an argument count
       # Lists keep an explicit record of their length
       # this is to avoid the need for using a special 'nil' symbol
       # ... which cannot itself be placed in the list.
       # pending: should introduce number? check function
[hear] (define list-helper /
         ? n /
         ? ret /
         if (> (n) 1)
          (? x /
           list-helper
            (- (n) 1)
            (? y /
             ? z /
             ret (+ 1 (y)) (cons (x) (z))))
          (? x /
           ret 1 (x)));

[hear] (define list /
         ? n /
         if (= (n) 0)
          (cons 0 0)
          (list-helper (n) (? y / ? z / cons (y) (z))));

[hear] (define head /
         ? lst /
         if (= (car / lst) 0)
          (undefined)
          (if (= (car / lst) 1)
            (cdr /
             lst)
            (car /
             cdr /
             lst)));

[hear] (define tail /
         ? lst /
         if (= (car / lst) 0)
          (undefined)
          (if (= (car / lst) 1)
            (cons 0 0)
            (cons (- (car / lst) 1) (cdr / cdr / lst))));

[hear] (define list-length / ? lst / car / lst);

[hear] (define list-ref /
         ? lst /
         ? n /
         if (= (list-ref / lst) 0)
          (undefined)
          (if (= (n) 0)
            (head /
             lst)
            (list-ref (tail / lst) (- (n) 1))));

[hear] (define prepend /
         ? x /
         ? lst /
         if (= (list-length / lst) 0)
          (cons 1 (x))
          (cons (+ (list-length / lst) 1)
                (cons (x) (cdr / lst))));

[hear] (define equal /
         ? x /
         ? y /
         if (= (number? (x)) (number? (y)))
          (if (number? (x)) (= (x) (y)) (list= (x) (y)))
          (false));

[hear] (define list= /
         ? x /
         ? y /
         if (= (list-length / x) (list-length / y))
          (if (> (list-length / x) 0)
            (and (equal (head / x) (head / y))
                 (list= (tail / x) (tail / y)))
            (true))
          (false));

[hear] (= (list-length / (list 0)) 0);

[hear] (= (list-length / (list 4) 6 1 0 4) 4);

[hear] (= (list-length / (list 6) 6 2 7 0 9 4) 6);

[hear] (= (list-length / (list 2) 4 9) 2);

[hear] (= (list-length / (list 3) 6 1 7) 3);

[hear] (= (head / (list 6) 12 11 10 4 1 5) 12);

[hear] (list= (tail /
                (list 6) 12 11 10 4 1 5)
               ((list 5) 11 10 4 1 5));

[hear] (= (head / (list 8) 15 13 12 7 10 11 13 18) 15);

[hear] (list= (tail /
                (list 8) 15 13 12 7 10 11 13 18)
               ((list 7) 13 12 7 10 11 13 18));

[hear] (= (head / (list 2) 11 1) 11);

[hear] (list= (tail / (list 2) 11 1) ((list 1) 1));

[hear] (= (head / (list 6) 5 19 4 16 6 11) 5);

[hear] (list= (tail /
                (list 6) 5 19 4 16 6 11)
               ((list 5) 19 4 16 6 11));

[hear] (= (head /
            (list 10) 12 18 7 4 9 18 6 16 6 18)
           12);

[hear] (list= (tail /
                (list 10) 12 18 7 4 9 18 6 16 6 18)
               ((list 9) 18 7 4 9 18 6 16 6 18));

[hear] (= (head / (list 6) 19 7 3 10 19 13) 19);

[hear] (list= (tail /
                (list 6) 19 7 3 10 19 13)
               ((list 5) 7 3 10 19 13));

[hear] (= (head / (list 6) 19 7 19 12 16 13) 19);

[hear] (list= (tail /
                (list 6) 19 7 19 12 16 13)
               ((list 5) 7 19 12 16 13));

[hear] (= (head / (list 1) 3) 3);

[hear] (list= (tail / (list 1) 3) ((list 0)));

[hear] (= (head / (list 3) 2 19 17) 2);

[hear] (list= (tail /
                (list 3) 2 19 17)
               ((list 2) 19 17));

[hear] (= (head / (list 7) 1 16 5 14 6 19 2) 1);

[hear] (list= (tail /
                (list 7) 1 16 5 14 6 19 2)
               ((list 6) 16 5 14 6 19 2));

[hear] (= (list-ref ((list 3) 18 14 17) 1) 14);

[hear] (= (list-ref ((list 3) 8 11 10) 2) 10);

[hear] (= (list-ref ((list 8) 15 0 4 9 9 2 10 17) 3) 9);

[hear] (= (list-ref ((list 7) 4 8 8 5 14 5 13) 4) 14);

[hear] (= (list-ref ((list 4) 1 4 7 18) 2) 7);

[hear] (= (list-ref ((list 3) 12 2 3) 1) 2);

[hear] (= (list-ref ((list 6) 12 5 7 15 7 16) 2) 7);

[hear] (= (list-ref ((list 8) 5 15 7 14 7 1 11 19) 0) 5);

[hear] (= (list-ref ((list 3) 19 17 8) 2) 8);

[hear] (= (list-ref ((list 4) 10 10 4 11) 1) 10);

[hear] (list= ((list 0)) ((list 0)));

[hear] (list= ((list 1) 4) ((list 1) 4));

[hear] (list= ((list 2) 7 5) ((list 2) 7 5));

[hear] (list= ((list 3) 15 13 11) ((list 3) 15 13 11));

[hear] (list= ((list 4) 2 8 0 6) ((list 4) 2 8 0 6));

       # this next batch of examples are a bit misleading, should streamline
[hear] (not / list= ((list 0)) ((list 1) 9));

[hear] (not / list= ((list 0)) ((list 1) 5));

[hear] (not / list= ((list 1) 18) ((list 2) 8 18));

[hear] (not / list= ((list 1) 18) ((list 2) 18 5));

[hear] (not /
         list= ((list 2) 11 18) ((list 3) 7 11 18));

[hear] (not /
         list= ((list 2) 11 18) ((list 3) 11 18 6));

[hear] (not /
         list= ((list 3) 7 19 17) ((list 4) 6 7 19 17));

[hear] (not /
         list= ((list 3) 7 19 17) ((list 4) 7 19 17 0));

[hear] (not /
         list= ((list 4) 10 0 11 1)
               ((list 5) 0 10 0 11 1));

[hear] (not /
         list= ((list 4) 10 0 11 1)
               ((list 5) 10 0 11 1 8));

       # some helpful functions
[hear] (list= (prepend 8 ((list 0))) ((list 1) 8));

[hear] (list= (prepend 11 ((list 1) 8)) ((list 2) 11 8));

[hear] (list= (prepend 13 ((list 2) 1 12))
               ((list 3) 13 1 12));

[hear] (list= (prepend 0 ((list 3) 7 7 5))
               ((list 4) 0 7 7 5));

[hear] (list= (prepend 16 ((list 4) 16 0 19 3))
               ((list 5) 16 16 0 19 3));

[hear] (list= (prepend 10 ((list 5) 5 6 7 9 10))
               ((list 6) 10 5 6 7 9 10));

[hear] (list= (prepend 19 ((list 6) 3 19 18 6 10 16))
               ((list 7) 19 3 19 18 6 10 16));

[hear] (list= (prepend 19 ((list 7) 17 17 10 1 18 12 14))
               ((list 8) 19 17 17 10 1 18 12 14));

[hear] (define pair /
         ? x /
         ? y /
         (list 2) (x) (y));

[hear] (define first / ? lst / head / lst);

[hear] (define second /
         ? lst /
         head /
         tail /
         lst);

[hear] (list= (pair 3 6) ((list 2) 3 6));

[hear] (= (first / pair 3 6) 3);

[hear] (= (second / pair 3 6) 6);

[hear] (list= (pair 4 9) ((list 2) 4 9));

[hear] (= (first / pair 4 9) 4);

[hear] (= (second / pair 4 9) 9);

[hear] (list= (pair 8 3) ((list 2) 8 3));

[hear] (= (first / pair 8 3) 8);

[hear] (= (second / pair 8 3) 3);

[hear] (define list-find-helper /
         ? lst /
         ? key /
         ? fail /
         ? idx /
         if (= (list-length / lst) 0)
          (fail 0)
          (if (equal (head / lst) (key))
            (idx)
            (list-find-helper
              (tail /
               lst)
              (key)
              (fail)
              (+ (idx) 1))));

[hear] (define list-find /
         ? lst /
         ? key /
         ? fail /
         list-find-helper (lst) (key) (fail) 0);

[hear] (define example-fail / ? x 100);

[hear] (= (list-find ((list 1) 13) 13 (example-fail)) 0);

[hear] (= (list-find
             ((list 10) 0 9 8 16 15 14 17 5 9 2)
             15
             (example-fail))
           4);

[hear] (= (list-find ((list 3) 7 4 10) 7 (example-fail))
           0);

[hear] (= (list-find
             ((list 6) 0 17 10 13 11 5)
             17
             (example-fail))
           1);

[hear] (= (list-find ((list 3) 12 9 6) 12 (example-fail))
           0);

[hear] (= (list-find
             ((list 7) 17 1 4 17 14 13 13)
             14
             (example-fail))
           4);

[hear] (= (list-find ((list 3) 2 15 2) 15 (example-fail))
           1);

[hear] (= (list-find
             ((list 9) 6 13 10 8 10 9 6 15 18)
             13
             (example-fail))
           1);

[hear] (= (list-find ((list 3) 12 16 0) 12 (example-fail))
           0);

[hear] (= (list-find ((list 1) 15) 15 (example-fail)) 0);

[hear] (= (list-find
             ((list 4) 2 17 11 5)
             14
             (example-fail))
           100);

[hear] (= (list-find
             ((list 6) 12 1 19 6 17 9)
             2
             (example-fail))
           100);

[hear] (= (list-find
             ((list 8) 11 6 17 8 13 10 9 16)
             19
             (example-fail))
           100);


       # HACK describe changes to the implicit interpreter to allow new special forms
[hear] (define base-translate / translate);

[hear] (define translate /
         ? x /
         if (= (x) 32) 64 (base-translate / x));

[hear] (= 32 64);

[hear] (= (+ 32 64) 128);

[hear] (define translate / base-translate);

[hear] (not / = 32 64);

[hear] (= (+ 32 64) 96);

       # now can create a special form for lists
[hear] (define translate /
         ? x /
         if (number? /
           x)
          (base-translate /
           x)
          (if (= (head / x) vector)
            (translate /
             prepend
              ((list 2) list (list-length / tail / x))
              (tail /
               x))
            (base-translate /
             x)));

[hear] (list= (vector 1 2 3) ((list 3) 1 2 3));

       # now to desugar let expressions
[hear] (define translate-with-vector / translate);

[hear] (define translate-let-form /
         ? x /
         ? body /
         if (= (list-length / x) 0)
          (translate /
           body)
          (translate-let-form
            (tail /
             x)
            (vector
              (vector ? (head / head / x) (body))
              (head /
               tail /
               head /
               x))));

[hear] (define translate /
         ? x /
         if (number? /
           x)
          (translate-with-vector /
           x)
          (if (= (head / x) let)
            (translate-let-form
              (head /
               tail /
               x)
              (head /
               tail /
               tail /
               x))
            (translate-with-vector /
             x)));

[hear] (let ((x 20)) (= (x) 20));

[hear] (let ((x 50) (y 20)) (= (- (x) (y)) 30));

       # the is-list function is now on dubious ground
       # this stuff will be replaced with typing ASAP
[hear] (define is-list /
         ? x /
         not /
         number? /
         x);

[hear] (is-list / (list 2) 1 3);

[hear] (is-list / (list 0));

[hear] (not / is-list 23);

[hear] (is-list /
         (list 3) ((list 2) 2 3) 1 (? x / + (x) 10));


       # MATH introduce sugar for let
       # if would be good to introduce desugarings more rigorously, but for now...
       # ... just a very vague sketch
[hear] (intro let);

[hear] (= (let ((x 10)) (+ (x) 5))
           ((? x / + (x) 5) 10));

[hear] (= (let ((x 10) (y 5)) (+ (x) (y)))
           (((? x / ? y / + (x) (y)) 10) 5));


       # MATH build up functions of several variables
[hear] (= ((? x / ? y / - (x) (y)) 4 0) 4);

[hear] (= ((? x / ? y / - (x) (y)) 11 8) 3);

[hear] (= ((? x / ? y / - (x) (y)) 5 5) 0);

[hear] (= ((? x / ? y / - (x) (y)) 10 1) 9);

[hear] (= ((? x / ? y / - (x) (y)) 10 7) 3);

[hear] (define last /
         ? x /
         list-ref (x) (- (list-length / x) 1));

[hear] (define except-last /
         ? x /
         if (> (list-length / x) 1)
          (prepend
            (head /
             x)
            (except-last /
             tail /
             x))
          (vector));

       # test last and except-last
[hear] (= 15 (last / vector 4 5 15));

[hear] (list= (vector 4 5)
               (except-last /
                vector 4 5 15));

[hear] (intro lambda);

[hear] (define prev-translate / translate);

[hear] (define translate /
         let ((prev (prev-translate)))
          (? x /
           if (number? /
             x)
            (prev /
             x)
            (if (= (head / x) lambda)
              (let ((formals (head / tail / x))
                    (body (head / tail / tail / x)))
                (if (> (list-length / formals) 0)
                  (translate
                    (vector
                      lambda
                      (except-last /
                       formals)
                      (vector ? (last / formals) (body))))
                  (translate (body))))
              (prev /
               x))));

       # test lambda
[hear] (= ((lambda (x y) (- (x) (y))) 8 3) 5);

[hear] (= ((lambda (x y) (- (x) (y))) 1 1) 0);

[hear] (= ((lambda (x y) (- (x) (y))) 10 9) 1);

[hear] (= ((lambda (x y) (- (x) (y))) 7 5) 2);

[hear] (= ((lambda (x y) (- (x) (y))) 9 8) 1);

[hear] (define apply /
         lambda (x y)
          (if (list= (y) (vector))
            (x)
            (apply ((x) (head / y)) (tail / y))));

[hear] (= (apply (lambda (x y) (- (x) (y))) (vector 8 6))
           2);

[hear] (= (apply (lambda (x y) (- (x) (y))) (vector 5 0))
           5);

[hear] (= (apply (lambda (x y) (- (x) (y))) (vector 12 9))
           3);

[hear] (= (apply (lambda (x y) (- (x) (y))) (vector 13 8))
           5);

[hear] (= (apply (lambda (x y) (- (x) (y))) (vector 11 3))
           8);


       # MATH show map function for applying a function across the elements of a list
[hear] (define map /
         lambda (p lst)
          (if (> (list-length / lst) 0)
            (prepend
              (p (head / lst))
              (map (p) (tail / lst)))
            (vector)));

[hear] (list= (map (? x / * (x) 2) (vector 0 8 15))
               (vector 0 16 30));

[hear] (list= (map (? x / * (x) 2) (vector 12 4 0 9))
               (vector 24 8 0 18));

[hear] (list= (map (? x / * (x) 2) (vector 8 9 5 7 10))
               (vector 16 18 10 14 20));

[hear] (list= (map (? x / * (x) 2) (vector 10 12 19 8 3 1))
               (vector 20 24 38 16 6 2));

[hear] (list= (map (? x 42) (vector 5 18 4))
               (vector 42 42 42));

[hear] (list= (map (? x 42) (vector 3 10 17 11))
               (vector 42 42 42 42));

[hear] (list= (map (? x 42) (vector 5 13 6 16 2))
               (vector 42 42 42 42 42));

[hear] (list= (map (? x 42) (vector 9 1 19 14 6 10))
               (vector 42 42 42 42 42 42));

[hear] (define crunch /
         lambda (p lst)
          (if (>= (list-length / lst) 2)
            (p (head / lst) (crunch (p) (tail / lst)))
            (if (= (list-length / lst) 1)
              (head /
               lst)
              (undefined))));

[hear] (= (crunch (+) (vector 5 12 2)) 19);

[hear] (= (crunch (+) (vector 11 18 1 4)) 34);

[hear] (= (crunch (+) (vector 15 13 10 12 2)) 52);

[hear] (= (crunch (+) (vector 12 6 17 15 4 10)) 64);


       # NOTE end of part 1, start of part 2
       # The following parts of the message are experimental, and not
       # carefully integrated with the main body
[hear] (intro part2);


       # MATH show an example of recursive evaluation
       # skipping over a lot of definitions and desugarings
[hear] (define easy-factorial /
         ? f /
         ? x /
         if (> (x) 0) (* (x) / f (f) (- (x) 1)) 1);

[hear] (define factorial /
         ? x /
         if (> (x) 0)
          (* (x) /
           factorial /
           - (x) 1)
          1);

[hear] (= (easy-factorial (easy-factorial) 0) 1);

[hear] (= (easy-factorial (easy-factorial) 1) 1);

[hear] (= (easy-factorial (easy-factorial) 2) 2);

[hear] (= (easy-factorial (easy-factorial) 3) 6);

[hear] (= (easy-factorial (easy-factorial) 4) 24);

[hear] (= (easy-factorial (easy-factorial) 5) 120);

[hear] (= (factorial 0) 1);

[hear] (= (factorial 1) 1);

[hear] (= (factorial 2) 2);

[hear] (= (factorial 3) 6);

[hear] (= (factorial 4) 24);

[hear] (= (factorial 5) 120);


       # MATH some pure lambda calculus definitions - optional
       # these definitions are not quite what we want
       # since thinking of everything as a function requires headscratching
       # it would be better to use these as a parallel means of evaluation
       # ... for expressions
[hear] (define pure-if /
         ? x /
         ? y /
         ? z /
         x (y) (z));

[hear] (define pure-true / ? y / ? z / y);

[hear] (define pure-false / ? y / ? z / z);

[hear] (define pure-cons /
         ? x /
         ? y /
         ? z /
         pure-if (z) (x) (y));

[hear] (define pure-car / ? x / x (pure-true));

[hear] (define pure-cdr / ? x / x (pure-false));

[hear] (define zero / ? f / ? x / x);

[hear] (define one / ? f / ? x / f (x));

[hear] (define two / ? f / ? x / f (f (x)));

[hear] (define succ /
         ? n /
         ? f /
         ? x /
         f ((n (f)) (x)));

[hear] (define add / ? a / ? b / (a (succ)) (b));

[hear] (define mult /
         ? a /
         ? b /
         (a (add / b)) (zero));

[hear] (define pred /
         ? n /
         pure-cdr /
         (n (? p /
             pure-cons
              (succ /
               pure-car /
               p)
              (pure-car /
               p)))
         (pure-cons (zero) (zero)));

[hear] (define is-zero /
         ? n /
         (n (? dummy / pure-false) (pure-true)));

[hear] (define fixed-point /
         ? f /
         (? x / f (x (x))) (? x / f (x (x))));

       # .. but for rest of message will assume that define does fixed-point for us
       # now build a link between numbers and church number functions
[hear] (define unchurch /
         ? c /
         c (? x / + (x) 1) 0);

[hear] (= 0 (unchurch / zero));

[hear] (= 1 (unchurch / one));

[hear] (= 2 (unchurch / two));

[hear] (define church /
         ? x /
         if (= 0 (x))
          (zero)
          (succ /
           church /
           - (x) 1));


       # MATH introduce universal quantifier
       # really need to link with sets for true correctness
       # and the examples here are REALLY sparse, need much more
[hear] (intro forall);

[hear] (< 5 (+ 5 1));

[hear] (< 4 (+ 4 1));

[hear] (< 3 (+ 3 1));

[hear] (< 2 (+ 2 1));

[hear] (< 1 (+ 1 1));

[hear] (< 0 (+ 0 1));

[hear] (forall (? x / < (x) (+ (x) 1)));

[hear] (< 5 (* 5 2));

[hear] (< 4 (* 4 2));

[hear] (< 3 (* 3 2));

[hear] (< 2 (* 2 2));

[hear] (< 1 (* 1 2));

[hear] (not / < 0 (* 0 2));

[hear] (not / forall (? x / < (x) (* (x) 2)));


       # MATH introduce existential quantifier
       # really need to link with sets for true correctness
       # and the examples here are REALLY sparse, need much more
[hear] (not / = 5 (* 2 2));

[hear] (= 4 (* 2 2));

[hear] (not / = 3 (* 2 2));

[hear] (not / = 2 (* 2 2));

[hear] (not / = 1 (* 2 2));

[hear] (not / = 0 (* 2 2));

[hear] (intro exists);

[hear] (exists (? x / = (x) (* 2 2)));

[hear] (not / = 5 (+ 5 2));

[hear] (not / = 4 (+ 4 2));

[hear] (not / = 3 (+ 3 2));

[hear] (not / = 2 (+ 2 2));

[hear] (not / = 1 (+ 1 2));

[hear] (not / = 0 (+ 0 2));

[hear] (not (exists (? x / = (x) (+ (x) 2))));


       # MATH introduce logical implication
[hear] (intro =>);

[hear] (define => /
         ? x /
         ? y /
         not /
         and (x) (not / y));

[hear] (=> (true) (true));

[hear] (not / => (true) (false));

[hear] (=> (false) (true));

[hear] (=> (false) (false));

[hear] (forall
          (? x /
           forall
            (? y /
             => (=> (x) (y)) (=> (not / y) (not / x)))));


       # MATH introduce sets and set membership
[hear] (intro element);

[hear] (define element /
         ? x /
         ? lst /
         not /
         = (list-find-helper (lst) (x) (? y 0) 1) 0);

[hear] (element 8 (vector 8 4 3 0 5));

[hear] (element 5 (vector 8 4 3 0 5));

[hear] (element 0 (vector 8 4 3 0 5));

[hear] (element 1 (vector 1 0 3 9 5));

[hear] (element 3 (vector 1 0 3 9 5));

[hear] (element 0 (vector 1 0 3 9 5));

[hear] (element 5 (vector 6 8 1 0 2 5));

[hear] (element 1 (vector 6 8 1 0 2 5));

[hear] (element 5 (vector 6 8 1 0 2 5));

[hear] (element 6 (vector 6 8 3 9 2 5));

[hear] (element 6 (vector 6 8 3 9 2 5));

[hear] (element 5 (vector 6 8 3 9 2 5));

[hear] (element 4 (vector 6 4 1 7 2 5));

[hear] (element 1 (vector 6 4 1 7 2 5));

[hear] (element 7 (vector 6 4 1 7 2 5));

[hear] (not / element 6 (vector 8 3 7 9));

[hear] (not / element 6 (vector 8 4 1 3 5));

[hear] (not / element 6 (vector 9 2 5));

[hear] (not / element 0 (vector 7 2 5));

[hear] (not / element 6 (vector 3 5));

       # rules for set equality
[hear] (define set-subset /
         ? x /
         ? y /
         if (> (list-length / x) 0)
          (and (element (head / x) (y))
               (set-subset (tail / x) (y)))
          (true));

[hear] (define set= /
         ? x /
         ? y /
         and (set-subset (x) (y)) (set-subset (y) (x)));

[hear] (set= (vector 1 5 9) (vector 5 1 9));

[hear] (set= (vector 1 5 9) (vector 9 1 5));

[hear] (not / set= (vector 1 5 9) (vector 1 5));

       # let's go leave ourselves wide open to Russell's paradox
       # ... by using characteristic functions
       # ... since it doesn't really matter for communication purposes
       # ... and so far this is just used / tested with sets of integers really
[hear] (element 5 (all (? x / = (+ (x) 10) 15)));

[hear] (element 3 (all (? x / = (* (x) 3) (+ (x) 6))));

[hear] (define empty-set / vector);

[hear] (element 0 (natural-set));

[hear] (forall
          (? x /
           => (element (x) (natural-set))
              (element (+ (x) 1) (natural-set))));

[hear] (element 1 (natural-set));

[hear] (element 2 (natural-set));

[hear] (element 3 (natural-set));

[hear] (element 4 (natural-set));

[hear] (element 5 (natural-set));

[hear] (element 6 (natural-set));

[hear] (element 7 (natural-set));

[hear] (element 8 (natural-set));

[hear] (element 9 (natural-set));

[hear] (define boolean-set / vector (true) (false));

[hear] (element (true) (boolean-set));

[hear] (element (false) (boolean-set));

       # actually, to simplify semantics elsewhere, true and false
       # are now just 0 and 1 so they are not distinct from ints
[hear] (define even-natural-set /
         all /
         ? x /
         exists /
         ? y /
         and (element (y) (natural-set))
             (= (* 2 (y)) (x)));

[hear] (element 0 (natural-set));

[hear] (element 0 (even-natural-set));

[hear] (element 1 (natural-set));

[hear] (not / element 1 (even-natural-set));

[hear] (element 2 (natural-set));

[hear] (element 2 (even-natural-set));

[hear] (element 3 (natural-set));

[hear] (not / element 3 (even-natural-set));

[hear] (element 4 (natural-set));

[hear] (element 4 (even-natural-set));

[hear] (element 5 (natural-set));

[hear] (not / element 5 (even-natural-set));

[hear] (element 6 (natural-set));

[hear] (element 6 (even-natural-set));


       # MATH introduce graph structures
[hear] (define make-graph /
         lambda (nodes links) (pair (nodes) (links)));

[hear] (define test-graph /
         make-graph
          (vector 1 2 3 4)
          (vector (vector 1 2) (vector 2 3) (vector 1 4)));

[hear] (define graph-linked /
         lambda (g n1 n2)
          (exists /
           ? idx /
           if (and (>= (idx) 0)
                   (< (idx) (list-length / list-ref (g) 1)))
            (list= (list-ref (list-ref (g) 1) (idx))
                   (vector (n1) (n2)))
            (false)));

[hear] (= (graph-linked (test-graph) 1 2) (true));

[hear] (= (graph-linked (test-graph) 1 3) (false));

[hear] (= (graph-linked (test-graph) 2 4) (false));

       # 'if' is used a lot in the next definition in place of and / or
       # this is because I haven't established lazy evaluation forms for and / or
       # so this very inefficient algorithm completely bogs down when combined
       # ... during testing with a dumb implementation for 'exists'.
[hear] (define graph-linked* /
         lambda (g n1 n2)
          (if (= (n1) (n2))
            (true)
            (if (graph-linked (g) (n1) (n2))
              (true)
              (exists
                (? n3 /
                 if (graph-linked (g) (n1) (n3))
                  (graph-linked* (g) (n3) (n2))
                  (false))))));

[hear] (= (graph-linked* (test-graph) 1 2) (true));

[hear] (= (graph-linked* (test-graph) 1 3) (true));

[hear] (= (graph-linked* (test-graph) 2 4) (false));


       # MATH show how to execute a sequence of instructions
[hear] (intro begin);

[hear] (define prev-translate / translate);

[hear] (define reverse /
         ? x /
         if (>= (list-length / x) 1)
          (prepend
            (last /
             x)
            (reverse /
             except-last /
             x))
          (x));

       # test reverse
[hear] (list= (vector 1 2 3) (reverse / vector 3 2 1));

[hear] (define translate /
         let ((prev (prev-translate)))
          (? x /
           if (number? /
             x)
            (prev /
             x)
            (if (= (head / x) begin)
              (translate
                (vector
                  (vector ? x (vector head (vector x)))
                  (prepend vector (reverse / tail / x))))
              (prev /
               x))));

[hear] (= (begin 1 7 2 4) 4);

[hear] (= (begin
             (set! (demo-mut1) 88)
             (set! (demo-mut1) 6)
             (get! /
              demo-mut1))
           6);

[hear] (= (begin
             (set! (demo-mut2) 88)
             (set! (demo-mut1) 6)
             (get! /
              demo-mut2))
           88);

[hear] (= (begin
             (set! (demo-mut1) 88)
             (set! (demo-mut1) 6)
             (get! /
              demo-mut1)
             4)
           4);


       # MATH introduce environment / hashmap structure
       # this section needs a LOT more examples :-
       # note that at the time of writing (h 1 2) is same as ((h) 1 2)
[hear] (define hash-add /
         lambda (h x y z)
          (if (equal (z) (x)) (y) (h (z))));

[hear] (define hash-ref / lambda (h x) (h (x)));

[hear] (define hash-null / ? x / undefined);

[hear] (define hash-default /
         ? default /
         ? x /
         default);

[hear] (define test-hash /
         hash-add (hash-add (hash-null) 3 2) 4 9);

[hear] (= (hash-ref (test-hash) 4) 9);

[hear] (= (hash-ref (test-hash) 3) 2);

[hear] (= (hash-ref (test-hash) 8) (undefined));

[hear] (= (hash-ref (test-hash) 15) (undefined));

[hear] (= (hash-ref (hash-add (test-hash) 15 33) 15) 33);

[hear] (= (hash-ref (test-hash) 15) (undefined));

[hear] (define make-hash /
         ? x /
         if (list= (x) (vector))
          (hash-null)
          (hash-add
            (make-hash (tail / x))
            (first /
             head /
             x)
            (second /
             head /
             x)));

[hear] (= (hash-ref
             (make-hash /
              vector (pair 3 10) (pair 2 20) (pair 1 30))
             3)
           10);

[hear] (= (hash-ref
             (make-hash /
              vector (pair 3 10) (pair 2 20) (pair 1 30))
             1)
           30);


       # OBJECT introduce simple mutable structures
[hear] (define mutable-struct /
         ? lst /
         let ((data (map (? x / make-cell 0) (lst))))
          (? key /
           list-ref (data) (list-find (lst) (key) (? x 0))));

[hear] (define test-struct1 /
         mutable-struct /
         vector item1 item2 item3);

[hear] (set! (test-struct1 item1) 15);

[hear] (= (get! / test-struct1 item1) 15);


       # OBJECT introduce method handler wrappers
[hear] (define add-method /
         lambda (object name method)
          (hash-add
            (object)
            (name)
            (? dummy /
             method /
             object)));

[hear] (define call / ? x / x 0);

[hear] (define test-struct2 /
         mutable-struct /
         vector x y);

[hear] (set! (test-struct2 x) 10);

[hear] (set! (test-struct2 y) 20);

[hear] (= (get! / test-struct2 x) 10);

[hear] (= (get! / test-struct2 y) 20);

[hear] (define test-struct3 /
         add-method
          (test-struct2)
          sum
          (? self /
           + (get! / self x) (get! / self y)));

[hear] (= (get! / test-struct3 x) 10);

[hear] (= (get! / test-struct3 y) 20);

[hear] (= (call / test-struct3 sum) 30);

[hear] (set! (test-struct3 y) 10);

[hear] (= (call / test-struct3 sum) 20);

[hear] (set! (test-struct2 y) 5);

[hear] (= (call / test-struct3 sum) 15);


       # TURING introduce turing machine model
       # just for fun!
[hear] (define safe-tail /
         ? x /
         if (> (list-length / x) 0)
          (if (> (list-length / x) 1)
            (tail /
             x)
            (vector /
             vector))
          (vector /
           vector));

[hear] (define safe-head /
         ? x /
         if (> (list-length / x) 0)
          (head /
           x)
          (vector));

[hear] (define tape-read /
         ? tape /
         let ((x (second / tape)))
          (if (> (list-length / x) 0)
            (head /
             x)
            (vector)));

[hear] (define tape-transition /
         lambda (tape shift value)
          (if (= (shift) 1)
            (pair (prepend (value) (first / tape))
                  (safe-tail /
                   second /
                   tape))
            (if (= (shift) 0)
              (pair (safe-tail /
                     first /
                     tape)
                    (prepend
                      (safe-head /
                       first /
                       tape)
                      (prepend (value) (safe-tail / second / tape))))
              (pair (first /
                     tape)
                    (prepend (value) (safe-tail / second / tape))))));

[hear] (define turing /
         lambda (machine current last tape)
          (if (= (current) (last))
            (tape)
            (let ((next (machine (current) (tape-read / tape))))
              (turing
                (machine)
                (list-ref (next) 0)
                (last)
                (tape-transition
                  (tape)
                  (list-ref (next) 1)
                  (list-ref (next) 2))))));

[hear] (define make-tape /
         ? x /
         pair (vector) (x));

[hear] (define remove-trail /
         ? x /
         ? lst /
         if (> (list-length / lst) 0)
          (if (equal (last / lst) (x))
            (remove-trail (x) (except-last / lst))
            (lst))
          (lst));

[hear] (define extract-tape /
         ? x /
         remove-trail (vector) (second / x));

[hear] (define tm-binary-increment /
         make-hash /
         vector
          (pair right
                (make-hash /
                 vector
                  (pair 0 (vector right 1 0))
                  (pair 1 (vector right 1 1))
                  (pair (vector) (vector inc 0 (vector)))))
          (pair inc
                (make-hash /
                 vector
                  (pair 0 (vector noinc 0 1))
                  (pair 1 (vector inc 0 0))
                  (pair (vector) (vector halt 2 1))))
          (pair noinc
                (make-hash /
                 vector
                  (pair 0 (vector noinc 0 0))
                  (pair 1 (vector noinc 0 1))
                  (pair (vector) (vector halt 1 (vector)))))
          (pair halt (make-hash / vector)));

[hear] (list= (extract-tape /
                turing
                 (tm-binary-increment)
                 right
                 halt
                 (make-tape /
                  vector 1 0 0 1))
               (vector 1 0 1 0));

[hear] (list= (extract-tape /
                turing
                 (tm-binary-increment)
                 right
                 halt
                 (make-tape /
                  vector 1 1 1))
               (vector 1 0 0 0));

[hear] (list= (extract-tape /
                turing
                 (tm-binary-increment)
                 right
                 halt
                 (make-tape /
                  vector 1 1 1 0 0 0 1 1 1))
               (vector 1 1 1 0 0 1 0 0 0));


       # OBJECT introduce simple form of typing, for ease of documentation.
       # An object is simply a function that takes an argument.
       # The argument is the method to call on the object.
       # Types are here taken to be just the existence of a particular method,
       # with that method returning an object of the appropriate type.
[hear] (define make-integer
         (lambda (v)
           (lambda (x)
             (if (= (x) int)
             (v)
             0))));

[hear] (define objectify
         (? x
            (if (number? (x))
            (make-integer (x))
            (x))));

[hear] (define instanceof
         (lambda (T t)
           (if (number? (t))
           (= (T) int)
           (not (number? ((objectify (t)) (T)))))));

       # add version of lambda that allows types to be declared
[hear] (define prev-translate (translate));

[hear] (define translate
         (let ((prev (prev-translate)))
           (? x
             (if (number? (x))
               (prev (x))
               (if (= (head (x)) lambda)
                 (let ((formals (head (tail (x))))
                       (body (head (tail (tail (x))))))
                   (if (> (list-length (formals)) 0)
               (if (number? (last (formals)))
                   (translate
                    (vector
                     lambda
                     (except-last (formals))
                     (vector ? (last (formals)) (body))))
                   (let ((formal-name (first (last (formals))))
                     (formal-type (second (last (formals)))))
                     (translate
                      (vector
                   lambda
                   (except-last (formals))
                   (vector
                    ?
                    (formal-name)
                    (vector
                     let
                     (vector (vector
                          (formal-name)
                          (vector
                           (vector objectify (vector (formal-name)))
                           (formal-type))))
                     (body)))))))
               (translate (body))))
                 (prev (x)))))));

       # add conditional form
[hear] (define prev-translate (translate));

[hear] (define translate
         (let ((prev (prev-translate)))
           (? x
             (if (number? (x))
               (prev (x))
               (if (= (head (x)) cond)
                 (let ((cnd (head (tail (x))))
                       (rem (tail (tail (x)))))
                   (if (> (list-length (rem)) 0)
               (translate
                (vector
                         if
                 (first (cnd))
                 (second (cnd))
                 (prepend cond (rem))))
               (translate (cnd))))
                 (prev (x)))))));

[hear] (= 99 (cond 99));

[hear] (= 8 (cond ((true) 8) 11));

[hear] (= 11 (cond ((false) 8) 11));

[hear] (= 7 (cond ((false) 3) ((true) 7) 11));

[hear] (= 3 (cond ((true) 3) ((true) 7) 11));

[hear] (= 11 (cond ((false) 3) ((false) 7) 11));

[hear] (define remove-match
         (lambda (test lst)
           (if (> (list-length (lst)) 0)
           (if (test (head (lst)))
               (remove-match (test) (tail (lst)))
               (prepend (head (lst)) (remove-match (test) (tail (lst)))))
           (lst))));

[hear] (define remove-element
         (lambda (x)
           (remove-match (lambda (y) (= (y) (x))))));

[hear] (list= (vector 1 2 3 5) (remove-element 4 (vector 1 2 3 4 5)));

[hear] (list= (vector 1 2 3 5) (remove-element 4 (vector 1 4 2 4 3 4 5)));

[hear] (define return
         (lambda (T t)
           (let ((obj (objectify (t))))
             (obj (T)))));

[hear] (define tester
         (lambda ((x int) (y int))
           (return int (+ (x) (y)))));

[hear] (= 42 (tester (make-integer 10) (make-integer 32)));

[hear] (= 42 (tester 10 32));

[hear] (define reflective
         (lambda (f)
           ((lambda (x)
              (f (lambda (y) ((x (x)) (y)))))
            (lambda (x)
              (f (lambda (y) ((x (x)) (y))))))));


       # OBJECT an example object -- a 2D point
[hear] (define point
         (lambda (x y)
           (reflective
            (lambda (self msg)
              (cond ((= (msg) x) (x))
                ((= (msg) y) (y))
                ((= (msg) point) (self))
                ((= (msg) +)
                 (lambda ((p point))
               (point (+ (x) (p x))
                      (+ (y) (p y)))))
                ((= (msg) =)
                 (lambda ((p point))
               (and (= (x) (p x))
                    (= (y) (p y)))))
                0)))));

[hear] (define point1 (point 1 11));

[hear] (define point2 (point 2 22));

[hear] (= 1 (point1 x));

[hear] (= 22 (point2 y));

[hear] (= 11 ((point 11 12) x));

[hear] (= 11 (((point 11 12) point) x));

[hear] (= 16 (((point 16 17) point) x));

[hear] (= 33 (point1 + (point2) y));

[hear] (point1 + (point2) = (point 3 33));

[hear] (point2 + (point1) = (point 3 33));

[hear] ((point 100 200) + (point 200 100) = (point 300 300));

[hear] (instanceof point (point1));

[hear] (not (instanceof int (point1)));

[hear] (instanceof int 5);

[hear] (not (instanceof point 5));


       # OBJECT an example object -- a container
[hear] (define container
         (lambda (x)
           (let ((contents (make-cell (vector))))
             (reflective
              (lambda (self msg)
            (cond ((= (msg) container) (self))
                  ((= (msg) inventory) (get! (contents)))
                  ((= (msg) add)
               (lambda (x)
                 (if (not (element (x) (get! (contents))))
                     (set! (contents) (prepend (x) (get! (contents))))
                     (false))))
                  ((= (msg) remove)
               (lambda (x)
                 (set! (contents) (remove-element (x) (get! (contents))))))
                  ((= (msg) =)
               (lambda ((c container))
                 (set= (self inventory) (c inventory))))
                  0))))));

       # Can pass anything to container function to create an object
       # Should eventually use a consistent protocol for all objects,
       # but all this stuff is still in flux
[hear] (define pocket (container new));

[hear] (pocket add 77);

[hear] (pocket add 88);

[hear] (pocket add 99);

[hear] (set= (pocket inventory) (vector 77 88 99));

[hear] (pocket remove 88);

[hear] (set= (pocket inventory) (vector 77 99));

[hear] (define pocket2 (container new));

[hear] (pocket2 add 77);

[hear] (pocket2 add 99);

[hear] (pocket2 = (pocket));


       # OBJECT expressing inheritance
       # counter-container adds one method to container: count
[hear] (define counter-container
         (lambda (x)
           (let ((super (container new)))
             (reflective
              (lambda (self msg)
            (cond ((= (msg) counter-container) (self))
                  ((= (msg) count) (list-length (super inventory)))
                  (super (msg))))))));

[hear] (define cc1 (counter-container new));

[hear] (= 0 (cc1 count));

[hear] (cc1 add 4);

[hear] (= 1 (cc1 count));

[hear] (cc1 add 5);

[hear] (= 2 (cc1 count));


       # OBJECT adding a special form for classes
       # need a bunch of extra machinery first, will push this
       # back into previous sections eventually, and simplify
[hear] (define list-append
         (lambda (lst1 lst2)
           (if (> (list-length (lst1)) 0)
           (list-append (except-last (lst1))
                    (prepend (last (lst1)) (lst2)))
           (lst2))));

[hear] (list= (list-append (vector 1 2 3) (vector 4 5 6)) (vector 1 2 3 4 5 6));

[hear] (define append
         (? x
            (? lst
           (if (> (list-length (lst)) 0)
               (prepend (head (lst)) (append (x) (tail (lst))))
               (vector (x))))));

[hear] (list= (append 5 (vector 1 2)) (vector 1 2 5));

[hear] (define select-match
         (lambda (test lst)
           (if (> (list-length (lst)) 0)
           (if (test (head (lst)))
               (prepend (head (lst)) (select-match (test) (tail (lst))))
               (select-match (test) (tail (lst))))
           (lst))));

[hear] (define unique
         (let ((store (make-cell 0)))
           (lambda (x)
             (let ((id (get! (store))))
           (begin
             (set! (store) (+ (id) 1))
             (id))))));

[hear] (= (unique new) 0);

[hear] (= (unique new) 1);

[hear] (= (unique new) 2);

[hear] (not (= (unique new) (unique new)));

[hear] (define setup-this
         (lambda (this self)
           (if (number? / this)
           (self)
           (this))));

       # okay, here it comes. don't panic!
       # I need to split this up into helpers, and simplify.
       # It basically just writes code for classes like we saw in
       # a previous section.
[hear] (define prev-translate (translate));

[hear] (define translate
         (let ((prev (prev-translate)))
           (? x
              (if (number? (x))
              (prev (x))
              (if (= (head (x)) class)
                  (let ((name (list-ref (x) 1))
                    (args (list-ref (x) 2))
                    (fields (tail (tail (tail (x))))))
                (translate
                 (vector
                  define
                  (name)
                  (vector
                   lambda
                   (prepend ext-this (args))
                   (vector
                    let
                    (append
                     (vector unique-id (vector unique new))
                     (map
                      (tail)
                      (select-match (? x (= (first (x)) field)) (fields))))
                    (vector
                     let
                     (vector
                      (vector
                   self
                   (vector
                    reflective
                    (vector
                     lambda
                     (vector self)
                     (vector
                      let
                      (vector
                       (vector
                        this
                        (vector setup-this
                            (vector ext-this)
                            (vector self))))
                      (vector
                       let
                       (vector (vector ignore-this 1))
                       (vector
                        lambda
                        (vector method)
                        (vector
                         (list-append
                          (prepend
                       cond
                       (list-append
                        (map
                         (? x
                            (vector
                             (vector = (vector method) (first (x)))
                             (second (x))))
                         (map (tail)
                              (select-match
                           (? x (= (first (x)) method))
                           (fields))))
                        (map
                         (? x
                            (vector
                             (vector = (vector method) (x))
                             (vector (x))))
                         (map (second)
                              (select-match
                           (? x (= (first (x)) field))
                           (fields))))))
                          (vector
                       (vector
                        (vector = (vector method) self)
                        (vector self))
                       (vector
                        (vector = (vector method) (name))
                        (vector self self))
                       (vector
                        (vector = (vector method) classname)
                        (name))
                       (vector
                        (vector = (vector method) unknown)
                        (vector lambda (vector x) 0))
                       (vector
                        (vector = (vector method) new)
                        0)
                       (vector
                        (vector = (vector method) unique-id)
                        (vector unique-id))
                       (vector
                        (vector = (vector method) ==)
                        (vector
                         lambda
                         (vector x)
                         (vector =
                             (vector unique-id)
                             (vector x unique-id))))
                       (vector self unknown (vector method))))))))))))
                     (vector
                      begin
                      (vector self new)
                      (vector self))))))))
                  (prev (x)))))));

       # revisit the point class example
[hear] (class point (x y)
              (method x (x))
              (method y (y))
              (method + (lambda ((p point))
                  (point new
                     (+ (x) (p x))
                     (+ (y) (p y)))))
              (method = (lambda ((p point))
                  (and (= (x) (p x))
                   (= (y) (p y))))));

       # note the appearance of new in the next line --
       # this is the only difference to previous version
[hear] (define point1 (point new 1 11));

[hear] (define point2 (point new 2 22));

[hear] (= 1 (point1 x));

[hear] (= 22 (point2 y));

[hear] (= 11 ((point new 11 12) x));

[hear] (= 11 (((point new 11 12) point) x));

[hear] (= 16 (((point new 16 17) point) x));

[hear] (= 33 (point1 + (point2) y));

[hear] (point1 + (point2) = (point new 3 33));

[hear] (point2 + (point1) = (point new 3 33));

[hear] ((point new 100 200) + (point new 200 100) = (point new 300 300));

[hear] (instanceof point (point1));

[hear] (not (instanceof int (point1)));

       # Check that virtual calls can be made to work.
       # They are a little awkward right now.
       # Should they be the default?
[hear] (class c1 ()
              (method getid 100)
              (method altid (this getid)));

[hear] (class c2 ()
              (field super-ref (make-cell 0))
              (method new (set! (super-ref) (c1 / this)))
              (method super (? x ((get! / super-ref) (x))))
              (method unknown (? x (self super / x)))
              (method getid 200));

[hear] (= 100 / c1 new altid);

[hear] (= 200 / c2 new altid);


       # OBJECT wrapper class for cells
[hear] (class cell (initial-value)
              (field content (make-cell (initial-value)))
              (method get (get! (content)))
              (method set (lambda (new-value)
                    (set! (content) (new-value))))
              (method reset (self set (initial-value)))
              (method unknown (lambda (x) ((objectify (self get)) (x)))));

[hear] (define cell-test1 (cell new 15));

[hear] (= 15 (cell-test1 get));

[hear] (cell-test1 set 82);

[hear] (= 82 (cell-test1 get));

[hear] (define cell-test2 (cell new (point new 120 150)));

[hear] (define cell-test3 (cell new (point new 300 300)));

[hear] (cell-test2 + (cell-test3) = (point new 420 450));

[hear] (not (cell-test2 = (cell-test3)));

[hear] (cell-test3 set (cell-test2));

[hear] (cell-test2 = (cell-test3));


       # MUD playing around with doors and rooms
[hear] (class door ((src room) (dest room))
              (method new (begin
                    (src add (self))
                    (dest add (self))))
              (method access-from (lambda ((current room))
                        (cond ((current == (src)) (dest))
                          ((current == (dest)) (src))
                          0)))
              (method is-present (lambda ((current room))
                       (cond ((current == (src)) (true))
                         ((current == (dest)) (true))
                         (false)))));

[hear] (class room (name)
              (field content (container new))
              (method name (name))
              (method unknown (lambda (x) (content (x)))));

       # need to fix up containers to use object equality
[hear] (define object-element
         (lambda (n lst)
           (> (list-length
           (select-match (lambda (x) (x == (n))) (lst)))
              0)));

[hear] (class container ()
           (field contents (cell new (vector)))
           (method inventory (contents get))
           (method add (lambda (x)
                 (if (not (object-element (x) (contents get)))
                     (contents set (prepend (x) (contents get)))
                     (false)))));

[hear] (define hall (room new 0));

[hear] (define kitchen (room new 1));

[hear] (define door1 (door new (hall) (kitchen)));

[hear] ((first (hall inventory)) == (door1));

[hear] ((first (kitchen inventory)) == (door1));

[hear] (door1 access-from (hall) == (kitchen));

[hear] (not (door1 access-from (hall) == (hall)));

[hear] (door1 access-from (kitchen) == (hall));

[hear] (define stairs (room new 2));

[hear] (define lawn (room new 3));

[hear] (define bedroom (room new 4));

[hear] (define nowhere (room new 0));

[hear] (define door2 (door new (hall) (lawn)));

[hear] (define door3 (door new (hall) (stairs)));

[hear] (define door4 (door new (stairs) (bedroom)));

[hear] (class character ()
              (field location (cell new 0))
              (field name (cell new 0))
              (method set-room (lambda ((r room))
                     (begin
                       (if (not (number? / location get))
                       (location get remove (self))
                       0)
                       (r add (self))
                       (location set (r)))))
              (method get-room (location get))
              (method set-name (lambda (n) (name set / n)))
              (method get-name (name get))
              (method update 0));

[hear] (define find-max-helper
         (lambda (test max idx n lst)
           (if (> (list-length (lst)) 0)
           (if (> (test (head (lst))) (max))
               (find-max-helper (test) (test (head (lst))) (n) (+ (n) 1) (tail (lst)))
               (find-max-helper (test) (max) (idx) (+ (n) 1) (tail (lst))))
           (idx))));

[hear] (define find-max-idx
         (lambda (test lst)
           (find-max-helper (test) (test (head (lst))) 0 0 (lst))));

[hear] (define find-min-helper
         (lambda (test max idx n lst)
           (if (> (list-length (lst)) 0)
           (if (< (test (head (lst))) (max))
               (find-min-helper (test) (test (head (lst))) (n) (+ (n) 1) (tail (lst)))
               (find-min-helper (test) (max) (idx) (+ (n) 1) (tail (lst))))
           (idx))));

[hear] (define find-min-idx
         (lambda (test lst)
           (find-min-helper (test) (test (head (lst))) 0 0 (lst))));

[hear] (= 2 (find-max-idx (lambda (x) (x)) (vector 3 4 5 0)));

[hear] (= 1 (find-max-idx (lambda (x) (x)) (vector 3 5 4 0)));

[hear] (= 0 (find-max-idx (lambda (x) (x)) (vector 5 3 4 0)));

       # the robo class makes a character that patrols from room to room
[hear] (class robo ()
              (field super (character new))
              (field timestamp (cell new 1))
              (field timestamp-map (cell new (lambda (x) 0)))
              (method unknown (lambda (x) (super (x))))
              (method update
                  (let ((exits
                     (select-match (lambda (x) (instanceof door (x)))
                           (self location inventory))))
                (let ((timestamps
                   (map (lambda (x) (timestamp-map get (x)))
                        (exits))))
                  (let ((chosen-exit (list-ref
                              (exits)
                              (find-min-idx (lambda (x) (x))
                                    (timestamps))))
                    (current-tmap (timestamp-map get))
                    (current-t (timestamp get)))
                    (begin
                      (self location set (chosen-exit
                              access-from
                              (self location get)))
                      (timestamp-map set
                             (lambda ((d door))
                           (if (d == (chosen-exit))
                               (current-t)
                               (current-tmap (d)))))
                      (timestamp set (+ (timestamp get) 1))))))));

[hear] (define myrobo (robo new));

[hear] (myrobo set-room (stairs));

[hear] (define which-room
         (lambda ((rr robo))
           (find-max-idx
            (lambda ((r room)) (if (r == (rr get-room)) 1 0))
            (vector (hall) (kitchen) (stairs) (lawn) (bedroom)))));

[hear] (define sequencer
         (lambda (n current lst)
           (if (< (current) (n))
           (begin
             (myrobo update)
             (sequencer
              (n)
              (+ (current) 1)
              (append
               (which-room (myrobo))
               (lst))))
           (lst))));

       # here is a list of the first 30 rooms the robot character visits
       # 0=hall, 1=kitchen, 2=stairs, 3=lawn, 4=bedroom
[hear] (list= (sequencer 30 0 (vector)) (vector 4 2 0 3 0 1 0 2 4 2 0 3 0 1 0 2 4 2 0 3 0 1 0 2 4 2 0 3 0 1));

       # Now should start to introduce a language to talk about what is
       # going on in the simulated world, and start to move away from
       # detailed mechanism

       # NOTE end of part 2, start of part 3
       # The following parts of the message are the beginnings
       # of embedding an alternate visual primer
[hear] (intro part3);


       # GATE simulating unless gates
       # for embedded image-and-logic-based primer
       # practice with pure logic gate
       # X unless Y = (X if Y=0, otherwise 0)
[hear] (define unless /
         ? x / ? y /
         and (x) (not (y)));

       # if second input is true, output is blocked (false)
       # if second input is false, output copies first input
[hear] (= (false) (unless (false) (false)));

[hear] (= (true) (unless (true) (false)));

[hear] (= (false) (unless (false) (true)));

[hear] (= (false) (unless (true) (true)));

       # To do: add a simple simulator for non-grid-based
       # logic -- much simpler to understand than
       # grid-based
       # On to a grid-based logic simulation
       # first, need unbounded, mutable matrices
[hear] (define make-matrix /
         ? default /
         (make-cell (hash-default (default))));

[hear] (define matrix-set /
         ? m /
         ? x /
         ? addr /
         set! (m) / hash-add (get! (m)) (addr) (x));

[hear] (define matrix-get /
         ? m /
         ? addr /
         hash-ref (get! (m)) (addr));

[hear] (define test-matrix
         (make-matrix 0));

[hear] (= 0 / matrix-get (test-matrix) / vector 1 2 3);

[hear] (matrix-set (test-matrix) 10 / vector 1 2 3);

[hear] (= 10 / matrix-get (test-matrix) / vector 1 2 3);

       # go through a circuit of unless gates and analyze data flow
[hear] (define unless-phase-1 /
         ? circuit /
         assign state (make-matrix (false))
         (begin
           (map
            (? gate /
           assign x1 (list-ref (gate) 0) /
           assign y1 (list-ref (gate) 1) /
           assign x2 (list-ref (gate) 2) /
           assign y2 (list-ref (gate) 3) /
           assign v (list-ref (gate) 4) /
           (if (= (x1) (x2))
               (begin
                 (matrix-set (state) (v) / vector (x2) (y2) vert-value)
                 (matrix-set (state) (true) / vector (x2) (y2) vert-have)
                 (matrix-set (state) (true) / vector (x1) (y1) vert-want)
                 (gate))
               (begin
                 (matrix-set (state) (v) / vector (x2) (y2) horiz-value)
                 (matrix-set (state) (true) / vector (x2) (y2) horiz-have)
                 (matrix-set (state) (true) / vector (x1) (y1) horiz-want)
                 (gate))))
            (circuit))
           (state)));

       # move forward one simulation step
[hear] (define unless-phase-2 /
         ? circuit /
         ? state
         (map
          (? gate /
             assign x1 (list-ref (gate) 0) /
             assign y1 (list-ref (gate) 1) /
             assign x2 (list-ref (gate) 2) /
             assign y2 (list-ref (gate) 3) /
             assign v (list-ref (gate) 4) /
             assign nv (if (= (x1) (x2))
                   (if (matrix-get (state) / vector (x1) (y1) vert-have)
                   (and (matrix-get (state) /
                            vector (x1) (y1) vert-value)
                        (not (and (matrix-get (state) /
                                  vector (x1) (y1) horiz-value)
                              (not (matrix-get (state) /
                                   vector (x1) (y1) horiz-want)))))
                   (if (matrix-get (state) / vector (x1) (y1) horiz-have)
                       (matrix-get (state) / vector (x1) (y1) horiz-value)
                       (true)))
                   (if (matrix-get (state) / vector (x1) (y1) horiz-have)
                   (and (matrix-get (state) / vector (x1) (y1) horiz-value)
                        (not (and (matrix-get (state) /
                                  vector (x1) (y1) vert-value)
                              (not (matrix-get (state) /
                                   vector (x1) (y1) vert-want)))))
                   (if (matrix-get (state) / vector (x1) (y1) vert-have)
                       (matrix-get (state) / vector (x1) (y1) vert-value)
                       (true)))) /
                       vector (x1) (y1) (x2) (y2) (nv))
          (circuit)));

       # wrap up both phases of simulation
[hear] (define simulate-unless /
         ? circuit /
         assign state (unless-phase-1 (circuit)) /
         unless-phase-2 (circuit) (state));

       # A circuit is a list of gates
       # Each gate is a list (x1 y1 x2 y2 v)
       # where the coordinates (x1,y1) and (x2,y2) represent
       # start and end points of a wire on a plane, carrying a
       # logic value v.
       # Wires copy values from their start point.
       #   |
       #   | (A)
       #   V
       # -->-->
       # (B)(C)
       #
       # Wire C here copies from wire B.
       # If wire A is on, it blocks (sets to 0) C.
[hear] (assign circuit1
           (vector
            (vector 2 2 4 2 (true))
            (vector 4 2 6 2 (true))
            (vector 6 2 8 2 (true))
            (vector 6 4 6 2 (true))) /
            assign circuit2
            (vector
             (vector 2 2 4 2 (true))
             (vector 4 2 6 2 (true))
             (vector 6 2 8 2 (false))
             (vector 6 4 6 2 (true))) /
             equal (simulate-unless (circuit1)) (circuit2));

       # okay, now let us make a simple image class
       # we are going to encode each row as a single binary number,
       # rather than a vector, so that images will be pretty
       # obvious in the raw, uninterpreted message
[hear] (define bit-get /
         lambda (n offset) /
         assign div2 (div (n) 2)
         (if (= 0 / offset)
             (not / = (n) / * 2 / div2)
             (bit-get (div2) / - (offset) 1)));

[hear] (= 0 / bit-get (::.) 0);

[hear] (= 1 / bit-get (::.) 1);

[hear] (= 1 / bit-get (::.) 2);

[hear] (= 0 / bit-get (::.) 3);

[hear] (= 0 / bit-get (::.) 4);

[hear] (= 0 / bit-get 8 0);

[hear] (= 0 / bit-get 8 1);

[hear] (= 0 / bit-get 8 2);

[hear] (= 1 / bit-get 8 3);

[hear] (define make-image /
         lambda (h w lst) /
         vector (h) (w) (lst));

[hear] (define image-get /
         lambda (image row col) /
         assign h (list-ref (image) 0) /
         assign w (list-ref (image) 1) /
         assign lst (list-ref (image) 2) /
         assign bits (list-ref (lst) (row)) /
         bit-get (bits) (- (- (w) (col)) 1));

[hear] (define image-height /
         ? image /
         list-ref (image) 0);

[hear] (define image-width /
         ? image /
         list-ref (image) 1);

[hear] (define test-image /
         make-image 3 5 /
         vector (:....) (:...:) (:....));

[hear] (= 3 (image-height / test-image));

[hear] (= 5 (image-width / test-image));

[hear] (= (true) (image-get (test-image) 0 0));

[hear] (= (false) (image-get (test-image) 0 1));

[hear] (= (false) (image-get (test-image) 0 4));

[hear] (= (true) (image-get (test-image) 1 0));

[hear] (= (true) (image-get (test-image) 2 0));

[hear] (= (true) (image-get (test-image) 1 4));

       # need a way to join two lists
[hear] (define merge-list /
         ? lst1 /
         ? lst2 /
         (if (> (list-length / lst1) 0)
             (prepend (head / lst1) (merge-list (tail / lst1) (lst2)))
             (lst2)));

[hear] (define merge-lists /
         ? lst /
         (if (> (list-length / lst) 2)
             (merge-list (head / lst) (merge-lists / tail / lst))
             (if (= (list-length / lst) 2)
             (merge-list (head / lst) / (head / tail / lst))
             (if (= (list-length / lst) 1)
                 (head / lst)
                 (vector)))));

             
             
[hear] (equal (vector 1 2 3 4) (merge-list (vector 1 2) (vector 3 4)));

[hear] (equal (vector 1 2 3 4) (merge-lists (vector (vector 1 2) (vector 3) (vector 4))));

       # helper for pairing
[hear] (define prefix /
         ? x /
         ? lst /
         map (? y (vector (x) (y))) (lst));

[hear] (equal (vector (vector 1 10) (vector 1 11))
              (prefix 1 (vector 10 11)));

       # need a way to take product of domains
[hear] (define pairing /
         ? lst1 /
         ? lst2
         (if (> (list-length / lst1) 0)
             (merge-list (prefix (head / lst1) (lst2))
                 (pairing (tail / lst1) (lst2)))
             (vector)));

[hear] (equal (vector (vector 1 10) (vector 1 11) (vector 2 10) (vector 2 11))
              (pairing (vector 1 2) (vector 10 11)));

       # need a way to make counting sets
[hear] (define count /
         ? lo / ? hi
         (if (<= (lo) (hi))
             (prepend (lo) (count (+ (lo) 1) (hi)))
             (vector)));

[hear] (equal (vector 0 1 2 3 4) (count 0 4));

       # given an image of a circuit, extract a model.
       # wire elements are centered on multiples of 8
       # individual element...
[hear] (define distill-element /
         ? image / ? xlogic / ? ylogic / ? xmid / ? ymid
         (if (image-get (image) (ymid) (xmid))
             (assign vert (image-get (image) (+ (ymid) 4) (xmid)) /
                 assign dx (if (vert) 0 1) /
                 assign dy (if (vert) 1 0) /
                 assign pos (image-get (image)
                           (+ (ymid) / + (* 4 / dy) (* 2 / dx))
                           (+ (xmid) / - (* 4 / dx) (* 2 / dy))) /
                 assign sgn (if (pos) 1 (- 0 1)) /
                 assign dx (* (sgn) (dx)) /
                 assign dy (* (sgn) (dy)) /
                 assign active (image-get (image) (+ (ymid) (dx)) (- (xmid) (dy))) /
                 (vector
                  (vector (- (xlogic) (dx))
                      (- (ylogic) (dy))
                      (+ (xlogic) (dx))
                      (+ (ylogic) (dy))
                      (active))))
             (vector)));

             
       # full circuit...
[hear] (define distill-circuit /
         ? image /
         assign h (div (image-height / image) 8) /
         assign w (div (image-width / image) 8)
         (merge-lists
          (map (? v /
              assign xlogic (list-ref (v) 0) /
              assign ylogic (list-ref (v) 1) /
              assign xmid (* 8 / xlogic) /
              assign ymid (* 8 / ylogic) /
              distill-element (image) (xlogic) (ylogic) (xmid) (ymid))
           (pairing (count 1 (- (w) 1))
                (count 1 (- (h) 1))))));


       # GATE testing alternate primer based on gates: COS_NOT circuit
       # This section contains one or more representations of a circuit
       # constructed using UNLESS gates. Needs elaboration...
       # graphic representation :
       #
[hear] (define cos_not_gate /
         vector
          (vector 0 6 2 6 (true))
          (vector 2 6 4 6 (true))
          (vector 4 6 6 6 (true))
          (vector 6 6 8 6 (true))
          (vector 8 4 8 6 (true))
          (vector 8 6 8 8 (false))
          (vector 8 8 10 8 (false))
          (vector 10 8 12 8 (false))
          (vector 12 8 12 6 (false))
          (vector 12 6 14 6 (false))
          (vector 14 6 16 6 (false))
          (vector 16 6 18 6 (false))
          (vector 18 6 20 6 (false)));

[hear] (define cos_not_image /
         make-image 109 169 /
         vector
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:.............................................................:::::.....................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:...........:...............:...............:...............:...:...........................................:...............:...............:...............:...........:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..............................................::..............::..............::..............::..........:)
          (:..::::::::::::....::::::::::::....::::::::::::....::::::::::::....................................::::::::::::....::::::::::::....::::::::::::....::::::::::::.........:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..............................................::..............::..............::..............::..........:)
          (:...........:...............:...............:...............:...................................:...........:...............:...............:...............:...........:)
          (:...............................................................:..............................:::......................................................................:)
          (:...............................................................:.............................:::::.....................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:.............................................................:::::.............................:.......................................................................:)
          (:..............................................................:::..............................:.......................................................................:)
          (:...............................................................:...........:...............:...........................................................................:)
          (:...........................................................................::..............::..........................................................................:)
          (:..................................................................::::::::::::....::::::::::::.........................................................................:)
          (:...........................................................................::..............::..........................................................................:)
          (:...........................................................................:...............:...........................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::));

[hear] (equal (cos_not_gate)
               (distill-circuit (cos_not_image)));


       # GATE testing alternate primer based on gates: COS_AND circuit
       # This section contains one or more representations of a circuit
       # constructed using UNLESS gates. Needs elaboration...
       # graphic representation :
       #
[hear] (define cos_and_gate /
         vector
          (vector 0 2 2 2 (true))
          (vector 0 8 2 8 (true))
          (vector 2 2 4 2 (true))
          (vector 2 4 4 4 (true))
          (vector 2 6 4 6 (true))
          (vector 2 8 4 8 (true))
          (vector 4 2 4 4 (true))
          (vector 4 8 4 6 (true))
          (vector 4 4 6 4 (false))
          (vector 4 6 6 6 (false))
          (vector 6 4 8 4 (false))
          (vector 6 6 8 6 (false))
          (vector 8 4 10 4 (false))
          (vector 8 6 10 6 (false))
          (vector 10 2 10 4 (true))
          (vector 10 4 10 6 (true))
          (vector 10 6 10 8 (true))
          (vector 10 8 12 8 (true))
          (vector 12 8 14 8 (true))
          (vector 14 8 16 8 (true))
          (vector 16 8 18 8 (true)));

[hear] (define cos_and_image /
         make-image 88 153 /
         vector
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:...........:...............:...........................................................................................................................:)
          (:..:::::::::::.....:::::::::::..........................................................................................................................:)
          (:..::::::::::::....::::::::::::.........................................................................................................................:)
          (:..:::::::::::.....:::::::::::..........................................................................................................................:)
          (:...........:...............:...........................................................................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:.............................:::::...........................................:::::.....................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:...........................:...:...........:...............:...............:...:.......................................................................:)
          (:..................:::::::::::..............::..............::..............::..........................................................................:)
          (:..................::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................................................................:)
          (:..................:::::::::::..............::..............::..............::..........................................................................:)
          (:...........................:...............:...............:...............:...........................................................................:)
          (:..............................................................................:::......................................................................:)
          (:..............................................................................:::......................................................................:)
          (:..............................................................................:::......................................................................:)
          (:..............................................................................:::......................................................................:)
          (:..............................................................................:::......................................................................:)
          (:..............................................................................:::......................................................................:)
          (:..............................................................................:::......................................................................:)
          (:..............................................................................:::......................................................................:)
          (:..............................................................................:::......................................................................:)
          (:.............................................................................:::::.....................................................................:)
          (:..............................................................................:::......................................................................:)
          (:...........................:...............:...............:...............:...:.......................................................................:)
          (:..................:::::::::::..............::..............::..............::..........................................................................:)
          (:..................::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................................................................:)
          (:..................:::::::::::..............::..............::..............::..........................................................................:)
          (:...........................:...:...........:...............:...............:...........................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:.............................:::::............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:..............................:::............................................:::::.....................................................................:)
          (:..............................:::.............................................:::......................................................................:)
          (:...........:...............:...................................................:...........:...............:...............:...............:...........:)
          (:..:::::::::::.....:::::::::::.....................................................:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........:)
          (:..::::::::::::....::::::::::::....................................................::::::::::::....::::::::::::....::::::::::::....::::::::::::.........:)
          (:..:::::::::::.....:::::::::::.....................................................:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........:)
          (:...........:...............:...............................................................:...............:...............:...............:...........:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:.......................................................................................................................................................:)
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::));

[hear] (equal (cos_and_gate)
               (distill-circuit (cos_and_image)));


       # GATE testing alternate primer based on gates: COS_OR circuit
       # This section contains one or more representations of a circuit
       # constructed using UNLESS gates. Needs elaboration...
       # graphic representation :
       #
[hear] (define cos_or_gate /
         vector
          (vector 2 4 4 4 (true))
          (vector 2 6 4 6 (true))
          (vector 4 4 6 4 (true))
          (vector 4 6 6 6 (true))
          (vector 6 4 8 4 (true))
          (vector 6 6 8 6 (true))
          (vector 8 4 10 4 (true))
          (vector 8 6 10 6 (true))
          (vector 8 8 10 8 (true))
          (vector 10 2 10 4 (true))
          (vector 10 4 10 6 (false))
          (vector 10 6 10 8 (false))
          (vector 10 8 12 8 (true))
          (vector 12 8 14 8 (true))
          (vector 14 8 16 8 (true))
          (vector 16 8 18 8 (true)));

[hear] (define cos_or_image /
         make-image 93 169 /
         vector
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:.............................................................................:::::.....................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:...........................:...............:...............:...............:...:.......................................................................................:)
          (:..................:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................................:)
          (:..................::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................................................................................:)
          (:..................:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................................:)
          (:...........................:...............:...............:...............:...........................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:.............................................................................:::::.....................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:...........................:...............:...............:...............:...:.......................................................................................:)
          (:..................:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................................:)
          (:..................::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................................................................................:)
          (:..................:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................................:)
          (:...........................:...............:...............:...............:...........................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:...............................................................................:.......................................................................................:)
          (:.............................................................................:::::.....................................................................................:)
          (:..............................................................................:::......................................................................................:)
          (:...........................................................................:...:...........:...............:...............:...............:...........................:)
          (:..................................................................:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................:)
          (:..................................................................::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................:)
          (:..................................................................:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................:)
          (:...........................................................................:...............:...............:...............:...............:...........................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::));

[hear] (equal (cos_or_gate)
               (distill-circuit (cos_or_image)));


       # GATE testing alternate primer based on gates: COS_NOR circuit
       # This section contains one or more representations of a circuit
       # constructed using UNLESS gates. Needs elaboration...
       # graphic representation :
       #
[hear] (define cos_nor_gate /
         vector
          (vector 0 6 2 6 (true))
          (vector 0 8 2 8 (true))
          (vector 2 6 4 6 (true))
          (vector 2 8 4 8 (true))
          (vector 4 6 6 6 (true))
          (vector 4 8 6 8 (true))
          (vector 6 6 8 6 (true))
          (vector 6 8 8 8 (true))
          (vector 8 4 8 6 (true))
          (vector 8 6 8 8 (false))
          (vector 8 8 8 10 (false))
          (vector 8 10 10 10 (false))
          (vector 10 10 12 10 (false))
          (vector 12 10 14 10 (false))
          (vector 14 10 16 10 (false))
          (vector 16 10 18 10 (false))
          (vector 18 10 20 10 (false)));

[hear] (define cos_nor_image /
         make-image 125 169 /
         vector
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:.............................................................:::::.....................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:...........:...............:...............:...............:...:.......................................................................................................:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................................................:)
          (:..::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................................................................................................:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................................................:)
          (:...........:...............:...............:...............:...........................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:.............................................................:::::.....................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:...........:...............:...............:...............:...:.......................................................................................................:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................................................:)
          (:..::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................................................................................................:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................................................:)
          (:...........:...............:...............:...............:...........................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:...............................................................:.......................................................................................................:)
          (:.............................................................:::::.....................................................................................................:)
          (:..............................................................:::......................................................................................................:)
          (:...............................................................:...........:...............:...............:...............:...............:...............:...........:)
          (:...........................................................................::..............::..............::..............::..............::..............::..........:)
          (:..................................................................::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::.........:)
          (:...........................................................................::..............::..............::..............::..............::..............::..........:)
          (:...........................................................................:...............:...............:...............:...............:...............:...........:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::));

[hear] (equal (cos_nor_gate)
               (distill-circuit (cos_nor_image)));


       # GATE testing alternate primer based on gates: COS_OSC circuit
       # This section contains one or more representations of a circuit
       # constructed using UNLESS gates. Needs elaboration...
       # graphic representation :
       #
[hear] (define cos_osc_gate /
         vector
          (vector 4 8 6 8 (true))
          (vector 6 8 8 8 (true))
          (vector 8 6 8 8 (true))
          (vector 10 6 8 6 (true))
          (vector 8 8 10 8 (false))
          (vector 12 6 10 6 (false))
          (vector 10 8 12 8 (false))
          (vector 12 8 12 6 (false))
          (vector 12 8 14 8 (false))
          (vector 14 8 16 8 (false)));

[hear] (define cos_osc_image /
         make-image 120 169 /
         vector
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:...................................................................:...............:...................................................................................:)
          (:..................................................................:::::::::::.....::...................................................................................:)
          (:.................................................................::::::::::::....::::::::::::..........................................................................:)
          (:..................................................................:::::::::::.....::...................................................................................:)
          (:...................................................................:...............:...........:.......................................................................:)
          (:..............................................................:::.............................:::......................................................................:)
          (:..............................................................:::............................:::::.....................................................................:)
          (:..............................................................:::..............................:.......................................................................:)
          (:..............................................................:::..............................:.......................................................................:)
          (:..............................................................:::..............................:.......................................................................:)
          (:..............................................................:::..............................:.......................................................................:)
          (:..............................................................:::..............................:.......................................................................:)
          (:..............................................................:::..............................:.......................................................................:)
          (:..............................................................:::..............................:.......................................................................:)
          (:.............................................................:::::.............................:.......................................................................:)
          (:..............................................................:::..............................:.......................................................................:)
          (:...........................................:...............:...:...........:...............:...............:...............:...........................................:)
          (:..................................:::::::::::.....:::::::::::..............::..............::..............::..............::..........................................:)
          (:..................................::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................................:)
          (:..................................:::::::::::.....:::::::::::..............::..............::..............::..............::..........................................:)
          (:...........................................:...............:...............:...............:...............:...............:...........................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::));

[hear] (equal (cos_osc_gate)
               (distill-circuit (cos_osc_image)));


       # GATE testing alternate primer based on gates: COS_SR circuit
       # This section contains one or more representations of a circuit
       # constructed using UNLESS gates. Needs elaboration...
       # graphic representation :
       #
[hear] (define cos_sr_gate /
         vector
          (vector 0 2 2 2 (true))
          (vector 0 8 2 8 (true))
          (vector 2 2 4 2 (true))
          (vector 2 8 4 8 (true))
          (vector 4 2 6 2 (true))
          (vector 4 6 6 6 (true))
          (vector 4 8 6 8 (true))
          (vector 6 8 6 6 (true))
          (vector 6 2 8 2 (true))
          (vector 6 6 8 6 (false))
          (vector 8 4 8 6 (false))
          (vector 8 2 10 2 (true))
          (vector 10 4 8 4 (false))
          (vector 8 6 10 6 (false))
          (vector 10 6 10 8 (false))
          (vector 10 2 12 2 (true))
          (vector 12 4 10 4 (false))
          (vector 10 6 12 6 (false))
          (vector 10 8 12 8 (false))
          (vector 12 6 12 4 (false))
          (vector 12 2 14 2 (true))
          (vector 14 4 12 4 (false))
          (vector 12 8 14 8 (false))
          (vector 14 2 14 4 (true))
          (vector 16 4 14 4 (true))
          (vector 14 8 16 8 (false))
          (vector 16 8 18 8 (false))
          (vector 18 8 20 8 (false)));

[hear] (define cos_sr_image /
         make-image 88 169 /
         vector
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:...........:...............:...............:...............:...............:...............:...............:...........................................................:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................:)
          (:..::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................................................:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................:)
          (:...........:...............:...............:...............:...............:...............:...............:...........................................................:)
          (:..............................................................................................................:::......................................................:)
          (:..............................................................................................................:::......................................................:)
          (:..............................................................................................................:::......................................................:)
          (:..............................................................................................................:::......................................................:)
          (:..............................................................................................................:::......................................................:)
          (:..............................................................................................................:::......................................................:)
          (:..............................................................................................................:::......................................................:)
          (:..............................................................................................................:::......................................................:)
          (:..............................................................................................................:::......................................................:)
          (:.............................................................................................................:::::.....................................................:)
          (:..............................................................................................................:::......................................................:)
          (:...................................................................:...............:...............:...........:...:...................................................:)
          (:..................................................................::..............::..............::..............:::::::::::..........................................:)
          (:.................................................................::::::::::::....::::::::::::....::::::::::::....::::::::::::..........................................:)
          (:..................................................................::..............::..............::..............:::::::::::..........................................:)
          (:...................................................................:...............:...........:...:...............:...................................................:)
          (:...............................................................:..............................:::......................................................................:)
          (:...............................................................:.............................:::::.....................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:...............................................................:...............................:.......................................................................:)
          (:.............................................................:::::.............................:.......................................................................:)
          (:..............................................................:::..............................:.......................................................................:)
          (:...........................................:...............:...:...........:...............:...........................................................................:)
          (:..................................:::::::::::..............::..............::..............::..........................................................................:)
          (:..................................::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................................................................:)
          (:..................................:::::::::::..............::..............::..............::..........................................................................:)
          (:...........................................:...:...........:...............:...............:...........................................................................:)
          (:..............................................:::..............................:.......................................................................................:)
          (:.............................................:::::.............................:.......................................................................................:)
          (:..............................................:::..............................:.......................................................................................:)
          (:..............................................:::..............................:.......................................................................................:)
          (:..............................................:::..............................:.......................................................................................:)
          (:..............................................:::..............................:.......................................................................................:)
          (:..............................................:::..............................:.......................................................................................:)
          (:..............................................:::..............................:.......................................................................................:)
          (:..............................................:::..............................:.......................................................................................:)
          (:..............................................:::............................:::::.....................................................................................:)
          (:..............................................:::.............................:::......................................................................................:)
          (:...........:...............:...............:...................................:...........:...............:...............:...............:...............:...........:)
          (:..:::::::::::.....:::::::::::.....:::::::::::..............................................::..............::..............::..............::..............::..........:)
          (:..::::::::::::....::::::::::::....::::::::::::....................................::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::.........:)
          (:..:::::::::::.....:::::::::::.....:::::::::::..............................................::..............::..............::..............::..............::..........:)
          (:...........:...............:...............:...............................................:...............:...............:...............:...............:...........:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::));

[hear] (equal (cos_sr_gate)
               (distill-circuit (cos_sr_image)));


       # GATE testing alternate primer based on gates: COS_D circuit
       # This section contains one or more representations of a circuit
       # constructed using UNLESS gates. Needs elaboration...
       # graphic representation :
       #
[hear] (define cos_d_gate /
         vector
          (vector 0 2 2 2 (true))
          (vector 0 6 2 6 (true))
          (vector 2 2 4 2 (true))
          (vector 2 6 4 6 (true))
          (vector 4 2 6 2 (true))
          (vector 4 6 6 6 (true))
          (vector 6 2 8 2 (true))
          (vector 6 6 8 6 (true))
          (vector 8 2 10 2 (true))
          (vector 8 6 10 6 (true))
          (vector 10 6 10 4 (true))
          (vector 10 10 10 8 (true))
          (vector 10 2 12 2 (true))
          (vector 10 4 12 4 (true))
          (vector 10 6 12 6 (true))
          (vector 10 8 12 8 (true))
          (vector 12 10 10 10 (true))
          (vector 12 0 12 2 (true))
          (vector 12 2 12 4 (false))
          (vector 12 6 12 8 (true))
          (vector 12 10 12 12 (true))
          (vector 12 4 14 4 (true))
          (vector 12 8 14 8 (false))
          (vector 14 10 12 10 (true))
          (vector 12 12 14 12 (true))
          (vector 14 0 14 2 (true))
          (vector 14 2 14 4 (true))
          (vector 14 4 14 6 (false))
          (vector 14 6 14 8 (false))
          (vector 14 8 14 10 (false))
          (vector 16 10 14 10 (true))
          (vector 14 12 16 12 (true))
          (vector 16 12 18 12 (true))
          (vector 18 12 20 12 (true)));

[hear] (define cos_d_image /
         make-image 109 169 /
         vector
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:.............................................................................................:::::...........:::::.....................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:...........:...............:...............:...............:...............:...............:...:...............:.......................................................:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................:)
          (:..::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................................................................:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................:)
          (:...........:...............:...............:...............:...............:...............:...........................................................................:)
          (:...............................................................................................:..............:::......................................................:)
          (:...............................................................................................:..............:::......................................................:)
          (:...............................................................................................:..............:::......................................................:)
          (:...............................................................................................:..............:::......................................................:)
          (:...............................................................................................:..............:::......................................................:)
          (:...............................................................................................:..............:::......................................................:)
          (:...............................................................................................:..............:::......................................................:)
          (:...............................................................................................:..............:::......................................................:)
          (:...............................................................................................:..............:::......................................................:)
          (:.............................................................................................:::::...........:::::.....................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:...........................................................................................:...:...........:...:.......................................................:)
          (:..................................................................................:::::::::::.....:::::::::::..........................................................:)
          (:..................................................................................::::::::::::....::::::::::::.........................................................:)
          (:..................................................................................:::::::::::.....:::::::::::..........................................................:)
          (:...............................................................................:...........:...............:...........................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:.............................................................................:::::.............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::............................:::::.....................................................:)
          (:..............................................................................:::.............................:::......................................................:)
          (:...........:...............:...............:...............:...............:...............:...................:.......................................................:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................:)
          (:..::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::....::::::::::::.........................................................................:)
          (:..:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........................................................................:)
          (:...........:...............:...............:...............:...............:...............:...........................................................................:)
          (:..............................................................................................:::..............:.......................................................:)
          (:..............................................................................................:::..............:.......................................................:)
          (:..............................................................................................:::..............:.......................................................:)
          (:..............................................................................................:::..............:.......................................................:)
          (:..............................................................................................:::..............:.......................................................:)
          (:..............................................................................................:::..............:.......................................................:)
          (:..............................................................................................:::..............:.......................................................:)
          (:..............................................................................................:::..............:.......................................................:)
          (:..............................................................................................:::..............:.......................................................:)
          (:.............................................................................................:::::...........:::::.....................................................:)
          (:..............................................................................................:::.............:::......................................................:)
          (:...........................................................................................:...:...........:...:.......................................................:)
          (:..................................................................................:::::::::::..............::..........................................................:)
          (:..................................................................................::::::::::::....::::::::::::.........................................................:)
          (:..................................................................................:::::::::::..............::..........................................................:)
          (:...............................................................................:...........:...............:...........................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:.............................................................................:::::.............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::..............................:.......................................................:)
          (:..............................................................................:::............................:::::.....................................................:)
          (:..............................................................................:::.............................:::......................................................:)
          (:...................................................................................:...............:...........:...:...................................................:)
          (:..................................................................................:::::::::::.....:::::::::::.....:::::::::::..........................................:)
          (:.................................................................................::::::::::::....::::::::::::....::::::::::::..........................................:)
          (:..................................................................................:::::::::::.....:::::::::::.....:::::::::::..........................................:)
          (:...................................................................................:...............:...............:...................................................:)
          (:..............................................................................................:::......................................................................:)
          (:..............................................................................................:::......................................................................:)
          (:..............................................................................................:::......................................................................:)
          (:..............................................................................................:::......................................................................:)
          (:..............................................................................................:::......................................................................:)
          (:..............................................................................................:::......................................................................:)
          (:..............................................................................................:::......................................................................:)
          (:..............................................................................................:::......................................................................:)
          (:..............................................................................................:::......................................................................:)
          (:.............................................................................................:::::.....................................................................:)
          (:..............................................................................................:::......................................................................:)
          (:...............................................................................................:...........:...............:...............:...............:...........:)
          (:..................................................................................................:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........:)
          (:..................................................................................................::::::::::::....::::::::::::....::::::::::::....::::::::::::.........:)
          (:..................................................................................................:::::::::::.....:::::::::::.....:::::::::::.....:::::::::::..........:)
          (:...........................................................................................................:...............:...............:...............:...........:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:.......................................................................................................................................................................:)
          (:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::));

[hear] (equal (cos_d_gate)
               (distill-circuit (cos_d_image)));


       # GATE probing networks of unless gates
[hear] (define set-input /
         ? circuit /
         ? index /
         ? value /
         assign wire (list-ref (circuit) (index))
         (map (? w (if (equal (w) (wire))
               (vector (list-ref (w) 0)
                   (list-ref (w) 1)
                   (list-ref (w) 2)
                   (list-ref (w) 3)
                   (value))
               (w)))
              (circuit)));

[hear] (define read-output /
         ? circuit /
         ? index /
         assign len (list-length / circuit) /
         assign wire (list-ref (circuit) / - (- (len) 1) (index)) /
         list-ref (wire) 4);

[hear] (define sim /
         ? circuit / ? steps / ? setter
         (if (> (steps) 0)
             (sim (simulate-unless (setter / circuit)) (- (steps) 1) (setter))
             (circuit)));

[hear] (define smart-sim /
         ? circuit /
         ? setter /
         sim (circuit) (list-length / circuit) (setter));

       # test cos_not gate
[hear] (define cos_not_harness /
         ? x /
         assign c (cos_not_gate) /
         assign c (smart-sim (c) (? c (set-input (c) 0 (x)))) /
         read-output (c) 0);

[hear] (= (false) / cos_not_harness / true);

[hear] (= (true) / cos_not_harness / false);

       # test cos_and gate
[hear] (define cos_and_harness /
         ? x / ? y /
         assign c (cos_and_gate) /
         assign c (smart-sim (c) (? c (set-input (set-input (c) 0 (x)) 1 (y)))) /
         read-output (c) 0);

[hear] (= (false) / cos_and_harness (false) (false));

[hear] (= (false) / cos_and_harness (false) (true));

[hear] (= (false) / cos_and_harness (true) (false));

[hear] (= (true) / cos_and_harness (true) (true));

       # this code is more awkward than it needs to be -
       # should make circuits mutable

       # NOTE end of part 3, start of part 4
       # The following parts of the message start
       # to introduce some self-reference into the message
[hear] (intro part4);


       # SELF a mechanism for referring to parts of the message
       # Many choices for how to do this.
       # Could do it without special machinery by using the
       # standard A-B trick for giving e.g. a Turing machine
       # access to its own description.
       # Instead, will simply introduce a "primer" function
       # that gives access to every statement made so far
       # (question: should future statements be included?
       # tentatively assume YES: will simplify
       # discussion of creating modified copies of the
       # complete message).
       # For now, assume primer is a list of statements,
       # with each statement being a list in the same
       # form as "translate" functions expect.
       # This means that there is, for now, no
       # distinction between unary or binary,
       # and the "/" structure is expanded.
[hear] (intro primer);

       # this line is referred to later - change/move carefully
[hear] (equal (list-ref (primer) 0) (vector intro 1));

[hear] (equal (list-ref (primer) 1) (vector intro 2));

[hear] (equal (list-ref (primer) 2) (vector intro 3));

[hear] (assign idx (list-find (primer) (vector intro primer) (? x 0))
           (equal (list-ref (primer) (+ (idx) 1))
                  (vector equal
                      (vector list-ref (vector primer) 0)
                      (vector vector intro 1))));

                      
       # Now, we could return to the MUD, simulate an agent A
       # transferring a copy of the primer to another agent B,
       # and then show B making a modified copy of that primer
       # and passing it back to A.
       # We could also show agents experimenting with the
       # primer in various ways.
       # Message is pretty solid up to this point.
       # For testing purposes, useful to save state here to disk,
       # command: DISK-SAVE base

       # JAVA some preparatory work for integrating with Java code
[hear] (class Object ()
              (method add-one (lambda (x) (+ (x) 1)))
              (method unknown (lambda (x) (x)))
              (method <init>-V (self))
              (method <init> (self))
              (method classname Object)
              (method equals-Object-Z (this ==))
              (method equals (self equals-Object-Z))
              (method act (true))
              (method isobj (true)));

[hear] (define java-object / Object);

[hear] (define act / ? x / true);

       #(class java-string ()
       #       (field super (java-object new))
       #       (method classname String)
       #       (method unknown (lambda (x) (super (x)))));
       # inconsistency of various kinds of equality throughout message
       # needs to be cleaned up
[hear] (class Integer ()
              (field super (java-object new))
              (field value (cell new 0))
              (method <init> (self))
              (method <init>-V (self))
              (method <init>-I-V (lambda (v)
                       (begin
                         (value set (v))
                         (self))))
              (method intValue-V (value get))
              (method intValue (self intValue-V))
              (method equals-Object-Z (lambda (o) (if (= (o classname) Integer)
                                  (= (value get) (o value get))
                                  (false))))
              (method equals (self equals-Object-Z))
              (method get (value get))
              (method set (lambda(x)
                    (value set
                       (if (number? / x)
                       (x)
                       (x intValue)))))
              (method classname Integer)
              (method unknown (lambda (x) (super (x)))));

       # string is basically the same as an integer
[hear] (class String ()
              (field super (java-object new))
              (field value (cell new 0))
              (method <init> (self))
              (method <init>-V (self))
              (method <init>-String-V (lambda (v)
                        (begin
                          (value set (v value get))
                          (self))))
              (method int-init (lambda (x)
                     (begin
                       (value set (x))
                       (self))))
              (method intValue-V (value get))
              (method intValue (self intValue-V))
              (method get (value get))
              (method set (lambda(x)
                    (value set
                       (if (number? / x)
                       (x)
                       (x intValue)))))
              (method equals-Object-Z (lambda (o) (if (= (o classname) String)
                                  (= (value get) (o value get))
                                  (false))))
              (method equals (self equals-Object-Z))
              (method classname String)
              (method unknown (lambda (x) (super (x)))));

       # will need to install class hierarchy, just hardcode a few things for now
[hear] (define java
         (? x / ? y /
            (cond ((= (y) String) (String))
              ((= (y) Object) (java-object))
              ((= (y) Integer) (Integer))
              (java-object))));

              
[hear] ((java util String) new isobj);

[hear] (= ((java util String) new add-one 15) 16);

[hear] (class java-numeric ()
              (field super (java-object new))
              (method unknown (lambda (x) (super (x))))
              (field java-content (cell new 0))
              (method get (java-content get))
              (method init (lambda (v)
                     (begin
                   (self set (v))
                   (self))))
              (method set (lambda (v) (java-content set (v)))));

         
[hear] (define byte (java-numeric));

[hear] (define char (java-numeric));

[hear] (define double (java-numeric));

[hear] (define float