Chaining file inclusions - racket

I have a file, configuration.rkt:
(define file-path "path/to/file.rkt")
Then I have the 'main' file program.rkt, in which I would like to include the file specified by file-path. I tried this way, but it does not work:
(include "configuration.rkt")
(include file-path)
What can I do?

Use require instead of include.
http://docs.racket-lang.org/guide/module-require.html

As #soegaard said, you want to use Racket's module system via require, instead of textually including source files. It will save you a ton of grief.
I noticed that you seem to want the choice of source file to be determined at runtime?
If so, two options:
You could use units and signatures.
You could use dynamic-require. For example:
config.rkt:
#lang racket
(provide to-be-required)
(define to-be-required "foo.rkt")
foo.rkt:
#lang racket
(provide foo-value
foo-proc)
(define foo-value "I am from foo.rkt and I was dynamic-required!")
(define (foo-proc)
"I am foo-proc.")
main.rkt
#lang racket
(require "config.rkt")
(printf "Let's dynamic-require ~a, as config.rkt said.\n" to-be-required)
(define foo-value (dynamic-require to-be-required 'foo-value))
(define foo-proc (dynamic-require to-be-required 'foo-proc))
foo-value
(foo-proc)
Output:
Let's dynamic-require foo.rkt, as config.rkt said.
"I am from foo.rkt and I was dynamic-required!"
"I am foo-proc."

Related

How to re-export #%module-begin etc from scribble/text?

I want a language like scribble/text but with some additional functions offered. This is what I've tried:
#lang racket/base
(require scribble/text)
(provide (all-from-out scribble/text)
hello)
(define (hello name)
(format "Hello ~a!" name))
When I try to run a module written in this language, I get a message saying that there is no #%module-begin binding in the module's language.
I assume that scribble/text has a binding for #%module-begin, else how does it work when I use it as a #lang?
Maybe scribble/text knows that I am importing it using 'require' rather than as a #lang, and so I don't automatically get the #% macros? If that's the case, then how would I get them and re-export them?
Or is something else happening here?
The language for #lang scribble/text and the library for (require scribble/text) are different modules. The library version doesn't provide #%module-begin or the other bindings from racket:
When scribble/text is used via require instead of #lang, then .... it does not include the bindings of racket/base ....
The module-language #lang scribble/text actually uses is scribble/text/lang. So you can fix your module-language like this:
#lang racket/base
(require scribble/text/lang)
(provide (all-from-out scribble/text/lang)
hello)
(define (hello name)
(format "Hello ~a!" name))
However, the module scribble/text/lang is undocumented, so use at your own risk.

How to 'require' a Racket module that doesn't have a #lang header line?

As just one of many possible examples, break-example.rkt would be a perfectly valid Java program, except for the #lang mini-java header that Racket requires.
So e.g. if I've written a Java interpreter/compiler in Racket as a Racket module language, how can I say, "require this file Main.java which is written in module language mini-java but doesn't have any Racket-specific header"?
(Note that I have almost non-zero practical experience with Racket. I'm evaluating this for a specific use case I have for Racket + DrRacket, which has nothing to do with Java by the way. I searched the documentation but couldn't find any way to achieve this.)
I can’t run or test this right now, but maybe you can start from here and experiment with it. The main thing it uses is include/reader:
#lang racket
(require racket/include
syntax/parse/define
(for-syntax racket/syntax
racket/port
syntax/modread))
(define-simple-macro (require/mini-java path)
#:with modname (generate-temporary #'path)
(begin
(include/reader path (mini-java-reader 'modname))
(require 'modname)))
(begin-for-syntax
;; Symbol -> [Any InputPort -> Syntax]
(define ((mini-java-reader modname) src input)
(cond
[(port-closed? input) eof]
[else
(define stx
(with-module-reading-parameterization
(lambda ()
(read-syntax src
(input-port-append #t
(open-input-string "#lang mini-java\n")
input)))))
(close-input-port input)
(syntax-parse stx
[(module _ l . b)
#`(module #,modname l . b)])])))

Racket, include, require and provide don't work

I have a file with the name “functions.rkt”, where I have some functions.
And I am working in another file, let’s name it “working.rkt”
I have tried the following (one by one) at “working.rkt” to use the function defined at “functions.rkt”:
(require “functions.rkt”)
(include “functions.rkt”)
(provide “functions.rkt”)
And anyone of them hasn’t worked, any help?
They are in the same path.
In the file "functions.rkt:
#lang racket
(provide my-function)
(define (my-function x) (* 2 x))
In the file "working.rkt":
#lang racket
(require "functions.rkt")
(my-function 21)

How to use `typed/racket` in `scribble/lp`

Is it possible to use other #langs in #lang scribble/lp for literate programming?
For example, I want to use #lang typed/racket in #lang scribble/lp. How to realize that?
TL;DR: There are two ways. First, you can simply put your code in a submodule, and require it immediately. Second, you can use my fork of scribble/lp2, which allows you to specify the underlying module language.
First method
Due to an issue, you'll have to wrap the whole thing in a (begin …), although there's a pull request which should fix this on the way.
#lang scribble/lp2
#chunk[<*>
(begin
(module main typed/racket
(define a : Number 1)
(provide a)
(module moo racket/base '…)
(module+ test
(require typed/rackunit)
(check-equal? a (+ 1/2 1/2))))
(require 'main)
(provide (all-from-out 'main))
(module test typed/racket
(require (submod ".." main test))))]
Bear in mind that these are submodule, so you can't put your test module inside main, as it won't be executed. Here, I have a (module+ test …) inside main, and it is required by a test module at the root level. If the (module test at the root is commented out, then the tests won't be executed.
The same concern applies also when requiring moo from another module, you'll have to use (require (submod "myfile.lp2.typed.rkt" main moo)), or make an "alias" like is done above for test and main.
Keep also in mind that the more nested a submodule is, the more times it gets visited and/or instantiated (not sure which). This can have a serious impact on the loading speed in typed/racket programs, as they are already themselves visited and/or instantiated multiple times. You can see this by adding (begin-for-syntax (displayln 'executed)) in your code, it gets printed many times.
Second method
My fork of scribble/lp2, hyper-literate (for hypertext literate programming) only alters scribble/lp2 and relies on the original version of scribble for the rest.
I don't guarantee backwards compatibility, as I'm currently fixing some bugs and adding extra features like being able to re-print a chunk to refresh the reader's memory, etc. The stackoverflow-q-18877881 branch I linked to should remain stable, though. Newer stuff will go in master.
Here's a small example file. There's a more complete example in test/test.hl.rkt:
#lang hyper-literate/typed typed/racket/base
#(require (for-label typed/racket/base
typed/rackunit))
#title{Title}
Hello world.
#chunk[<*>
(require typed/rackunit)
;; Would give an error as typed/racket/base is used on the #lang line:
;curry
(check-equal? ((make-predicate One) 1) #t)
(define (f [x : 'e123]) x)
(define ee (ann (f 'e123) 'e123))
(provide ee)]
It doesn't appear so, but you can use a typed/racket evaluator with scribble/eval.
#lang scribble/manual
#(require racket/sandbox
scribble/eval)
#(define my-evaluator
(parameterize ([sandbox-output 'string]
[sandbox-error-output 'string])
(make-evaluator 'typed/racket/base)))
#interaction[#:eval my-evaluator
(: my-sqr (Real -> Real))
(define (my-sqr x)
(* x x))
(my-sqr 42)]
Example taken from here.

How do I include files in DrScheme?

I'm using DrScheme to work through SICP, and I've noticed that certain procedures (for example, square) get used over and over. I'd like to put these in a separate file so that I can include them in other programs without having to rewrite them every time, but I can't seem to figure out how to do this.
I've tried:
(load filename)
(load (filename))
(load ~/path-to-directory/filename)
(require filename)
(require ~/path-to-directory/filename)
(require path-from-root/filename)
None of these works. Obviously I'm grasping at straws -- any help is much appreciated.
It's not clear from your question what language level you're using; certain legacy languages may make certain mechanisms unavailable.
The best inclusion/abstraction mechanism is that of modules.
First, set your language level to "module". Then, if I have these two files in the same directory:
File uses-square.ss:
#lang scheme
(require "square.ss")
(define (super-duper x) (square (square x)))
File square.ss :
#lang scheme
(provide square)
(define (square x) (* x x))
Then I can hit "run" on the "uses-square.ss" buffer and everything will work the way you'd expect.
Caveat: untested code.
I believe you are looking for:
(include "relative/path/to/scheme/file.scm")
The (require) expression is for loading modules.
In MIT/GNU Scheme, you can load a file with something like this:
(load "c:\\sample-directory\\sample-file.scm")
But I do not know if it works in DrScheme.
(require "~/path-to-directory/filename")