views:

817

answers:

9

I am looking at writing a compiler and after I complete something in a "C" style I am looking at adapting it to other models. What are some syntactical constructs you would expect to see in a "natural" programming language?

The target platform for this compiler will be the CLR and I am currently using Oslo+MGrammar for the lexer/parser (as you can probably tell this is really just an excuse to play)

One of the goals of my project would be to allow programming to feel more like a conversation than structured syntax and demands.

Guess I should extend this out a little. One of the ideas I am working with is having a class declaration read like a paragraph.

    A Dog is a mammal.  It may Bark and Run.  To Run it
uses its feet to move forward. It does Lay.

...would translate too...

public class Dog : Mammal{

    public Feet Feet { get; set;}

    public virtual void Bark() {}
    public virtual void Run() {
     this.Feet.MoveForward();
    }
    public void Lay(){}
}
+6  A: 

Really, I don't think it is a good idea to make code more like natural language. It will become overly verbose. There's a reason why nobody uses COBOL very few people willingly uses COBOL :)

It might be a better idea to come up with a more standard way of pronouncing code out loud (it would help a lot if you're trying to explain code to somebody but can't show it to them).

Zifre
Again this is mainly for educational purposes. And in the future I believe that in the future the barrier to entry for development will be reduced by natural languages. These will most probably not replace standard structured languages (you are working with a digital state machine after all) but might work great for DSLs
Matthew Whited
Natural language is uncool. If I tell my computer to "do the dishes". I don't want it to watch big-bang theory instead.
Aiden Bell
nice save with changing "friends" to "big-bang theory"
Matthew Whited
The other half would slap me if she saw "friends" (she is a coder too)
Aiden Bell
"There's a reason why nobody uses COBOL." - Nobody uses COBOL? What? I guess you don't work in the financial and government sectors.
David Brown
If your thinking of DSLs (like 20.minutes.ago in Ruby), that might be okay, it's called a fluent interface. But I don't think it would lower the barrier to entry at all (which might be a bad thing too, with all the lousy developers out there raised on VB6).
Zifre
I can't say anything in absolute terms. Thats why we have maths. and programming languages.
Aiden Bell
+1 for a language you could read aloud, whether "natural" or not.
Dour High Arch
SQL is another failed attempt at making computing available to rank and file business managers. The basics start out innocent enough, but the sheer complexity of business data can even baffle the savviest of modelers/DBAs/programmers.
Wayne Hartman
Before you knock VB6 you should realize that its the developer that makes the language and not the language that makes the developer. There is plenty of crap code in any language.
Matthew Whited
+ 1 for COBOL bashing! My father works in JCL and COBOL, and I'm sure if it were somehow more profitable for him to pick up a more modern language he would.
TheMissingLINQ
+1  A: 

Look into fluent interfaces.

Joel Coehoorn
That's a design pattern not a language syntax.
Matthew Whited
@Matthew And since you said this is an educational exercise, the ideas behind fluent interfaces should help in crafting an approach to how and why certain aspects of interacting with a computer are more or less natural.
Rex M
Fluent interfaces would have nothing to do with writing a language. It is a way to use a language. I know what fluent interfaces are and I have used them. They are compelling but it doesn’t really like a sentence.
Matthew Whited
+2  A: 

this sentence is false

I like maths and programming because they are fairly absolute.

Human language communicates emotion and ambiguity. That's why I always end up arguing with people. If you don't want your computer to argue with you, and your compiler to make abstract random presumptions (like women :P ) then stick with logical languages I recon.

I imagine translating natural language into concrete semantics is much harder that extracting vague meaning from a sentence through NLP. Short of sentences like:

call method on sigkill then quit program

Which is pretty much what SQL-like in it's structure.

You could go for some form of self-defining, axiomatic-like semantic setup (like Self or similar) with some lambda calculus affinity. edit: Sounds like Lisp. I retract that statement .. ())((()))()()

Two examples from related question are LOLCODE and Inform7 ... both of which I would hate to program in.

Aiden Bell
Never know... might be fun... unless he's HAL
Matthew Whited
me: create an object that inherits the functionality of foo.computer: all or some?me:allcomputer: The interfaces too?... and on..and on..
Aiden Bell
That is why computers would obviously choose to speak Lojban. :)
Zifre
+1, and +1 for (like women), and +1 for LOLCODE :D
hasen j
"Fairly absolute"? Is that like "mostly dead"?
Ken
Mostly dead == dead with technical ambiguity. Like brain deadness. So yes :)
Aiden Bell
+2  A: 

There is one form of English I know that attempts to be as rigorous as a programming language (and fails): legalese. An attorney is trained to write unambiguously in English, or something vaguely resembling English. (Lots of Latin, some magic phrases, etc.)

If you want a general-purpose programming language, it isn't going to resemble natural language. Natural language is ambiguous, and relies on highly intelligent processing and, in many cases, a question-and-answer exercise to remove remaining ambiguity. Well, to attempt to remove it; in many cases, if person A says something to person B, A's opinion of what B understood is not the same as B's understanding.

This is not really a problem with technology, but rather with the nature of natural vs. artificial languages.

Of course, you could try implementing some form of ambiguity in your language, but I don't think it'd be useful. It might be fun to play with, though, and that's your goal.

David Thornley
General-purpose programming languages already *do* resemble natural language. They just generally restrict it to control flow ("for each object in my list, do ...") and user definitions (fluent interfaces), though. It sounds like he's simply moving it to data structure definitions. That could be a great idea, or a horrible one, but it's the exact *opposite* of legalese: natural language makes the potentially cryptic parts of programming easier, while legalese makes the easy parts of English more cryptic.
Ken
+6  A: 

If you want your design to be informed by something that has gone to an extreme in the direction of "naturalness", Graham Nelson has done some really stunning new work on a domain-specific programming language that is based on natural language. The system is called Inform 7 and in my opinion will amply repay analysis.

One issue with Inform 7 is that the presentation is totally geared toward non-programmers. It's not easy to discover even what the syntax is! But I'm quite impressed with the results, and I believe there are some novel binding constructs that offer genuinely new ideas---it's not just old stuff in a very attractive package. Well worth checking out!

Norman Ramsey
Inform 7 link is broken. Apparently should point to http://inform7.com/
notJim
A: 

From what I've seen recently, the most "natural" programming languages can be constructed for specific domains. Its when you get into general use programming languages that things have to become more general and as a result they read less naturally. I would consider CSS to be a pretty natural way of programming which is why non-programmers can pick it up and apply it pretty well. I think you will have a difficult time making a general use language more natural to read than some of the well composed Ruby code I've seen. On the other hand, if you are dealing with a specific domain, you can do some amazing things when you only have to handle a limited vocabulary.

jarrett
+1  A: 

Your statement:

A Dog is a mammal.  
It may Bark and Run.  
To Run it uses its feet to move forward. 
It does Lay.

Doesn't sound like a real natural language, but a form of controlled language.

Two examples which have machine comprehensible semantics are Attempto Controlled English which maps to conceptual graphs, and Gellish which is used as a data modelling language.

I can't think of a direct translation of your statements about 'A Dog', as the first statement appears to be talking about the sub-type of mammals which are dogs, but then you start talking about a single instance; you'd need to be a bit more rigorous to use existing controlled languages, something like.

Every dog is a mammal.
Every dog may bark, or run.
To run is a forward movement.
Every dog uses its feet to run.
Every dog does lay.

( though this loses any idea of from time to time about the laying )


What would "uses" imply?

What did "uses" imply in your example?

And I was describing the instance methods of the object “Dog”. I never said anything like “Spot is a Dog. Spot starts to run.”

Is "A Dog" an object, or were you referring to the class of all dogs? You appeared to be referring to all dogs, and most controlled languages require that distinction. "A Dog is a mammal" vs "A Dog is in the garden". It's called "the elephant problem" in nlp books.

To me this would create the instance and tell the instance to run.

I don’t know how I would describe a static method for a dog.

There's no such thing as a 'static method' in natural language, so why would you expect to be able to describe such a thing in something derived from a natural language?

You can scope define a relation which has a scope "If the topic of discussion is Dogs, then bark is a verb", but there isn't much call for defining the scope of a relation ( a static method is just a function with its scope defined within a class ); normally its not ambiguous.

Pete Kirkham
What would "uses" imply? And I was describing the instance methods of the object “Dog”. I never said anything like “Spot is a Dog. Spot starts to run.” To me this would create the instance and tell the instance to run. I don’t know how I would describe a static method for a dog. And I’m not sure how that would work in a natural form anyway.
Matthew Whited
+1  A: 

I'll second the suggestion to take a look at Inform 7. Over the past month or so I have been writing some interactive fiction with Inform7 and it really is quite an impressive language. I find it to be a refreshing departure from the languages I code in normally, because I still am creating something, yet describing it in a much more natural way.

For instance, this would be a perfectly valid (albeit short) program in Inform7:

The Building Lobby is a room.  "You are standing in the lobby of a building."
There is a supporter called desk here.  "A large reception desk is to your right."  
There is a device called lamp on the desk which is switched off.  
There is a container called trash can on the floor next to the desk. 
There is a thing called crumpled paper in the trash can.
Colin Cochrane
wow... that reminds me of text adventure games... look left... pick up box... open box... look in box...
Matthew Whited
A: 

Take a look at Rebol Parsing feature. It can define DSL based on a rule. You could meet your expectations easily with minimal code

Sudhakar Kalmari