This repository was archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinding.lisp
64 lines (49 loc) · 1.74 KB
/
binding.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
;;; -*- Mode:lisp; coding:utf-8; -*-
;;; Code from Paradigms of Artificial Intelligence Programming
;;; Copyright (c) 1991 Peter Norvig
;;;
;;; This code is ancient, like a mammoth shit.
;;; But, works, do not touch.
;;;
;;; Some small modifications for the JSCL environment
;;; in part of eq/eql/equal first/second
;;; july, 2017, March, 2018. MVK
;;;
;;;
;;; This file is part of the PM package (pattern match package)
;;; Copyright © 2017,2018 Vladimir Mezentsev
;;;
(defconstant fail nil)
(defconstant no-bindings '((t . t)))
(defconstant +fail+ nil)
(defconstant *no-bindings* no-bindings)
(defconstant +no-bindings+ no-bindings)
(defun variable-p (x)
"Is x a variable (a symbol beginning with `?')?"
;;(print (list 'variable-p x))
(and (symbolp x) (eql (char (symbol-name x) 0) #\?)))
(defun match-variable (var input bindings)
"Does VAR match input? Uses (or updates) and returns bindings."
(let ((binding (get-binding var bindings)))
(cond ((not binding) (extend-bindings var input bindings))
((equal input (binding-val binding)) bindings)
(t fail))))
(defun extend-bindings (var val bindings)
"Add a (var . value) pair to a binding list."
(cons (cons var val)
;; Once we add a "real" binding,
;; we can get rid of the dummy no-bindings
;; note: equal
(if (equal bindings no-bindings)
nil
bindings)))
(defun binding-var (binding)
"Get the variable part of a single binding."
(car binding))
(defun binding-val (binding)
"Get the value part of a single binding."
(cdr binding))
(defun get-binding (var bindings)
"Find a (variable . value) pair in a binding list."
(assoc var bindings))
;;; EOF