```Date: Nov 20, 2012 3:59 AM
Author: Graham Cooper
Subject: PROLOG ENGINES ARE REALLLLLLY SLOW!   ((SQL-UNIFY))

It's a simple algorithm to program,a simple problem to solve!f(a,X)   <=>   f(Q,g)  ?Can these two  TREES?  be made equivalent?f(a,g) !!Haha!X=gQ=a------------But examine a BIGGER NESTED ATOMIC PREDICATE!f(A,b(c,d), e(F,g(H,I,j(K,l),m,N),o,p(A,d,F)))).ANDf(z, Q , e(l(m,n), g(h,i,J,m,w),o,P)).I'll tell you I've been programming exactly this for 5 months!It is a debuggers dream if you like bugs!ALL THE UNIFY(F1,F2) ALGORITHMS ARE RECURSIVE!1st you unify f(.....) to f(.....)then 1 argument at a time....A to zb(c,d) to Q....then some of those terms are predicates that have to be unifiedFURTHER!A lot of variable renaming conventions, trial and error,backtracking... very tricky and particular code covering dozens ofpotential matching scenerios!----------------OK here is how it SHOULD BE DONE!Each term in the predicate is given a REFERENCE NUMBER!f(A,b(c,d), e(F,g(H,I,j(K,l),m,N),o,p(A,d,F)))).OK, see that  g(H,I).that 'I" might be ANYTHING, it's a capital VARIABLE.But it's reference number is:REF 3331 2 3f A e1 2 3e F g1 2 3g H II is at position 333!------------------Now all facts, rules and Queries fit into a simple SQL TABLE!A PROLOG FACTvert ( pnt( X,Y ) pnt( X,Z ) )TPROID  REF   FIELD   TYP=================21   1     vert       H21   2     pnt        P21   3     pnt        P21   21   X          V21   22   Y          V21   31   X          V21   32   Z          VA PROLOG QUERY?- vert ( pnt( 1,2 ) pnt( 1,4 ) )TTAILSID  REF   FIELD   TYP=================1    1     vert       H1    2     pnt        P1    3     pnt        P1    21   1          T1    22   2          T1    31   1          T1    32   4          TTHIS 1 LINE OF SQL WILL DO 100 LINES OF UNIFY RECURSION!The ORDER IN WHICH YOU WORK OUT THE TERMS is IRRELEVANT!No need to do a recursive descent match by match!SQL-UNIFYSELECT * FROM TTAILS, TPROWHERE TTAILS.REF = TPRO.REFAND TTAILS.FIELD = TPRO.FIELDGROUP BY COUNT(ID)ORDER BY ID DESCThis will put all the MAXIMUM TERMS MATCHED RESULTSat the top of the Query Result, and UNIFY just has to allocatevariables to terms, then recurse the tail facts of any matched rules.Slight drawback is program order is lost using the fastest matchmethod, but PROLOG is overused as a simple FETCH cycle for 3GL stylecoding.Herc--www.microPROLOG.comif( if(t(S),f(R)) , if(t(R),f(S)) ).    if it's sunny then it's not rainingergo       if it's raining then it's not sunny
```