@@ -265,3 +265,51 @@ In category terms, `a → c^b` is isomorphic to `a → b → c`
265265 (error " object ~A need to be of a product type, however it is of ~A " f (dom f))
266266 (let ((dom (dom f)))
267267 (curry-prod f (mcar dom) (mcadr dom)))))
268+
269+ ; ; Please rewrite this code, it's horrible
270+ (defun reducer (morph &optional (seen-set (fset :empty-set)))
271+ ; ; handle the easy cases, do the hard tracking later
272+ (typecase-of substmorph morph
273+ (project-left morph)
274+ (project-right morph)
275+ (inject-left morph)
276+ (inject-right morph)
277+ (terminal morph)
278+ (init morph)
279+ (distribute morph)
280+ (pair (pair (reducer (mcar morph))
281+ (reducer (mcdr morph))))
282+ (case (mcase (reducer (mcar morph))
283+ (reducer (mcadr morph))))
284+ (comp
285+ (let* ((linearized (linearize-comp morph))
286+ ; ; this code is absolutely horrible
287+ (left (mvfoldr (lambda (g flist)
288+ (let ((new-g (reducer g)))
289+ (typecase (car flist)
290+ (pair
291+ (typecase new-g
292+ (project-left (cons (mcar (car flist))
293+ (cdr flist)))
294+ (project-right (cons (mcdr (car flist))
295+ (cdr flist)))
296+ (otherwise (cons new-g flist))))
297+ (otherwise
298+ (cons new-g flist)))))
299+ (butlast linearized)
300+ (list (reducer (car (last linearized))))))
301+ (constructed (if (cdr left)
302+ (apply #' comp left)
303+ (car left))))
304+ ; ; g 。f
305+ (if (fset :member? constructed seen-set)
306+ (comp (reducer (mcar constructed)) (reducer (mcadr constructed)))
307+ (reducer constructed (fset :with seen-set constructed)))))
308+ (substobj morph)
309+ (otherwise (subclass-responsibility morph))))
310+
311+ (defun linearize-comp (morph)
312+ (if (typep morph ' comp)
313+ (append (linearize-comp (mcar morph))
314+ (linearize-comp (mcadr morph)))
315+ (list morph)))
0 commit comments