Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Show HN: An encoding of the UK's self-isolation rules in Prolog (github.com/jamespwilliams)
108 points by jamespwilliams on April 11, 2021 | hide | past | favorite | 23 comments


Pedantic comment: this encodes the rules from NHS England's website, which are presumably based on the English legislation. Each of the devolved legislatures have their own set of legislation, so this probably doesn't generalise to the entire UK.


If you want to read the code: "," is AND and ";" is OR.


Thank you. I love seeing what different languages choose as the meaning of their primary tokens. For example, Smalltalk uses whitespace to send messages, whereas Haskell uses whitespace to apply functions to arguments.


Haskell doesn't use whitespat for function application.

Haskell uses whitespace for tokenization (like most languages), and uses nothing for function application.


Also, \+ is NOT.


> I haven't written any Prolog since university.

Same here. Anyone here who uses it commercially willing to share their story?


Nice, but this:

  should_isolate(Person) :- has_symptoms(Person);
                            tested_positive(Person);
                            (
                                lives_with_others(Person),
                                format(atom(LivingWithPeople), 'anyone living with ~w', Person),
                                (
                                    has_symptoms(LivingWithPeople);
                                    tested_positive(LivingWithPeople)
                                )
                            );
                            (
                                in_bubble(Person),
                                format(atom(BubblePeople), 'anyone in ~w\'s bubble', Person),
                                (
                                    (
                                        has_symptoms(BubblePeople),
                                        contact_with_symptomatic_bubble_member(Person)
                                    );
                                    (
                                        tested_positive(BubblePeople),
                                        contact_with_positive_bubble_member(Person)
                                    )
                                )
                            );
                            contacted_by_test_and_trace(Person);
                            (
                                arrived_from_abroad_recently(Person),
                                \+ has_negative_test_to_release_result(Person)
                            ).

Is absolutely awful Prolog style :)

First of all it's hard to read, not least because you have your semocilons (semicola?) formatted inconsistently: some are at the end of a line, others at the start, so it's different to follow the program flow. More importantly, it's hard to see at a glance whether some "cases" are meant to backtrack on failure or not. Normally, if you wanted to write a Big Hairy, Horrible Case Statement in Prolog then you should at least use the control flow with ->/2 which amounts to a "soft cut" after the first <condition>:

  (   <if-condition>
  ->  <then-consequence>
  ;   <else-alternative>
  )
But the recommended thing to make your code easier to read (_and debug_) and less like they do in Java is to use separate clauses to handle different "cases" of your program's logic.

Also, bad idea to interleave messages to user with "business logic" like you do, e.g. in:

  lives_with_others(Person),
  format(atom(LivingWithPeople), 'anyone living with ~w', Person),
It's possible with a bit of elbow grease to completely separate the I/O of communication with the user from the rules-based logic of your program.

There are actually coding guidelines for Prolog! Here:

https://arxiv.org/abs/0911.2899

Really, really good source on good Prolog style that paper (and where I base my comments above).


Would love to see a rewritten version based on this!


<3 that shell.nix is included


I was expecting

    should_isolate(_).
meaning always "yes", but it's actually encoding rules.


I was expecting some sort of parody of the rules but it's actually quite a nice project :)


Hah! I was expecting a sort of Brexit joke in Prolog, instead it's something useful :)


I expected some kind of joke about really complex rules requiring an expert system to get right.

I am currently in France, in a kind-of-lockdown. Meaning we can go out in a 10km radius, or 30km, or within your department borders for some activities, but only during the day, with some exceptions. Generally, you need to write a paper where you write your reason for going out, but not during the day if you can show a proof of where you live. And I am not getting into which businesses are allowed to open...


Same in Belgium, where over winter the rules about whether you could see someone or not took into account your age, their age, whether you're indoors or outdoors, how many people you and they have seen that week, whether you are single, whether it's Christmas Eve, what time it is and whether you need assisted care.

Oh, and the rules are different if you want to use their bathroom.

I'm not making any of this up.


[flagged]


A self-referential comment?


Can you elaborate?


Your own "this Covid joke is getting old" refers to itself. Yes, that comment of yours is getting old.


I really love Prolog ! That is an awesome project !


Not related to the code itself, but this is the most dystopian thing I've seen in a while.

"Has ~w receieved a negative result through the Test to Release scheme?"

We're all captive and need a test to be "released"?


The whole thing may well be dystopian.

That said, you don’t seem to have looked up what test to release actually is. It’s a scheme for travellers to get out of quarantine early. So no, we aren’t ‘all’ captive to ‘test to release’—we (inhabitants of the UK) are captive to many other regulations though!


This is the most dystopian in a while? Geez I wish I had your peace of mind.


Yes, because there's a pandemic. Stop being a selfish twit.


This fear response is making it worth more. Suppose the pandemic never ends? The engineering only gets easier from here.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

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

Search: