Click here to Skip to main content
Click here to Skip to main content

A Natural Programming Method - Programming with Natural Language

, 30 Oct 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
The method doesn't replace existing Computer Language. Natural Language and Computer Language have to cooperate with each other.

Contents

Introduction

Are English and C different languages? Yes, the former is a natural language, and the latter is a computer language. Do they share something in common? Yes, they do, they are both logical languages. Since they are both logical languages, why don’t we just write English text to run a computer? Why do we program a computer with a computer language rather than a natural language? In the following text, we will be using natural language to program. When we refer to characters, we mean Hanji, aka Kanji in Japanese or Hanja in Korean, and Kana, Hangul. Characters are romanized when present in following text. One cell of a lookup table holds a definition for the table key. A definition is a language element such as a statement, a declaration, and so on.

Below is a dictionary for terms we use in the following paragraphs.

Term Meaning
Hangul Syllabic blocks of letters as Korean characters
Hanja Chinese characters
Hanji Chinese characters
Japanese language A language using Kanji, Hirakana, and Katakana
Kana Syllabic Japanese characters known as Hirakana and Katakana
Kanji Chinese characters
Korean language A language using Hangul and Hanja
Taiwanese language A language using Hanji and Latin alphabet

Below is another dictionary for the characters we use in the following paragraphs.

Character Meaning
ÌN v. to print
LIO̍K v. to record
n. records
LIO̍K ÌN v. to record the voice and then print it out
SOK v. to speed up the printer
SOK LIO̍K ÌN v. to speed up the printer, record the text, and print it out
SOK TSUÁN ÌN v. to speed up the printer, transfer the text, and print it out
TSUÁN v. to transfer
n. transfers
TSUÁN ÌN v. to transfer the text and print it out

Programming with Natural Language

Let’s have a look at the example code before diving into the details. Below is a lookup table:

LIO̍K: def record end
  ÌN: puts "a short example"; puts "This is a longer example."

The table has two Hanji table keys. For one of the table keys LIO̍K, which means "to record", it has one definition "def exemplify end." For the other table key ÌN, which means "to print", it has two definitions. One is puts "a short example," the other is puts "This is a longer example." They are separated by a semicolon. We can then compose LIO̍K and ÌN to form a phrase "LIO̍K ÌN." Any two given characters can be combined together and remain syntactically correct. No declension or conjugation is required. The pronunciation does change but we don’t cover this topic here. Below is the combined phrase and its correspondent code snippet:

//
// LIO̍K ÌN 
//
def record
  puts "This is a longer example"
end

Why is only one definition of ÌN is combined with LIO̍K’s definition? The selection of one of the definitions is regulated by a lookup rule as shown below:

LIO̍K.ÌN:0.2

The rule says, when ÌN is appended after LIO̍K, the 2nd definition of ÌN is selected and 0 means "don’t care" for LIO̍K. And why is ÌN nested in LIO̍K? Because the combination rule for LIO̍K and ÌN is as following:

LIO̍K > ÌN

A Greater Than symbol can mean the 1st character nest the 2nd one.

The English phrase "wise up" means "become aware of or informed about something," according to Oxford Dictionaries. It doesn't mean "become wise and go up." It changes the original meaning of "wise" and "up" to some extend and comes out with a new meaning. When any two characters form a phrase, the latter also somewhat changes the original meaning of the former. We would otherwise use a series of C macros to program the code if the meaning of a phrase is just a simple accumulation of the meaning of the individual word or character.

The Ambiguity

A character can have more than one definition when it is composed in a sentence. The same rule holds true for an English word. The above code snippets are an unambiguous example. Here is an ambiguous English example:

The house can be bought for 10 dollars or 1 million dollars.

The subject "the house" has 2 meanings. For 10 dollars, it means a toy house. For 1 million dollars, it means a concrete house. The lookup table for this English sentence is shown as below:

        The house: acquire(theToyHouse); acquire(theConcreteHouse); 
       10 dollars: pay(10);
1 million dollars: pay(1000000); 
can be bought for: void buy() {}

The lookup rule is as following:

       The house.10 dollars:1.0 
The house.1 million dollars:2.0

The first definition of "The house" will be selected for 10 dollars. The second definition of "The house" will be selected for 1 million dollars. We would then have 2 meanings for this sentence. One is "The toy house can be bought for 10 dollars." The other is "The concrete house can be bought for 1 million dollars." The combination rules is as following:

can be bought for >

"can be bought for" will nest all other statements, which is presented by a Greater-Than symbol. There are no combination rules set for others words in the sentence, so sequential combination is assumed for the other 4 statements:

void buy() {
  acquire(theToyHouse); 
  pay(10); 
  acquire(theConcreteHouse); 
  pay(1000000);
}

Not Polymorphic

Polymorphism is a feature of object-oriented programming. Polymorphism allows an object of a derived class to be passed to a polymorphic function which accepts a reference to its base class. The is-a or kind-of relationship is guaranteed through inheritance of class. Thus we say the derived class is substitutable for base class.

