0

I'm new to Prolog and I'm struggling to make headway with this problem.

I'd like to construct a list of levels a given teacher is assigned for a particular result. Then, I would like to count the discrete number of levels that appear. Obviously, the append() predicates I have below are total nonsense but they're there to show my general line of approach (and how ignorant I am of how it's all supposed to work). I've tried many iterations but I'm shooting in the dark with this.

Thanks!

     var program = 
        `:- use_module(library(lists)). 
            teacher_level(mike,a1).
            teacher_level(mike,a2).
            teacher_level(mike,b1).

            teacher_level(phil,b1).
            teacher_level(phil,b2).
            teacher_level(phil,c1).

            append([], List2, Result) :-
                Result = List2,
                !.                     
            append(List1, List2, Result) :-
                List1  = [Head1 | Tail1],
                Result = [HeadR | TailR],
                Head1  =  HeadR,
                append(Tail1, List2, TailR).
            append(L,List):-
                append([L],_,List).

            table([[T1,L1],[T2,L2],[T3,L3],[T4,L4]],List):-
                teacher_level(T1,L1),
                teacher_level(T2,L2),
                teacher_level(T3,L3),
                teacher_level(T4,L4),

                append([L],List):-
                    teacher_level(mike,L).
        `

    session.consult( program );
    session.query('table([[T1,L1],[T2,L2],[T3,L3],[T4,L4]],List).')
MikeyB
  • 460
  • 1
  • 6
  • 22
  • It looks like you're in Tau-Prolog? If so, `append/3` is a built-in available if you `:- use_module(library(lists)).` – Paul Brown Oct 01 '19 at 22:07
  • Thanks, I've moved on from this issue and am encountering more. Now I want to create unique variables instead of putting \= between everything and everything else. Ridiculous why it's not something basic built in, but I did find this advice https://stackoverflow.com/questions/14590761/force-prolog-to-choose-unique-values-of-variables but of course tau prolog doesn't have dif/2 and no variations I've tried have worked. Do you know maybe of a normal, logical solution that can be set at the start, therefore maximising speed? – MikeyB Oct 04 '19 at 13:14
  • I'm afraid Tau is a relatively young dialect, so doesn't have much support for things like this. At best you could write your own all different predicate that takes a list, asserts the head is different from the tail, and recurses on the tail. – Paul Brown Oct 04 '19 at 13:56
  • Any attempt I've made at that ended up being slower than just asserting it as a massive list of \\='s. It would help if I could set if at the start, rather than after, but that doesn't seem to work, unless there's some other mechanic of prolog I'm not aware of. – MikeyB Oct 04 '19 at 15:13
  • Not in Tau, you need `clpfd` for that. Could you move the query server-side and call with a pengine? SWI-Prolog has `clpfd` too. Here's my template SWI-Prolog pengines with Tau-Prolog stack repo: https://gitlab.com/PaulBrownMagic/fullstackprolog – Paul Brown Oct 04 '19 at 16:26
  • Looks interesting, it seems there's room for bringing what prolog does into the modern era. I'm only just starting out with it and have a lot to learn but did come across clpfd and thought it might suit what I'm doing. I've installed tau-prolog on the node.js server. SWI-Prolog requires a bunch of other stuff to be installed. Is it straightforward enough? Can you advise? – MikeyB Oct 04 '19 at 16:34
  • Yes, it's better to learn with a full featured Prolog like SWI. There's a GitHub repo called `swivm` that's great for managing your SWI-Prolog installs. SWI is best for web-dev. – Paul Brown Oct 04 '19 at 16:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200423/discussion-between-mikeyb-and-paul-brown). – MikeyB Oct 04 '19 at 21:44

0 Answers0