Go Language: All Gates Boolean Grammar

Posted
Comments None

The best strategy to generate the 6 Boolean Grammars of GeneXproTools (The All Gates Grammar, the NOT-AND-OR Grammar, the NAND Grammar, the NOR Grammar, the MUX Grammar, and the NOT-AND-XOR Grammar or Reed-Muller Grammar) for any programming language is to choose as template the built-in programming language that is closest to the language we want to add.

Perhaps the most important consideration in this phase is the comparison of code structures in both the template and the new language, especially the use of function prototypes and the placement of helper or auxiliary functions.

Additionally, the number and type of built-in logical functions in both the template and the new language also plays an important role, particularly the XOR function which in some languages (for example, in Matlab, Octave and R) is not implemented as a logical operator (the XOR function is essential for creating the Reed-Muller Grammar, which is a universal logical system using NOT, AND, and XOR).

And since the Go language also has a Boolean XOR operator with the syntax "x0 != x1" (see the previous post "Go Language: Boolean Xor Operator"), we can use any of the C languages (C, C++ or C#) as our starting point. As an example, I'm going to use C# because, like Go, it doesn't use function prototypes, which simplifies greatly the clean-up of all the new Boolean grammars.

So now that we've made our choice for the template, we just have to open all the C# Boolean Grammars of GeneXproTools and save them as Go.Boolean.01.AllGates.grm.xml, Go.Boolean.02.NotAndOrGates.grm.xml, Go.Boolean.03.NandGates.grm.xml, and so on.

And now we start doing the required transformations (useful tip: opening all the new Boolean grammars in Visual Studio is a good choice as it allows to make the changes at once in all open documents). I won't be going here over all the details, but the changes you'll have to make are pretty straightforward as all the blocks in the xml code are well documented and you just have to make the appropriate changes to implement your new programming language in GeneXproTools. For example, straight on top of the xml files you have to change the language name and file extension to "Go" and "go".

Other common changes to all 6 Boolean grammars are the keywords that determine which words show in blue in the Model Panel. So for Go we have "package", "func", "const", "return", "true", and "false".

Then you also have to make the adjustments to the Opening and Closing Statements, to the Default Header and the formatting of the Random Constants and Label Constants. For example, in Go we have "package gepModel{CRLF}{CRLF}" as the opening statement; as the header we have "{CRLF}{CRLF}func gepModel(d []bool) bool {"; the random constants as formatted as "{TAB}const {labelname} = {labelindex}{CRLF}"; and so on.

But anyway, the idea here is to make the appropriate changes in the template and then test them in the GeneXproTools environment in order to see if anything else needs to be done.

But going back to our Go All Gates Grammar and its basic components.

The first basic component is the list of all logic functions. There are 258 of them, most of which are functions of 3 and 4 arguments. And since most programming languages only implement the basic Boolean functions (NOT, AND, and OR) plus XOR, the changes we need to make are very few. For example, for the Go language we just have to change the definition of the XOR function by replacing "^"with "!=".

Next come the Helper Functions, which are 245 in total. You'll notice that they are all defined using just the Boolean operators NOT, AND, and OR, and so they can be used by any language that implements these logical operators. This is very handy for implementing all kinds of Custom Programming Languages (well, besides the functional languages such as Lisp, I don't know of any language that implements the basic AND and OR operators as "And(x,y)" or "Or(x,y)" but I wouldn't be surprised if they existed) since they all implement the Boolean operators AND and OR similarly (only the names and symbols change from language to language).

So, for example, for the Go language we don't have to make any changes in the definitions of the Helper Functions as both C# and Go use "&&", "||", and "!" for AND, OR, and NOT, respectively. We need, however, to take care of the function headers which are different in both languages (this is the kind of change that is easily done in all Boolean grammars at once, as we only have 4 different headers that must be replaced: one for the functions of 1 argument, another for the functions of 2 and also 3 and 4 arguments).

And that's all for the All Gates Grammar for the Go language!

So let's play a little with this new grammar and show some code generated with it.

For example, this simple Go code is a minimal logic circuit for the 5-Majority function created using just the basic Boolean functions NOT, AND, and OR:

package gepModel

func gepModel(d []bool) bool {
    y := false

    y = ((d[2] && (d[1] || d[3])) && (d[0] || d[4]))
    y = y || (d[4] && (d[1] && d[3]))
    y = y || ((d[2] || d[0]) && ((d[1] || d[4]) && d[3]))
    y = y || ((d[2] || d[1]) && (d[0] && d[4]))

    return y
}

But the beauty of GeneXproTools is that it lets you play with all kinds of logic functions! For example, the code below is another minimal logic circuit for the 5-Majority function designed with NOT, AND, OR, NAND, NOR, XOR, and NXOR just to show you the use of helper functions in the code:

package gepModel

func gepModel(d []bool) bool {
    y := false

    y = (d[4] && (d[1] && d[0]))
    y = y || (gepNor(gepNor(d[2],d[3]),gepNor(d[0],d[1])) && d[4])
    y = y || (gepNor((!((d[4] != d[1]))),gepNor(d[2],d[0])) && d[3])
    y = y || (d[0] && (!(gepNand((d[3] != d[1]),d[2]))))

    return y
}

func gepNand(x, y bool) bool {
    return (!(x && y))
}

func gepNor(x, y bool) bool {
    return (!(x || y))
}

And we can choose even wilder architectures such as a universal logical system based on the 3-multiplexer function (this is also for the 5-Majority function):

package gepModel

func gepModel(d []bool) bool {
    y := false

    y = gepMux(gepMux(gepMux(d[0],d[3],d[1]),gepMux(d[3],d[4],d[2]),
      gepMux(d[2],d[3],d[1])),d[4], gepMux(gepMux(d[2],d[4],d[1]),d[0],
      gepMux(d[3],d[2],d[1])))

    return y
}

func gepMux(x, y, z bool) bool {
    return (((!(x)) && y) || (x && z))
}

In the next post I'll show you how to generate and use the Boolean universal system of NOT, AND, and OR for the Go language.

Author

Comments

There are currently no comments on this article.

Comment

your_ip_is_blacklisted_by sbl.spamhaus.org

← Older Newer →