The method has nothing to do with polymorphism. It does compose polymorphic code, but it is not involved in dynamic binding. When program code is composed, we are also composing a natural phrase or sentence in parallel. We may as well use the literal meaning of is-a or kind-of relationship to refer to the polysemy or monosemy of a word or character. The relationship is then obviously not guaranteed through inheritance of class, but through polysemy or monosemy of a word or character.

Another Perspective of Code

The method is not intended to replace computer language. In fact, it provides a new perspective of source code.

When we write program in a traditional way, firstly a thought or idea is formed in our brain. We then break down the thought or idea into logical units. The process of breaking down the thought or idea is mostly conducted by induction and deduction. The logical units, such as function calls, variables, or class declarations, are constructed and organized into a working program. Then this well composed program code is ready to be parsed, interpreted or compiled, and then executed.

From another perspective of source code, we can break it up into statements. Each statement in a program code is composed in such a way that it forms a nested or sequential combination with previous or next statement. Given that, each statement can be represented by a word or character. We try to make it easier to understand by assigning only one word or character to each statement, a statement could be represented by a couple of words or characters though. We can then have a sequence of words or characters, which should be in itself a phrase or sentence of natural language.

Not a Mere Translation

A computer language is made of a fixed set of keywords and punctuations. Furthermore, the keywords of main stream computer languages are all English words. There have been efforts made to develop non-English-based computer languages. They are, however, not widely used all over the world. The non-English-based computer languages have non-English keywords. They are suitable for those programmers whose mother tongues are not English.

Do English-based and non-English-based computer languages have something in common? Yes, they are all logical languages. A few of them are mere translations from existing computer languages. By translation, I mean the keywords are translated from English- based computer languages.

The method is not a mere translation from any computer languages. As the title suggests, a programming method has to be operable. We can operate the method to write computer programs. In other words, we write computer programs via this method.

The method is also not a Text Entry Method/Input Method. A text entry method is just a software tool we use to generate a sequence of words or characters by typing on keyboard. The generated sequence of words or characters will then be used by the method. The method can sure co-work with non-English-based computer languages.

Reducing Barriers to Entry

The method is a step further from computer language to human beings. It intermediates between computer language and programmers. Since it intermediates, there has to be a mechanism for it. The words or phrases we type in natural language will be mapped to certain statements in computer language via this mechanism. Since we type in natural language, we understand the source code through natural lexicon and sentences. Since we can understand computer language through natural language, we definitely reduce the entry barriers to computer languages.

When we assign a word or character to a statement, the word or character itself can be regarded as a comment to the statement. It may not be an elaborate one, but it could just co-exist with source code comments. They can refer to each other.

Adaptive Design

When we assign a set of statements to a word or character, what we are actually doing is providing options to it. One of the provided options will be selected according to the context of a natural language phrase or sentence. In other words, the action of selecting means adapting to the context. If the context changes, the selection changes. When code can be adaptive to the context, it can be more flexible and responsive to the context.

We can view source code as 2 parts in terms of adaptation, one part is adaptive and the other is unadaptive. Where does adaptation come from? A software program may be fitted into a variety of hardware. A software program may be even used by different users under different conditions. An adaptive software program can be responsive to the context with the support of its unadaptive program core. The adaptive part of software program makes the unadaptive part flexible and responsive, and the unadaptive part of software program provides core functionality to the adaptive part and support it. Let’s add a new entry TSUÁN to the lookup table:

 LIO̍K: def record end
TSUÁN: def transfer end
   ÌN: puts "a short example"; puts "This is a longer example."

Add a lookup rule like this:

TSUÁN.ÌN:0.1

And add a combination rule like this:

TSUÁN > ÌN

We can hence get the code snippet by writing a phrase TSUÁN ÌN:

//
// TSUÁN ÌN
//
def transfer
  puts "a short example"
end

It is obvious that we switch the meaning of ÌN by replacing LIO̍K with TSUÁN, so that ÌN is adaptive and responsive to the changing context of ÌN. We can also add unadaptive code to the snippet. Provided the character SOK means to speed up a virtual printer for printing text, we can add a new entry SOK to the lookup table:

  SOK: def speedUpPrinter end
 LIO̍K: def record end
TSUÁN: def transfer end
   ÌN: puts "a short example"; puts "This is a longer example."

And add 2 entries of combination rules:

SOK <> LIO̍K
SOK <> TSUÁN

The notation Less-Than followed by a Greater-Than means that SOK and LIO̍K forms a sequential combination. In other words, SOK does not nest LIO̍K and SOK is not nested by LIO̍K. The same rule applies to SOK and TSUÁN. In practice, we can usually assume sequential combination for any two adjacent characters and omit the combination rules for them.

Hence for the 2 phrases SOK LIO̍K ÌN and SOK TSUÁN ÌN, we have their relative code snippets:

//
// SOK LIO̍K ÌN
//
def speedUpPrinter
end

def record
  puts "This is a longer example."
end

and:

//
// SOK TSUÁN ÌN
//
def speedUpPrinter
end

