Codebase list cafeobj / 6f133ad
1. Optimized term parser for not accumulate same forms for possible arguments. 2. Modified old examples for adapting the current syntax. tswd 11 years ago
17 changed file(s) with 272 addition(s) and 258 deletion(s). Raw diff Collapse all Expand all
1212
1313 PACKAGE = cafeobj
1414 VERSION = 1.4
15 VMINOR = .9rc8
15 VMINOR = .9rc9
1616 VMEMO = PigNose0.99
1717 PATCHLEVEL =
1818
16941694
16951695 PACKAGE=cafeobj
16961696 VERSION=1.4
1697 VMINOR=.9rc8
1697 VMINOR=.9rc9
16981698 VMEMO=PigNose0.99
16991699 PATCHLEVEL=
17001700
1313 'configure.in'
1414 ],
1515 {
16 'AM_PROG_F77_C_O' => 1,
1617 '_LT_AC_TAGCONFIG' => 1,
17 'AM_PROG_F77_C_O' => 1,
18 'm4_pattern_forbid' => 1,
1819 'AC_INIT' => 1,
19 'm4_pattern_forbid' => 1,
20 'AC_CANONICAL_TARGET' => 1,
2021 '_AM_COND_IF' => 1,
21 'AC_CANONICAL_TARGET' => 1,
22 'AC_CONFIG_LIBOBJ_DIR' => 1,
2223 'AC_SUBST' => 1,
23 'AC_CONFIG_LIBOBJ_DIR' => 1,
24 'AC_CANONICAL_HOST' => 1,
2425 'AC_FC_SRCEXT' => 1,
25 'AC_CANONICAL_HOST' => 1,
2626 'AC_PROG_LIBTOOL' => 1,
2727 'AM_INIT_AUTOMAKE' => 1,
28 'AC_CONFIG_SUBDIRS' => 1,
2829 'AM_PATH_GUILE' => 1,
29 'AC_CONFIG_SUBDIRS' => 1,
3030 'AM_AUTOMAKE_VERSION' => 1,
3131 'LT_CONFIG_LTDL_DIR' => 1,
32 'AC_CONFIG_LINKS' => 1,
3233 'AC_REQUIRE_AUX_FILE' => 1,
33 'AC_CONFIG_LINKS' => 1,
34 'LT_SUPPORTED_TAG' => 1,
3435 'm4_sinclude' => 1,
35 'LT_SUPPORTED_TAG' => 1,
3636 'AM_MAINTAINER_MODE' => 1,
3737 'AM_NLS' => 1,
3838 'AC_FC_PP_DEFINE' => 1,
3939 'AM_GNU_GETTEXT_INTL_SUBDIR' => 1,
40 '_m4_warn' => 1,
4041 'AM_MAKEFILE_INCLUDE' => 1,
41 '_m4_warn' => 1,
4242 'AM_PROG_CXX_C_O' => 1,
43 '_AM_MAKEFILE_INCLUDE' => 1,
4344 '_AM_COND_ENDIF' => 1,
44 '_AM_MAKEFILE_INCLUDE' => 1,
4545 'AM_ENABLE_MULTILIB' => 1,
4646 'AM_SILENT_RULES' => 1,
4747 'AM_PROG_MOC' => 1,
4848 'AC_CONFIG_FILES' => 1,
49 'LT_INIT' => 1,
4950 'include' => 1,
50 'LT_INIT' => 1,
51 'AM_GNU_GETTEXT' => 1,
5152 'AM_PROG_AR' => 1,
52 'AM_GNU_GETTEXT' => 1,
5353 'AC_LIBSOURCE' => 1,
54 'AC_CANONICAL_BUILD' => 1,
5455 'AM_PROG_FC_C_O' => 1,
55 'AC_CANONICAL_BUILD' => 1,
5656 'AC_FC_FREEFORM' => 1,
57 'AC_FC_PP_SRCEXT' => 1,
5758 'AH_OUTPUT' => 1,
58 'AC_FC_PP_SRCEXT' => 1,
59 'AC_CONFIG_AUX_DIR' => 1,
5960 '_AM_SUBST_NOTMAKE' => 1,
60 'AC_CONFIG_AUX_DIR' => 1,
61 'm4_pattern_allow' => 1,
62 'AM_PROG_CC_C_O' => 1,
6163 'sinclude' => 1,
62 'AM_PROG_CC_C_O' => 1,
63 'm4_pattern_allow' => 1,
64 'AM_CONDITIONAL' => 1,
65 'AC_CANONICAL_SYSTEM' => 1,
6466 'AM_XGETTEXT_OPTION' => 1,
65 'AC_CANONICAL_SYSTEM' => 1,
66 'AM_CONDITIONAL' => 1,
6767 'AC_CONFIG_HEADERS' => 1,
6868 'AC_DEFINE_TRACE_LITERAL' => 1,
6969 'AM_POT_TOOLS' => 1,
216216 (format t "~&dictionary info token = ~s" token))
217217 (let ((res nil)
218218 (mod-token nil))
219 (cond ((consp token)
220 ;; special builtin tokens
221 (setq res (list (info-for-special-builtins token))))
222 ;; normal token
223 (t (setq res (dictionary-get-token-info (dictionary-table dictionary)
219 (block collect
220 (cond ((consp token)
221 ;; special builtin tokens
222 (setq res (list (info-for-special-builtins token))))
223 ;; normal token
224 (t (setq res (dictionary-get-token-info (dictionary-table dictionary)
224225 token))
225 ;; blocked let variable?
226 ;; *TODO*
226 ;; blocked let variable?
227 ;; *TODO*
227228
228 ;; bound variable?
229 (catch 'term-context-error
230 (let ((val (get-bound-value token)))
231 (when val
232 (if (is-special-let-variable? token)
233 (push val res)
229 ;; bound variable?
230 (catch 'term-context-error
231 (let ((val (get-bound-value token)))
232 (when val
233 (if (is-special-let-variable? token)
234 (push val res)
234235 (push (simple-copy-term-sharing-variables
235236 val dictionary)
236237 res)))))
237
238 ;; try other possiblities.
239 ;; variable ?
240 (let ((res2 (assoc (intern token) *parse-variables*)))
241 (cond
242 (res2
243 ;; there's a registered variable with name 'token', accumlate
244 ;; it. now that vars are left in modules want
245 ;; *parser-variables* to replace.
246 (when *on-parse-debug*
247 (format t "~&found var : ~s" (car res2)))
248 (setq res (cons (cdr res2) (dictionary-delete-vars res)))
249 )
250 (t
251 ;; check sort qualified variable reference
252 ;; = on-the-fly (dynamic) variable declaration.
253 ;;
254 (let ((q-pos (position #\: (the simple-string token)
255 :from-end t)))
256 (declare (type (or null fixnum) q-pos))
257 (cond ((and q-pos
258 (not (zerop (the fixnum q-pos)))
259 (not (= (the fixnum q-pos)
260 (the fixnum
261 (1- (length
262 (the simple-string token)))))))
263 (let ((sort nil)
264 (var-name nil)
265 (var nil))
266 ;; assumes the token is qualified vriable
267 ;; declaration.
268 (setq var-name (subseq (the simple-string token)
269 0
270 (the fixnum q-pos)))
271 ;;; (setf var-name (intern var-name))
272 (setf sort (find-sort-in *current-module*
273 (subseq
274 (the simple-string token)
275 (1+ (the fixnum q-pos)))))
276 (unless sort
277 (with-output-chaos-error ('no-such-sort)
278 (format t "unknown sort ~a for variable form ~a."
279 (subseq token (1+ q-pos))
280 token)
281 ))
282 (let ((bi (check-var-name-overloading-with-builtin
283 var-name sort *current-module*)))
284 (when bi
285 (with-output-chaos-warning ()
286 (format t "declaring variable ~s on the fly,"
287 var-name)
288 (print-next)
289 (princ "the name is conflicting with built-in constant of sort ")
290 (print-sort-name bi *current-module*)
291 (princ ".")
292 (terpri)
293 )))
294 ;;
295 (let ((gv (dictionary-get-token-info
296 (dictionary-table dictionary)
297 var-name)))
298 (when gv
299 (dolist (op-v gv)
300 (when (eq (object-syntactic-type op-v)
301 'variable)
302 (with-output-chaos-error ('already-used-name)
303 (format t "~&on the fly variable name ~A is already used for static variable declaration..." var-name))))))
304 ;; OK
305 (setq var-name (intern var-name))
306 ;; success parsing it as a variable declaration.
307 ;; checks if there alredy a variable with the same
308 ;; name.
309 (when *on-parse-debug*
310 (format t "~&on-the-fly var decl: ~A" var-name)
311 (format t "... ~A" *parse-variables*))
312 (let ((old-var (assoc var-name *parse-variables*)))
313 (if old-var
314 (unless (sort= (variable-sort (cdr old-var))
315 sort)
316 (with-output-chaos-error ()
317 (format t "on the fly variable ~A has been declared as sort ~A, but now being redefined as sort ~A.~%"
318 (string var-name)
319 (string (sort-id
320 (variable-sort (cdr
321 old-var))))
322 (string (sort-id sort))))
323 ;;(setf (cdr old-var)
324 ;; (make-variable-term sort var-name))
325 )
326 (progn
327 ;; check name, if it start with `, we make
328 ;; pseudo variable
329 (if (eql #\` (char (the simple-string
330 (string var-name))
331 0))
332 (setf var (make-pvariable-term sort
333 var-name))
334 (setf var (make-variable-term sort
335 var-name)))
336 (push (cons var-name var) *parse-variables*)))
337 (if old-var
338 (progn
339 (push (cdr old-var) res)
340 #||
341 (when (err-sort-p (variable-sort
342 (cdr old-var)))
343 (pushew (cdr old-var)
344 (module-error-variables
345 *current-module*)))
346 ||#
347 )
348 (progn
349 (push var res)
350 #||
351 (when (err-sort-p (variable-sort var))
352 (pushnew var (module-error-variables
353 *current-module*)))
354 ||#
355 )))))
356
357 ;; must not be a variable declaration.
358 ;; try yet other possibilities.
359 (t (let ((pos nil))
360 ;; builtin constant tokens?
361 (dolist (bi (dictionary-builtins dictionary))
362 (let ((token-pred (bsort-token-predicate bi)))
363 (when (and token-pred
364 (funcall token-pred token))
365 (push bi pos))))
366 (if pos
367 ;; may be builtin constant.
368 (if (cdr pos)
369 (let ((so (module-sort-order
370 *current-module*)))
371 (dolist (bi pos)
372 (unless (some #'(lambda (x)
373 (sort< x bi so))
374 pos)
375 (push
376 (dictionary-make-builtin-constant
377 token bi)
378 res))))
379 (push (dictionary-make-builtin-constant
380 token
381 (car pos))
382 res))
383
384 ;; no possibility for vairable nor builtin
385 ;; constant.
386 (let ((ast (gethash token *builtin-ast-dict*)))
387 (if ast
388 ;; abstract syntax tree.
389 (setf res ast))
390 (unless res
391 (multiple-value-setq (res mod-token)
392 (get-qualified-op-pattern token)))
393 ;;
394 (when (and (null res)
395 (memq *identifier-sort*
396 (module-all-sorts
397 *current-module*)))
398 (let ((ident (intern token)))
399 (unless (member ident '(|.| |,|
400 |(| |)|
401 |{| |}|
402 |[| |]|))
403 (push (make-bconst-term
404 *identifier-sort* ident)
405 res))))))))
406 )))))
407 ;; #||
408 ;;; final possibility
409 (unless res
410 (multiple-value-setq (res mod-token)
411 (get-qualified-op-pattern token)))
412 ;; ||#
413 ))
238 ;;----
239 ;; (when res (return-from collect nil))
240 ;;----
241
242 ;; try other possiblities.
243 ;; variable ?
244 (let ((res2 (assoc (intern token) *parse-variables*)))
245 (cond (res2
246 ;; there's a registered variable with name 'token', accumlate
247 ;; it. now that vars are left in modules want
248 ;; *parser-variables* to replace.
249 (when *on-parse-debug*
250 (format t "~&found var : ~s" (car res2)))
251 (setq res (cons (cdr res2) (dictionary-delete-vars res))))
252 (t
253 ;; check sort qualified variable reference
254 ;; = on-the-fly (dynamic) variable declaration.
255 ;;
256 (let ((q-pos (position #\: (the simple-string token)
257 :from-end t)))
258 (declare (type (or null fixnum) q-pos))
259 (cond ((and q-pos
260 (not (zerop (the fixnum q-pos)))
261 (not (= (the fixnum q-pos)
262 (the fixnum
263 (1- (length
264 (the simple-string token)))))))
265 (let ((sort nil)
266 (var-name nil)
267 (var nil))
268 ;; assumes the token is qualified vriable
269 ;; declaration.
270 (setq var-name (subseq (the simple-string token)
271 0
272 (the fixnum q-pos)))
273 ;; (setf var-name (intern var-name))
274 (setf sort (find-sort-in *current-module*
275 (subseq
276 (the simple-string token)
277 (1+ (the fixnum q-pos)))))
278 (unless sort
279 (when res (return-from collect nil))
280 (with-output-chaos-error ('no-such-sort)
281 (format t "unknown sort ~a for variable form ~a."
282 (subseq token (1+ q-pos))
283 token)))
284 (let ((bi (check-var-name-overloading-with-builtin
285 var-name sort *current-module*)))
286 (when bi
287 (with-output-chaos-warning ()
288 (format t "declaring variable ~s on the fly,"
289 var-name)
290 (print-next)
291 (princ "the name is conflicting with built-in constant of sort ")
292 (print-sort-name bi *current-module*)
293 (princ ".")
294 (terpri))))
295 ;;
296 (let ((gv (dictionary-get-token-info
297 (dictionary-table dictionary)
298 var-name)))
299 (when gv
300 (dolist (op-v gv)
301 (when (eq (object-syntactic-type op-v)
302 'variable)
303 (with-output-chaos-error ('already-used-name)
304 (format t "~&on the fly variable name ~A is already used for static variable declaration..." var-name))))))
305 ;; OK
306 (setq var-name (intern var-name))
307 ;; success parsing it as a variable declaration.
308 ;; checks if there alredy a variable with the same
309 ;; name.
310 (when *on-parse-debug*
311 (format t "~&on-the-fly var decl: ~A" var-name)
312 (format t "... ~A" *parse-variables*))
313 (let ((old-var (assoc var-name *parse-variables*)))
314 (if old-var
315 (unless (sort= (variable-sort (cdr old-var))
316 sort)
317 (with-output-chaos-error ()
318 (format t "on the fly variable ~A has been declared as sort ~A, but now being redefined as sort ~A.~%"
319 (string var-name)
320 (string (sort-id
321 (variable-sort (cdr
322 old-var))))
323 (string (sort-id sort))))
324 ;;(setf (cdr old-var)
325 ;; (make-variable-term sort var-name))
326 )
327 (progn
328 ;; check name, if it start with `, we make
329 ;; pseudo variable
330 (if (eql #\` (char (the simple-string
331 (string var-name))
332 0))
333 (setf var (make-pvariable-term sort
334 var-name))
335 (setf var (make-variable-term sort
336 var-name)))
337 (push (cons var-name var) *parse-variables*)))
338 (if old-var
339 (progn
340 (push (cdr old-var) res)
341 #||
342 (when (err-sort-p (variable-sort
343 (cdr old-var)))
344 (pushew (cdr old-var)
345 (module-error-variables
346 *current-module*)))
347 ||#
348 )
349 (progn
350 (push var res)
351 #||
352 (when (err-sort-p (variable-sort var))
353 (pushnew var (module-error-variables
354 *current-module*)))
355 ||#
356 )))))
357
358 ;; must not be a variable declaration.
359 ;; try yet other possibilities.
360 (t (let ((pos nil))
361 ;; builtin constant tokens?
362 (dolist (bi (dictionary-builtins dictionary))
363 (let ((token-pred (bsort-token-predicate bi)))
364 (when (and token-pred
365 (funcall token-pred token))
366 (push bi pos))))
367 (if pos
368 ;; may be builtin constant.
369 (if (cdr pos)
370 (let ((so (module-sort-order
371 *current-module*)))
372 (dolist (bi pos)
373 (unless (some #'(lambda (x)
374 (sort< x bi so))
375 pos)
376 (push
377 (dictionary-make-builtin-constant
378 token bi)
379 res))))
380 (push (dictionary-make-builtin-constant
381 token
382 (car pos))
383 res))
384
385 ;; no possibilities of vairable nor builtin
386 ;; constant.
387 (let ((ast (gethash token *builtin-ast-dict*)))
388 (if ast
389 ;; abstract syntax tree.
390 (setf res ast))
391 (unless res
392 (multiple-value-setq (res mod-token)
393 (get-qualified-op-pattern token)))
394 ;;
395 (when (and (null res)
396 (memq *identifier-sort*
397 (module-all-sorts
398 *current-module*)))
399 (let ((ident (intern token)))
400 (unless (member ident '(|.| |,|
401 |(| |)|
402 |{| |}|
403 |[| |]|))
404 (push (make-bconst-term
405 *identifier-sort* ident)
406 res))))))))
407 )))))
408 ;; #||
409 ;; final possibility
410 (unless res
411 (multiple-value-setq (res mod-token)
412 (get-qualified-op-pattern token)))
413 ;; ||#
414 )))
414415 ;;
415416 (when sort-constraint
416417 (let ((real-res nil))
15261527 (print-in-progress "!"))
15271528 (or res arg-acc-list-prime))
15281529 ||#
1529 arg-acc-list-prime
1530 ))
1530 arg-acc-list-prime))
15311531
15321532 ;;; op parser-collect-one-argument :
15331533 ;;; LIST[ ( ChaosTermList . TokenList ) ] -- not empty !
15441544 (type fixnum level-constraint)
15451545 (type sort* sort-constraint))
15461546 (let ((arg-acc-list-prime nil))
1547 (dolist (arg-acc arg-acc-list arg-acc-list-prime)
1547 (dolist (arg-acc arg-acc-list (delete-duplicates arg-acc-list-prime :test #'equal))
15481548 (let ((token-list (cdr arg-acc)) )
15491549 (if (null token-list)
15501550 nil ;this iteration is finished
16941694
16951695 PACKAGE=cafeobj
16961696 VERSION=1.4
1697 VMINOR=.9rc8
1697 VMINOR=.9rc9
16981698 VMEMO=PigNose0.99
16991699 PATCHLEVEL=
17001700
55 AC_PREREQ(2.4)dnl Required Autoconf version.
66 PACKAGE=cafeobj
77 VERSION=1.4
8 VMINOR=.9rc8
8 VMINOR=.9rc9
99 VMEMO=PigNose0.99
1010 PATCHLEVEL=
1111 AC_SUBST(PACKAGE)
3535
3636 set exec normalize on
3737
38 select SORTING-STRG(NAT)
38 open SORTING-STRG(NAT) .
3939 exec (4 3 5 3 1) .
4040 -- fast execution by brute engine
4141 set tram path brute .
4242 -- tram exec (40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1) .
43 close
4344
4445 -- functions for proving the generic termination of the sorting algorithm
4546 mod! SORTING-STRG-PROOF {
7778 ** hypothesis
7879 eq e' <= e = true .
7980 ** conclusion
80 red disorder(e' e s) < disorder(e e' s) .
81 -- red disorder(e' e s) < disorder(e e' s) .
8182 close
8283
8384 **> LEMMA2: disorder(e s) < disorder(e s')
9293 eq (e >> s) = (e >> s') .
9394 eq disorder(s) < disorder(s') = true .
9495 ** conclusion
95 red disorder(e s) < disorder(e s') .
96 red disorder(e s) < disorder(e s') .
9697 close
9798
9899 -- proof of (generic) local confluence for the sorting algorithm
2121 reduce xx(3) .
2222 **> should be 256
2323 reduce xx(4) .
24 select 2(F <= view to FNS { op f -> dbl_ })
24 **> does not work select 2(F <= view to FNS { op f -> dbl_ })
25 -- select 2(F <= view to FNS { op f -> dbl_ })
26 open 2(F <= view to FNS { op f -> dbl_ }) .
2527 **> should be 12
2628 reduce xx(3) .
27 select 2((2(FNS { op f -> sq_ })*{ op xx -> f }){ sort S -> Int, op f -> f })
29 close
30
31 -- select 2((2(FNS { op f -> sq_ })*{ op xx -> f }){ sort S -> Int, op f -> f })
32 open 2((2(FNS { op f -> sq_ })*{ op xx -> f }){ sort S -> Int, op f -> f }) .
2833 **> should be 65536
2934 reduce xx(2) .
3035 **> should be 43046721
3136 reduce xx(3) .
37
38 close
3239 --
3340 eof
3441 --
3939 op L1 < L2 -> L1 divides L2 and L1 =/= L2
4040 }
4141
42 select BUBBLES[NAT]
42 open BUBBLES[NAT] .
4343 reduce sorting(18 5 6 3) . **> should be: 3 5 6 18
4444 reduce sorting(8 5 4 2) . **> should be: 2 4 5 8
45 select BUBBLES[NATD]
45 close
46 open BUBBLES[NATD] .
4647 reduce sorting(18 5 6 3) . **> mightnt contain: 3 6 18
4748 reduce sorting(8 5 4 2) . **> mightnt contain: 2 4 8
48
49 close
4950 --
5051 eof
5152 **
11 ** was combinators.obj(in OBJ3 distribution) translated to CafeOBJ.
22 --
33 module! COMBINATORS {
4 protecting (CHAOS) -- to import Identifier, should be used with care.
4 -- protecting (CHAOS) -- to import Identifier, should be used with care.
55 signature {
66 [ Identifier < T ]
77 op __ : T T -> T {l-assoc strat: (1 2 0)}
1515 }
1616 }
1717
18 select COMBINATORS
18 open COMBINATORS .
19 ops m n p : -> Identifier .
1920 red S K K m == I m .
2021 red S K S m == I m .
2122 red S I I I m == I m .
2223
23 red K m n == S(S(K S)(S(K K)K))(K(S K K))m n .
24 red S m n p == S(S(K S)(S(K(S(K S)))(S(K(S(K K)))S)))(K(K(S K K)))m n p .
24 red K m n == S(S(K S)(S(K K)K))(K(S K K)) m n .
25 --
26 red S m n p == S(S(K S)(S(K(S(K S)))(S(K(S(K K)))S)))(K(K(S K K))) m n p .
2527 red S(K K) m n p == S(S(K S)(S(K K)(S(K S)K)))(K K) m n p .
26
28 --
29 close
2730 --
2831 eof
2932 **
4747 signature {
4848 [ Stmt ]
4949 op _;_ : Stmt Stmt -> Stmt {assoc}
50 op _:=_ : Id IntExp -> Stmt
50 op _::=_ : Id IntExp -> Stmt
5151 op while_do_od : BoolExp Stmt -> Stmt
5252 op [[_]]_ : Stmt Env -> Env
5353 }
5656 var E : IntExp var B : BoolExp
5757 var I : Id
5858 -- -----------------------------------------
59 eq [[ I := E ]] V = put(I,[[ E ]] V, V) .
59 eq [[ I ::= E ]] V = put(I,[[ E ]] V, V) .
6060 eq [[ S ; S' ]] V = [[ S' ]] [[ S ]] V .
6161 eq [[ while B do S od ]] V
6262 = if [[ B ]] V then
8282 signature {
8383 [ Fun ]
8484 op fun _ _ is vars _ body: _ : Id IdList InitList Stmt -> Fun
85 op [[_:=_]]_ : IdList IntList Env -> Env
85 op [[_::=_]]_ : IdList IntList Env -> Env
8686 op [[_]]_ : InitList Env -> Env
8787 op [[_]][_]_ : Fun Env IntList -> Env
8888 op [[_]]_ : Fun IntList -> Int
9595 var S : Stmt var V : Env
9696 -- -------------------------------------
9797 eq [[ nil-init ]] V = V .
98 eq [[(I initially E); INs ]] V = [[ INs ]] [[ I := E ]] V .
99 eq [[ I Is := N Ns ]] V = [[ (I := N) ]] ([[ Is := Ns ]] V) .
100 eq [[(nil):IdList := (nil):IntList ]] V = V .
98 eq [[(I initially E); INs ]] V = [[ INs ]] [[ I ::= E ]] V .
99 eq [[ I Is ::= N Ns ]] V = [[ (I ::= N) ]] ([[ Is ::= Ns ]] V) .
100 eq [[(nil):IdList ::= (nil):IntList ]] V = V .
101101 eq [[ fun F(Is) is vars nil-init body: S ]][ V ](Ns) = [[ S ]] V .
102102 eq [[ fun F(Is) is vars INs body: S ]][ V ](Ns) =
103 [[ S ]] [[ INs ]] [[ Is := Ns ]] V .
103 [[ S ]] [[ INs ]] [[ Is ::= Ns ]] V .
104104 eq [[ fun F(Is) is vars INs body: S ]](Ns) =
105105 [[ fun F(Is) is vars INs body: S ]][ nilArr ](Ns) [ F ] .
106 cq [[ Is := Ns ]] V = wrong#args if | Is | =/= | Ns | . ** err-qn
106 cq [[ Is ::= Ns ]] V = wrong#args if | Is | =/= | Ns | . ** err-qn
107107 }
108108 }
109109
111111 select FUN
112112 **> pow(n m) finds the nth power of m for positive n or 0
113113 reduce [[ fun 'pow('n 'm) is vars 'pow initially 1 body:
114 while 0 < 'n do ('pow := 'pow * 'm);('n := 'n - 1) od ]](4 2) .
114 while 0 < 'n do ('pow ::= 'pow * 'm);('n ::= 'n - 1) od ]](4 2) .
115115 -- tram reduce [[ fun 'pow('n 'm) is vars 'pow initially 1 body:
116 -- while 0 < 'n do ('pow := 'pow * 'm);('n := 'n - 1) od ]](4 2) .
116 -- while 0 < 'n do ('pow ::= 'pow * 'm);('n ::= 'n - 1) od ]](4 2) .
117117 reduce [[ fun 'pow('n 'm) is vars 'pow initially 1 body:
118 while 0 < 'n do ('pow := 'pow * 'm);('n := 'n - 1) od ]](4 2) .
118 while 0 < 'n do ('pow ::= 'pow * 'm);('n ::= 'n - 1) od ]](4 2) .
119119 **> should be: 16
120120
121121 **> factorial of n
122122 reduce [[ fun 'fac('n) is vars ('fac initially 1);('i initially 0) body:
123 while 'i < 'n do ('i := 'i + 1); ('fac := 'i * 'fac) od ]](5) .
123 while 'i < 'n do ('i ::= 'i + 1); ('fac ::= 'i * 'fac) od ]](5) .
124124 -- tram reduce [[ fun 'fac('n) is vars ('fac initially 1);('i initially 0) body:
125 -- while 'i < 'n do ('i := 'i + 1); ('fac := 'i * 'fac) od ]](5) .
125 -- while 'i < 'n do ('i ::= 'i + 1); ('fac ::= 'i * 'fac) od ]](5) .
126126 **> should be: 120
127127
128128 **> max finds the maximum of a list of three numbers
129129 reduce [[ fun 'max('a 'b 'c) is vars ('n initially 2);('max initially 'a) body:
130130 while 0 < 'n do
131 ('n := 'n - 1); ('x := 'b); ('b := 'c);
132 ('max := if 'x < 'max then 'max else 'x endif) od ]](3 123 32) .
131 ('n ::= 'n - 1); ('x ::= 'b); ('b ::= 'c);
132 ('max ::= if 'x < 'max then 'max else 'x endif) od ]](3 123 32) .
133133 -- tram reduce [[ fun 'max('a 'b 'c) is vars ('n initially 2);('max initially 'a) body:
134134 -- while 0 < 'n do
135 -- ('n := 'n - 1); ('x := 'b); ('b := 'c);
136 -- ('max := if 'x < 'max then 'max else 'x endif) od ]](3 123 32) .
135 -- ('n ::= 'n - 1); ('x ::= 'b); ('b ::= 'c);
136 -- ('max ::= if 'x < 'max then 'max else 'x endif) od ]](3 123 32) .
137137 **> should be: 123
138138 --
139139 eof
1111 eq [las]: A * (B * C) = (A * B) * C .
1212 }
1313
14 open GROUPL
14 open GROUPL .
1515 op a : -> Elt .
1616
1717 ** first, prove the right inverse law:
1111 eq [linv]: A -1 * A = e .
1212 }
1313
14 open GROUPLA
14 open GROUPLA .
1515 op a : -> Elt .
1616
1717 ** first, prove the right inverse law:
4444 op inc : expr -> expr { prec: 8 }
4545 -- statements ----------------------------------------
4646 -- assignment
47 op (_:=_) : var expr -> assignment { prec: 20 }
47 op (_::=_) : var expr -> assignment { prec: 20 }
4848 -- conditional
4949 op if : expr program program -> conditional { prec: 20 }
5050 -- loop
7676 -- expression
7777 op evale : expr env -> nat
7878
79 eq [assignment]: evalp((X:var := E:expr), C:env)
79 eq [assignment]: evalp((X:var ::= E:expr), C:env)
8080 = update(C,X,evale(E,C)) .
8181 eq [if]: evalp((if(E:expr, P:program, P':program)), C:env)
8282 = if evale(E,C) == 0 then evalp(P',C)
107107
108108
109109 red in TINY-IPL :
110 evalp((v(0) := 0, while le(v(0), s(s(s(s(s(0)))))) { v(0) := inc(v(0)) }), empty) .
110 evalp((v(0) ::= 0, while le(v(0), s(s(s(s(s(0)))))) { v(0) ::= inc(v(0)) }), empty) .
111111
112112 --
113113 eof
9090 eq pop push(N,S) = S .
9191 }
9292
93 open BSTACK
93 open BSTACK .
9494 eq length push(N,S) = 1 + length S .
9595
9696 red length(empty) .
44 require lexl
55
66 module! SORTING(X :: POSET) {
7 protecting (LIST[X])
7 protecting (LIST(X))
88 op sorting_ : List -> List
99 op unsorted_ : List -> Bool
1010 vars L L' L'' : List
1414 cq unsorted L E L' E' L'' = true if E' < E .
1515 }
1616
17 select SORTING[NATD]
17 open SORTING(NATD) .
1818 reduce sorting(18 5 6 3) . **> should contain: 3 6 18
1919 reduce sorting(8 5 4 2) . **> should contain: 2 4 8
2020 reduce sorting(6 3 1) . **> should be: 1 3 6
2121 reduce sorting(18 6 5 3 1) . **> should contain: 1 3 6 18
22 close
2223
23 select SORTING[INT]
24 open SORTING(INT) .
2425 reduce unsorted 1 2 3 4 . **> should not be: true
2526 reduce unsorted 4 1 2 3 . **> should be: true
2627 reduce sorting 1 4 2 3 . **> should be: 1 2 3 4
28 close
2729
2830 make SORTING-PH-LIST (SORTING[(LEXL[QIDL]*{op __ -> _._})
2931 {sort Elt -> List,
1212 (if (not (equal "" cafeobj-version-memo))
1313 (if (not (equal "" patch-level))
1414 (setq cafeobj-version-minor
15 (format nil ".9rc8(~a,~A)"
15 (format nil ".9rc9(~a,~A)"
1616 cafeobj-version-memo
1717 patch-level))
1818 (setq cafeobj-version-minor
19 (format nil ".9rc8(~a)" cafeobj-version-memo)))
20 (setq cafeobj-version-minor ".9rc8"))
19 (format nil ".9rc9(~a)" cafeobj-version-memo)))
20 (setq cafeobj-version-minor ".9rc9"))
2121 (setq cafeobj-version (concatenate 'string
2222 cafeobj-version-major
2323 cafeobj-version-minor))