changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/lib/q/dql.lisp

changeset 698: 96958d3eb5b0
parent: 35bb0d5ec95e
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 ;;; dql.lisp --- Deductive Query Langs
2 
3 ;; Query Engine for Inference-based query langs.
4 
5 ;;; Commentary:
6 
7 ;; Prolog, Datalog, etc.
8 
9 ;;;; SQL vs Prolog
10 
11 ;; My current understanding is that Prolog and SQL-derived langs share much in
12 ;; common. You can certainly do deductive logic in SQL and you can do
13 ;; relational table-based logic in Prolog.
14 
15 ;; It is interesting to note that they were both discovered around the same
16 ;; time in the 60s-70s, but in very different contexts. Prolog was intended
17 ;; for NLP and SQL (relational-algebra/RA) was the foundation for
18 ;; RDBMS. Prolog never really found the same sort of success had by SQL, but
19 ;; with the AI summer in full bloom and NLP being a hot-topic, perhaps we will
20 ;; see the scales shift.
21 
22 ;;;; Design
23 
24 ;; The WAM compiler is a bit too much to understand let alone implement at
25 ;; this stage. The design of this package will be much simpler and optimized
26 ;; for compatibility with Lisp Objects.
27 
28 ;; The design we're going for in this package is what I would consider the
29 ;; Lisper's version of Datalog. We want to implement just enough to be useful
30 ;; as a query language, and then use it to bootstrap a more elegant Prolog
31 ;; compiler, likely in SYN/PROLOG.
32 
33 ;;;;; Data Model
34 
35 ;; compiled code + constants -> physical plan -> arena + hash-tables -> engine
36 
37 ;;;;; Compiler
38 
39 ;; Predicates
40 
41 ;; Rules/Facts
42 
43 ;;;;; Runtime
44 
45 ;; Engine
46 
47 ;; Execution
48 
49 ;; Persistence
50 
51 ;;;;; Refs
52 
53 ;; https://franz.com/support/documentation/11.0/prolog.html
54 
55 ;; https://github.com/wmannis/cl-gambol
56 
57 ;; https://norvig.com/paip/README.html
58 
59 ;; https://en.wikipedia.org/wiki/Negation_as_failure
60 
61 ;; https://github.com/bobschrag/clolog/blob/main/architecture.md
62 
63 ;; https://www.swi-prolog.org/pldoc/man?section=predsummary
64 
65 ;; https://citeseerx.ist.psu.edu/document?repid=rep1&type=pdf&doi=cc7dcdf130adbd7be4d0ed5d3f4ea890e4477223
66 
67 ;;; Code:
68 (in-package :q/dql)
69 
70 ;;; Vars
71 
72 (declaim (fixnum *lips*))
73 (defvar *lips* 0
74  "Count of logical inferences performed.")
75 
76 (defvar *leash-limit* nil)
77 
78 (defvar *leash-indent-wrap* 20
79  "The output to *LEASH-OUTPUT* indents by one level for each level of a predicate, modulo this value.")
80 
81 (defvar *leash-output* (make-synonym-stream '*trace-output*))
82 
83 ;; from GAMBOL
84 (defvar *interactive* t
85  "true iff interacting with user")
86 (defvar *auto-backtrack* nil
87  "return all solutions if true")
88 (defvar *last-continuation* nil
89  "saved state of the system")
90 (defvar *trail* nil
91  "the trail, for backtracking")
92 (defvar *x-env* nil
93  "env for goals")
94 (defvar *y-env* nil
95  "env for rules")
96 (defvar *top-level-envs* nil
97  "saves top-level environments")
98 (defvar *top-level-vars* nil
99  "saves top-level variable names")
100 (defvar *rules* (make-hash-table)
101  "hash table for prolog rule heads")
102 (defvar *facts* nil
103  "Facts are uncoditional truths. They are expressed simply as rules with no
104 variables in the head and no clauses in the body. During reading of a DQL
105 form, if we find any facts we evaluate them and store them here.")
106 
107 ;;; Utils
108 (defconstant +impossible+ 'no "make impossible look nice")
109 (defconstant +solved+ 'yes "make solved look nice")
110 
111 (defconstant +?+ #\?)
112 
113 (defun dql-variable-p (sym)
114  "Valid DQL variables are symbols which start with the character #\? as in '?FOO
115 and '?BAR."
116  (and (symbolp sym)
117  (eql (char (symbol-name sym) 0) +?+)))
118 
119 (deftype dql-variable () `(satisfies dql-variable-p))
120 
121 (defun dql-anonymous-p (sym)
122  "Return T if SYM is a DQL anonymous variable represented by the value of +?+."
123  (eq sym (symbolicate +?+)))
124 
125 (deftype dql-anonymous () '(satisfies dql-anonymous-p))
126 
127 (defgeneric proof-tree (self))
128 
129 (defgeneric print-proof-tree (self &optional stream))
130 
131 ;;; Conditions
132 (define-condition dql-error (error) ())
133 
134 (deferror simple-dql-error (dql-error simple-error) ())
135 
136 (defun simple-dql-error (ctrl &rest args)
137  (error 'simpl-dql-error :format-control ctrl :format-arguments args))
138 
139 (define-condition invalid-dql-anonymous (dql-error) ())
140 
141 (define-condition invalid-dql-variable (dql-error) ())
142 
143 ;;; Prolog Predicates
144 (defun dql-predicate-p (sym)
145  "Check if SYM looks like a DQL predicate. It shoulb be suffixed by a #\/
146 followed by either '* for vararg functors or an integer indicating the arity
147 of the predicate. On success returns the arity or T for varargs."
148  (when-let ((arity (cdr (ssplit #\/ (symbol-name sym)))))
149  (setf (the simple-string arity) (car arity))
150  (or (and
151  (digit-char-p (char arity 0))
152  (parse-integer arity))
153  (char= (char arity 0) #\*))))
154 
155 ;; ports: call, exit, redo, and fail
156 
157 ;; define-functor
158 
159 ;;; Lisp Operators
160 
161 (defmacro <- (head &body body))
162 (defmacro <-- (head &body body))
163 
164 (defmacro ?- (&body clauses)
165  "Enter the interactive DQL execution environment, attempting to solve for
166 CLAUSES.")
167 
168 
169 (defmacro leash (&body (functor arity))
170  "Prolog equivalent of CL:TRACE."
171  (print functor) (print arity))
172 
173 (defmacro unleash (&body (functor arity))
174  "Prolog equivalent of CL:UNTRACE."
175  (print functor) (print arity))
176 
177 (defun prolog-compile-symbols (&rest functors))
178 
179 ;; cut
180 ;; ref: https://en.wikipedia.org/wiki/Cut_(logic_programming)
181 (defconstant +!+ #\!)
182 (defun ! () )
183 (define-symbol-macro ! (!))
184 
185 ;; equality
186 
187 ;; db manipulation
188 
189 ;; assert, retract, asserta, and assertz
190 
191 ;;; Resolution
192 
193 ;; herbrand universe | ground-terms + pred(P) -> herbrand base | map(true) -> herbrand interpretation
194 
195 ;;;; Unification
196 (defun unify (goal))
197 
198 ;; optimistic vs pessimistic when presented with infinite recursion
199 
200 ;;; CLOS
201 (defclass dql-query (query) ())
202 
203 (defclass dql-data-source (data-source) ()
204  (:documentation "Data source which can be used withing DQL expressions."))
205 
206 ;;; Parser
207 (defclass dql-parser (query-parser) ())