Live data from Hacker News

Show HN: Writing an HTTP server in Prolog

jamesbvaughan.com

31–40 of 74 posts

Re: Show HN: Writing an HTTP server in Prolog

#31

Prolog is extremely different than other programming languages and as a result, learning it and writing a few small programs is very mind expanding. It really drives home how different paradigms are suited to different problems. Prolog is suited toward problems like Einsteins riddle https://udel.edu/~os/riddle.html

A very interesting aspect of Prolog is how its primary primitive is backtracking search.

This is orders of magnitude more complex than C-language style primary primitives which closely match single machine instructions. Or even slightly more complex models in lazy languages like Haskell.

It's strange that you can build a language almost purely off of graph search yet have it be generally useful. Makes you wonder what other techniques spawn their own paradigm.

Re: Show HN: Writing an HTTP server in Prolog

#32

Prolog is extremely different than other programming languages and as a result, learning it and writing a few small programs is very mind expanding. It really drives home how different paradigms are suited to different problems. Prolog is suited toward problems like Einsteins riddle https://udel.edu/~os/riddle.html

The class I was taking kind of blew my mind when I saw how you can solve some problems in such cool ways with Prolog, but I would certainly admit that this server isn't exactly the kind of use it's best suited for.

You might be surprised! SWI Prolog has libraries for web applications (http://www.pathwayslms.com/swipltuts/html/index.html) and other modern application development toolkits. Their online IDE (http://swish.swi-prolog.org/) is written in Prolog.

Re: Show HN: Writing an HTTP server in Prolog

#35

Earlier quoted context omitted.

Any particular reason you think Prolog should be abandoned? I think it looks like very nice language for untyped, high-level programming.

My understanding is that there's a very limited amount of research going into Prolog so it ends up being incredibly slow for larger tasks. And a lot of it's functionality can be done via standard SQL so there's not a ton of use cases for it.

I don't know how much research is going into Prolog itself, but there are logic programming languages that have seen active development recently, like Mercury https://mercurylang.org/

Re: Show HN: Writing an HTTP server in Prolog

#36
post #28

Earlier quoted context omitted.

Chris Granger's Eve is based on Datalog. Perhaps his work is finally what breaks out the logic programming prolog paradigm.

Chris Granger is charismatic, but not particularly insightful. It would be better if he found a mentor.

Yea I agree. It really looks like he just took the datalog language, put it in a browser, and added an in-browser IDE (basically like he did with clojurescript and Light Table). Not that this might be incredibly useful, and not that his vision of a billion non-developers able to program might become a reality (through Eve), but I think everyone was expecting something more--something which had that groundbreaking Bret Victor feel-which is what I think what you're alluding to. Unfortunately, I don't think there's anyone that could mentor him--or they would do it themselves--bar possibly Bret Victor. My hope is that his grand plan evolves into something that in sum is as groundbreaking/insightful as we had imagined. At the end of the day, you gotta give it to him regardless. Not many people are going for it like him. I once was, but am back to application development. So nothing but the best of luck to Chris coming from me.

Re: Show HN: Writing an HTTP server in Prolog

#37
post #13

Earlier quoted context omitted.

Python's set class and comprehension syntax can get you much of the declarative power of Prolog.

How so? I mean, there are logic libraries available for python to be sure. But the "set class and comprehension syntax" are two features only tangentially related to logic programming. Using them alone doesn't get you inference, or even unification.

Inference is straightforward using dictionaries, membership-testing, and set methods, like subset, superset, disjoint, union, intersection, and difference.

Python can't do a conditional assignment as nicely as Prolog's unification, but you can get similar behavior via the application of predicates in comprehensions and using set methods. I'm not saying Python is equivalent, but simply that you can get some of Prolog's style in Python. Erlang's pattern matching and concept of bound vs unbound variables is a bit closer to Prolog's unification.

Here's some Python implementations for examples shown in my first Google result for "prolog examples" (http://www.cs.toronto.edu/~sheila/384/w11/simple-prolog-exam...)

1. Here are some simple clauses.

    >>> likes = {'mary': {'food', 'wine'},
    ...          'john': {'wine', 'mary'}}
    >>> 'food' in likes['mary']
    True
    >>> 'wine' in likes['john']
    True
    >>> 'food' in likes['john']
    False
    >>> # John likes anything that Mary likes
    >>> likes['john'] |= likes['mary']
    >>> # John likes anyone who likes wine
    >>> likes['john'] |= {name for name, objs likes.items() if 'wine' in objs}
    >>> # John likes anyone who likes themselves
    >>> likes['john'] |= {name for name, objs in likes.items() if name in objs}
2. Slightly more complicated family tree.

   >>> male = {'james1', 'charles1', 'charles2', 'james2', 'george1'}
   >>> female = {'catherine', 'elizabeth', 'sophia'}
    >>> parents = {'charles1': 'james1',
    ...            'elizabeth': 'james1',
    ...            'charles2': 'charles1',
    ...            'catherine': 'charles1',
    ...            'james2': 'charles1',
    ...            'sophia': 'elizabeth',
    ...            'george1': 'sophia'}
    >>> # Was George I the parent of Charles I?
    >>> parents['charles1'] == 'george1'
    False
    >>> # Who was Charles I's parent?
    >>> parents['charles1']
    'james1'
    >>> # Who were the children of Charles I?
    >>> {child for child, parent in parents.items() if parent == 'charles1'}
    {'charles2', 'james2', 'catherine'}
3. Recursion: Towers of Hanoi

    >>> def move_one(x, y):
    ...     print('Move top disk from {0} to {1}'.format(x, y))
    ...     return True
    ...
    >>> def move(n, x, y, z):
    ...     if n == 1:
    ...             return move_one(x, y)
    ...     m = n - 1
    ...     move(m, x, z, y)
    ...     move_one(x, y)
    ...     return move(m, z, y, x)
    ...
    >>> move(3, 'left', 'right', 'center')
    Move top disk from left to right
    Move top disk from left to center
    Move top disk from right to center
    Move top disk from left to right
    Move top disk from center to left
    Move top disk from center to right
    Move top disk from left to right
    True
4. An example using lists:

This last one is a bit silly in Python as all the tools shown are built-ins.

Re: Show HN: Writing an HTTP server in Prolog

#38

James, check out Mercury next as it's a faster, better Prolog with some industrial use. https://mercurylang.org

Last time I looked (admittedly years ago), it wasn't an unambiguously "better Prolog", because Mercury's static mode system couldn't capture all of what Prolog can do with unification.

Re: Show HN: Writing an HTTP server in Prolog

#39
Awesome, one of the most fun i had at university was building a Uri parser with prolog.

After i discovered that you could define operators i built a combinatorial parser, something akin to https://hackage.haskell.org/package/parsec (of course nowhere as powerful) with similar syntax and do notation. https://gist.github.com/ga2arch/e8904177f722c6560e37

And i didn't have to define backtracking because it is handled by prolog itself.

Re: Show HN: Writing an HTTP server in Prolog

#40
post #31

Prolog is extremely different than other programming languages and as a result, learning it and writing a few small programs is very mind expanding. It really drives home how different paradigms are suited to different problems. Prolog is suited toward problems like Einsteins riddle https://udel.edu/~os/riddle.html

A very interesting aspect of Prolog is how its primary primitive is backtracking search. This is orders of magnitude more complex than C-language style primary primitives which closely match single machine instructions. Or even slightly more complex models in lazy languages like Haskell. It's strange that you can build a language almost purely off of graph search yet have it be generally useful. Makes you wonder what…

There's at least one language (Refal: there may be more!) based on Markov algorithms. Another language (XL: as far as I know this is the only one of its breed thus far) works at its core on parse tree substitution. Yet another (CHR) does a weird generalization of term re-writing to provide a toolkit for making constraint languages. (Yes, a language to make languages.)

There's a reason I've become a programming language whore.

Post reply on HN