From: Mark Tarver
Subject: factorising code: how to improve on this?
Date: 
Message-ID: <1184007409.743013.35500@k79g2000hse.googlegroups.com>
I've been pulled to this group by an interesting discussion initiated
by Dr Harrop w.r.t. to
pattern matching in Lisp and the factorisation of overlapping
patterns.

I've been experimenting with the optimisation of Qi code w.r.t.
overlapping patterns.
When I created Qi, my first concern was clarity and correctness -
speed was not the most important requirement.

These are initial experiments and the product of 48 hours intermittent
work.  This version is
Qi-Turbo-e (for experimental) and has no version number.

Experiment 1
==========

Compile the following rewrite function through Turbo-e

(define foo
   [4] -> 4
   [5] -> 5)

Result:

(DEFUN foo (V433)
 (BLOCK NIL
  (TAGBODY
   (IF (CONSP V433)
    (LET ((Car443 (CAR V433)) (Cdr444 (CDR V433)))
     (TAGBODY (IF (EQL 4 Car443) (IF (NULL Cdr444) (RETURN 4) (GO
tag439)))
      tag439
      (IF (EQL 5 Car443) (IF (NULL Cdr444) (RETURN 5) (GO tag438))
       (GO tag438)))))
   tag438 (RETURN (qi::f_error 'foo)))))

Disassembly of function foo
(CONST 0) = 4
(CONST 1) = 5
(CONST 2) = foo
(CONST 3) = qi::f_error
1 required argument
0 optional arguments
No rest parameter
No keyword parameters
25 byte-code instructions:
0     (LOAD 1)
1     (JMPIFATOM L20)
3     (LOAD&CAR&PUSH 1)
5     (LOAD&CDR&PUSH 2)
7     (LOAD&PUSH 1)
8     (JMPIFNOTEQTO 0 L14)                ; 4
11    (LOAD&JMPIFNOT 0 L25)
14    L14
14    (LOAD&PUSH 1)
15    (JMPIFEQTO 1 L31)                   ; 5
18    (SKIP 2)
20    L20
20    (CONST&PUSH 2)                      ; foo
21    (CALL1 3)                           ; qi::f_error
23    (SKIP&RET 2)
25    L25
25    (CONST 0)                           ; 4
26    (SKIP&RET 4)
28    L28
28    (CONST 1)                           ; 5
29    (SKIP&RET 4)
31    L31
31    (LOAD&JMPIFNOT 0 L28)
34    (SKIP 2)
36    (JMP L20)

Comment; can the object Lisp code be improved and if so how?

Mark