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.)
Excellent! I'm installing v8 right now to give this a try.
--
BD
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
> 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.
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.
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
> 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.
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.
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
Checked Firefox, it dies with a "too much recursion" error. Will try to pin it down.
cheers, Hakan
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),
> > 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