File: bmodexp.lisp

package info (click to toggle)
cafeobj 1.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 19,900 kB
  • sloc: lisp: 85,055; sh: 659; makefile: 437; perl: 147
file content (350 lines) | stat: -rw-r--r-- 12,236 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
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
53
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
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
289
290
291
292
293
294
295
296
297
298
299
300
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*-
;;;
;;; Copyright (c) 2000-2015, Toshimi Sawada. All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;;   * Redistributions of source code must retain the above copyright
;;;     notice, this list of conditions and the following disclaimer.
;;;
;;;   * Redistributions in binary form must reproduce the above
;;;     copyright notice, this list of conditions and the following
;;;     disclaimer in the documentation and/or other materials
;;;     provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;
(in-package :chaos)
#|==============================================================================
                                 System: CHAOS
                               Module: primitives
                               File: bmodexp.lisp
==============================================================================|#
#-:chaos-debug
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
#+:chaos-debug
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))

;;;*****************************************************************************
;;; MODULE EXPRESSION & VIEW ***************************************************
;;;*****************************************************************************

;;; MODULE EXPRESSION___________________________________________________________
;;; <Modexpr> ::= Identifier              -- simple module name
;;;            |  <Modexpr> * <RenameMap> -- module rename
;;;            |  <Modexpr> + <Modexpr>+  -- module sum
;;;            |  <Modexpr> [ Args ]      -- instantiation
;;;
;;; module MODEXPR {
;;;  signature {
;;;    [ Identifier < ModExpr ]
;;;    [ Idetifier < ViewMap,
;;;      SortMap OpMap <  MapList ]
;;;    op _+_  : ModExpr ModExpr -> ModExpr
;;;    op _*_  : ModExpr MapList -> ModExpr
;;;    op _[_] : ModExpr ViewMap -> ModExpr
;;;    attr _+_ { assoc comm idem l-assoc prec: 3 }
;;;    attr _*_ { l-assoc prec: 2 }
;;;    attr _[_] { l-assoc prec: 1 }
;;;
;;;    op (sort_->_) : Identifier Identifier -> SortMap
;;;    op (op_->_) : Identifier Identifier -> OpMap
;;;    op null-map : -> MapList
;;;    op _,_ : MapList MapList -> MapList
;;;    -- really, _,_ is idempotent : see `axioms' below.
;;;    attr _,_ { assoc comm id: null-map }
;;;
;;;    op (view from_to_{_}) : ModExpr ModExpr MapList -> ViewMap
;;;
;;;    }
;;;
;;;   axioms {
;;;     vars X Y Z : Identifier
;;;     var L : MapList
;;;
;;;     eq sort X -> Y, L, sort X -> Z = L, sort X -> Z  .
;;;     eq op X -> Y, L, op X -> Z = L, op X -> Z .
;;;    }
;;;
;;;  }
;;;
;;; * NOTE *
;;; there are some more forms for modexpr. 

;;;-----------------------------------------------------------------------------
;;; MODULE EXPRESSION
;;; :modexp-value holds internal form of each specific type of module expression.
;;; used only for representing top-level modexpr.
;;; ** NOT USED.
;;;-----------------------------------------------------------------------------
(deftype modexp () '(or simple-string list int-instantiation int-plus int-rename view-struct module))

(defparameter .modexp-keywords. '(%modexp %error %plus %rename %instantiation %view))
                                  
(defun is-modexp? (object)
  (or (stringp object)
      (and (listp object)
           (memq (car object) .modexp-keywords.))))

(defterm modexp (%ast)
  :visible (value)
  :print print-modexp)

;;;-----------------------------------------------------------------------------
;;; RENAME MAPS -- redundant, not used now.
;;; sort/operator/parameter rename maps.
;;;  map : sequence of map (%ren-sort, %ren-op or, %ren-param or %vars)
;;;
(defterm rmap (%ast)
  :visible (map)
  :print print-rename-map)

;;; REN-SORT represents sort mapping.
;;;   maps   : List[pair of source & target]
;;;     - source : sort-ref
;;;     - target : string representing new sort name.
;;;
(defterm ren-sort (%ast)                ; visible sort
  ;; :visible (source target)
  :visible (&rest maps)
  :print print-ren-sort)

(defterm ren-hsort (%ast)               ; hidden sort
  ;; :visible (source target)
  :visible (&rest maps)
  :print print-ren-sort)

;;; REN-OP represents operator mapping.
;;;   maps  : List [ pair of source & target ]
;;;    - source : op-ref.
;;;   -  target : string or list of string representing new operator name.
;;;
(defterm ren-op (%ast)                  ; functional operator
  ;; :visible (source target)
  :visible (&rest maps)
  :print print-ren-op)

(defterm ren-bop (%ast)                 ; behavioural operator
  ;; :visible (source target)
  :visible (&rest maps)
  :print print-ren-op)

;;; VARS
;;;
(defterm vars (%ast)
  :visible (elements)
  :print print-vars-ast)

;;; REN-PARAM
;;; parameter renaming.
;;; source : string 
;;; target : string
;;;
(defterm ren-param (%ast)
  ;; :visible (source target)
  :visible (&rest maps)
  :print print-ren-param)

;;;-----------------------------------------------------------------------------
;;; PLUS : modexp + modexp
;;; module sum.
;;;-----------------------------------------------------------------------------
(defterm + (%ast)
  :name plus
  ;; :visible (plus1 plus2)
  :visible (&rest args)
  :print print-plus-modexp)

;;;-----------------------------------------------------------------------------
;;; RENAME : modexp * { rename_map }
;;;
;;; pre-module-expression level:
;;;   %rename-map ::= { (%ren-sort (a b) (c d) ...) |
;;;                     (%ren-op (x y) (p q) ...)   |
;;;                     (%ren-param (s t) (u v) ...}*)
;;;-----------------------------------------------------------------------------
(defterm * (%ast)
  :name rename
  :visible (module map)
  :print print-rename-modexp)

;;;-----------------------------------------------------------------------------
;;; INSTANTIATION : modexp[args]
;;; an instatiation of parameterized module.
;;;-----------------------------------------------------------------------------
;;; args ::= LIST[ Argname . View]
;;;
(defterm ! (%ast)
  :name instantiation
  :visible (module args)
  :print print-instantiation-modexp)

;;; argument
(defterm !arg (%ast)
  :visible (name view)
  :print print-instantiation-arg)

;;; parse view argument in instantiation
;;; -- NOTE : arg can be a single modexp without any mappings.
;;;           this can be a name of view(string), or arbitrary module expression.
;;;           in this case we construct a special modexp ?name
;;;
(defmacro make-?-name (_modexp_)  `(cons ':?name ,_modexp_))

(defun modexp-is-?name? (modexp)
  (declare (type t modexp)
           (values (or null t)))
  (and (consp modexp) (eq (car modexp) ':?name)))

(defun ?name-name (modexp)
  (cdr modexp))

;;; MODEXP-IS-PARAMETER-THEORY : MODEXP -> BOOL
;;; The form '(string :: modexp ..) is used for representing a module which is a
;;; parameter.
;;;
(defun modexp-is-parameter-theory (e)
  (declare (type t e)
           (values (or null t)))
  (and (consp e)
       (equal "::" (cadr e))))

;;; ****
;;; VIEW
;;; ****

(defterm view (%ast)
  :visible (module                      ; theory module
            target                      ; target module
            map)                        ; mappings
  :print print-view-modexp)

;;; ********************
;;; PREDICATES ON MODEXP________________________________________________________
;;; ********************

;;; MODEXP-IS-ERROR : Modexpr -> Bool
;;; returns t iff the given modexp is error;
;;; the form (:error ....) is used for representing invalid module expressions.
;;;
(defun modexp-is-error (val)
  (declare (type t)
           (values (or null t)))
  (and (consp val) (eq :error (car val))))

;;; MODEXP-IS-SIMPLE-NAME : object -> Bool
;;; returns t iff given object is a 
;;;
(defun modexp-is-simple-name (x)
  (declare (type (or atom modexp) x)
           (values (or null t)))
  (or (stringp x)
      (symbolp x)
      (modexp-is-parameter-theory x)))

;;; *********
;;; MODEXP DB___________________________________________________________________
;;; *********

;;; *MODEXP-VIEW-TABLE* --------------------------------------------------------
;;; alias mapping : canonicalized view expressions -> views.
;;;-----------------------------------------------------------------------------
(declaim (type list *modexp-view-table*))

(defvar *modexp-view-table* nil)

#||
(eval-when (:execute :load-toplevel)
  (setq *modexp-view-table* (make-hash-table :test #'eq)))

(defun find-view-in-env (view)
  (gethash view *modexp-view-table*)
  )

(defun add-view-defn (view value)
  (setf (gethash view *modexp-view-table*) value)
  )

||#

(defun find-view-in-env (view)
  (declare (type modexp view)
           (values (or null t)))
  (find-in-assoc-table *modexp-view-table* view))

(defun add-view-defn (view value)
  (add-to-assoc-table *modexp-view-table* view value))

;;; ********
;;; MODMORHP____________________________________________________________________
;;; ********

;;; IMPLEMENTATION -------------------------------------------------------------
;;; sort : sort mapping, assoc list (source-sort . image-sort).
;;; op   : operator mapping,
;;;        - simple map: renaming modexp also uses this 
;;;          (method :simple-map . method)
;;;        - map to term: general mapping, used by views
;;;          (method :replacement psuedo-variables target-pattern)
;;; module : module replacement, assoc list of (module . module).
;;;
(defstruct (modmorph (:constructor create-modmorph (name sort op module)))
   (name nil :type t)                   ; name of mapping; e.g. name of view or
                                        ; module. (taken from may be more than
                                        ; one view).
   (sort nil :type list)                ; sort map.
   (op nil :type list)                  ; operator map, really is a method map.
   (module nil :type list)              ; module map
   )

;;; TODO : ugly
(defun modmorph-is-rename (map)
  (let ((nm (modmorph-name map)))
    (and (chaos-ast? nm) (eq '%* (ast-type nm)))))

(defmacro  modmorph-assoc-image (_assoc-list _x)
  (once-only (_x)
    ` (or (cdr (assq ,_x ,_assoc-list)) ,_x)))

(defmacro modmorph-assoc-images (_assoc-list _lst)
  ` (mapcar #'(lambda (x)
                (or (cdr (assq x ,_assoc-list))
                    x))
            ,_lst))

;;; ******************************
;;; TOPLEVEL MODEXP INTERNAL FORMS
;;; ******************************
;;; here are internal forms, i.e., modexp with arguments are all evaluated).
;;; these are stored as names of modules, used for reconstructing modexp (internal form)
;;; **********
;;; RENAME MAP__________________________________________________________________
;;; **********

;;; Rename map is used for representing both module renaming and view.
;;; It's just a specification morphism. Applying a map to a signature/module
;;; results to a new signature/module. Theoretically, it is a map (function) 
;;; whose domain is a pair < Sorts, OPS > where Sorts is a set of sort symbols
;;; and OPS is a set of operator names. 

;;; the elements of a map is represented as the followings:
;;; <RENAME-MAP-ELT> ::= (%ren-sort <SortRef> <SortRef>) 
;;;                   |  (%ren-op <OpRef> <OpRef>)
;;;                   |  (%ren-op <Op-Pttern> <Op-Pattern>)
;;;                   |  (%ren-param <ParamName> <ParamName>)
;;;                   |  (%vars (<VarName> ..) <SortRef>)
;;;

;;; EOF