Commit aa776df1 authored by Marco Antoniotti's avatar Marco Antoniotti

Fixed documentation strings. Added two utility functions.

parent 35747994
......@@ -8,6 +8,7 @@
(in-package "CL.EXT.DACF.UNIFICATION") ; DACF = Data And Control Flow.
;;;---------------------------------------------------------------------------
;;; Bindings.
......@@ -49,6 +50,7 @@
(define-condition unification-variable-unbound (unbound-variable)
()
(:documentation "This is the condition that is signaled")
)
(define-condition unification-failure (simple-error)
......@@ -92,8 +94,13 @@
(defstruct (environment (:print-object print-environment)
(:copier nil))
"The ENVIRONMENT structure.
The data structure containing the associations (bindings) between
variables and values."
(frames () :type list))
(defun print-environment (env stream)
(if *print-readably*
(format stream "#S(ENVIRONMENT FRAMES ~S)"
......@@ -103,7 +110,16 @@
(empty-environment-p env)
(list-length (environment-frames env))))))
(deftype substitution () 'environment)
(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)
(defun substitution-p (x) (environment-p x))
......@@ -144,8 +160,93 @@
(defparameter *null-environment* (make-empty-environment))
(defun find-variable-value (variable &optional (env *null-environment*) errorp)
(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."
(declare (type environment env))
(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))
(labels ((find-var-value (frames)
(cond (frames
(multiple-value-bind (val foundp)
......@@ -185,6 +286,18 @@
v?))
(defun v? (s env &optional (plain-symbol-p nil))
"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.
"
(find-variable-value (if plain-symbol-p
(make-var-name s)
s)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment