Saturday, February 01, 2014

somerby.net/mack/logic

Here's a web application that decides simple statements in a fragment of first-order symbolic logic: somerby.net/mack/logic. It's based on that .NET console application I was calling "Monadic Predicate Logic".

Tuesday, February 12, 2013

Perl Poetry

I think Perl poetry is pretty cool. I should write some sometime. I've made one or two attempts, but being the lazy programmer I am, instead of actually finishing any Perl poems, I wrote a Perl module that facilitates the writing of Perl poetry. The module is based on one observation: plain English text, especially the kind you would find in a poem or a novel, is often very close to being syntactically valid Perl. In fact, were you to declare a Perl operator (that is, a subroutine whose arguments don't need to be wrapped in parentheses) for each word that is not already a Perl keyword in some English text, the text would only need small changes to become a syntactically valid Perl script that executes without warnings or errors. The Perl module I wrote does just that: if you use it in a Perl script, it pre-reads the script and declares operators for each bareword that is not a keyword.
To show how it works, here's an example. This is not a valid Perl script:
As I walked through the wilderness of this world, I lighted on a certain place where was a den, and laid me down in that place to sleep; and as I slept, I dreamed a dream. I dreamed, and behold, I saw a man clothed with rags, standing in a certain place, with his face from his own house, a book in his hand, and a great burden upon his back.  I looked and saw him open the book, and read therein; and as he read, he wept and trembled; and not being able longer to contain, he brake out with a lamentable cry, saying, "What shall I do?" 
This is a valid Perl script:
use perlify;

As I walked through the wilderness of this world, I lighted on a certain place where was a den, and laid me down in that place to_sleep; and_as I slept, I dreamed a dream. I dreamed, and behold, I saw a man clothed with rags, standing in a certain place, with his face from his own house, a book in his hand, and a great burden upon his back.  I looked and saw him open_the book, and read_therein; and_as he_read, he wept and trembled; and_not being able longer to contain, he brake out with a lamentable cry, saying, "What shall I do?"
 They are not very different. To convert the text to a working Perl script, I just used perlify.pm and changed some spaces to underscores to prevent the text from calling certain Perl operators without the correct number of arguments. The default action for a word in this text is to print itself and then return 1. The output of the script is
world den a was where place certain a on lighted I this of wilderness the through walked I As to_sleep place that in down me laid slept dream dreamed I a dreamed I I and_as behold rags place house hand his in book a own his from face his with certain a in standing with clothed man a saw I back looked I his upon burden great a book open_the him saw read_therein he_read wept he and_as trembled contain cry saying lamentable a with out brake he to longer able being and_not
 This example isn't especially creative, but there are ways to embellish it to make it more interesting.

One more thing: if you read perlify.pm, notice a variable named "our $callback".  If you care enough to use the module, I probably don't need to tell you what to do with it.

Tuesday, September 04, 2012

Drum Machine Joy!

I made a drum machine.  Actually, a web page that's a drum machine.  It's all in the browser, HTML5 and Javascript.  It works in Firefox, Chrome and Safari, but not Internet Explorer.  It's fully functional, but it could use some more features.

Monday, October 31, 2011

Monadic Predicate Logic with Some Modal Operators

I have a new version of the monadic predicate logic program that I posted a while ago.  It has operators for possibility and necessity (<> and [], respectively).  They bind to sub-expressions the same way negation (~) does; they bind to the nearest existential or universal quantifier to the right, or, failing that, the nearest predication to the right, so <>Px&Py is equivalent to (<>Px)&(Py) but not <>(Px&Py)<>x,Px&Py is equivalent to <>(x,Px&Py) but not (<>x,Px)&Py.  I'm posting it and its source code, now that I have written a parser that I'm not ashamed of.  The one downside is that it no longer supports the "old school" dot grouping.  If any one wants to use it, tell me and I'll add it back in.  If anyone uses this program at all, tell me!

Wednesday, October 26, 2011

Setting Local Variables in Visual Studio

Have you ever wanted to change the value of a local variable inside of a method?  Without changing its code or pausing it in the debugger, that is?  Maybe not, but I did, while writing a unit test this week.  I figured out how to do it in Visual Studio, using Visual Studio's DTE object.  The DTE object provides a programmatic interface to Visual Studio's debugger.  With it, breakpoints can be set and deleted programmatically.  But what about changing the variable?  Breakpoints can have conditions, so...


using EnvDTE;
using System.Runtime.InteropServices;

...

// Get the DTE object.
DTE lDTE =
  Marshal.GetActiveObject( "VisualStudio.DTE" ) as DTE;

// Set a conditional breakpoint.  In this example, the
// break is in file.cs, line 422.
lDTE.Debugger.Breakpoints.Add(
  null,
  "file.cs", 
  422,
  1,
  // Use the break condition to set the local variable
  // you want changed.  Note that the expression always
  // returns false; the debugger will assign 1 to
  // lVariable every time the condition is evaluated,
  // but never pause the unit test.
  "(lVariable = 1.0f).ToString() == null",
  dbgBreakpointConditionType
    .dbgBreakpointConditionTypeWhenTrue,
  null,
  null,
  0,
  null,
  0,
  dbgHitCountType.dbgHitCountTypeNone );

// Call that method...
...

// Delete the breakpoint if you don't want it
// to affect later code.
lDTE.Debugger.Breakpoints.Item( 1 ).Delete();

Tuesday, October 25, 2011

John McCarthy

John McCarthy died yesterday.  It's funny, I looked at his website yesterday and later thought about how old he must be, not knowing he had passed.  He invented the LISP programming language back in the late 1950s. Even though I don't program in LISP - I wrote a Scheme plugin for GIMP a few years ago; that's about all - I admire it a lot.  Thinking about how simple its syntax is and how simple its runtime is, yet how powerful it is just makes me feel good inside.  It's like a work of art.

So, those of you who are programmers: to commemorate, read McCarthy's original specification for LISP, which is an excellent read for any computer scientist.  And then write a program in the functional style.


John McCarthy, 1927-2011

Tuesday, September 27, 2011

Golf This!

Here's something for all you programmers out there:

print((@ARGV=map glob,@ARGV)?do{0 while(<>);$.}:0)
  1. Easy: What language is it?
  2. Moderate: What does it do?
  3. Difficult: Can you golf it?  That is, can you write a script that's even shorter but does the same thing?