Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to implement lisp "language" in Perl 6?

In here, interpretation of Hello $world per each quoting symbol I mean language.

 $world = "WΩrlδ"
"(Hell)*o $world\n"           # <--- plain (Hell)*o, resolve $world, escape \n
'(Hell)*o $world\n'           # <--- plain (Hell)*o, plain $world, escape \n
/(Hell)*o $world\n/           # <--- regexp (Hell)*, resolve $world, interpret \n
<(Hell)*o $world\n>           # <--- make list ["(Hello*o", "$world\n"]
{(Hell)*o $world\n}           # <--- syntax error, this language cant' parse it

So is Perl 6 powerful enough to be able to exist in future language for something like

my $emacs_func = (defun perl-backward-to-start-of-continued-exp (lim)
      (if (= (preceding-char) ?\))
          (forward-sexp -1))
      (beginning-of-line)
      (if (<= (point) lim)
          (goto-char (1+ lim)))
      (skip-chars-forward " \t\f"))


$  typeof($emacs_func)
> Emacs Lisp list

So, question obviously is: can it be done in present specification (or even implementation) of Perl 6?

like image 715
DinGODzilla Avatar asked Nov 08 '11 20:11

DinGODzilla


3 Answers

Carl Masak recently (late 2014) created ipso, "A metacircular Lisp in Perl 6" that works on current Rakudo.

There will be ways it can be combined with P6 inline; please explore "slangs" for more about that, eg a recent blog post about macros/slangs enabling recursive inlining of arbitrary langs.

See also Damian Conway's Quicksort in (P6ish) Lisp

like image 180
raiph Avatar answered Nov 10 '22 16:11

raiph


Perl 6's grammar is just a grammar written in Perl 6, and very malleable (though current implementations don't quite provide all of the specced flexibility).

So what you ask is possible in principle, but might need more care. In particular are round parenthesis perfectly valid Perl 6 syntax, and even (defun a b) parses as valid Perl 6. So you'd need to be /really/ careful with disambiguation rules, and it would be a huge can of worms.

It probably makes more sense to restrict Lisp syntax to some specially delimited syntactic construct (like lisp(...) or q:lisp{...}), though some amount of mixing would probably be implementable.

I'm sure that once such features are available in a compiler, we'll see a whole lot of interesting experiments, and only those experiments will show what kind of language mixing is both feasible and useful.

like image 34
moritz Avatar answered Nov 10 '22 14:11

moritz


[I seem to have missed the question is about Perl6, not Perl5. Leaving my answer up anyway since it might be of interest to some.]

It's my understanding that Scheme and Lisp are related. If you're ok with Scheme instead, Inline::MzScheme allows one to have blocks of Scheme code in Perl.

Even if you're not ok with Scheme, you could surely fork the module to edit it to use your favorite Lisp engine without too much trouble.

It's not quite what you described, but as moritz explained, what you described is impossible because there's no way to know what parts of the code should be treated as Perl code and which parts should be treated as Lisp code.

On the other handle, through the use of 5.14's pluggable token handler (used by feature::qw_comments to override qw, for example), it should be relatively easy to do the following:

my $emacs_func = lisp(defun perl-backward-to-start-of-continued-exp (lim)
   (if (= (preceding-char) ?\))
      (forward-sexp -1))
   (beginning-of-line)
   (if (<= (point) lim)
      (goto-char (1+ lim)))
   (skip-chars-forward " \t\f"));

(Note the addition of lisp to your code.)

like image 7
ikegami Avatar answered Nov 10 '22 16:11

ikegami