lunes, 21 de febrero de 2011

run interactive applications inside emacs

While implementing a more decent repl for HAScheme, I stumbled upon a nifty emacs feature: comint.

To run an interactive shell, get history for free, and completion (at least hippie-expand if you teach your emacs to use it), you just have to use make-comint.


(defun run-has ()
(interactive)
(make-comint "has" "/path/to/hascheme/script/lis.pl" nil "/path/to/hascheme/script/builtins.scm"))


the last parameter is the argv, you can fill it with whatever you want. In my case, the scheme file with some builtins.

On the hascheme side, I've been experimenting with tail call optimitzation, an improved repl with multiline support and with some cool debugging features. You'll know shortly about them if you follow this little blog of mine.

Well, and here's a screenshot of the comint buffer on hascheme. note the completion thing on the minibuffer.

martes, 8 de febrero de 2011

(let ((there be)) lisp)

I keep having lots of fun with hascheme. In the last couple of
days, I implemented quite a few things. Mostly they are syntax
sugar for things you could already do in HAS, or useful procedures
needed for my basic interpreter testing.


  • Perl side

    • (define (fun args) body) is now accepted as an alternative way
      to declare procedures.

    • (let) implemented. These bindings for local vars are just sintactic sugar for
      what you'd write in perl:
      sub { my $a=shift; … }->(initialval)


  • Scheme side
    Reread the 'Why functional programming matters' paper, and implemented:

    • make-counter, foldr

    • append, sum-list, length, map . All are specific cases of the previous


Reading chapter 4 of our MIT beloved book, I saw the next step, making a lazy
interpreter from this Half Assed one.

Ah, the repo

lunes, 7 de febrero de 2011

Put more vim into your emacs


I'm a vim editing model fan, but emacs is slowly crawling into my
life. emacs had viper (vi emulator) for ages, but it falls short if
you're a vim power user. Vi is not Vim.



For some time there's been vimpulse package that tried to narrow the
difference, adding Visual mode, and some other goodies. Recently,
vimpulse got reactivated, and now, we get text objects, and many
other features from our beloved text editor.



You can even use map, imap, vmap and the like;



the first thing I tried was mapping jk to esc, but the tip in the
vimpulse wiki didn't work. I managed to make it work with this line
in my .emacs file.



(vimpulse-imap "jk" 'viper-exit-insert-state)



Now, there's no need to reach the far escape key.

viernes, 4 de febrero de 2011

QuasiQuine

Little updates to Hascheme, I finally implemented cons, car, cdr,
and list as primitives, After implementing the prove that you can
have datastructures in a language without no more ground than
lambdas, I went for a bit more of efficiency (not that hascheme is
going to be in production ever, but you know…)



Another motivation to implement lists as primitives is that this
way, I can get a kind of homoiconicity. If conses are implemented
in the implemented language (scheme), a list will never be similar
to code, because Perl never sees the datastructure as something it
can deal with.



And what's the ultimate motivation for having all this stuff
working? fun.



I used a very common lisp quine to test that code and data are
implemented internally the same way. That gave a good testbed for
quote too. Unfortunately, using Perl Data::Dump* modules, I can't
tune how lists are printed, and the final nil gets printed too.



Well, here's the proof that Half Assed Scheme can do Half Assed
Quines.




Next step, testing that closures work. (I'm afraid they don't work quite well)

martes, 25 de enero de 2011

(cadr has)

I've been hacking on HAS a bit more, to push forward its functionality.
Mostly what's been achieved by now:


  • Perl side

    • (repl) primitive implemented. it acts as a debugger in some way,
      because it gives you the environment in the state of the moment
      when you executed it. I couldn't implement it in scheme itself
      because I have no eval available from scheme.

    • Read plain files. As I started implementing some functionality
      in scheme, A scheme file must be loaded at start. just enter the
      file name as a parameter. Last sexp should be (repl) to have
      your repl available.

    • Comments. ';' is the comment marker

    • = operator

    • nil. transformed to 0.

    • list. yeah. As HAScheme can't understand variable length
      arguments, I implemented it to make it more confortable to build
      lists. It's built on an ugly hack. More on that later.

  • Scheme side

    • cons, car, cdr. Implemented basic structures. Implemented as
      lambdas. as in SICP, somewhere in chapter 2 IIRC. Simple though
      impressive IMHO (and inefficient, but…)

    • print_list. conses down a list and prints each element in a
      line

I run it using builtins.scm as the first parameter, and then, repl is
yours, with lists and conses available for you.

Now the not-so-beautiful parts


If I want lists, I just have to nest conses, but who doesn't want a
litte luxury?.

To implement list, I've thought 3 possible solutions (haven't read

about how to implement them correctly, just brainstorming):


  • Variable actual arguments. If I could implement the dot (.) syntax
    for variable number of arguments, I could implement list as a
    simple scheme function. Problem is that I need lists to stash
    the resting parameters (hascheme lists, not Perl ), and I haven't thought how to bootstrap.

  • Write a new primitive (on the perl side) that builds the correct
    cons structures, on the perl side. Mind you, that list is VERY
    coupled to cons implementation. And quite fuzzy on the perl side
    too.

  • Substitute list for the appropiate number of conses (as a string),
    and let the already written parser do the job. That's the one I implemented.

Probably, I'll start from the scratch building another scheme interpreter, following BillHails book. Or at least, I'll go as far as my motivation and brain allow me.

That's all for now. Here's my other post about Hascheme, and the github repo.

Byez.

domingo, 23 de enero de 2011

I can has HAS (Half Assed Scheme)


If you've been reading along these bloglines, you already know I'm on
my way to learn lisp, and given that a typical exercise on the lispers
path is writing a scheme interpreter, I decided to try to impement a
scheme interpreter in perl. I had a great guidance of Peter Norvig's lispy tutorial, and more thoughtful explanations from Bill Hails' book.




I can't say I wrote it as quick as I thought I would, and it could
definately be clearer (OOP in such small projects is clearly
overengineering), but in the end, it works, and given the fact that I
hadn't been programming perl at work for 2 months, it's been a good exercice.



The relevant code is in 4 files,


  • script/lis.pl (REPL and primitives),

  • lib/hascheme/Reader.pm (Parsing)

  • lib/hascheme/Env.pm (environment, a tree structure built on hashes)

  • lib/hascheme/Evaluator.pm (special operators are treated here, and overall evaluation)



Anyway, here you are, the code is in a github repo. For now, It's
called half assed scheme, or hascheme, or HAS.

Related article: next post on HAS


Now, you can has HAS too.

jueves, 6 de enero de 2011

generate Moose classes from CLI

It seems I've been a bit out of Perl lately (last 2 or 3 weeks), but that's not completely true.

I have a couple of projects in mind (one bigger than the other, but both interesting nonetheless), but I have not been in my best moods to program... difficulties to focus. At work, I do exactly 0% perl :(, so I have to push myself to use it...

In one of my projects I'm using OOP intensively (maybe overengineering?, we'll see), and I always tend to forget how to correctly set up a bare Moose class.

So here's what I've written:


Just a simple script that generates scaffolds for Moose classes and gives a couple of hints on attributes' properties. A good friend of the script template I did some time ago

I've tried to make it work the most DWIM-Y way, so you call it like "genclass.pl lib/My/App/Entity" , and generates My::App::Entity class in the appropiate path and with appropiate namespace.

SeeYapp! ;p