|
Prolog operators like `4 > 5` are a syntax sugar which desugars into a normal function call `>(4, 5)`. This part of the language is programmable, so you can add your own function `#>(X, Y)` and then declare it as an operator and use it like `4 #> 5`. See [1]. Nitpickingly, this means we can't be sure what #> is without looking, but it's common in Scryer and SWI Prolog (at least) that the #> #< versions of numeric comparison operators are used by constraint solver libraries. In imaginary Python it might be this code: import solver
solver.add_variable(x)
solver.variable_range(x, 0, 100)
solver.add_constraint(x, greater_than, 50)
solver.solve_for(x)
in pseudo-Prolog it can be: :- using constraint solver library
X in 0..100,
X #> 50,
label(X)
where "in" and "#>" were added into the language at runtime by the import of the constraint library. That is, it calls out to a custom 'function' which tells the constraint solver to restrict possible values for for X from 0..100 down to 51..100.> "and what ! does" This is a concept which doesn't translate easily to other languages, but analogously it's like the performance difference between this code which always searches the entire haystack: found = false
for item in haystack:
if item == 'needle':
found = true
return found
and this which stops searching the haystack if the needle is found, but still searches the entire haystack in the worst case: for item in haystack:
if item == 'needle':
return true
return false
The catch being that ! is not exactly a performance thing, it's an instruction to the Prolog runtime to skip some of the code, which can speed up performance but if you throw it in carelessly, your code no longer gives the right answers.[1] They aren't Prolog "functions", they are predicates, functions are different, but it will do for this explanation. |