Archive for the ‘Scheme’ Category

Beating my head with PLT Scheme

Thursday, February 21st, 2008

I’ve been trying to communicate with a perl process from PLT Scheme using the process library call from scheme/system.

I’ve been a good systems boy making sure that I call flush-output after every string I write to the perl process. But the perl process never gets any input unless I close the output side of the pipe connected to perl’s stdin filehandle.

After hours of no progress I found this

http://pre.plt-scheme.org/plt/doc/release-notes/mzscheme/MzScheme_300.txt

* File-stream output ports (including file ports, the initial output
port, and ports created by `subprocess’) are now block-buffered by
default, instead of line-buffered. The exception is when an output
port corresponds to a terminal, in which case it is line-buffered
by default. Also, the initial error port remains unbuffered.

TCP output ports are block buffered (instead of unbuffered) by
default.

The file-stream changes are especially likely to affect stdio-based
communication among OS-level processes. For example, when
communicating with an ispell subprocess, adding a newline at the
end of a command previously would have been enough to send the
command to ispell. Now, the output must be flushed explicitly
(using `flush-output’) or the buffer mode must be explicitly
changed to by-line (using `file-stream-buffer-mode’).

The TCP changes affect most TCP-based communication. Explicitly
flush output using `flush-output’ or change the buffer mode using
`file-stream-buffer-mode’.

So I said what the heck, I’ll just add a (file-stream-buffer-mode outp ‘line)
call for grins.

And it started working.

DivaScheme

Wednesday, November 1st, 2006

My master thesis was written in Scheme, so I’ve spend a long of time with PLT Scheme in the last year. DrScheme is cool for teaching others scheme especially in classroom settings. The editor key bindings however are atrocious. Enter DivaScheme to the rescue.

Skuery 0.0.1 Release

Thursday, September 21st, 2006

Skuery 0.0.1 Release

This is the first release of Skuery a partial embedding of XQuery functionality in PLT Scheme.

The release was a part of my master thesis work.

skuery-0.0.1.tgz

Steve Yegge’s Tour-de-Babel

Wednesday, April 12th, 2006

I rad across a couple good finds today on the web. So I thought I would share

Steve Yegge’s Tour-de-babel

First: Steve says that Lisp is the most powerful abstraction language available. Something my thesis advisor Dr. Phil Windley would agree with. Steven goes on to say that even if you don’t use Lisp day to day in your programming, learning will forever change you method of thinking when programming. I completely agree. I’m writing a XQuery engine in Scheme currently. It took me some time to get up to speed on Lisp/Scheme. Now that I’m over the learning curve, I love Scheme.

Second: Steve has respect for Perl. I’ve come to really love Perl. It wierd, wacky, and has a lot of ugly warts, but text processing and system tasks are just so easy. Perl has also changed me as a programmer. I’ve been following the development of Perl6 and Parrot for over a year now, and have high hopes that Perl6 will remove alot of the warts and wacky behaviors of Perl5.

Third: Steve praises Ruby as the best of the Perl, Lisp, Smalltalk worlds. And I have to agree. I’ve spent a lot of time lately with Ruby and enjoyed every minute of it. In an attempt to better understand the Perl6 Rules and the Parrot Grammar Engine, I’ve been porting a Perl5 version of Perl6 Rules to Ruby. The Perl5 implementation is continuation based, which allows for backtracking. Lexical analysis is based on regular expressions. Hence parsing occurs using nested regular-expressions(rules) and backtracking. Rules return match objects which are the primitive beginnings of a abstact syntax tree.

My other latest foree into Ruby has been a Reputation Framework(RepKept) for blogs entries and comments. RepKept calculates and maintaines reputations much like ebay and amazon.com’s reputation systems. RepKept, however is a general framework for building a wide variety of reputation implementations. At the core of RepKept is a rules engine that allows for dynamic reputation calculations. RepKept currently supports the MoveableType blogging engine.

KSXPath 2.0 XPath 2.0 for Scheme and Skuery

Wednesday, March 29th, 2006

Skuery is the new name for my masters thesis software previously named SchemeQuery.
In summary, Skuery is the native embeding of data query in general purpose programming languages.
In my case XQuery and Scheme.

XPath 1.0 can be simply described a tree traversal and subtree selection domain specific language.
XPath 2.0 builds upon version 1.0 by adding more powerful looping and decision flow control.
XPath 2.0 as a subset of XQuery 1.0 functionality, or in other words XPath 2.0 functionality is wholely contained in XQuery 1.0 Specification.
Most importantly to Skuery, XPath 2.0 supports user defined node filtering and selection functions.

The Scheme XPath implementation (SXPath) is architected using a layered approach.

SXPath like Skuery, processes both the orginal plain text form of the defined language as well as a S-expression encoded form.
Both initial forms of XPath: plain text and sexpression base are translated by SXPath into a sequence of low-level primitive operations.

Further functions such as node-join, node-reduce, node-union, and filter provide the core functionality of the primitive SXPath operations.
The elegance of SXPath and XPath in general is the realization that each step in the path can be represented as a projection, selection, transitive closure or a filter operation.
A XPath expression can then be evaluated by combining steps as chains of sequential operations or as parallel union operations.

Unfortunately the scheme SXPath library only supports XPath 1.0 functionality.
However the XPath 1.0 and 2.0 do share the same low level XML path traversal, selection, and iteration operations.

Skuery implements XPath 2.0 present in XQuery 1.0 traditional syntax by transforming XQuery AST segments into a super SXPath 1.0 S-expression syntax called KSXPath 2.0.
KSXPath 2.0 can be use directly by the end user as a native scheme syntax of XPath 2.0, which Skuery adopts for XPath 2.0 expression in S-expr described XQuery 1.0 queries.

KSXPath 2.0 reuses the low level implementation functions of SXPath and injects its own combinators into the evaluation pipeline to implement XPath 2.0 functionality.


Learning Scheme Macros

Wednesday, March 1st, 2006

I’ve been really impressed with the expertise and academic richness of the functional programming and programming language theory community. I have found the community relatively open, transparent, and willing to adopt and mentor newcomers.
However the word choice in vocabulary and nomenclature could use some help.

[PDF] Writing Hygienic Macros in Scheme with Syntax-Case finally helped me understand Scheme Macros.

Note that implicit-identifier is now called datum->syntax-object and that the examples need syntx->list inserted wherever list operations are attempted on a syntax object.

PLT Scheme Macros

Saturday, February 25th, 2006

Scheme has a very rich macro system that helps to reduce redundant code. PLT Scheme support two ways of writting macros (syntax-rules) and (syntax-case). I’m writting a shallow embedding of XQuery in Scheme. In order to to acomplish the end goal, I’m parsing XQuery syntax into an abstract syntax tree form that I can then maninpulate and transform into scheme code for execution.
An abstact syntax tree is just a tree of specialized nodes. Each individual node in the tree can be seen as an arrays of values, a hashs or specialized data structure. I Choose the data-type structure format. Scheme provides a (define-struct) form and Essentials of Programming Languages (EOPL) provides a (define-datatype) form, both of which define data structures. However, neither one was exactly what I was looking for.

EOPL syntax would look like the following
(define-datatype Term Term? (StringTerm string) (RationalTerm numerator denominator))

That bothered me was having to provide both the name of the datatype and the name of the datatype test predicate, Term and Term?. Convention says that the predicate function name should be the same as the datatype name with a question mark appended. Why should I have to specify both. The define-datatype macro should take care of that for me.

Back to PLT Scheme. The syntax-rules form really is just a pattern/template replacement system. The syntax-case form on the other hand allow arbitrary transformation of syntax, using transformational expressions that are executed in a seperate environment at macro expansion time before actual execution begins.

To take a symbol such as Term and expand that to two functions Term and Term?, we need the power of arbitrary expressions, not the limitations of simple pattern/template replacement. So (syntax-case) is the for we need to use. It didn’t seem that hard, so I set out to write a new (define-datatype) macro.

Well after several days of agony and failure, I finally achieve success this morning.
The trick, Quasisyntax.

Just like quoting) and quasiquoting in standard scheme expressions, you can do the same thing in macro. But when developing syntax-case macros one uses the (syntax) and (quasisyntax) forms.

In the final form below #` is the abbreviated form of (quasisyntax) and #, is the abbreviated form of (unsyntax).
The let statement is where the magic occurs to transform “Type-name” into “Type-name?”

(define-syntax (define-datatype stx)
(syntax-case stx ()
((_ Type-name (Variant-name Field-name …) …)
(let ((Type-name? (datum->syntax-object (syntax a) (string->symbol (string-append (symbol->string (syntax-object->datum (syntax Type-name))) “?”)))))
#`(begin
(define (Type-name) (display “It works\n”)
(define (#,Type-name?) (display “This works too\n)
)

Cool Scheme Resources

Saturday, February 4th, 2006

Several weeks ago I started to download the Structure and Interpretation of Computer Programs Video Lectures by Hal Abelson and Gerald Jay Sussman. I’ve had an opportunity now to view the first three or so. Both Abelson and Sussman are very good lecturers and teachers. I’ve greatly enjoyed them and would recommend them.

Second is the Scheme to C in 90 minutes presentation, about how to write a scheme to c compiler in 90 minutes.

Pretty cool stuff :)