Shen-js

277 views
Skip to first unread message

Ramil Farkhshatov

unread,
Jan 26, 2012, 3:51:41 PM1/26/12
to qil...@googlegroups.com
Hello.

I've just finished K-Lambda to Javascript translator and cli shen-js
implementation (attached).

It is a simple translator of K-Lambda to js and shen-js made by it.
Translator does the work without complex code transformation and is done
only as an exercise. I've never written in Javascript before. Consider
it as a proof of concept.

Shen-js passes all standard shen tests but one ("Cannot map unit string
to byte" in whist.shen).

# Working environment

Shen-js is developed and tested on

* SpiderMonkey[1] cli interpreter (`js`),
* V8[2] cli interpreter (`d8`).

Browser version can be done by implementing file IO and REPL.

# Implementation details

Writing files is not supported since neither `js` nor `d8` can write
files.

Working around three K-Lambda features listed below makes js-code messy,
horrible and slow. Currently I'm working on some code transformations
what will improve those matters.

* let — since javascript scope is limited to function-level
(let X V Code) is implemented as a ((lambda X Code) V).

* TCO — is done via trampolines.

* Partial application — is going to be simplified but currently any
function (func X Y Z) is transformed to:
(lambda (X) (lambda (Y) (lambda (Z) ... )))
As benchmarks shows that it is a fastest way of doing currying
without serious code transformation.

# Usage

See README in attached tarball.
Please note, that it takes some time for repl to start.

# Bugs

There are surely some bugs. If found — complain.

# Links

1. https://developer.mozilla.org/En/SpiderMonkey/
2. http://code.google.com/p/v8

(Sorry for my english I don't write in it much.)

shen-js.tar.gz

Bruno Deferrari

unread,
Jan 28, 2012, 10:58:08 AM1/28/12
to qil...@googlegroups.com
> --
> You received this message because you are subscribed to the Google Groups "Qilang" group.
> To post to this group, send email to qil...@googlegroups.com.
> To unsubscribe from this group, send email to qilang+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/qilang?hl=en.
>

Excellent! I'm installing v8 right now to give this a try.

--
BD

Martial Boniou

unread,
Jan 30, 2012, 5:42:12 AM1/30/12
to qil...@googlegroups.com
Hi,

Congratulations, it works like a charm on both vms. Very promising... On shen for Common Lisp we can exceptionally access CL objects and native function, is there a simple strategy to do the same with JavaScript ?
About file systems, it doesn't look like an easy task but AFAIK, Mozilla supports native FS access with OS.File...

On Jan 26, 2012, at 21:51 , Ramil Farkhshatov wrote:

> Hello.
>
> I've just finished K-Lambda to Javascript translator and cli shen-js
> implementation (attached).

> (...)


> # Links
>
> 1. https://developer.mozilla.org/En/SpiderMonkey/
> 2. http://code.google.com/p/v8
>
> (Sorry for my english I don't write in it much.)
>

> --
> <shen-js.tar.gz>


--
Martial

Ramil Farkhshatov

unread,
Jan 30, 2012, 8:08:53 AM1/30/12
to qil...@googlegroups.com
Martial Boniou <hon...@gmx.com> wrote:

> Congratulations, it works like a charm on both vms. Very promising...
> On shen for Common Lisp we can exceptionally access CL objects and
> native function, is there a simple strategy to do the same with
> JavaScript ?

There will be, but currently you have to wrap native functions. Keep in
mind that function names are transformed to be js-compatible names (look
into `str-js-from-shen` function in translator.shen and
`str_js_from_shen_js` in runtime.js) so shen function `is-nice-weather?`
in js would be `is_nice_weather$question$`.

Here is an example:

Consider the function make_coffee(type, volume, with_milk). To call it
from shen it must be wrapped into

function js_make_coffee(type) {
if (type == undefined)
return js_make_coffee;
return (function f1(volume) {
if (volume == undefined)
return f1;
return (function f2(with_milk) {
if (with_mikl == undefined)
return f2;
return make_coffee(type, volume, with_milk);
});
});
}

In shen it would be called as (js-make-coffee Type Volume With-milk).

If you want to make use of TCO the wrapper code will be larger and
uglier.

> About file systems, it doesn't look like an easy task but AFAIK,
> Mozilla supports native FS access with OS.File...

Not in cli interpreter as far as know.

Mark Tarver

unread,
Jan 30, 2012, 10:09:33 AM1/30/12
to Qilang
I've downlaoded this under Windows but the readme does not seem to
work for me

C:\Users\Mark Tarver\Desktop\shen-js\src>js -f shen.js
'js' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\Mark Tarver\Desktop\shen-js\src>d8 -f shen.js
'd8' is not recognized as an internal or external command,
operable program or batch file.

Mark

Ramil Farkhshatov

unread,
Jan 30, 2012, 10:29:24 AM1/30/12
to qil...@googlegroups.com
Mark Tarver <dr.mt...@gmail.com> wrote:

My bad, I didn't provide links to interpreters in readme as I did in
mail.

- https://developer.mozilla.org/En/SpiderMonkey/
- http://code.google.com/p/v8

Nevertheless their developers didn't supply binaries for windows.
Currently I'm trying to build win32 version of d8. Probably someone on
this mailing list is more capable of doing such task for I don't have
windows installed.

Martial Boniou

unread,
Jan 30, 2012, 11:08:39 AM1/30/12
to qil...@googlegroups.com
Thanks for your example.

At Mon, 30 Jan 2012 17:08:53 +0400,


Ramil Farkhshatov <ra...@gmx.co.uk> wrote:
>
> Martial Boniou <hon...@gmx.com> wrote:
>
> > Congratulations, it works like a charm on both vms. Very promising...
> > On shen for Common Lisp we can exceptionally access CL objects and
> > native function, is there a simple strategy to do the same with
> > JavaScript ?
>
> There will be, but currently you have to wrap native functions. Keep in
> mind that function names are transformed to be js-compatible names (look
> into `str-js-from-shen` function in translator.shen and
> `str_js_from_shen_js` in runtime.js) so shen function `is-nice-weather?`
> in js would be `is_nice_weather$question$`.
>

Actually I notice the wrapping, when I type: (define some-thing -> []), the REPL answers some_thing. Maybe the REPL should answer in shen form and not in javascript form...

> Here is an example:
>
> Consider the function make_coffee(type, volume, with_milk). To call it
> from shen it must be wrapped into
>
> function js_make_coffee(type) {
> if (type == undefined)
> return js_make_coffee;
> return (function f1(volume) {
> if (volume == undefined)
> return f1;
> return (function f2(with_milk) {
> if (with_mikl == undefined)
> return f2;
> return make_coffee(type, volume, with_milk);
> });
> });
> }

I am not a JS-friendly developer to know a better way to express partial application.

> In shen it would be called as (js-make-coffee Type Volume With-milk).
>
> If you want to make use of TCO the wrapper code will be larger and
> uglier.
>
> > About file systems, it doesn't look like an easy task but AFAIK,
> > Mozilla supports native FS access with OS.File...
>
> Not in cli interpreter as far as know.

You're problably right! It's so sad JavaScript inherits Self and Scheme without providing enough functionality from its parent in server side context.

--
Martial

Ramil Farkhshatov

unread,
Jan 30, 2012, 11:44:09 AM1/30/12
to qil...@googlegroups.com
Martial Boniou <hon...@gmx.com> wrote:

> Actually I notice the wrapping, when I type: (define some-thing ->
> []), the REPL answers some_thing. Maybe the REPL should answer in shen
> form and not in javascript form...

Missed this one. Fixed.

> I am not a JS-friendly developer to know a better way to express
> partial application.

There is. Will be implemented in next versions.

shen.js

Håkan Råberg

unread,
Jan 30, 2012, 4:22:34 PM1/30/12
to qil...@googlegroups.com
Hi,

I'm new to the group - awesome work on the JS port Ramil, and of course by Mark, for Shen itself - a language I look forward to learn.

I've been looking forward to the JS port for quite some time, and I hacked together a weekend project building a Browser REPL around Ramil's Javascript port, it kinda works (only tested in Chrome):
http://ghettojedi.org/shen-js-repl/

(sources: https://github.com/hraberg/shen-js-repl)

It's not done "properly" and is just a jQuery hack around the CLI version, stubbing out read, write, readline, load and print, while naively driving the REPL - and is most likely missing something important; plan to dig into the Shen Porting doc to see how it really supposed to work.

cheers, Hakan

PS. Please let me know if there's something by doing this that goes against the Shen License and I will take it down.

Bruno Deferrari

unread,
Jan 30, 2012, 5:45:33 PM1/30/12
to qil...@googlegroups.com

This is great, good job!

Works fine in Opera too, except that ctrl+enter isn't working. I will
let you know if I figure out why.

> --
> You received this message because you are subscribed to the Google Groups "Qilang" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/qilang/-/qWl80CjBErcJ.


> To post to this group, send email to qil...@googlegroups.com.
> To unsubscribe from this group, send email to qilang+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/qilang?hl=en.
>

--
BD

Mark Tarver

unread,
Jan 30, 2012, 11:24:49 PM1/30/12
to Qilang
Got it going under Windows; many thanks for your mail and
congratulations.

Mark
> 2.http://code.google.com/p/v8
>
> (Sorry for my english I don't write in it much.)
>
>  shen-js.tar.gz
> 209KViewDownload

Mark Tarver

unread,
Jan 31, 2012, 8:59:30 AM1/31/12
to Qilang
Hi,

Looks interesting; in Explorer I get

Error: The variable -1,$asterisk$version$asterisk$ is unbound.

in the pink box.

Mark

Mark Tarver

unread,
Jan 31, 2012, 9:03:43 AM1/31/12
to Qilang
In Chrome it does work.

Looks good.

Mark
> > PS. Please let me know if there's something by doing this that goes against the Shen License and I will take it down.- Hide quoted text -
>
> - Show quoted text -

Håkan Råberg

unread,
Jan 31, 2012, 4:30:54 PM1/31/12
to qil...@googlegroups.com
Bruno, Mark - thanks for the kind feedback.

Checked Firefox, it dies with a "too much recursion" error. Will try to pin it down.

cheers, Hakan

tcholoka

unread,
Feb 1, 2012, 4:59:48 AM2/1/12
to Qilang

> I've been looking forward to the JS port for quite some time, and I hacked together a weekend project building a Browser REPL around Ramil's Javascript port, it kinda works (only tested in Chrome):http://ghettojedi.org/shen-js-repl/
>
> (sources:https://github.com/hraberg/shen-js-repl)

I'm new to Shen and I tend to be impressed by it's power and
expressiveness. I tried http://ghettojedi.org/shen-js-repl, so I
defined a fonction called total:
(define total
[] -> 0
[H | T] -> (+ H (total T)))

It's OK, but once I activated type checking (tc +), I got "Error:
syntax error in total".

Is it normal or there is a bug in the javascript translator?

And does Shen have (maybe optional) exhaustive pattern matching like
Standard ML?

Regards,

Konstantin Tcholokachvili

Martial Boniou

unread,
Feb 1, 2012, 8:52:35 AM2/1/12
to qil...@googlegroups.com
Hi,

if you activate typechecking your function must be typed. Before (tc +), type:

(declare total [[list number] --> number])

Or declare types in your function:

(define total
{ (list number) --> number }
...)

I advise you the buying and reading of the Qi book version 2. I also recommemd some readings about Edinburgh syntax based languages for functional & logic programming (say, Prolog). The 'Programming Erlang' book presents you some good practices for creating folding functions like the accumulator overused in those declarative languages:

(define total
{ (list number) --> number }
List -> (total-aux List 0))

(define total-aux
{ (list number) --> number --> number }
[] Acc -> Acc
[ H | T ] Acc -> (total-aux T (+ Acc H)))

The Shen-libs on github also presents good examples like reduce in sequence.shen...

At Wed, 1 Feb 2012 01:59:48 -0800 (PST),

Mark Tarver

unread,
Feb 1, 2012, 11:58:49 AM2/1/12
to Qilang
As Martial says, Shen is explicitly typed so

(define total
{(list number) --> number}
[] -> 0
[H | T] -> (+ H (total T)))

is what you need.

Mark

On Feb 1, 9:59 am, tcholoka <tchol...@gmail.com> wrote:
> > I've been looking forward to the JS port for quite some time, and I hacked together a weekend project building a Browser REPL around Ramil's Javascript port, it kinda works (only tested in Chrome):http://ghettojedi.org/shen-js-repl/
>
> > (sources:https://github.com/hraberg/shen-js-repl)
>
> I'm new to Shen and I tend to be impressed by it's power and
> expressiveness. I triedhttp://ghettojedi.org/shen-js-repl, so I

Mark Tarver

unread,
Feb 2, 2012, 7:44:12 AM2/2/12
to Qilang
> Writing files is not supported since neither `js` nor `d8` can write
> files.

It would be good to have this because it is a) part of the spec and b)
a very useful feature. I guess the role of JS as a ubiquitous web
tool makes it undesirable to have the facility to write to a file?
Can you dump JS or KLambda from Shen?

If it proves imposible to match the spec here in JS then we will have
to weaken the requirement.

Mark

Mark Tarver

unread,
Feb 2, 2012, 7:45:12 AM2/2/12
to Qilang
If you get this going we can link it or embed it into the Shen website
which would be great.

Mark

Ramil Farkhshatov

unread,
Feb 2, 2012, 9:09:03 AM2/2/12
to qil...@googlegroups.com
Mark Tarver <dr.mt...@gmail.com> wrote:

> > Writing files is not supported since neither `js` nor `d8` can write
> > files.
>
> It would be good to have this because it is a) part of the spec and b)
> a very useful feature. I guess the role of JS as a ubiquitous web
> tool makes it undesirable to have the facility to write to a file?

This restriction only relates to js, d8 cli-tools and to pre-html5
javascript engines embedded in browsers. Javascript code can access
files in following environments.

1. Javascript engine embedded in arbitrary application which provides
files access api.

2. Javascript embedded in browser that supports HTML5 File API[1,2]
([2] states that currenlty only Google Chrome supports that).

3. Using browser extensions [3,4].

4. In custom builds of d8 and js where support of file access is
added.

> Can you dump JS or KLambda from Shen?

JS can be dumped from KLambda in any other Shen.

> If it proves imposible to match the spec here in JS then we will have
> to weaken the requirement.

It just depends on javascript environment. Since mainly javascript is
used in web then after HTML5 File API will be supported by mainstream
browsers the problem will cease to exist.

Links:

1. http://www.w3.org/TR/FileAPI/
2. http://www.html5rocks.com/en/tutorials/file/filesystem/
3. http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm
4. https://dutherenverseauborddelatable.wordpress.com/2011/12/06/introducing-javascript-native-file-management

Mark Tarver

unread,
Feb 24, 2012, 5:47:37 AM2/24/12
to Qilang
I would love to link this to the main site and advertise it; can you
fix it to run under Explorer?

Mark

On Jan 30, 9:22 pm, Håkan Råberg <hakan.rab...@gmail.com> wrote:
> Hi,
>
> I'm new to the group - awesome work on the JS port Ramil, and of course by Mark, for Shen itself - a language I look forward to learn.
>
> I've been looking forward to the JS port for quite some time, and I hacked together a weekend project building a Browser REPL around Ramil's Javascript port, it kinda works (only tested inChrome):http://ghettojedi.org/shen-js-repl/

Håkan Råberg

unread,
Feb 24, 2012, 8:02:36 PM2/24/12
to qil...@googlegroups.com
Hi Mark,

I will revisit this over the weekend + upgrade it to Shen 3.1 and the official release of Ramil's port (been working on the Clojure effort).

My impression is that at least for Firefox the issue might be hard to fix without digging into the actual implementation. I think it blows the stack when loading declarations(.js).

I did spike to run shen.js itself as a web worker a few week back in an attempt to overcome this (and the unresponsiveness when running the tests in Chrome), and slicing the file in different ways , but no luck yet. IE had a different issue, will check it out.

You're obviously more than welcome to link to it!

cheers, Hakan
Reply all
Reply to author
Forward
0 new messages