Live data from Hacker News

Ask HN: What are some interesting examples of Prolog?

news.ycombinator.com

51–60 of 64 posts

Re: Ask HN: What are some interesting examples of Prolog?

#52

Here's an example for a problem that's moderately difficult to do recursively, hard to do iteratively, and trivial in Prolog: Given that you have the following coins, 1¢ (i.e. 1 cent or $0.01), 5¢, 10¢, 25¢, 50¢, how many different ways are there to give change for a dollar? For example, one way would be to give 2x50c, another would be to give 100x1c. How many are there total? In most languages this is not super easy…

Worth noting the entirety of the actual code would be

  :- use_module(library(clpfd)).
  
  main :-
    100 #= H*50 + Q*25 + D*10 + N*5 + P*1.
That's it.

Re: Ask HN: What are some interesting examples of Prolog?

#53
eval(I,I):-integer(I).

eval(E1+E2,I):- eval(E1,I1),eval(E2,I2), I is I1+I2.

eval(E1E2,I):- eval(E1,I1),eval(E2,I2), I is I1I2.

:- eval(12+34,R),writeln(R),R=14.

:- halt.

Very simple Operational Semantics.

this mean

syntax

e ::= i | e+e | e * e

evaluation rule

-------------- (E-Int)

i1-->i1

e1-->i1 e2-->i2

i is i1+i2

------------------- (E-Plus)

e1+e2 --> i

e1-->i1 e2-->i2

i is i1i2

------------------- (E-Times)

e1*e2 --> i

Re: Ask HN: What are some interesting examples of Prolog?

#54

You could look at "PRESS: PRolog Equation Solving System", "a system for solving symbolic, transcendental, non-differential equations." Journal of Symbolic Computation Volume 7, Issue 1, January 1989, Pages 71-84 "Solving symbolic equations with PRESS" https://www.sciencedirect.com/science/article/pii/S074771718... Source: https://github.com/maths/PRESS - - - - BTW, Does anyone know where I can find the source for MI…

The author has a linkedin account in Sweden, I would message them directly.

Re: Ask HN: What are some interesting examples of Prolog?

#55

What’s the best way to get started with Prolog on macOS? Implementations, editors, etc?

SWI is by far the best documented of the open source versions with Ciao coming second. They both ship with dev environments, the Ciao one is an elaborate Emacs mode and the SWI one is implemented in a custom GUI toolkit.

Re: Ask HN: What are some interesting examples of Prolog?

#56
post #2

Not exactly a big codebase, but it was a revelation for me how natural typecheckers can feel in Prolog: I basically rewrote typing rules with some tweaks: [1] Also, tests were surprisingly enjoyable in Prolog: [2]. [1] https://github.com/EarlGray/language-incubator/blob/29755c32... [2] https://github.com/EarlGray/language-incubator/blob/29755c32...

Not a criticism of the code here by any stretch, I always find it impressive when people manage to understand languages that are totally foreign to me, that being said however Prolog has always struck me as uniquely unreadable. Even back in university when we looked briefly at different styles of programming, the code always ended up looking something like lines 107-132 in your example, which to someone jumping in is…

That's the largest piece of Prolog code I've ever written, so criticism is warranted and I'm glad to learn how to improve it. I still wanted to share it to show how typing rules translated almost directly to Prolog clauses.

I agree that this piece of code was written in a more academic mindset, with large dose of TAPL-specific jargon and abbreviations which can easily throw anybody off if not known beforehand. I do not have good examples of industrial-grade Prolog code bases, if that's what was asked.

Re: Ask HN: What are some interesting examples of Prolog?

#57
post #54

You could look at "PRESS: PRolog Equation Solving System", "a system for solving symbolic, transcendental, non-differential equations." Journal of Symbolic Computation Volume 7, Issue 1, January 1989, Pages 71-84 "Solving symbolic equations with PRESS" https://www.sciencedirect.com/science/article/pii/S074771718... Source: https://github.com/maths/PRESS - - - - BTW, Does anyone know where I can find the source for MI…

The author has a linkedin account in Sweden, I would message them directly.

Tack så mycket! (That's Swedish for "Thank you very much." :)

Re: Ask HN: What are some interesting examples of Prolog?

#58
post #14
post #5

One of the most interesting applications of Prolog I have seen in the recent past is the formalization of dose-escalation trial designs that occur in clinical oncology. In particular, David C. Norris has implemented a Prolog formulation of the cumulative cohort design (CCD) as part of his precautionary package: https://github.com/dcnorris/precautionary/blob/main/exec/pro... This Prolog program can be used to exhausti…

Wow this is the best thing since the Actually Portable Executable source code. https://justine.lol/thun.png How did the author generate those letters?

What color scheme is that? Anyone know?

Re: Ask HN: What are some interesting examples of Prolog?

#59

Here's an example for a problem that's moderately difficult to do recursively, hard to do iteratively, and trivial in Prolog: Given that you have the following coins, 1¢ (i.e. 1 cent or $0.01), 5¢, 10¢, 25¢, 50¢, how many different ways are there to give change for a dollar? For example, one way would be to give 2x50c, another would be to give 100x1c. How many are there total? In most languages this is not super easy…

To be fair, that is not that difficult to accomplish in Python either:

    from ortools.sat.python.cp_model import *
    m = CpModel()
    a, b, c, d, e = [m.NewIntVar(0, 100, '') for _ in range(5)]
    m.Add(a*50 + b*25 + c*10 + d*5 + e*1 == 100)
    s = CpSolver()
    s.Solve(m)
    print([s.Value(x) for x in [a, b, c, d, e]])

Re: Ask HN: What are some interesting examples of Prolog?

#60
post #2

Not exactly a big codebase, but it was a revelation for me how natural typecheckers can feel in Prolog: I basically rewrote typing rules with some tweaks: [1] Also, tests were surprisingly enjoyable in Prolog: [2]. [1] https://github.com/EarlGray/language-incubator/blob/29755c32... [2] https://github.com/EarlGray/language-incubator/blob/29755c32...

https://github.com/hsk/simpletapl/blob/master/prolog5/fullup...

I have rewritten all TAPL code in Prolog. It was very interesting.

https://github.com/mitsuchi/copl-in-prolog/

This is another textbook "Concept of Programming Language" implementations.

https://www.fos.kuis.kyoto-u.ac.jp/~igarashi/CoPL/

Post reply on HN