Using chicken scheme, i installed some 'eggs', but when trying to use any procedure from them, the procedures name is never bound to an actual procedure. For example, on the csi interpreter:
#;1> (import glfw3)
#;2> (init)
Error: unbound variable: glfw3#init
The same thing happens with any procedure or value from any other 'egg' that I've tested, both in the interpreter and the compiler; nothing from any 'egg' ever gets bound.
Do you happen to be using CHICKEN 4? If so, you probably need (use glfw3) instead of (import glfw3). If you are using CHICKEN 5, (import glfw3) is correct and init should work.
Related
I'm trying to write a program compatible with both Chez and Chicken Scheme. Starting with something maximally simple:
(c1) R:\>type hello.ss
(display "hello, world\n")
(c1) R:\>csc hello.ss
(c1) R:\>hello
hello, world
(c1) R:\>"C:\Program Files\Chez Scheme 9.5.8\bin\a6nt\scheme.exe" --program hello.ss
Exception: invalid top-level program import subform (display "hello, world\n") at line 1, char 1 of hello.ss
Chicken is happy with the simplest version, but Chez isn't. Okay, I know a way to make Chez happy:
(c1) R:\>type hello2.ss
(import (rnrs))
(display "hello, world\n")
(c1) R:\>"C:\Program Files\Chez Scheme 9.5.8\bin\a6nt\scheme.exe" --program hello2.ss
hello, world
But does Chicken like that version?
(c1) R:\>csc hello2.ss
Syntax error (import): cannot import from undefined module
rnrs
Expansion history:
<syntax> (##core#begin (import (rnrs)))
<syntax> (import (rnrs)) <--
Error: shell command terminated with non-zero exit status 70: ""c:/chicken/bin/chicken.exe" "hello2.ss" -output-file "hello2.c""
Sadly, no.
How can I write a program that both implementations are happy with?
I have created executable of following code in Racket (choosing Racket and not GRacket):
#lang racket
(print "Hello World!")
It creates a tgz of 3.6 mb with an executable of 6.2 mb. This seems very large for this simplest program. Executable created by Chicken Scheme with same code (print "Hello World!") is of size 16984 bytes (16.6 kb) only.
I think I am missing something (possibly some optimization setting) while creating executable in Racket. How can this executable be made smaller?
The documentation for raco exe recommends using as small a base language as possible. In this case, replace #lang racket with #lang racket/base. On my machine (Linux, Racket 6.8) that drops the executable size from 6.6M to 988K.
I was able to further reduce the size of the executable by running the demodularizer first. I saved the program as hello.rkt and ran
$ raco demod hello.rkt
$ raco exe -o hello hello_rkt_merged.zo
That produces an executable of 277K.
Apparently my previous question was too broad. So here's the question again, simplified, and with example source code.
I'm trying to compile a Chicken Scheme project containing multiple files:
test-a.scm:
#!/usr/bin/csi -script
(declare (unit test-a))
(declare (uses test-b))
(load "test-b.scm")
(use test-b)
(test-syntax)
test-b.scm:
(declare (unit test-b))
(module test-b *
(import scheme chicken)
(define-syntax test-syntax
(syntax-rules ()
((_)
(print "In test-syntax")))))
According to the official manual, I should do it like this:
csc -c test-b.scm
csc -c test-a.scm
csc test-a.o test-b.o -o test
What I actually get is this:
Syntax error (import): cannot import from undefined module
Things to note:
I'm calling a macro.
I have a (declare (uses clause, yet csc can't find my sources.
csc test-a.scm test-b.o -o test doesn't work either.
If I remove load, the program will not work in csi.
If I remove use, the program will not work in csi.
I need the program to work in csi.
How, without breaking compatibility with csi, can I make this compile?
There are four(!) problems here:
test-a.scm contains a unit declaration. This is incorrect; there's always one file that needs to be compiled to have a main() C function. That's the file without a unit declaration. If you study the manual page you linked more closely, it says "In this case foo.scm is the main module, because it doesn't have a unit declaration".
Since you decided to use modules, you'll need to compile test-b.scm as follows: csc -c -j test-b test-b.scm. The -j switch will cause the compiler to emit a module library test-b.import.scm, which is what the compiler is looking for when compiling test-a.scm. When an import library is missing, it will complain that the module is undefined. In the interpreter it's no issue because you load the file before importing the module that it defines.
You're using load, even in the compiled version of the program. This means that it will read and evaluate the test-b.scm file (and complain if it's missing) in every situation.
You're using use, which will require the library at runtime. This is meant for loading and importing modules defined by dynamically linked libraries.
So, to solve this, you could do it like this:
test-a.scm
#!/usr/bin/csi -script
;; Declare that this uses test-b, so that its toplevel is initialised
(declare (uses test-b))
;; No (declare (unit test-a)) because this file should generate main().
;; Because we tell the compiler what to link together and we want to
;; avoid passing all the .scm files on the csi command line, we can load
;; the test-b.scm file here, but only when interpreting:
(cond-expand
((not compiling) (load "test-b.scm"))
(else))
;; Only import the module; we take care of loading the code above,
;; or in the linking step when compiling. If we had (use test-b),
;; the library would be searched for at runtime.
;; Alternatively, (use test-b) here, but add (register-feature! 'test-b)
;; to test-b.scm, which prevents the runtime from attempting to load test-b.
(import test-b)
(test-syntax)
test-b.scm (unchanged)
(declare (unit test-b))
(module test-b *
(import scheme chicken)
(define-syntax test-syntax
(syntax-rules ()
((_)
(print "In test-syntax")))))
And, to compile it:
csc -c -j test-b test-b.scm
csc -c test-a.scm
csc test-a.o test-b.o -o test
I realise this is quite a lot of stuff to know, and tricky too and some things like the use plus register-feature! simply don't make much sense. We're attempting to make this less fiddly in CHICKEN 5, and we're also going to add a FAQ to the wiki, because this really isn't obvious and a bit of a FAQ.
The manual page you linked hasn't been changed in a long time: it completely ignores the existence of modules, for example. That's why you couldn't get it to compile, the -j switch was missing because the example files in the manual page don't define modules.
Edit:
This can be cleaned up a bit because declare is only honored by the compiler anyway. So we can move that into the cond-expand as well:
test-a.scm
#!/usr/bin/csi -script
(cond-expand
(compiling (declare (uses test-b)))
(else (load "test-b.scm")))
(import test-b)
(test-syntax)
I have created a abc.scm file and tried to compile it to a binary (or whatever guile scheme compiles to) using "guild compile", "scm" and "guile" commands in terminal in Ubuntu.
For "guild compile abc.scm", I get the output "wrote `/home/tarunmaganti/.cache/guile/ccache/2.0-LE-8-2.0/home/tarunmaganti/abc.scm.go'"
I find the file and run like this - "./abc.scm.go" which says permission denied.
When I give the necessary permissions using "chmod 755" or "chmod 777", I get an error like - "bash: ./abc.scm.go: cannot execute binary file: Exec format error".
The "scm whatever-file-name.scm" just opens up the scm interpreter.
The "guile whatever-file-name.scm does nothing.
The link of official GNU/Guile Scheme isn't very helpful.
Please, help me. I would like to create a guile scheme script file. Compile it and run it as C/C++ program. Is it possible?
If compilation isn't possible, I would like to know, how to at least run the script file of GNU/guile scheme or MIT-Scheme.
{Step-by-step is highly appreciated, I'm still a beginner in using Ubuntu and also in Scheme.}
Thanks in advance.
You can use the shebang notation to create a Guile script (the -s is optional in newer versions of Guile):
#!/usr/bin/guile -s
!#
(display "Hello, world!\n")
Notice the !# on the second line. Guile treats the #! as the start of a block comment (similar to what #| is in standard Scheme), which has to be terminated using !# (similar to |# in standard Scheme).
If you want your script to pass any command-line options to Guile itself, then read about the meta switch. Here's an example of such:
#!/usr/bin/guile \
-e main -s
!#
(define (main args)
(if (null? (cdr args))
(format #t "Hello, world!~%")
(for-each (lambda (name)
(format #t "Hello, ~a!~%" name))
(cdr args))))
Apparently guile foo assumes that foo is a Scheme source file. If foo is a precompiled binary .go file it tries to treat it as a text file anyway and compile it a second time, which fails. I could not find any equivalent command line syntax for executing a pre-compiled .go file.
However, you can get almost the same effect as follows:
Write a source file foo.scm that exports a main procedure:
(define-module (foo)
#:export (main))
(define (main)
(display "Hello world!")
(newline))
Pre-compile it with guild compile -o foo.go foo.scm
Write a shell script that runs guile -C "$PWD" -c "(use-modules (foo)) (main)". The -C dir flag (capital C) tells it to load pre-compiled files from the directory dir. The -c expr flag (lowercase c) tells it to evaluate the Scheme code expr which in this case just does use-module to load the pre-compiled module and then calls our main procedure.
Guile's compiler is like Java's compiler: it produces bytecode that is then run in Guile's VM. I don't know of any way to go straight from Guile Scheme code to native code. Guile is really meant to be an extension language that allows you to add Scheme scripting to your C/C++ program.
I don't know much about MIT Scheme, but from what I can tell it also does not compile to a standalone executable. Feel free to correct me on that.
There's a way around all this, though. As long as you don't mind a dependency on libguile, you can write a wrapper in C/C++ and hand libguile a string containing your Scheme code. There's a basic example given in the Guile manual for Dia here. Create your script in a header file and wrap it in a null-terminated C string, then (after a bit of boilerplate and whatnot) evaluate it with scm_eval_string().
If you want to write Scheme and output native binaries, I've heard good things about Chicken.
I have a fresh install of mingw64 and chicken scheme but whenever I try to compile a file with csc from the command line I get the following:
error: shell command terminated with non-zero exit status 1: "gcc" "foo.o" -o "foo" -wl,--enable-auto-import -Lc:\chicken-iup\lib -L"c:\chicken-iup/lib/" -lchicken -lm -lws2_32"
foo.scm:
(define (fac n)
(if (zero? n)
1
(* n (fac (- n 1)))))
(write (fac 10))
(newline)
Could someone help in figuring out why it cant compile the file? If I have a file browser open to the C:\chicken-iup\ folder I can see it makes a .c file and then makes an .o file. The error happens I assume during the linking of the .o file but I'm not certain.
Looks like you're using chicken-iup. Looking at its web page, it looks like it's designed to work with mingw, and not mingw-w64. The "incompatible" libraries are probably because mingw-w64 is designed to link in 64-bit libraries, but the chicken-iup libraries were 32-bit.
If you want to use mingw-w64, you probably have to compile CHICKEN yourself.