;;;======================================================================
;;; NLP code for use with Natural Language Understanding, 2nd ed.
;;; Copyright (C) 1994 James F. Allen
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2, or (at your option)
;;; any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;;======================================================================

;; The extensions needed for semantic interpretation

;;  Semantic Interpretation can be enabled and disabled by the following
;;    functions
(let ((semEnableFlag nil))
  
   (defun semEnabled nil
    semEnableFlag)

  (defun noSemEnabled nil
    (not semEnableFlag))

  (defun enableSem nil
    (setq semEnableFlag t))

  (defun disableSem nil
    (setq semEnableFlag nil)))

;;  New version of make-entry makes one pass at simpligying lambda expressions
;;   each time a consituent is constructed. Note this would not guarantee 
;;   that each constit has the most simplified form. But works well for simple
;;    examples and is correct in any case since simplification is not logically necessary!

(defun make-entry-with-sem (constit start end rhs name)
    (make-entry :constit (sem-simplify constit) 
                :start start :end end :rhs rhs :name name))

(defun sem-simplify (constit)
  (let* ((sem (get-value constit 'sem))
        (newsem (simplify-lambda sem)))
    (if (equal sem newsem)
      constit
      (make-constit :cat (constit-cat constit)
                    :feats (subst newsem sem (constit-feats constit))
                    :head (constit-head constit)))))

;; Simplify lambda expressions
(defun simplify-lambda (expr)
  (cond ((atom expr) expr)
        ((and (listp (car expr))
              (eq (caar expr) 'lambda)
              (cadr expr))
         (simplify-lambda (subst (cadr expr) (cadar expr) (caddar expr))))
        (t (cons (simplify-lambda (car expr)) (simplify-lambda (cdr expr))))))

;; If semantic interpretation is enabled, a discourse variable must be
;;     created for the VAR feature
(defun instantiateVAR (constit)
  (if (semEnabled)
    (subst-in constit (list (list (get-value constit 'VAR) (gen-symbol 'V))))
    constit))

