Live data from Hacker News

The Power of Prolog

metalevel.at

151–160 of 164 posts

Re: The Power of Prolog

#151

Earlier quoted context omitted.

I've worked a bit on it. My solution is not incredible or anything, probably there are far more efficient and/or elegant solutions out there. Mine is not done yet but I'm going to continue with this one later and by then the activity in this thread will be over so I leave this link here now. https://github.com/eriknstr/puzzles/blob/master/zebra/soluti...

Fun problem. My solution doesn't do really any "logic programming", and it takes around 2 minutes to run on my laptop. https://gist.github.com/philsnow/02e747241d46106b14b60146e58... The middle huge nested thing (where `some_possible_worlds` is defined) is gross but I'm not great with ruby and I couldn't think of another way to not blow up memory enumerating all the worlds just to eliminate 99% of them.

I worked up this from the SEND MORE MONEY example in the CLP(FD) repo.

https://gist.github.com/calroc/603ed919bc814ccee10c1b3df6142...

    There are five houses.
    The Englishman lives in the red house.
    The Spaniard owns the dog.
    Coffee is drunk in the green house.
    The Ukrainian drinks tea.
    The green house is immediately to the right of the ivory house.
    The Old Gold smoker owns snails.
    Kools are smoked in the yellow house.
    Milk is drunk in the middle house.
    The Norwegian lives in the first house.
    The man who smokes Chesterfields lives in the house next to the man with the fox.
    Kools are smoked in the house next to the house where the horse is kept.
    The Lucky Strike smoker drinks orange juice.
    The Japanese smokes Parliaments.
    The Norwegian lives next to the blue house.
Now, who drinks water? Who owns the zebra?

In the interest of clarity, it must be added that each of the five houses is painted a different color, and their inhabitants are of different national extractions, own different pets, drink different beverages and smoke different brands of American cigarets [sic]. One other thing: in statement 6, right means your right.

    :- use_module(library(clpfd)).

    house_next_to(H0, H1) :- abs(H0 - H1) #= 1.

    puzzle([
		GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse,
		Englishman, Spaniard, Ukrainian, Norwegian, Japanese,
		Dog, Snails, Fox, Horse, Zebra,
		Coffee, Tea, Milk, OrangeJuice, Water,
		OldGold, Kools, Chesterfields, LuckyStrike, Parliaments
		]) :-

		Vars = [GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse,
			Englishman, Spaniard, Ukrainian, Norwegian, Japanese,
			Dog, Snails, Fox, Horse, Zebra,
			Coffee, Tea, Milk, OrangeJuice, Water,
			OldGold, Kools, Chesterfields, LuckyStrike, Parliaments
		],
		
		Vars ins 1..5,
		
		all_distinct([GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse]),
		all_distinct([Englishman, Spaniard, Ukrainian, Norwegian, Japanese]),
		all_distinct([Dog, Snails, Fox, Horse, Zebra]),
		all_distinct([Coffee, Tea, Milk, OrangeJuice, Water]),
		all_distinct([OldGold, Kools, Chesterfields, LuckyStrike, Parliaments]),
		
		Englishman #= RedHouse,
		Spaniard #= Dog,
		Coffee #= GreenHouse,
		Ukrainian #= Tea,
		GreenHouse #= 1 + IvoryHouse,
		OldGold #= Snails,
		Kools #= YellowHouse,
		Milk #= 3,
		Norwegian #= 1,
		house_next_to(Chesterfields, Fox),
		house_next_to(Kools, Horse),
		LuckyStrike #= OrangeJuice,
		Japanese #= Parliaments,
		house_next_to(Norwegian, BlueHouse).


Then to print out the solution after loading the program above use:

    puzzle([
		GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse,
		Englishman, Spaniard, Ukrainian, Norwegian, Japanese,
		Dog, Snails, Fox, Horse, Zebra,
		Coffee, Tea, Milk, OrangeJuice, Water,
		OldGold, Kools, Chesterfields, LuckyStrike, Parliaments]),
	label([
		GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse,
		Englishman, Spaniard, Ukrainian, Norwegian, Japanese,
		Dog, Snails, Fox, Horse, Zebra,
		Coffee, Tea, Milk, OrangeJuice, Water,
		OldGold, Kools, Chesterfields, LuckyStrike, Parliaments]).

Re: The Power of Prolog

#152
post #136
post #28

Earlier quoted context omitted.

The fact list_length([], 0) says "the length of an empty list [] is 0". The clause says "the length of the list [_|Ls] (i.e, any element concat Ls) has length N if N>0, N=N0+1, and the length of Ls is N0". Hope this helps.

Why do you have to specify that N>0? Seems to me that this ought to be sufficient: the list [_|Ls] has length N if N=N0+1, and the length of Ls is N0