def transfer
  puts "a short example" 
end

SOK is therefore the unadaptive part of the code.

Co-working with Existing Computer Languages

The method doesn’t replace existing computer language. Natural language will be co- working with computer language. The operations applied on natural languages will be reflected on the combinations of statements of computer languages. The following are examples for the method to co-work with different programming languages, such as the lookup table for Python:

  SOK: def speedUpPrinter():
 LIO̍K: def record(): 
TSUÁN: def transfer():
   ÌN: print 'python code'; print 'longer python code'

The Python code snippet for the phrase SOK TSUÁN ÌN:

//
// SOK TSUÁN ÌN
//
def speedUpPrinter():

def transfer():
  print 'python code'

The lookup table for C:

  SOK: void speedUpPrinter(){}
 LIO̍K: void record(){}
TSUÁN: void transfer(){}
   ÌN: printf("c code\n"); printf("longer c code\n");

The C code snippet for the phrase SOK TSUÁN ÌN:

//
// SOK TSUÁN ÌN
//
void speedUpPrinter(){
}

void transfer(){
  printf("c code");
}

The lookup table for Swift:

  SOK: func speedUpPrinter() {}
 LIO̍K: func record() {}
TSUÁN: func transfer() {}
   ÌN: println("swift code"); println("longer swift code")

The Swift code snippet for the phrase SOK TSUÁN ÌN:

//
// SOK TSUÁN ÌN
//
func speedUpPrinter(){
}

func transfer(){
  println("swift code") 
}

The lookup table for XML Markup languages:

  SOK: <printer>speeding up</printer>
 LIO̍K: <records> </records>
TSUÁN: <transfers> </transfers>
   ÌN: print xml text; print longer xml text

The XML code snippet for the phrase SOK TSUÁN ÌN:

//
// SOK TSUÁN ÌN
//
<printer>
  speeding up
</printer>
<transfer>
  xml text
</transfer>

Not a Programming Paradigm

The method can be applied to existing programming paradigms, being it functional, procedural, or object-oriented. Given a code snippet, it can be parsed into 2 types of combination. One type is nested combination, and the other type is sequential combination. In the example of C language, a for-loop can enclose a couple of statements. We can say for-loop is the nesting statement and the enclosed statements are the nested statements. In the example of a function definition, the function name and its return type are the nesting statement and the function body is the nested statement. The next one is sequential combination. When 2 statements are aligned with the same indentation, we say they form a sequential combination. For example, when 2 function calls are aligned, we say they are sequential.

The method is not a new programming paradigm. In other words, a programming method is not a programming paradigm. The method can also be applied to any newly invented paradigms. The lookup table for Object-oriented paradigm:

  SOK: void speedUpPrinter() {}
 LIO̍K: void record (PrintingObject prnObj) {}
TSUÁN: void transfer (PrintingObject prnObj) {}
   ÌN: prnObj.Print(); prnObj.PrintLonger();

The Object-Oriented code snippet for the phrase SOK TSUÁN ÌN:

//
// SOK TSUÁN ÌN
//
void speedUpPrinter(){
}

void transfer(PrintingObject prnObj){
  prnObj.Print();
}

The lookup table for Scripting paradigm:

  SOK: function speedUpPrinter(){}
 LIO̍K: function record(){} 
TSUÁN: function transfer(){}
   ÌN: alert('javascript code'); alert('longer javascript code')

The Scripting code snippet for the phrase SOK TSUÁN ÌN:

//
// SOK TSUÁN ÌN 
//
void speedUpPrinter(){
}

void transfer(){
  alert(‘javascript code); 
}

The lookup table for Functional paradigm:

  SOK: (defun speedupPrinter)
 LIO̍K: (defun record ()) 
TSUÁN: (defun transfer())
   ÌN: (format t "lisp code"); (format t "longer lisp code")

The Functional code snippet for the phrase SOK TSUÁN ÌN:

//
// SOK TSUÁN ÌN 
//
(defun speedupprinter)
(defun transfer()
  (format t "lisp code") 
)

The lookup table for Declarative paradigm:

  SOK: #speedingUpThePrinter {transition-duration: 1s;}
 LIO̍K: #records {} 
TSUÁN: #transfers {}
   ÌN: background-color: #ffffff; background-color: #000000

The Declarative code snippet for the phrase SOK TSUÁN ÌN:

//
// SOK TSUÁN ÌN 
//
#speedingUpThePrinter {
  transition-duration: 1s;
}

#transfers {
  background-color: #ffffff 
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tsun-Huai Soo

Taiwan Taiwan
No Biography provided
Follow on   LinkedIn

Comments and Discussions

 
QuestionNice! PinprofessionalVolynsky Alex30-Oct-14 10:05 
AnswerRe: Nice! PinmemberTsun-Huai Soo30-Oct-14 15:58 
GeneralRe: Nice! PinprofessionalVolynsky Alex30-Oct-14 18:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 30 Oct 2014
Article Copyright 2014 by Tsun-Huai Soo
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid