Are Pigs able to fly?

Perl, 363 353 350 347 343 297 266 264

$_=<>;s/able to fly/X/g;$m=' ?(not )?\b(P|\w+)';$h{$1?N.$2:$2}{$3?N.$4:$4}=$h{$3?$4:N.$4}{$1?$2:N.$2}=1while s/$m.{8}$m\.//;map{%x=0,r($_,$_)}%h;sub r{($a,$b)=@_;$e+=$h{$a}{N.$b};$x{$b}++or$h{$a}{$b}=1,map{r($a,$_)}%{$h{$b}}}print$e|$h{P}{X}?Yes:$h{P}{NX}?No:Maybe


# Read one line from STDIN
# Replaces special attribute with X
s/able to fly/X/g;
# Prepare attribute match
$m=' ?(not )?\b(P|\w+)';
# Match "Everything that is A is also B. "
#                        "\bA........ \bB\."
# Match "Pigs are B. "
#     "\bP........\bB\."
  # Add facts for A => B and !B => !A, where A may equal "P" for "Pigs are"
  # Facts are stored as a hash of hashes %h; keys%h are the source attributes;
  # keys%{$h{$a}} are the attributes that follow from attribute $a
  # A "not attribute" is stored as "Nattribute", while a "attribute" is just stored as "attribute"
# For all known source attributes ... (this should really be keys%h but we dont mind the extra hashrefs)
sub r
  # ... remember that we hit a negation and therefor an inconsistency ...
  # If we check/add $b and find an existing "N$b" that means that attribute $b is supposed to be true and not true at the same time
  # It is cheaper bytewise to just add up all consistency errors (remember each fact has a hard value of 1) than to exit right here
  # ... remember that we processed this attribute for the current source attribute so we prevent loops ...
  # ... add a new fact and then follow the chains (again omitting keys).
# Did we happen on an inconsistency? Do pigs fly? Dont pigs fly? Maybe (Bitwise or is okay too)

Haskell, 586 566 547 bytes

I wrote this on the assumption that for every property P there must exist some x and y such that P(x) is true and P(y) is false; without this assumption, the fourth example input wouldn't have a contradiction and would answer "No".

#define X p s q m
#define W where
u 0=[[]];u n=[x:y|x<-[t,f],y<-u$n-1]
c l=all(==h l)l#and l
X[]|or[fst$c$map(!!(n-1))e|n<-[1..l$h e]]=y|z t=y|z f="No"|t="Maybe"W e=m$u$l s;z m=t#m==(c$map h$q e)
X("Pigs":_:y)=p v((r$(==a).(!!k)).q)m z W((k,v),z,a)=s%y
X(_:_:_:y)=p w q((r(\x->(x!!j/=a)||(x!!k==b))).m)v W((j,u),_:_:z,a)=s%y;((k,w),v,b)=u%z
s%("not":w)=(i,u,not p)W(i,u,p)=s%w
s%(w:z)=(maybe(l s,s++[w#l s])(#s)$lookup w s,z,t)
main=interact$p[""#0]id id.words.r(/='.')

This should be compiled with the ghc command line option "-cpp". Input must be terminated by EOF (^D). You can try it online at, but you cannot set command line options. Instead, you can prefix the program with the language option


It works by collecting the set of traits, then filtering the set of trait -> truth valuations using the implications in the input. The result is then tested to ensure that every trait can be validly assigned to both True and False (failure here is the ex falso quodlibet case). Finally, it looks for valuations which match the pig facts, checking the value for "able to fly" in each valuation.

Quite a few bytes were lost to threading state around: the set of traits seen so far, the pig-fact-selector function, and the filtering function determined by the implications. Probably the exact same idea would be much shorter in an impure language.

Edit: Saved several bytes by proud haskeller's suggestion, then a couple extra by replacing the binding of z and "u%drop 2 z" with a binding to "_ : _ : z" and "u%z", saving 3.

Edit 2: Saved some more. Used the (#)=(,) trick to save 2 bytes and learned about pattern synonyms (, but the notation was too verbose to get a savings from eliminating the rest of the pairs in this program. Squeezed out some more savings by changing the patterns that the parser searches for. For example: if a sentence doesn't start with Pigs and we have anything left in the parser state, we parse a "Everything that is.." sentence. This saved lots of characters in the patterns for X and %.

Python, 547 536 525 521 513 509 497 503 501

o='not ';R={'':{''}}
S=lambda x,y:filter(len,m(str.strip,x.split(y)))
N=lambda a:[o+a,a[4:]][a[:4]==o]
def C(s):a,c=S(s[19:],'is also');return[(a,c),(N(c),N(a))]
X=lambda A:A&set(m(N,A))and 1/0 or A
for a,c in sum(m(lambda x:[('',x[9:])]if'P'==x[0]else C(x),S(raw_input(),'.')),[]):R.setdefault(a,{a}).add(c)
def F(s):
 while 1:
  for r in i:n|=R.get(r,n)
  if X(i)>=n:return i
try:a='able to fly';n=N(a);c={n:'No','':'Maybe'}[''.join({a,n}&m(F,R)[0])]
print c

For each a -> b in the input, we add the given clause and its negation not b -> not a to the set of clauses and then compute the set of propositions that are ->-reachable from any proposition using a fixpoint loop. Whenever we encounter a contradiction, we throw (and later catch) a ZeroDivisionError and print Yes.

Finally, we check if 'able to fly' (and/or its negation) is reachable from the 'is pig' proposition '' and print the appropriate response.

EDIT: This is buggy, fixing it. Fixed.