Author Topic: Elk Scheme Extension Module.  (Read 468 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2787
    • ScriptBasic Open Source Project
Elk Scheme Extension Module.
« on: May 27, 2019, 06:33:01 PM »
In the process to trying to get a Fibonacci Scheme script running under TinyScheme it produced the wrong answer. The TinyScheme extension module was just a minimal API connection to Scheme. Basically a Scheme EVAL function.

I ran into Elk Scheme which has an extensive C API and BIGINT support. I would like to create an extension module for ScriptBasic with Elk. My hope is Mike will have a casual interest and mentor this project along.



Elk Project Site   Git Repository


Code: Text
  1. What is Elk?
  2. ------------
  3.  
  4. Elk is an implementation of the Scheme programming language.
  5. In contrast to existing, stand-alone Scheme systems Elk has been
  6. designed specifically as an embeddable, reusable extension language
  7. subsystem for applications written in C or C++.
  8.  
  9. Developers using Elk can deliver applications with different components
  10. written in different languages, such as an efficient core written in
  11. C or C++ and an extensible user interface layer implemented in Scheme.
  12. To help building hybrid application architectures, Elk supports a
  13. tightly-knit interworking of the C/C++ parts of applications with
  14. Scheme code.
  15.  
  16. Elk is also useful as a stand-alone Scheme implementation, in particular
  17. as a platform for rapid prototyping of X11-based Scheme programs.
  18.  
  19. The Elk project was started in 1987 to support ISOTEXT, a multimedia
  20. document editor that has been developed at the Technical University of
  21. Berlin.  The first freely available version, Elk 1.0, was published in
  22. USENET in September 1989.  Since then, Elk has been successfully used as
  23. the extension language framework for numerous applications (commercial
  24. products as well as free software projects).
  25.  
  26.  
  27. Getting Elk
  28. -----------
  29.  
  30. You can obtain the Elk 3.99 distribution as well as additional information
  31. about Elk in the World Wide Web at
  32.  
  33.    http://sam.zoy.org/elk/
  34.  
  35. The distribution is also available for anonymous FTP from a number of
  36. servers including these:
  37.    
  38.    ftp://ftp.x.org/contrib/devel_tools/elk-3.0.tar.gz
  39.    ftp://ftp.uni-bremen.de/pub/programming/languages/scheme/elk/elk-3.0.tar.gz
  40.  
  41.  
  42. A non-trivial example application using Elk as its extension language
  43. is available as source and pre-compiled binaries (`unroff' is a troff
  44. translator with back-ends for HTML and the -ms and -man macros):
  45.  
  46.   http://www.informatik.uni-bremen.de/~net/unroff
  47.  
  48.  
  49. Elk features
  50. ------------
  51.  
  52.   o  Full incremental, dynamic loading
  53.  
  54.      This facility enables Scheme code to load compiled Scheme extensions
  55.      into the running interpreter (or into the application) on demand.
  56.      Complex Elk-based applications can be decomposed into dynamically
  57.      loadable components to avoid large, monolithic executables.
  58.      Furthermore, user-supplied extension need not be written entirely in
  59.      Scheme; they may include an efficient, low-level layer written in C
  60.      or C++.
  61.  
  62.      Dynamic loading in Elk is supported on many platforms and is not
  63.      restricted to a dlopen() interface.  Elk provides automatic
  64.      initialization of dynamically loaded extensions and takes care of
  65.      C++ static constructors/destructors embedded in object files.
  66.  
  67.   o  Freezing of fully customized applications into executable files
  68.  
  69.      Elk provides a new Scheme primitive `dump' which freezes the dynamic
  70.      runtime image of the Scheme interpreter into an executable file
  71.      (including an enclosing application if present).  This facility
  72.      resembles unexec() in Emacs, but the new executable resumes execution
  73.      by returning from the call to `dump' by which that executable was
  74.      created (not unlike fork() in UNIX).  Dynamic loading and `dump'
  75.      increase the usability of Elk as the backbone of complex applications.
  76.  
  77.   o  Powerful C/C++ interface for language interoperability
  78.  
  79.      Elk provides for a tight integration of the C/C++ core of applications
  80.      (or extensions) with the extension language.  Applications can define
  81.      their own Scheme primitives (three calling disciplines are supported),
  82.      define application-specific first-class Scheme types with customized
  83.      print and read functions, convert objects between Scheme types and
  84.      C/C++ types in various ways, implement weak data structures, raise
  85.      Scheme errors, define Scheme variables and symbols, evaluate
  86.      S-expressions encoded as C strings, and utilize the garbage collector.
  87.  
  88.   o  Full Scheme bindings for X11 and Motif
  89.  
  90.      Several dynamically loadable extensions provide full Scheme access to
  91.      the X11/OpenWindows Xlib, to the application programmer interface of
  92.      the Xt intrinsics, and to the Athena and OSF/Motif widget sets.
  93.      Using these extensions, the graphical user-interfaces of Elk-based
  94.      applications can be built entirely in the extension language.
  95.  
  96.   o  UNIX interface
  97.  
  98.      Elk provides Scheme access to most UNIX system calls and common C
  99.      library functions.  The UNIX extension supports a wide range of
  100.      different UNIX platforms without restricting its functionality to the
  101.      lowest common denominator or to the POSIX 1003.1 functions.
  102.  
  103.   o  Stop-and-copy and generational, incremental garbage collection
  104.  
  105.      Elk employs two garbage collection strategies selectable at compile
  106.      time: a traditional stop-and-copy garbage collector and a generational
  107.      garbage collector which is more efficient and thus reduces the time the
  108.      application is disrupted by a garbage collection.  On platforms with
  109.      advanced memory management, `incremental' mode can be enabled for the
  110.      generational garbage collector to further reduce wait times.
  111.  
  112.   o  Non-standard Scheme features
  113.  
  114.      In addition to the standard Scheme core, Elk supports first-class
  115.      environments, error handling, provide/require and autoloading,
  116.      fluid bindings and dynamic-wind, simple `eval-twice'-style macros,
  117.      property lists, string ports and bidirectional ports, shell-style
  118.      `tilde expansion' in filenames, an interactive top-level written
  119.      in Scheme, a Scheme debugger and a pretty printer, arbitrary length
  120.      bitstrings, and Scheme records.
  121.  
  122.   o  Comprehensive documentation
  123.  
  124.      The distribution includes 230+ pages of fully indexed documentation.
  125.      All manuals exist as troff input files which can be translated to HTML
  126.      (with `unroff') for online browsing in addition to producing typeset-
  127.      quality printed versions.
  128.  
  129.   o  Distributed in legally unencumbered form
  130.  
  131.      The copyright/license agreement permits free redistribution and use
  132.      of Elk in commercial products.
  133.  
  134.  
  135. Why is Elk using Scheme?
  136. ------------------------
  137.  
  138. As extensions can get as large and complex as freestanding programs,
  139. extension language programmers (usually the users of the application)
  140. deserve the same language features that other programmers are
  141. accustomed to.  By using a general-purpose programming language rather
  142. than a specialized scripting language, non-trivial extensions can
  143. benefit from the structuring functionality inherent in real programming
  144. languages (such as Lisp).
  145.  
  146. Members of the Lisp language family are particularly suitable as an
  147. extension language:  Lisp has a simple syntax but powerful semantics,
  148. it allows for small implementations, and its interactive nature
  149. supports rapid prototyping and encourages users to explore and test
  150. solutions to problems in an incremental way.
  151.  
  152. Consequently, Lisp has become increasingly popular for this purpose, to
  153. the point where the abundance of different dialects has grown into a
  154. problem.  Of the standardized dialects of Lisp, only Scheme is suitably
  155. modest, yet sufficiently general, to serve as a reusable extension
  156. language for a wide range of applications.  Scheme is orthogonal and
  157. well-defined, and it is small enough to not dominate the application it
  158. serves and to be fully understood with acceptable effort.
  159.  
  160.  
  161. The Elk distribution
  162. --------------------
  163.  
  164. Here is a brief roadmap for the subdirectories and files included in
  165. the distribution.
  166.  
  167.  
  168. elk-3.0 --+-- README      Explains the purpose and release status of Elk
  169.           |
  170.           +-- CHANGES     Lists the changes between this and earlier releases
  171.           |               of Elk
  172.           |
  173.           +-- MIGRATE     Explains how C/C++ code (applications or extensions)
  174.           |               written for older versions of Elk may have to be
  175.           |               modified to make it work with this version
  176.           |
  177.           +-- INSTALL     Instructions for configuring, compiling, and
  178.           |               installing Elk; a brief description of the files that
  179.           |               get installed in the process; and a description of
  180.           |               the structure of the Makefiles and the purpose of
  181.           |               Makefile.local and `build' in each source directory
  182.           |
  183.           +-- MACHINES    Additional, platform-specific advice for installing
  184.           |               and using Elk, such as compiler bugs, unsupported
  185.           |               features, problems with older OS versions and other
  186.           |               pitfalls
  187.           |
  188.           +-- BUGS        Information about known problems with this release
  189.           |
  190.           +-- TODO        Ideas, improvements and projects for future releases
  191.           |
  192.           +-- COPYING     The copyright status of the distribution
  193.           |
  194.           +-- AUTHORS     A list of people who have contributed significantly
  195.           |               to Elk; acknowledgments and credits
  196.           |
  197.           +-- include/    The include files to be #included by applications
  198.           |               that use Elk as their extension language, and by
  199.           |               extensions to Elk.  Including scheme.h from this
  200.           |               directory causes all the other .h files to be
  201.           |               included in the right order.  The include files may
  202.           |               or may not use ANSI/ISO-C prototypes, depending on
  203.           |               the config file you have chosen.
  204.           |
  205.           +-- src/        The C source files of the Scheme interpreter
  206.           |
  207.           +-- scm/        Scheme files that are loaded during runtime.  These
  208.           |               are copied to a destination directory specified in
  209.           |               config/site when Elk is installed.
  210.           |
  211.           +-- lib --,     This directory tree holds the C source for various
  212.           |         |     Elk extensions that can be loaded into the Scheme
  213.           |         |     interpreter or linked with an application
  214.           |         |
  215.           |         +-- xlib/  The C source files of the X11 Xlib extension
  216.           |         |
  217.           |         +-- xt/    The C source files of the Xt (X11 Toolkit
  218.           |         |          Intrinsics) extension
  219.           |         |
  220.           |         +-- xaw/   The Scheme interfaces to the X11 Athena widgets.
  221.           |         |          There is one .d file for each widget class.
  222.           |         |          Each of these is compiled into a C source file
  223.           |         |          when running `make' and then compiled into a
  224.           |         |          dynamically loadable object.
  225.           |         |
  226.           |         +-- xm/    The .d files for the Motif widgets
  227.           |         |
  228.           |         +-- unix/  The C source files of the UNIX extension
  229.           |         |
  230.           |         `-- misc/  The C source files of the record extension, the
  231.           |                    bitstring extension, the regular expression
  232.           |                    extension, and various other dynamically
  233.           |                    loadable Elk extensions
  234.           |
  235.           +-- doc/        The directory tree holding the documentation for
  236.           |               Elk as troff input files and pre-generated
  237.           |               PostScript files.  See doc/README for a roadmap
  238.           |               of the `doc' tree.
  239.           |
  240.           |
  241.           +-- examples --,   A collection of demonstration programs for Elk
  242.           |              |   and the various extensions (mostly in Scheme)
  243.           |              |
  244.           |              +-- scheme  Basic Scheme demos (collected from USENET
  245.           |              |           and other sources)
  246.           |              |
  247.           |              +-- xlib    Programs demonstrating the Xlib, Athena,
  248.           |              +-- xaw     and Motif extensions
  249.           |              +-- xm
  250.           |              |
  251.           |              +-- unix    Example programs for the UNIX extension
  252.           |              |
  253.           |              +-- regexp  A demonstration of the regexp extension
  254.           |              |
  255.           |              `-- c++     A few simple C++ programs demonstrating
  256.           |                          use of Elk with C++ applications (see
  257.           |                          README in this directory)
  258.           |
  259.           `-- util/       Various utilities, some of which may aid in preparing
  260.                           a config file for an as yet unsupported platform.
  261.  
  262.  


Here is the 1 million digit fabonacci in Elk. It took an hour and 15 minutes in ScriptBasic native.

Code: Scheme
  1. ; Fibonacci using fast doubling formulas, good for millions of digits!
  2. (define (fibo n)
  3.     (cond
  4.         ((= n 0) 0)
  5.         ((= n 1) 1)
  6.         (else
  7.  
  8.             (let*
  9.                 (
  10.                     (k (quotient n 2))
  11.                     (a (fibo k))
  12.                     (b (fibo (- k 1)))
  13.                 )
  14.  
  15.                 (if (even? n)
  16.                     (* a (+ (* 2 b) a))
  17.                     (let*
  18.                         (
  19.                             (2a (* 2 a))
  20.                             (c (* (+ 2a b) (- 2a b)))
  21.                         )
  22.  
  23.                         (if (= (modulo n 4) 1)
  24.                             (+ c 2)
  25.                             (- c 2)
  26.                         )
  27.                     )
  28.                 )
  29.             )
  30.         )
  31.     )
  32. )
  33. (Display (fibo 4784969))
  34.  


jrs@jrs-laptop:~/sb/examples/test$ time elk -l elkfibo.scm > elkfibo.out

real   11m1.066s
user   11m0.855s
sys   0m0.016s
jrs@jrs-laptop:~/sb/examples/test$ ls -l elkfibo.out
-rw-r--r-- 1 jrs jrs 1000000 May 27 18:40 elkfibo.out
jrs@jrs-laptop:~/sb/examples/test$ tail -c64 elkfibo.out
6330930391815964864885353407167474856539211500699706378405156269
jrs@jrs-laptop:~/sb/examples/test$


« Last Edit: May 27, 2019, 07:13:57 PM by John »

Offline Mike Lobanovsky

  • (re)TIRED
  • BASIC Developer
  • Posts: 281
Re: Elk Scheme Extension Module.
« Reply #1 on: May 28, 2019, 03:09:48 AM »
... TinyScheme ... produced the wrong answer.

No wonder. TinyScheme has no BIGINT support, and it can only perform calc on, and print, numeric quantities it supports natively, of which double precision is the largest.

And thanks but no, I am not interested. Elk is a full scale ...NIX-oriented project with only minimal support for Windows. It can probably take one's lifetime to develop and maintain a Windows fork of it as a hobby, which isn't exactly my idea of bumming around in the declension of my years. ;)
Mike
____________________________________________________________________
(3.6GHz Intel Core i5, 16GB RAM / nVidia GTX 1060Ti , 6GB VRAM / x64 Win7 Ult.)

Offline John

  • Forum Support / SB Dev
  • Posts: 2787
    • ScriptBasic Open Source Project
Re: Elk Scheme Extension Module.
« Reply #2 on: May 28, 2019, 07:48:43 AM »
Actually TinyScheme wasn't at fault, it was the users Scheme code. Thanks for your feed back on Elk.
« Last Edit: May 28, 2019, 08:29:37 AM by John »

Offline Mike Lobanovsky

  • (re)TIRED
  • BASIC Developer
  • Posts: 281
Re: Elk Scheme Extension Module.
« Reply #3 on: May 28, 2019, 11:21:45 AM »
What does a corrected version of Scheme script look like, please?
Mike
____________________________________________________________________
(3.6GHz Intel Core i5, 16GB RAM / nVidia GTX 1060Ti , 6GB VRAM / x64 Win7 Ult.)

Offline John

  • Forum Support / SB Dev
  • Posts: 2787
    • ScriptBasic Open Source Project
Re: Elk Scheme Extension Module.
« Reply #4 on: May 28, 2019, 01:08:23 PM »
Here is a Fibonacci Scheme example that works great in TinyScheme.

Heater@RPi  posted the funky Scheme code that made me think TinyScheme was broken.

I think ScriptBasic and Scheme would make a very powerful language.

RPI Post
« Last Edit: May 28, 2019, 04:12:59 PM by John »

Offline Mike Lobanovsky

  • (re)TIRED
  • BASIC Developer
  • Posts: 281
Re: Elk Scheme Extension Module.
« Reply #5 on: May 29, 2019, 01:13:53 AM »
... example that works great in TinyScheme.

:o

It might work great if the parameter is sufficiently low not to overflow the "registers". But TinyScheme will invariably falter at (fibo 4784969) with either the earlier "fast doubling formulas" code or this example you've pointed me to.

(see screenshots below for 3 variants of TinyScheme we used to work on in the past; none of them prints the expected result)

As I said, TinyScheme can only calc and print integers it can store in its "registers" as a whole, which is up to +4,294,967,295 unsigned on x86, or up to +18,446,744,073,709,551,615 unsigned, on x64.

Or has there any other, more advanced version of TinyScheme appeared in the meantime that supports BIGINT calc? ???
Mike
____________________________________________________________________
(3.6GHz Intel Core i5, 16GB RAM / nVidia GTX 1060Ti , 6GB VRAM / x64 Win7 Ult.)

Offline John

  • Forum Support / SB Dev
  • Posts: 2787
    • ScriptBasic Open Source Project
Re: Elk Scheme Extension Module.
« Reply #6 on: May 29, 2019, 07:00:32 AM »
Quote
Or has there any other, more advanced version of TinyScheme appeared in the meantime that supports BIGINT calc? 


It seems the RPi guys are having issues getting these embeddable Scheme languages to run under Raspian 32 bit.