Mimics
Inactive cells
Active cells
Specs
Inactive cells (details)
kind = "Message Rewriter Unification"
Active cells (details)
addUnification(name, p, msg)

nil

method(name, p, msg, 
  case([](name, p arguments length), 
    [](:(":all"), 2), 
    count = p arguments [](0) evaluateOn(Ground) 
    nm = :(p arguments [](1) name) 
    unifications [](nm) = (msg, count) 
    count, 
    [](:(":all"), 1), 
    nm = :(p arguments [](0) name) 
    count = 0 
    curr = msg 
    until(curr nil? ||(curr terminator?), 
      curr = curr next 
      ++(count)) 
    unifications [](nm) = (msg, count) 
    count, 
    [](:(":until"), 2), 
    stopsym = :(p arguments [](0) name) 
    nm = :(p arguments [](1) name) 
    count = 0 
    curr = msg 
    until(curr nil? ||(curr name ==(stopsym)), 
      curr = curr next 
      ++(count)) 
    if(curr nil?, 
      return(-(1))) 
    ++(count) 
    unifications [](nm) = (msg, count) 
    count, 
    else, 
    if(name ==(:(":not")) &&(p arguments length >(0)), 
      capture = nil 
      avoidNames = p arguments map(name) 
      if(p arguments last symbol?, 
        capture = p arguments last 
        avoidNames removeAt!(avoidNames size -(1))) 
      if(avoidNames include?(msg name), 
        -(1), 
        if(capture, 
          unifications [](capture name) = (msg, 1)) 
        1) 
      , 
      unifications [](name) = (msg, 1) 
      1)))
initialize()

nil

method(
  @ unifications = {} 
  @ nexts = 0 
  )
internal:eitherLiteral?(pattern, msg)

nil

method(pattern, msg, 
  internal:literal?(pattern) ||(internal:literal?(msg)))
internal:literal?(msg)

nil

method(msg, 
  if(msg name asText =~(#/^internal:/), 
    true, 
    false))
internal:macroSymbol?(p)

nil

method(p, 
  (p name ==(:(":all")) ||(p name ==(:(":until"))) ||(p name ==(:(":not")))) &&(p arguments length >(0)) 
  )
internal:unify(pattern, msg, countNexts false)

nil

method(pattern, msg, countNexts false, 
  p = pattern 
  m = msg 
  while(p, 
    unless(m, 
      return(false) 
      ) 
    amount = 1 
    if(p symbol?, 
      amount = addUnification(p name, p, m) 
      if(amount ==(-(1)), 
        return(false)), 
      unless(p name ==(m name), 
        return(false) 
        ) 
      ) 
    unless(internal:macroSymbol?(p), 
      if(internal:eitherLiteral?(p, m), 
        unless(internal:unifyLiterals(p, m), 
          return(false)), 
        if(p arguments length !=(m arguments length), 
          return(false)) 
        p arguments zip(m arguments) each(pm, 
          unless(internal:unify(pm first, pm second), 
            return(false))))) 
    p = p next 
    amount times(
      unless(m, 
        return(false)) 
      m = m next) 
    if(countNexts, 
      @ nexts = @ nexts +(amount)) 
    ) 
  true 
  )
internal:unifyLiterals(pattern, msg)

nil

method(pattern, msg, 
  patternLiteral = internal:literal?(pattern) 
  msgLiteral = internal:literal?(msg) 
  
  case([](patternLiteral, msgLiteral), 
    [](false, true), 
    if(pattern symbol?, 
      true, 
      false), 
    [](true, false), 
    false, 
    [](true, true), 
    pattern name ==(msg name) &&(pattern arguments ==(msg arguments)) 
    ) 
  )
tryUnify(pattern, msg)

nil

method(pattern, msg, 
  u = mimic 
  if(u internal:unify(pattern, msg, true), 
    u, 
    false) 
  )