In this particular example, specifying that N > 0 helps with running the predicate "backwards": You can use the query "length(L, 3)" to enumerate "all" lists of length 3, for example:

    ?- length(L, 3).
    L = [_G926, _G929, _G932].
This is not particularly informative for beginners, but it's a list containing exactly three distinct logic variables. Any three-element list is an instance of this.

When running in this mode, the N #> 0 constraint ensures termination; otherwise, the recursive clause would find the three-element list, but then continue a futile infinite search for further lists (of negative length, which cannot exist). It takes a bit of getting used to Prolog's execution model to really understand why this is the case.

Re: The Power of Prolog

#153

Earlier quoted context omitted.

Fun problem. My solution doesn't do really any "logic programming", and it takes around 2 minutes to run on my laptop. https://gist.github.com/philsnow/02e747241d46106b14b60146e58... The middle huge nested thing (where `some_possible_worlds` is defined) is gross but I'm not great with ruby and I couldn't think of another way to not blow up memory enumerating all the worlds just to eliminate 99% of them.

I worked up this from the SEND MORE MONEY example in the CLP(FD) repo. https://gist.github.com/calroc/603ed919bc814ccee10c1b3df6142... There are five houses. The Englishman lives in the red house. The Spaniard owns the dog. Coffee is drunk in the green house. The Ukrainian drinks tea. The green house is immediately to the right of the ivory house. The Old Gold smoker owns snails. Kools are smoked in the yellow house.…

That's one of the most elegant Prolog formulations of this task I have seen so far. Great use of CLP(FD), especially considering that you have only started to learn this technology, as you mentioned in the other thread!

I have only two small suggestion: First, since you know that Vars = [GreenHouse, ...] (the Prolog program states this as a constraint), you can simply substitute Vars every time for this list. Therefore, you can start the whole program with: puzzle(Vars) :- ...

Second, exactly the same reasoning applies for the query you show. You can write it as: ?- puzzle(Vs), label(Vs).

Thank you very much for posting this beautiful solution, and also for carefully clarifying the additional assumptions!

Re: The Power of Prolog

#154
post #136

Earlier quoted context omitted.

Why do you have to specify that N>0? Seems to me that this ought to be sufficient: the list [_|Ls] has length N if N=N0+1, and the length of Ls is N0

In this particular example, specifying that N > 0 helps with running the predicate "backwards": You can use the query "length(L, 3)" to enumerate "all" lists of length 3, for example: ?- length(L, 3). L = [_G926, _G929, _G932]. This is not particularly informative for beginners, but it's a list containing exactly three distinct logic variables. Any three-element list is an instance of this. When running in this mode,…

Thanks for the informative reply.

A related observation, by the way, regarding programming languages: often it all sort of makes sense, and you can put stuff together that gets you the correct result without really knowing what's actually going on behind the scenes.

However, when you need stuff to go fast, then you really need to know the underlying runtime, execution model, memory model, etc. to find the incantation that's not only right, but efficient.

Re: The Power of Prolog

#155
post #129
post #116

Earlier quoted context omitted.

IBM uses a special version of Prolog with a different syntax and database access functionality. They hired a department of a university. MS Windows (network code) contains a Prolog with C-style-syntax.

Thanks. Do you know of any other resources describing Watson's inner workings? ...cause just googling for it leads to one of the zillions of marketing bullshit pages or whitepapers devoid of any real infos that IBM's marketing drones have flooded the web with.

Look for papers, search for Watson Prolog UIMA

Re: The Power of Prolog

#156
post #116

Earlier quoted context omitted.

IBM uses a special version of Prolog with a different syntax and database access functionality. They hired a department of a university. MS Windows (network code) contains a Prolog with C-style-syntax.

> MS Windows (network code) contains a Prolog with C-style-syntax. Can you tell me details?

someone else wrote more details about it in this thread.

Re: The Power of Prolog

#157
post #8
post #2

Roughly half the 'power of prolog' comes from the 'power of logic programming' and prolog is by far not the only logic programming language, e.g., - You can do logic programming using minikanren in scheme. (you can also extend the minikanren system if you find a feature missing). - Minikanren was implemented in clojure and called core.logic. - It was also ported to python by Matthew Rocklin I think, called logpy. - T…

>Zebra puzzle Interesting puzzle. I copied the puzzle text to a separate text file so that one does not accidentally read anything else in the Wikipedia article. https://pastebin.com/0DWbSSx3

My z3 based solution in python looks like this: https://gist.github.com/sriram-srinivasan/2981825217f0802d9f...

Re: The Power of Prolog

#158
post #141

Earlier quoted context omitted.

>> Roughly half the 'power of prolog' comes from the 'power of logic programming' and prolog is by far not the only logic programming language, e.g., It is also by far the most popular logic programming language, in terms of the number of users and the number of different interpreters. It's a bit like LISP and functional languages, although of course functional programming has been adopted far more than logic program…

