[racket] How to view source code of procedures? MIT scheme "pp" equivalent?

From: Scott Klarenbach (scott at pointyhat.ca)
Date: Sat Dec 28 14:01:48 EST 2013

Matthias,

I'm likely very misguided here and really just learning by hacking around,
so bare with me :).

I'm playing around with the idea of a sort of "Language Integrated Query"
like LINQ.  For example:

> (sql (query 'my-table
     #:where (λ (row) (> (hash-ref row 'id) 3))))
"select * from my-table where id > 3"

I can have a static mapping of known procedure translations to sql (such as
>, <, not), but I thought if I could unroll procedure source code I could
reduce simple definitions to see if they *ultimately* have translations to
sql.  For example:

> (define (some-pred? row) (> (hash-ref row 'id) 3))
> (sql (query 'my-table
     #:where (λ (row) (or (some-pred? row) (> (hash-ref row 'id) 10)))))
"select * from my-table where id > 3 or id > 10"

> (sql (query 'my-table #:where (λ (row) (> (hash-count row) 3))))
error: hash-count has no translation to sql

If that was so, I could transparently reuse procedures designed for
filter/map operations on lists to be reduced down and *potentially*
translated to sql.

I suppose it might be possible to do this with a macro *(define-for-sql
(some-pred? row) ...)* which would provide more power for syntax local
expansion and (possibly achieve???) what I'm trying to do.  But that has
the disadvantage of not being able to reuse functions that were designed
for other purposes yet which fit the required protocol.  It would be nice
if the same query form expression(s) could be reused on lists, vectors,
xml, database tables, etc.

In another thread, I'd like to poll this list as to the benefits (if any)
of such a linq-style approach.  I got inspired by this paper:
http://homepages.inf.ed.ac.uk/slindley/papers/practical-theory-of-linq.pdf and
really enjoyed the c# linq stuff I've played around with.  It may very well
be; however, that this is not desired or useful, and rather I should have
simple macros that work *exclusively* on databases and employ a mini-dsl
that maps directly to sql strings at expansion, ie:

(query 'my-table #:where (id <> 3 or id > 10))

As an aside:


> This might be a MIT misunderstanding about the relationship between Scheme
> and the lambda calculus. It is certainly not possible in general and I see
> no pragmatic use case for an approximation.
>

It would be really nice to print the source code of a known procedure on
the repl, in order to get a quick view of what it does.  Or, the "pa"
function which prints out the arguments of a procedure is even more useful.


On Fri, Dec 27, 2013 at 4:35 PM, Matthias Felleisen <matthias at ccs.neu.edu>wrote:

>
>
> On Dec 27, 2013, at 6:27 PM, Scott Klarenbach wrote:
>
> I noticed that MIT Scheme has the "pp" procedure which prints the source
> code of a given function.  Is there an equivalent way to do this in Racket?
>
>
> No. The 'pp' idea probably dates back to the age of Lisp-machine repls
> when MIT programmers thought that you enter functions into the repl and the
> program is the current state of the repl. Since definitions for large
> programs scroll off the visible screen, you need pp.
>
>
> More generally, I'm looking to normalize nested procedures to their most
> rudimentary expressions.
>
>
>
> This might be a MIT misunderstanding about the relationship between Scheme
> and the lambda calculus. It is certainly not possible in general and I see
> no pragmatic use case for an approximation.
>
> -- Matthias
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> For example:
>
> (define (something? x) (> x 3))
> (define (something-else? x) (< x 10))
> (define (combined x) (and (something? x) (something-else? x)))
>
> (normalize combined) =>
> '(lambda (x) (and (> x 3) (< x 10)))
>
> ((normalize combined) 7) => #t
>
> I'm sure it's not so simple and a robust general solution involves some
> type of beta reduction and/or argument constraints, but I'm just looking
> for a nudge in the right direction.
>
> Thanks.
>
>
>
>
> --
> Talk to you soon,
>
> Scott Klarenbach
>
> PointyHat Software Corp.
> www.pointyhat.ca
> p 604-568-4280
> e scott at pointyhat.ca
> 200-1575 W. Georgia
> Vancouver, BC V6G2V3
>
> _______________________________________
> To iterate is human; to recur, divine
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users
>
>
>


-- 
Talk to you soon,

Scott Klarenbach

PointyHat Software Corp.
www.pointyhat.ca
p 604-568-4280
e scott at pointyhat.ca
200-1575 W. Georgia
Vancouver, BC V6G2V3

_______________________________________
To iterate is human; to recur, divine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131228/8ca6d922/attachment.html>

Posted on the users mailing list.