Problem solving with layered design (domain specific small languages) in Prolog

Bowles and Wilks wrote

A programming methodology often used in AI, as well as in other fields, is stratified or layered design. When faced with a problem, rather than implementing a solution directly in some base language, a new, more appropriate, higher level language is designed in which to express a solution, and this is implemented in the base language. This methodology can have several iterations yielding as a final solution a hierarchy of different languages each more appropriate to expressing the solution than the language below.

This is something that is done quite often by Prolog and Lisp programmers. We've all read the same Prolog books so we've all seen an example of this in those expert system shells that chain backwards from goal to fact.

I thought I'd share another example, something of my own where the idea of creating a little language to solve the problem was quite obvious and very helpful.

In this case the domain is relation semantics used in the field of programming language design. The description with code can be found here:

http://barrywatson.se/lsi/lsi_relational_semantics.html

The thing to know is that in my prolog I can configure the reader so that any sequence of three or more 0'- characters is read as '---'. This is done with the flag 'collapse_multiple_minuses' which your prolog will most likely not have.

The notation used in the description of the semantics may be new to you, it is described here: http://barrywatson.se/lsi/lsi_inductively_defined_subsets.html

Feel free to ask any questions.

Feel free to show me any little languages you've designed yourself as this is an interest of mine.

Posted Mar 24, 2015, 6:22 AM UTC

35
walrusesarecool•

I dont really understand this idea of domain specific languages.. what would be a really simple example?

2
logic_programmerOP•

I dont really understand this idea of domain specific languages..

It's about reducing the gap between problem and implementation. In the words of Forbus and DeKleer:

An inevitable tension in designing problem solvers comes from the gap between the constructs in which one prefers to think and the primitives supplied by the language in which the system is to be implemented. ... One way to narrow that gap is to design domain-specific representation (or programming) languages that capture exactly the right distinctions needed to solve particular classes of problems.

.

what would be a really simple example?

The one I linked to was a very simple one, probably the simplest I've ever seen.

2
abathologist•

This is great! I really enjoyed looking it over. Thanking for sharing it here. Also, I think your website is very nice in general. This passage struck me as a beautiful sentiment:

Back in 1929 the logician S. K. Langer wrote that "every universe of discourse has its logical structure". These words have become part of my personal credo. Whenever I'm facing a problem that seems overwhelming I just remember that there's a logical structure to be found, and if I can just find it, then I'll be on my way towards a solution.

Do I understand correctly that, in this code, you first specify and define the DSL for describing relational semantics, and then proceed to implement IMP? So you're exhibiting here two DSLs (if IMP qualifies as such)? That certainly illustrates the layered design.

Are there any other unique properties of your Prolog worth mentioning, in addition to the collapse_multiple_minuses pre-compilation?

I have had some little ideas about DSLs, but have never had the proper follow through. The closes I've come is some little syntax extensions. But this is quite inspiring!

1
logic_programmerOP•

Do I understand correctly that, in this code, you first specify and define the DSL for describing relational semantics, and then proceed to implement IMP?

Yes.

So you're exhibiting here two DSLs (if IMP qualifies as such)?

Yes and no. IMP is just a simple IMPerative language used to teach operational semantics. It's not used for anything other than executing test code.

Are there any other unique properties of your Prolog worth mentioning, in addition to the collapse_multiple_minuses

Not really. I do have arbitrary precision floats. I actually added the changes to the reader for collapse_multiple_minuses just for the example given - one of the advantages fo rolling your own implementation.

2
logic_programmerOP•

Just for interest....

This passage struck me as a beautiful sentiment:

I first came across the quote (and inspiration for the DSL) from the following paper:

http://www.kurims.kyoto-u.ac.jp/~kyodo/kokyuroku/contents/pdf/0709-09.pdf

It's a nice read about a proof assistant system developed on the Japanese 5th generation Prolog machines. I also implement the BUP (Bottom-Up Parser) mentioned in the paper in my Prolog system. It comes as a library.

The quote is from http://monist.oxfordjournals.org/content/39/4/561

2