Prolog

18 Nov 2026 - 22 Nov 2026
Open in Tine · Open in Logseq
    • Another language I need to refresh myself on for Emergence.
    • Practicalites
      • Swipl
      • trace(foo/2)
      • findall(S, simp(3+x*0, S), L). – how to get all results in a list
    • Basics
      • Left-recursion is death
      • Rule order matters! (see simp example)
      • Think of unification as kind of like:
        • a fn + let
        • with deterministic backtracking
      • The non-unification operators
        • /+ etc. cuts? there must be some name
      • DCG: Definite Clause Grammars
        • pretty simple? except cant really drecursion so whats the point? I bet “definite clause” means it isn’t that powerful
          • Ask Claude
            • DCGs: Recursion and What "Definite Clause" Means

            • Both parts of the note are off.
            • 1. DCGs recurse fine. Only left recursion is the problem.

            • A DCG is syntactic sugar over ordinary Prolog clauses using difference lists:
            • s --> np, vp.
              % compiles to:
              s(S0, S) :- np(S0, S1), vp(S1, S).
              
            • Because it is plain Prolog, it inherits Prolog's top-down, depth-first search. That search loops on left recursion (expr --> expr, [+], term.), but right recursion works normally:
            • digits --> digit, digits.
              digits --> digit.
              
            • There are two fixes for left-recursive grammars:
            • Rewrite the grammar to remove left recursion:
            • expr --> term, expr_rest.
              expr_rest --> [+], term, expr_rest.
              expr_rest --> [].
              
            • Use tabling (SWI-Prolog, XSB). With :- table expr//0. the left-recursive version terminates.
            • 2. "Definite clause" is a logic term, not a power limit.

            • A definite clause is a Horn clause with exactly one positive literal, H :- B1, ..., Bn., which is just a normal Prolog rule. Definite-clause programs are Turing-complete, so the name implies no restriction.
            • 3. DCGs are more powerful than CFGs.

            • Nonterminals can take arguments, and { Goal } embeds arbitrary Prolog. This lets you:
            • Parse context-sensitive languages. For example, aⁿbⁿcⁿ, which no context-free grammar can describe:
            • abc --> as(N), bs(N), cs(N).
              as(0) --> [].
              as(s(N)) --> [a], as(N).
              bs(0) --> [].
              bs(s(N)) --> [b], bs(N).
              cs(0) --> [].
              cs(s(N)) --> [c], cs(N).
              % ?- phrase(abc, [a,a,b,b,c,c]).  → true
              
            • Build parse trees or ASTs through the extra arguments.
            • Run in reverse. The same grammar can often generate strings as well as parse them, e.g. phrase(abc, L).
            • Thread other state. Difference lists can carry things like a symbol table, not just token lists.
            • Corrections to other items in the outline

            • Negation: the operator is \+ (negation as failure), not /+. It is a separate construct from cut (!). Both are usually called "extra-logical" or "non-logical" control constructs.
            • CLP(FD) example:X #> 3, X #< 6, label([X]) yields only X = 4 ; X = 5, because both bounds are strict.
      • CLP(FD) constraint languge
        • Plain Prolog can be regarded as~CLP(H) (CLP over Herbrand terms)
        • ?- X #> 3, X #< 6, label([X]).
          
          What do you get?
          X = 3 ; X = 4 ; X = 5 ; X = 6
          
          
    • Cuts (!) to stop backtracking. this acts like a cond, without it you get multiple values.
      • classify(X, negative) :- X < 0, !.
        classify(0, zero) :- !.
        classify(_, positive).
        
        
    • Sheer hatred
      • My intuitions are worse than useless in these bass-ackwards languages. I mean, I get why they are powerful but they are alien to me. Fuck. Trying to force myself but it doesn’t work.