Monday, May 13, 2013

Bash local variable gotcha


Bash local variables aren't as local as you'd like them to be:

function b() {
  local bvar
  echo "In b: '${avar}' '${bvar}'"
  avar=B  # modifies a's "local" variable
  bvar=B
}

function a() {
  local avar bvar
  avar=A
  bvar=A
  echo "In a before: '${avar}' '{$bvar}'"
  b
  echo "In a after:  '{$avar}' '{$bvar}'"
}

a

#---------------------- Prints --------------------------------

In a before: 'A' '{A}'
In b: 'A' ''
In a after:  '{B}' '{A}'

Saturday, February 11, 2012

A trick for parsing first-order logic with equality

Suppose your language is some superset of the following minimal subset of first-order logic with
equality[1].

id     ::= [a-z]+
func ::= id
prop ::= id
term ::= func ( term ... ) 
rel    ::= prop (term ... ) | term = term
form ::= rel | true | form & form | form => form 

This is a natural representation of first-order terms, and is seen for example in the TPTP library.  The problem is that this language is not LR(N) for any N.  To see this, consider the following atomic formulas:

p(t) = q
p(t)

Assume t is huge.  A trick to force this into your LALR grammar is to parse the equality branch not as 

  term = term 

but as 

  rel = term.  

If this rule matches, you can convert the LHS back to a term before adding it to the AST.  This works because = is non-associative; a = b = c is not well formed.  (In higher-order logic this is not the case, but that's another story.)  Thus you know that the LHS has the form 

  prop (term ...) 

which can easily be converted to 

  func (term ...).

[1] This trick obviously holds for any infix predicate symbol.  Equality is just the most common.

Sunday, July 10, 2011

You can't buy a puppy in the West Village when you've been drinking.

I was taking a walking tour of the WV today (yes, my own neighborhood) I got as a Groupon awhile ago.  The guide said threre's a law against buying puppies when drunk in the neighborhood.  It turns out he was exaggerating, but only a bit.  It's not a 'law' but a policy actually practiced by all the pet stores in the area.

http://www.huffingtonpost.com/2011/07/05/drunk-puppy-buying-banned_n_890153.html

Monday, July 04, 2011

Where people take pictures in NYC


Red is tourists, blue is locals.

Saturday, January 01, 2011

Surprising fact: taxicabs can't refuse to take you anywhere in NYC

Drivers are required to pick up the first or closest passenger they see, and may not refuse a trip to a destination anywhere within the five boroughs, neighboring Westchester, Nassau or Suffolk Counties or to Newark Liberty International Airport.