Live data from Hacker News

Ask HN: What are some interesting examples of Prolog?

news.ycombinator.com

11–20 of 64 posts

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

#12
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 MIXTUS partial evaluation system?

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

#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?

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

#15
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...

Worth checking out if you're interested in using constraint/logic programming languages for type checking is the Statix (https://www.spoofax.dev/references/statix/) language, which is used in the Spoofax language workbench. It works similarly to Prolog in solving constraints, but adds support for scope graphs, which are a generic approach to specifying the name binding behavior of programming languages through logic constraints.

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

#16
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?

https://manytools.org/hacker-tools/ascii-banner/

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

#17
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…

Regarding the code in question, it is possible to write it a bit more elegantly and more compactly, with a combination of techniques. For example, let's consider this snippet from lines 110 to 119:

    type(Ctx, case(T0, {z, Tz}, {s(V), Ts}), Ty) :- !,
      isvar(V),
      type(Ctx, T0, nat),
      type(Ctx, Tz, Ty),
      CtxS = [{V, nat} | Ctx], type(CtxS, Ts, Ty).
    type(Ctx, case(T0, {inl(Vl), Tl}, {inr(Vr), Tr}), Ty) :- !,
      isvar(Vl), isvar(Vr),
      type(Ctx, T0, uni(Ty1, Ty2)),
      Ctx1 = [{Vl, Ty1} | Ctx], type(Ctx1, Tl, Ty),
      Ctx2 = [{Vr, Ty2} | Ctx], type(Ctx2, Tr, Ty).
It is clear that the two clauses are mutually exclusive if the second argument is known, because they differ in various terms already in the head. For instance, {z, Tz} is clearly different from {inl(Vl), Tl}. A Prolog system with deep indexing, which is also used by Mercury, can detect this, and in such a system, we can therefore remove the two occurrences of !/0 in the code, yielding a more general and shorter version:

    type(Ctx, case(T0, {z, Tz}, {s(V), Ts}), Ty) :-
      isvar(V),
      type(Ctx, T0, nat),
      type(Ctx, Tz, Ty),
      CtxS = [{V, nat} | Ctx], type(CtxS, Ts, Ty).
    type(Ctx, case(T0, {inl(Vl), Tl}, {inr(Vr), Tr}), Ty) :-
      isvar(Vl), isvar(Vr),
      type(Ctx, T0, uni(Ty1, Ty2)),
      Ctx1 = [{Vl, Ty1} | Ctx], type(Ctx1, Tl, Ty),
      Ctx2 = [{Vr, Ty2} | Ctx], type(Ctx2, Tr, Ty).
The isvar/1 tests can be removed if we use a better representation of our data. For instance, if we use the wrapper v/1 to denote variables in the data, we can replace these clauses by:

    type(Ctx, case(T0, {z, Tz}, {s(v(V)), Ts}), Ty) :-
      type(Ctx, T0, nat),
      type(Ctx, Tz, Ty),
      CtxS = [{V, nat} | Ctx], type(CtxS, Ts, Ty).
    type(Ctx, case(T0, {inl(v(Vl)), Tl}, {inr(v(Vr)), Tr}), Ty) :-
      type(Ctx, T0, uni(Ty1, Ty2)),
      Ctx1 = [{Vl, Ty1} | Ctx], type(Ctx1, Tl, Ty),
      Ctx2 = [{Vr, Ty2} | Ctx], type(Ctx2, Tr, Ty).
Finally, the variables CtxS, Ctx1 and Ctx2 are only used once each, so we can simply write:

    type(Ctx, case(T0, {z, Tz}, {s(v(V)), Ts}), Ty) :-
      type(Ctx, T0, nat),
      type(Ctx, Tz, Ty),
      type([{V, nat} | Ctx], Ts, Ty).
    type(Ctx, case(T0, {inl(v(Vl)), Tl}, {inr(v(Vr)), Tr}), Ty) :-
      type(Ctx, T0, uni(Ty1, Ty2)),
      type([{Vl, Ty1} | Ctx], Tl, Ty),
      type([{Vr, Ty2} | Ctx], Tr, Ty).
With the necessary provisions in place, it may be possible to shorten it further, by generating such code automatically from more declarative descriptions, using a mechanism called term expansion which is available in most Prolog systems. It is analogous to what other languages such as Rust call macros.

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

#18
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?

figlet is the classic tool to generate these: http://www.figlet.org/

toilet is a slightly more modern reimplementation with some added features (like color): http://caca.zoy.org/wiki/toilet

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

#20
Not a lot of code but a somewhat different use of Prolog than you're likely to see elsewhere. I used my fork of a MQTT library for Prolog (https://github.com/sprior/swi-mqtt-pack) to implement the central controller for my home automation system. The system responds to MQTT events and then coordinates the appropriate action by sending MQTT messages to other home services. Recent versions of SWI-Prolog also support redis and I've started using that to store device configuration and state between services. The MQTT version is actually a reimplementation of my previous version which used CORBA for inter-service communication.

I don't distribute the home automation code however it's pretty specific to my house. The MQTT library provides some building block examples.

You can also see some other comments I've made on HN which describe some other details.

Post reply on HN