changelog shortlog graph tags branches changeset files revisions annotate raw help

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

changeset 583: 1a0df4444a9f
parent: efb4a19ff530
child: 35bb0d5ec95e
author: Richard Westhaver <ellis@rwest.io>
date: Thu, 08 Aug 2024 23:01:20 -0400
permissions: -rw-r--r--
description: dql notes
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 ;;; Code:
64 (in-package :q/dql)
65 
66 ;;; Vars
67 
68 (declaim (fixnum *lips*))
69 (defvar *lips* 0
70  "Count of logical inferences performed.")
71 
72 ;; from GAMBOL
73 (defvar *interactive* t "true iff interacting with user")
74 (defvar *auto-backtrack* nil "return all solutions if true")
75 (defvar *last-continuation* nil "saved state of the system")
76 (defvar *trail* nil "the trail, for backtracking")
77 (defvar *x-env* nil "env for goals")
78 (defvar *y-env* nil "env for rules")
79 (defvar *top-level-envs* nil "saves top-level environments")
80 (defvar *top-level-vars* nil "saves top-level variable names")
81 (defvar *num-slots* -1 "number of logical variables in a query")
82 (defvar *rules* (make-hash-table) "hash table for prolog rule heads")
83 
84 ;;; Conditions
85 (define-condition dql-error (error) ())
86 
87 (deferror simple-dql-error (dql-error simple-error) ())
88 
89 (defun simple-dql-error (ctrl &rest args)
90  (error 'simpl-dql-error :format-control ctrl :format-arguments args))
91 
92 ;;; CLOS
93 (defclass dql-query (query) ())
94 
95 (defclass dql-data-source (data-source) ()
96  (:documentation "Data source which can be used withing DQL expressions."))
97 
98 ;;; Prolog Semantics
99 
100 ;; NOTE 2024-08-03: we're loosely following along with CL-GAMBOL, but sticking
101 ;; with defstructs instead of vectors for the most part. I'm willing to pay
102 ;; the immediate cost of not vectorizing in hopes that the fact that structs
103 ;; are vector-backed and multi-threaded contexts exist will minimize the
104 ;; effect.
105 
106 ;;; Macros
107 (defmacro ?- (&body clauses))
108 
109 (defmacro *- (head &body body))
110 
111 ;;; Parser
112 (defclass dql-parser (query-parser) ())