# [PEAK] Logix, macros, and PEAK

John Landahl john at landahl.org
Wed Jul 20 05:47:42 EDT 2005

```I've been working quite a bit lately with Logix[1], which is something
like a language construction kit built on Python, and have been
thinking about how its macro capabilities could simplify some aspects
of PEAK.  So I took a stab at a "defmethod"[2] command for simplifying
generic function definitions, and came up with the following working
example:

limport peak
setlang peak.peak

defmethod foo(a, b) default:
print "default"

defmethod foo(a, b) around { a > 0 }:
print "before"
next_method(a, b)
print "after"

defmethod foo(a, b) when { a == 1 and b == 2 }:
print "hi there"

foo(0, 1); print
foo(1, 1); print
foo(1, 2); print

...with the expected output:

default

before
default
after

before
hi there
after

If anyone is interested in this, I'd be happy to share the 'peak.lx'
module which has the definition and macro.  (Not for the faint of
heart, however; Logix syntax can get rather, um, complex).

Another candidate for simplification might be peak.model definitions.
Maybe something like:

model class Foo:
attribute x (type: String,  default: None)
sequence  y (type: Integer, default: [])
attribute z (type: "Bar",   default: None, ref: "foos")

...to replace:

class Foo(model.Element):
class x(model.Attribute):
referencedType = model.String
defaultValue = None

class y(model.Sequence):
referencedType = model.Integer
defaultValue = []

class z(model.Attribute):
referencedType = "Bar"
defaultValue = None
referencedEnd = "foos"

The syntax could use more thought, but the potential is there for a
pretty substantial reduction of code and complexity.

- John

[1] http://www.livelogix.net/logix/

[2] A Lispism; could just as easily be 'pddef', 'define generic',
'dispatch def', etc.  (Logix allows spaces in its "operators").

```