Out of curiosity, have you used Curry? The combination of logic and functional paradigms in one language is intriguing to me, but I have no time (right now) to play with it.

I haven't, no. I should probably check it out, the combination is very promising. I tried out Mercury very briefly a few years ago but only briefly.

Swi Prolog 7.x has added some rudimentary functional ish facilities (basically, dicts, named data structures that you can access using a dot-notation), although nothing as complete as Mercury.

Re: The Power of Prolog

#159

Earlier quoted context omitted.

>> EDIT: ILP at an advanced level starts making connections with PGMs (probabilistic graphical models) and hence machine learning, but its a long way to go for me (in terms of learning) before I start to make sense of all these puzzle pieces. ILP is machine learning, it's a family of algorithms that learn logic programs usually from structured data. So for instance, table rows go in one end and Prolog clauses come ou…

Thanks for the comment and references. Good luck with your PhD program. > ... Connections with PGMs... hmm, not sure what you mean. What I meant is that ILP, PGM, HMM, are some of the topics that were being explored as an extension to GOFAI in the 1990s and first half of 2000s before the 2006 breakthrough results by Hinton which drastically shifted the focus towards NNs, Deep learning, etc. Essentially these topics w…

>> What I meant is that ILP, PGM, HMM, are some of the topics that were being explored as an extension to GOFAI in the 1990s and first half of 2000s before the 2006 breakthrough results by Hinton which drastically shifted the focus towards NNs, Deep learning, etc.

The way I know it, PGMs and HMMs are firmly within GOFAI- they're basically graphs with weights. Although I guess you can say the same thing about ANNs, the primary purpose of PGMs and HMMs is to represent structure (specifically, dependency relations). Which is a very GOFAI thing, to my mind.

It really depends on where you put the distinction between GOFAI and something else. My understanding is that the commonly accepted split is between AI with a (symbolic) representation and AI without, and the original proponent of the latter is Rodney Brooks who coined the term "Nouvelle AI" (sometimes "Nouveau") that is usually used as the modern counterpart to GOFAI.

Personally, I believe the distinction is a bit stale, especially if you think that ANNs are actually older than expert systems and that even if you look just a couple of years in the past you'll see that one of the most popular (family of) machine learning algorithms was Decision Tree learners, which are basically a perfect mix of the old algorithms and the new: a propositional logic rule-base built from examples using information-theoretic measures.

As to the revolution brought by Hinton, this is a bit political but I think that's just a good story, with little grounding in reality. Here's an example:

https://webcache.googleusercontent.com/search?q=cache:2JwVBr...

That's a (cached) page from an online machine learning data repository, with data sets taken from various papers etc. The page is about a familial relationship data set from two papers, one by Hinton and one by Quinlan (the Decision Tree guy). Hinton's paper is from 1986, Quinlan's from 1989. Hinton's paper has 43 citations, all but one of which is from 2002 or earlier and going back to the '90s (Quinlan's only has one, from 1990).

Which tells me that Hinton's "breakthrough result" did not revive interest in ANNs, neither was he the tenacious, lone underdog, that he likes to present thimself as: interest in ANNs and specifically Hinton's work has always been very popular, as has been the work of others (er, Schmidhuber).

My understanding is that machine learning goes way back, and basically starts with GOFAI: the first thing people noticed about expert systems was the difficulty of hand-crafting non-trivial rule-bases, so they looked for ways to automate that (see, e.g. Michalski's work that goes back to the '60s) and eventually realised that you didn't actually need the expert system if you could just automate the rule extraction.

But that's also probably just a cute but inaccurate narrative- there was a great deal of work in machine learning and a great deal of interest in it, that was not labelled as such, for instance, in "inductive inference" and so on.

tl;dr: when we talk of machine learning and AI these days we're hardly even scratching the surface. There's a lot of past work that most university courses simply don't have the time to teach and we newer students are missing lots of useful bits of it... and are probably condemned to reinvent.

Re: The Power of Prolog

#160
post #140
post #83

Earlier quoted context omitted.

Prolog was part of course that I'd taken during my Masters. I loved it then. I would love to take a closer look when I have time ... whenever that happens ... Interestingly, IBM Watson uses Prolog. [1] [1] https://www.cs.nmsu.edu/ALP/2011/03/natural-language-process...

I used it for a graduate course in formal semantics. As our final project, we had to implement Lisp in Prolog and prove the semantics formally. Until that point, I thought I had a good grip on Prolog. Debugging the project nearly drove me nuts! If I had a do-over, I'd learn the SWI debugging tools before attempting it. I love Prolog and hope to spend more time with it some day. But, the step from Novice to Intermedia…

That's helpful advice - thank you!
Post reply on HN