In expressions there can be a lot of repetition of code:

  [[ Equal(e1, e2) ^ (s) : TTuple(TBoolean(), out_mult, out_ord) ]] :=
    [[ e1 ^ (s) : TTuple(e1_type, e1_mult, e1_ord) ]],
    [[ e2 ^ (s) : TTuple(e2_type, e2_mult, e2_ord) ]],
    out_type is sub.lub of (e1_type,e2_type),
    out_type <test? IsType(), // make sure it evaluates to something
    (out_mult, out_ord) is mulOrd.lub of ((e1_mult,e1_ord),(e2_mult,e2_ord)),
    e1_mult_u is multUpper of e1_mult,
    e1_mult_u == TUOne() | error $[Multiplicity mismatch: expected One or ZeroOrOne got [e1_mult]] @ e1,
    e2_mult_u is multUpper of e2_mult,
    e2_mult_u == TUOne() | error $[Multiplicity mismatch: expected One or ZeroOrOne got [e2_mult]] @ e2.
  
  [[ Inequal(e1, e2) ^ (s) : TTuple(TBoolean(), out_mult, out_ord) ]] := // identical to Equal
    [[ e1 ^ (s) : TTuple(e1_type, e1_mult, e1_ord) ]],
    [[ e2 ^ (s) : TTuple(e2_type, e2_mult, e2_ord) ]],
    out_type is sub.lub of (e1_type,e2_type),
    out_type <test? IsType(), // make sure it evaluates to something
    (out_mult, out_ord) is mulOrd.lub of ((e1_mult,e1_ord),(e2_mult,e2_ord)),
    e1_mult_u is multUpper of e1_mult,
    e1_mult_u == TUOne() | error $[Multiplicity mismatch: expected One or ZeroOrOne got [e1_mult]] @ e1,
    e2_mult_u is multUpper of e2_mult,
    e2_mult_u == TUOne() | error $[Multiplicity mismatch: expected One or ZeroOrOne got [e2_mult]] @ e2.

We can remove some code duplication by allowing multiple pattern matches:

  [[ Equal(e1, e2)
   + Inequal(e1, e2) ^ (s) : TTuple(TBoolean(), out_mult, out_ord) ]] :=
    [[ e1 ^ (s) : TTuple(e1_type, e1_mult, e1_ord) ]],
    [[ e2 ^ (s) : TTuple(e2_type, e2_mult, e2_ord) ]],
    out_type is sub.lub of (e1_type,e2_type),
    out_type <test? IsType(), // make sure it evaluates to something
    (out_mult, out_ord) is mulOrd.lub of ((e1_mult,e1_ord),(e2_mult,e2_ord)),
    e1_mult_u is multUpper of e1_mult,
    e1_mult_u == TUOne() | error $[Multiplicity mismatch: expected One or ZeroOrOne got [e1_mult]] @ e1,
    e2_mult_u is multUpper of e2_mult,
    e2_mult_u == TUOne() | error $[Multiplicity mismatch: expected One or ZeroOrOne got [e2_mult]] @ e2.

The compilation for this should be easy, its a local rewrite.

An alternative would be to add abstraction to NaBL, so that the duplicate code can be extracted into a function (or other abstraction mechanism).

Submitted by Daco Harkes on 19 January 2017 at 15:00

Log in to post comments