Re: Building Unification Table - tranforming prolog like notation into lisp notation
- From: Slobodan Blazeski <slobodan.blazeski@xxxxxxxxx>
- Date: Tue, 29 Jan 2008 10:22:58 -0800 (PST)
On Jan 29, 6:22 pm, Madhu <enom...@xxxxxxxx> wrote:
* Marco Antoniotti Wrote on Tue, 29 Jan 2008 05:30:12 -0800 (PST):It more Prologish but harder to impose convetion to || the STRs.
| On Jan 29, 11:49 am, Slobodan Blazeski wrote:
|> There is one case that I didn't quite understand . Ok let me show you
|> the source of this problem. I'm trying to implement Alin Suciu's Yet
|> another efficient unification algorithm (4 pages). It's described
|> in:http://arxiv.org/PS_cache/cs/pdf/0603/0603080v1.pdfhere's an
|> implementation in chttp://paste.lisp.org/display/54935 The question
|> above is simplified from step 1/ page 1 of the paper. The notation
|> problem comes from tranlating prolog notation into lisp notation.
|> The original notation : p(Z,h(Z,W),f(W)) p(f(X),h(Y,f(a)),Y)
|> Lisp notation: (p Z (h Z W) (f W)) (p (f X) (h Y (f a)) Y)
|
| This is already wrong in ANSI Common Lisp. It will work in Franz
| "Modern" setup but then you will loose other people. Your notation
| turns out to be (P Z (H Z W) (F W)) inside the CL environment. Prolog
| uses (as you may know) capitalized symbols for logical variables.
| You do not have that capability in CL. Hence you *must* resort to
| something like
|
| (p ?z (h ?z ?w) (f ?w))
Or you could just use symbols with lowercase names as |a|:
(defun var-p (symbol)
"Check if SYMBOL is of Suciu's type VAR. Our CL convention is variables are
upper case lisp symbols."
(upper-case-p (elt (symbol-name symbol) 0)))
(deftype variable-term () '(satisfies var-p))
(defun str-p (symbol)
"Check if SYMBOL is of Suciu's type STR. Our convention is that constants
and functors are lower case lisp symbols."
Usually there's fewer variables
than STRs. Also the unification list may come from some other
computation, in order to cope with our convention programmer would
have to travel the whole list andlowercase all the symbols.
It's probably better to stick with ? for practical reasons.
(lower-case-p (elt (symbol-name symbol) 0)))Cool solution something like I started to write,just if I made it
(deftype constant-term () '(satisfies str-p))
;; Here is a quickly done stack based solution for the problem Slobodan
;; originally posted. the iter based solution posted elsewhere in this
;; thread just hurt my eyes too much
;; it should be trivial to convert this function to a stack based one:
(defun principal-term (x)
"Returns the main-functor or variable or constant"
(etypecase x
(cons (main-functor (car x)))
(atom x)))
(defstruct composite-term str indices)
(defun slobodan (x &aux stack result
(var-table (make-hash-table))
(str-alist nil)
(counter 0))
(flet ((rec (item)
(cond ((atom item)
(etypecase item
(composite-term
(push (cons (composite-term-str item) counter) str-alist)
(push (list (composite-term-str item)
counter
(mapcar (lambda (x)
(setq x (principal-term x))
(cond ((str-p x)
(cdr (assoc x str-alist)))
((var-p x)
(gethash x var-table))))
(composite-term-indices item)))
result)
(incf counter))
(variable-term
(unless (gethash item var-table)
(setf (gethash item var-table) counter)
(push (list item counter nil) result)
(incf counter)))
(constant-term
(push (cons item counter) str-alist)
(push (list item counter nil) result)
(incf counter))))
(t
(assert (atom (car item)))
(assert (str-p (car item)))
(push (make-composite-term :str (car item)
:indices (cdr item))
stack)
(loop for elem in (cdr item) do (push elem stack))))))
(assert (= (length x) 2))
(rec (car x))
(rec (cadr x))
(loop while stack do (rec (pop stack)))
(nreverse result)))
* (slobodan '((|p| Z (|h| Z W) (|f| W)) (|p| (|f| X) (|h| Y (|f| |a|)) Y)))
=> ((Y 0 NIL) (|a| 1 NIL) (|f| 2 (1)) (|h| 3 (0 2)) (X 4 NIL) (|f| 5 (4))
(|p| 6 (5 3 0)) (W 7 NIL) (|f| 8 (7)) (Z 9 NIL) (|h| 10 (9 7))
(|p| 11 (9 10 8)))
--
Madhu
work. You didn't pasted the definition of main-functor, could you
please post solution on lisp paste you ca annotate iter based solution
at http://paste.lisp.org/display/54994 for comparasion.
thanks
Slobodan
.
- Follow-Ups:
- References:
- Got stack with collecting
- From: Slobodan Blazeski
- Re: Got stack with collecting
- From: Thomas A. Russ
- Building Unification Table - tranforming prolog like notation into lisp notation
- From: Slobodan Blazeski
- Re: Building Unification Table - tranforming prolog like notation into lisp notation
- From: Marco Antoniotti
- Re: Building Unification Table - tranforming prolog like notation into lisp notation
- From: Madhu
- Got stack with collecting
- Prev by Date: Re: Building Unification Table - tranforming prolog like notation into lisp notation
- Next by Date: Re: Building Unification Table - tranforming prolog like notation into lisp notation
- Previous by thread: Re: Building Unification Table - tranforming prolog like notation into lisp notation
- Next by thread: Re: Building Unification Table - tranforming prolog like notation into lisp notation
- Index(es):
Relevant Pages
|