interesting. the 32 bits is done specifically to test 5 variables. this probably helps see the structure:
p,q,r,s,t 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 the obvious extention would be to cover a list (db)of givens and then query the state of propositions. Do you think your framework is extendable to such? ----- Original Message ----- From: Michal Wallace <michal.wall...@gmail.com> To: programm...@jsoftware.com Cc: Sent: Monday, March 3, 2014 1:06:19 AM Subject: [Jprogramming] logic Here's a library I use quite a bit for talking about and studying logic on IRC: https://github.com/tangentstorm/tangentlabs/blob/master/j/logic.ijs Most of the lines in that file are just assertions demonstrating the laws of propositional logic. The two most interesting lines, though, are the definition of 'given' and the declaration of some variables that represent every possible combination of 5 boolean values. 'p q r s t' =: |: #: ,. i. 2 ^ 5 given =: ,@([: _:^:(2=#)@ ~."1 [ #"1~ *./@]) These two lines, combined with J's logical connectives are all you need to experiment with many different problems in propositional logic. (You can also use it for modal logic or for /some/ problems in predicate logic, if you can map the statement to simple propositions.) Here's how you use them: p , q NB. shape of each array is (1 32) so they line up nice. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 p <: q NB. 'p implies q'. (Can also be written 'p ! q') 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 p +. q NB. 'p or q' 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 p *. q NB. 'p and q' 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 p *. -.q NB. 'p and not-q' 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 (-.p) *. q NB. 'not-p and q' 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -. p *. q NB. 'not (p and q)' 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 p *: q NB. 'p nand q' 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 p +. -.p NB. some things are always true 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 p *. -.p NB. some things are never true 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (p *. -.p) given '' NB. 'given' can summarize the results 0 p given '' NB. If it can't deduce an answer, it shows '_'. _ (p,q) given '' NB. You can query multiple variables. _ _ (p,q) given p NB. Given p, it knows p=1, but q is still unknown. 1 _ (p,q) given p,q NB. Commas on the right are treated as 'AND'. 1 1 (p,q) given p, p <: q NB. Given p=1 and p implies q, it can deduce q for you. 1 1 NB. But, it needn't be so simple: (p,q,r,s,t) given ((-.s) <: q), (q <: (r *. t)), (-.t), (p=r), q~:p 1 0 1 1 0 I find that ( +. , *, -. , <: , =, ~:, ! ) are the connectives I use most often, but you can write all 16 boolean dyads quite easily: http://www.jsoftware.com/help/dictionary/dbdotn.htm Dyads <: and ! are both equivalent to logical implication given boolean arguments, (also <. and >. are standins for *. and .+, respectively ). Having two symbols is nice because you can use one for propositions and the other for inference rules: ((p<:q) *. (q<:r)) ! (p <:r) NB. this says if (p->q) and (q->r), then you can deduce (p->r) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Together, the boolean primitives, the canned variables, and the 'given' verb make solving many basic logic problems a snap. For example, you can solve these "knights and knaves" puzzles, simply by figuring out how to restate the facts in J: http://philosophy.hku.hk/think/logic/knight.php I'm happy to demonstrate if anyone would like, but I thought I'd give someone else a chance to try it first... :) -Michal ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm