Hacker News new | past | comments | ask | show | jobs | submit login

Nice quiz :-) My attempt would be:

    select b1.Brand as brand1, b2.Brand as brand2
    from bicycles as b1
    inner join bicycles as b2
    on (b1.Colour = b2.Colour)
    where b1.Brand <> b2.Brand



Cool! That's actually really close to the Prolog version.


True that. Only major difference is that I had to explicitly specify how to relate the two brands together in SQL whereas in Prolog it's implicit by using the same variable in two different patterns. I find the idea of a simple Prolog, like a Datalog, very intriguing as a potential simple query engine for apps in mainstream languages–not having to worry about the maps, filters, reduces, flatmaps, and all the other operations we have to do to coax data into the right shapes in line-of-business apps. Unfortunately I just haven't been able to figure out how to do a left outer join (or if that's even the right approach in Prolog :-)


> Unfortunately I just haven't been able to figure out how to do a left outer join (or if that's even the right approach in Prolog :-)

That is probably my main issue with SQL: That the names of some of the keywords are very table-ish and not very semantic; like LEFT OUTER JOIN. I mean, if we have some facts, like how old people are, and what they like to eat, then what does it even mean that any of those facts is to the left or on the inside of another? I can't fit that into my brain.

But let's take an example. We have these sets of facts (which, if they were in SQL, would be in two different tables):

    age(alice, 20).           
    age(beatrice, 30).        
    age(charlie, 90).                                   
    likes(alice, carrots).    
    likes(alice, chips).      
    likes(charlie, crumpets). 
Then an INNER JOIN would probably be like:

    age(Name,Age), likes(Name, Something).
This can be read as "What is the age of anyone, who like something?" (or more absurdly, "What does anyone, who has an age, like?", depending on what variable we focus on.)

I'm probably getting this wrong, but if we wanted LEFT OUTER JOIN it would be more like:

    age(Name,Age); likes(Name, Something).
Which is more like "What's all people's ages, and what does anyone like?"

In Prolog, the comma means 'and', and the semicolon means 'or', so in the second query anyone, who has an age but doesn't like anything, will still be included. In this query, that would be Beatrice. That's a bit like a LEFT OUTER JOIN, isn't it?

> I find the idea of a simple Prolog, like a Datalog, very intriguing as a potential simple query engine for apps in mainstream languages–not having to worry about the maps, filters, reduces, flatmaps, and all the other operations we have to do to coax data into the right shapes in line-of-business apps.

Seen DataScript?[0] I think MiniKanren has also been implemented in most languages. I like how Prolog "forces" a declarative style though.

[0]: https://github.com/tonsky/datascript




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: