substitutions.lisp 9.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
;;;; -*- Mode: Lisp -*-

;;;; substitutions.lisp --
;;;; General CL structures unifier.
;;;; Substitution definitions.  Mostly a rehash of the usual SICP stuff.

;;;; See file COPYING for copyright licensing information.

(in-package "CL.EXT.DACF.UNIFICATION") ; DACF = Data And Control Flow.

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
;;;---------------------------------------------------------------------------
;;; Bindings.

(deftype binding () 'cons)

(deftype bindings () 'list) ; An A-LIST.

(defun make-binding (variable value)
  (cons variable value))

(defun extend-bindings (variable value bindings)
  (acons variable value bindings))


(defun binding-variable (b)
  (declare (type binding b))
  (car b))

(defun (setf binding-variable) (v b)
  (declare (type binding b))
  (setf (car b) v))


(defun binding-value (b)
  (declare (type binding b))
  (cdr b))


(defun (setf binding-value) (v b)
  (declare (type binding b))
  (setf (cdr b) v))


(defun bindings-values (bindings) (mapcar #'cdr bindings))

(defun bindings-keys (bindings) (mapcar #'car bindings))



(define-condition unification-variable-unbound (unbound-variable)
  ()
53
  (:documentation "This is the condition that is signaled")
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  )

(define-condition unification-failure (simple-error)
  ())


;;;---------------------------------------------------------------------------
;;; Frames.

(defstruct (frame (:constructor make-frame (&optional bindings)))
  (bindings () :type bindings))

(defun empty-frame-p (f)
  (declare (type frame f))
  (null (frame-bindings f)))


(defun find-variable-binding-in-frame (v f)
  (declare (type frame f))
  (assoc v (frame-bindings f)))


(defun find-variable-value-in-frame (v f)
  (declare (type frame f))
  (let ((b (find-variable-binding-in-frame v f)))
    (declare (type (or null binding) b))
    (if b
        (values (cdr b) t)
        (values nil nil))))

(defun frame-variables (frame)
  (mapcar 'binding-variable (frame-bindings frame)))


(defun frame-values (frame)
  (mapcar 'binding-value (frame-bindings frame)))


;;;---------------------------------------------------------------------------
;;; Environments.

(defstruct (environment (:print-object print-environment)
                        (:copier nil))
97 98 99 100
  "The ENVIRONMENT structure.

The data structure containing the associations (bindings) between
variables and values."
101 102
  (frames () :type list))

103

104 105 106 107 108 109 110 111 112
(defun print-environment (env stream)
  (if *print-readably*
      (format stream "#S(ENVIRONMENT FRAMES ~S)"
              (environment-frames env))
      (print-unreadable-object (env stream :type nil :identity t)
        (format stream "~:[~;EMPTY ~]UNIFY ENVIRONMENT: ~D frame~:P"
                (empty-environment-p env)
                (list-length (environment-frames env))))))

113 114 115 116 117 118 119 120 121 122

(deftype substitution ()
  "The SUBSTITUTION type.

The type representing the associations between variables and values.

Notes:

It is a synonim for the structure type ENVIRONMENT."
  'environment)
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

(defun substitution-p (x) (environment-p x))

(defun first-frame (env)
  (first (environment-frames env)))


(defun make-empty-environment ()
  (make-environment :frames (list (make-frame))))

(defun copy-environment (env)
  (declare (type environment env))
  (make-environment :frames (copy-list (environment-frames env))))

(defun make-shared-environment (env &optional (pushp nil))
  (declare (type environment env))
  (make-environment :frames (if pushp
                                (cons (make-frame) (environment-frames env))
                                (environment-frames env))))

(defun push-frame (env)
  (declare (type environment env))
  (push (make-frame) (environment-frames env)))


(defun pop-frame (env)
  (declare (type environment env))
  (pop (environment-frames env)))


(defun empty-environment-p (env)
  (declare (type environment env))
  (let ((env-frames (environment-frames env)))
    (declare (type list env-frames))
    (and (= 1 (list-length env-frames))
         (empty-frame-p (first env-frames)))))

(defparameter *null-environment* (make-empty-environment))


163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
(defun is-variable-bound (variable &optional (env *null-environment*))
  "Checks whether a VARIABLE is bound in ENV.

The function returns two values: a BOOLEAN that indicates whether
VARIABLE is bound in ENV and its value if so; otherwise the second
value is NIL.

Arguments and Values:

VARIABLE : a unification variable
ENV : and ENVIRONMENT
bounddp : a BOOLEAN
result : a T

Notes:

IS-VARIABLE-BOUND is almost symmetric to FIND-VARIABLE-VALUE."

181
  (declare (type environment env))
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

  (labels ((find-var-value (frames)
             (if frames
                 (let ((vb (find-variable-binding-in-frame
                            variable
                            (first frames)))
                       )
                   (if vb
                       (values t (binding-value vb))
                       (find-var-value (rest frames))))
                 (values nil nil)))
           )
    (find-var-value (environment-frames env))))


(defun find-variable-binding (variable &optional (env *null-environment*))
  "Finds a BINDING for a VARIABLE in ENV.

The function returns a BINDING for VARIABLE in ENV, if it finds one.
Otherwise returns NIL.

Arguments and Values:

VARIABLE : a unification variable
ENV : and ENVIRONMENT
result : a BINDING or NIL."

  (declare (type environment env))

  (labels ((find-var-value (frames)
             (when frames
               (or (find-variable-binding-in-frame variable
                                                   (first frames))
                   (find-var-value (rest frames)))))
           )
    (find-var-value (environment-frames env))))


(defun find-variable-value (variable
                            &optional
                            (env *null-environment*)
                            errorp)
  "Finds the value associated to VARIABLE in ENV.

VARIABLE is a unification variable; if ERRORP is non-null an error is
signaled if VARIABLE is not found in the environment ENV.

The function returns two values: the value associated to VARIABLE or
NIL, and an indication about whether an association was actually found
in ENV.

Arguments and Values:

VARIABLE : a unification variable
ENV : and ENVIRONMENT
ERRORP : a generalized Boolean
result : a T
foundp : a BOOLEAN

Exceptional Situations:

If an error is signaled, then it is an instance of UNIFICATION-VARIABLE-UNBOUND.

Notes:

FIND-VARIABLE-VALUE is almost symmetric to IS-VARIABLE-BOUND."
  (declare (type environment env))

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  (labels ((find-var-value (frames)
             (cond (frames
                    (multiple-value-bind (val foundp)
                        (find-variable-value-in-frame variable (first frames))
                      (if foundp
                          (values val t)
                          (find-var-value (rest frames)))))
                   (errorp
                    (error 'unification-variable-unbound :variable variable))
                   (t (values nil nil))))
           )
    (find-var-value (environment-frames env))))



(defun extend-environment (var pat &optional (env (make-empty-environment)))
  (let ((first-frame (first-frame env)))
    (setf (frame-bindings first-frame)
          (extend-bindings var pat (frame-bindings first-frame)))
    env))


(defun fill-environment (vars pats &optional (env (make-empty-environment)))
  (map nil (lambda (v p) (extend-environment v p env)) vars pats)
  env)


(defun fill-environment* (vars-pats &optional (env (make-empty-environment)))
  (loop for (v . p) in vars-pats do (extend-environment v p env))
  env)


(declaim (inline v?))
(declaim (ftype (function (symbol environment &optional boolean)
                          (values t boolean))
                find-variable-value
                v?))

(defun v? (s env &optional (plain-symbol-p nil))
289 290 291 292 293 294 295 296 297 298 299 300
  "Finds the value associated to the variable S in ENV.

This function is essentialy a convenience wrapper around
FIND-VARIABLE-VALUE.  If PLAIN-SYMBOL-P is non-NIL, then the symbol S
is ensured to be a 'unification variable' (with a #\? first
character).

Exceptional Situations:

A UNIFICATION-VARIABLE-UNBOUND may be signaled is ERRORP is non-nil
and no association si found for S in ENV.
"
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
  (find-variable-value (if plain-symbol-p
                           (make-var-name s)
                           s)
                       env))


(defun environment-variables (env)
  (mapcan #'frame-variables (environment-frames env)))

(defun environment-values (env)
  (mapcan #'frame-values (environment-frames env)))


;;;---------------------------------------------------------------------------
;;; Simple debugging.

(defun dump-frame (f &optional (out *standard-output*))
  (declare (type frame f))
  (loop for (var . value) in (frame-bindings f)
        do (format out "~&~A~VT= ~A~%" var 8 value))
  )

(defun dump-environment (env &optional (out *standard-output*))
  (declare (type environment env))
  (if (empty-environment-p env)
      (format out ">>> Empty unify environment ~S.~%" env)
      (loop initially (format out ">>> Dumping unify environment ~S.~%" env)
            for fr in (environment-frames env)
            for fr-n downfrom (list-length (environment-frames env))
            do (format out ">>> Frame ~D:~%" fr-n)
            do (dump-frame fr out)
            do (terpri out)
            )))

;;;; end of file -- substitutions.lisp --