4
|
1 ;;; tl-atype.el --- atype functions
|
|
2
|
|
3 ;; Copyright (C) 1994,1995,1996 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
|
|
6 ;; Version: $Id: tl-atype.el,v 1.1.1.1 1996/12/18 03:55:31 steve Exp $
|
|
7 ;; Keywords: atype
|
|
8
|
|
9 ;; This file is part of tl (Tiny Library).
|
|
10
|
|
11 ;; This program is free software; you can redistribute it and/or
|
|
12 ;; modify it under the terms of the GNU General Public License as
|
|
13 ;; published by the Free Software Foundation; either version 2, or (at
|
|
14 ;; your option) any later version.
|
|
15
|
|
16 ;; This program is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
|
25
|
|
26 ;;; Code:
|
|
27
|
|
28 (require 'emu)
|
|
29 (require 'tl-str)
|
|
30 (require 'tl-list)
|
|
31
|
|
32
|
|
33 ;;; @ field
|
|
34 ;;;
|
|
35
|
|
36 (defalias 'fetch-field 'assoc)
|
|
37 (defalias 'fetch-field-value 'assoc-value)
|
|
38 (defalias 'put-field 'put-alist)
|
|
39 (defalias 'delete-field 'del-alist)
|
|
40
|
|
41 (defun put-fields (tp c)
|
|
42 (catch 'tag
|
|
43 (let ((r tp) f ret)
|
|
44 (while r
|
|
45 (setq f (car r))
|
|
46 (if (not (if (setq ret (fetch-field (car f) c))
|
|
47 (equal (cdr ret)(cdr f))
|
|
48 (setq c (cons f c))
|
|
49 ))
|
|
50 (throw 'tag 'error))
|
|
51 (setq r (cdr r))
|
|
52 ))
|
|
53 c))
|
|
54
|
|
55
|
|
56 ;;; @ field unifier
|
|
57 ;;;
|
|
58
|
|
59 (defun field-unifier-for-default (a b)
|
|
60 (let ((ret
|
|
61 (cond ((equal a b) a)
|
|
62 ((null (cdr b)) a)
|
|
63 ((null (cdr a)) b)
|
|
64 )))
|
|
65 (if ret
|
|
66 (list nil ret nil)
|
|
67 )))
|
|
68
|
|
69 (defun field-unify (a b)
|
|
70 (let ((sym (symbol-concat "field-unifier-for-" (car a))))
|
|
71 (if (not (fboundp sym))
|
|
72 (setq sym (function field-unifier-for-default))
|
|
73 )
|
|
74 (funcall sym a b)
|
|
75 ))
|
|
76
|
|
77
|
|
78 ;;; @ type unifier
|
|
79 ;;;
|
|
80
|
|
81 (defun assoc-unify (class instance)
|
|
82 (catch 'tag
|
|
83 (let ((cla (copy-alist class))
|
|
84 (ins (copy-alist instance))
|
|
85 (r class)
|
|
86 cell aret ret prev rest)
|
|
87 (while r
|
|
88 (setq cell (car r))
|
|
89 (setq aret (fetch-field (car cell) ins))
|
|
90 (if aret
|
|
91 (if (setq ret (field-unify cell aret))
|
|
92 (progn
|
|
93 (if (car ret)
|
|
94 (setq prev (put-field (car (car ret))
|
|
95 (cdr (car ret))
|
|
96 prev))
|
|
97 )
|
|
98 (if (nth 2 ret)
|
|
99 (setq rest (put-field (car (nth 2 ret))
|
|
100 (cdr (nth 2 ret))
|
|
101 rest))
|
|
102 )
|
|
103 (setq cla (put-field (car cell)(cdr (nth 1 ret)) cla))
|
|
104 (setq ins (delete-field (car cell) ins))
|
|
105 )
|
|
106 (throw 'tag nil)
|
|
107 ))
|
|
108 (setq r (cdr r))
|
|
109 )
|
|
110 (setq r (copy-alist ins))
|
|
111 (while r
|
|
112 (setq cell (car r))
|
|
113 (setq aret (fetch-field (car cell) cla))
|
|
114 (if aret
|
|
115 (if (setq ret (field-unify cell aret))
|
|
116 (progn
|
|
117 (if (car ret)
|
|
118 (setq prev (put-field (car (car ret))
|
|
119 (cdr (car ret))
|
|
120 prev))
|
|
121 )
|
|
122 (if (nth 2 ret)
|
|
123 (setq rest (put-field (car (nth 2 ret))
|
|
124 (cdr (nth 2 ret))
|
|
125 rest))
|
|
126 )
|
|
127 (setq cla (delete-field (car cell) cla))
|
|
128 (setq ins (put-field (car cell)(cdr (nth 1 ret)) ins))
|
|
129 )
|
|
130 (throw 'tag nil)
|
|
131 ))
|
|
132 (setq r (cdr r))
|
|
133 )
|
|
134 (list prev (append cla ins) rest)
|
|
135 )))
|
|
136
|
|
137 (defun get-unified-alist (db al)
|
|
138 (let ((r db) ret)
|
|
139 (catch 'tag
|
|
140 (while r
|
|
141 (if (setq ret (nth 1 (assoc-unify (car r) al)))
|
|
142 (throw 'tag ret)
|
|
143 )
|
|
144 (setq r (cdr r))
|
|
145 ))))
|
|
146
|
|
147 (defun delete-atype (atl al)
|
|
148 (let* ((r atl) ret oal)
|
|
149 (setq oal
|
|
150 (catch 'tag
|
|
151 (while r
|
|
152 (if (setq ret (nth 1 (assoc-unify (car r) al)))
|
|
153 (throw 'tag (car r))
|
|
154 )
|
|
155 (setq r (cdr r))
|
|
156 )))
|
|
157 (delete oal atl)
|
|
158 ))
|
|
159
|
|
160 (defun remove-atype (sym al)
|
|
161 (and (boundp sym)
|
|
162 (set sym (delete-atype (eval sym) al))
|
|
163 ))
|
|
164
|
|
165 (defun replace-atype (atl old-al new-al)
|
|
166 (let* ((r atl) ret oal)
|
|
167 (if (catch 'tag
|
|
168 (while r
|
|
169 (if (setq ret (nth 1 (assoc-unify (car r) old-al)))
|
|
170 (throw 'tag (rplaca r new-al))
|
|
171 )
|
|
172 (setq r (cdr r))
|
|
173 ))
|
|
174 atl)))
|
|
175
|
|
176 (defun set-atype (sym al &rest options)
|
|
177 (if (null (boundp sym))
|
|
178 (set sym al)
|
|
179 (let* ((replacement (memq 'replacement options))
|
|
180 (ignore-fields (car (cdr (memq 'ignore options))))
|
|
181 (remove (or (car (cdr (memq 'remove options)))
|
|
182 (let ((ral (copy-alist al)))
|
|
183 (mapcar (function
|
|
184 (lambda (type)
|
|
185 (setq ral (del-alist type ral))
|
|
186 ))
|
|
187 ignore-fields)
|
|
188 ral)))
|
|
189 )
|
|
190 (set sym
|
|
191 (or (if replacement
|
|
192 (replace-atype (eval sym) remove al)
|
|
193 )
|
|
194 (cons al
|
|
195 (delete-atype (eval sym) remove)
|
|
196 )
|
|
197 )))))
|
|
198
|
|
199
|
|
200 ;;; @ end
|
|
201 ;;;
|
|
202
|
|
203 (provide 'tl-atype)
|
|
204
|
|
205 ;;; tl-atype.el ends here
|