Static signature for higher order function in Bigloo Scheme - scheme

Does anyone know how to create a static signature for a higher order function in the module export section in the Bigloo Scheme language?
Here is how far I got
(module test
(export (adder ::double)))
(define (adder x)
(lambda (y)
(set! x (+ x y))
x))
The following will work, but I want to keep all the type data in the module declaration
(module test
(export (adder ::double)))
(define (adder x)
(lambda (y::double)::double
(set! x (+ x y))
x))

Related

How to fix the 'cannot change constant' error

I have this code so far
(define (max f g)
(define (int-max x y)
(if (> x y) x y))
(lambda (x) (int-max (f x) (g x))))
it gives me the error define-values: assignment disallowed;
cannot change constant
constant: max
I'm not sure how to fix this or what it means.
In DrRacket in the bottom left there is a dropdown where you can select lanaguage. From there you can select "Choose language" and click "Show details". For legacy languages such as R5RS you have the option "Disallow redefinition of initial bindings".
Now according to the R5RS your redefinition can only extend the functionality and that for the data types the original binding worked with should work the same in the new definition. The reason for this is the ability to constant fold the code. Thus the code below is invalid:
(define + -)
(+ 5 3)
; ==> 7
This might look strange but the program is in violation with the report and because of that the result might as well have been the string "banana" as far as the report is concerned. In R5RS you need to make it compatible for it to be Scheme:
(define numeric-max max)
(define max
(lambda (v1 . vs)
(if (number? v1)
(apply numeric-max v1 vs)
(lambda (x) (numeric-max (v1 x) ((car vs) x))))))
With R6RS you are free of this by not importing the binding at all:
#!r6rs
(import (except (rnrs base) max))
(define (max f g)
(define (int-max x y)
(if (> x y) x y))
(lambda (x) (int-max (f x) (g x))))
If you want to make max available you can do the same as in R5RS with named imports:
#!r6rs
(import (except (rnrs base) max)
(only (rnrs control) case-lambda)
(rename (rnrs base) (max numeric-max)))
(define max
(case-lambda
((v1 v2)
(if (number? v1)
(numeric-max v1 v2)
(lambda (x) (numeric-max (v1 x) (v2 x)))))
(args
(apply numeric-max args))))
And of coruse this works in #lang racket as well:
#lang racket
(require (rename-in racket/base [max numeric-max]))
(define max
(case-lambda
((v1 v2)
(if (number? v1)
(numeric-max v1 v2)
(lambda (x) (numeric-max (v1 x) (v2 x)))))
(args
(apply numeric-max args))))
The problem you are facing is that max is already defined and you are trying to re-define it.
More importantly, name max is not appropriate for what you are trying to use it for. You are calling max with couple of arguments that are functions. It returns a lambda that can be invoked with a variable.
Your envisioning usage such as
((max sin cos) 10)
A name such as max-proc-value would be more appropriate and will avoid the problem that you have run into.
If you put this in the definition window (the upper one) everything works.
#lang racket
(define (max f g)
(define (int-max x y)
(if (> x y) x y))
(lambda (x) (int-max (f x) (g x))))

Internal Definitions in Block Structure

(define (sqrt x)
  (define (good-enough? guess x)
    (< (abs (- (square guess) x)) 0.001))
  (define (improve guess x)
    (average guess (/ x guess)))
  (define (sqrt-iter guess x)
    (if (good-enough? guess x)
        guess
        (sqrt-iter (improve guess x) x)))
  (sqrt-iter 1.0 x))
I can't seem to wrap my head around around internal block structure. If the syntax for define is: (define procedure arg arg body). How are you able to define all the other local-scope variables inside the top-level define?
Is there a syntax exception for defines?
The syntax for define is not (define procedure arg .. body). It is either (define (name . args) defines ... body1 bodyn ...) which is a shortcut for (define name (lambda args defines ... body1 bodyn ...)). Note that x ... means zero or more so (lambda (a b) (+ a b)) is fine but (lambda (a b) (define (test) (+ a b))) isn't.
Internal define is handled by the lambda syntax. Basically it gets rewritten to a letrec. So
(define (sqrt x)
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(sqrt-iter 1.0 x))
Becomes:
(define sqrt
(lambda (x)
(letrec ((good-enough? (lambda (guess x)
(< (abs (- (square guess) x)) 0.001)))
(improve (lambda (guess x)
(average guess (/ x guess))))
(sqrt-iter (lambda (guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))))
(sqrt-iter 1.0 x))))
Obviously the reason for using local define is to keep it flatter and more readable than letrec. Keeping the same name even though they are totally different beasts that are handled by different parts of the implementation is a simplification for scheme programmers but harder to grasp and understand if you try to figure out how scheme implementations work.

How do I use a pair to find which of two functions will evaluate the largest value? Scheme

Basically there is a pair made up of two functions and the code has to take the pair input x to find the highest evaluation for x and print that evaluation.
I receive the error:
car: contract violation expected: pair? given: 4
define (max x)
(lambda (x) ;I wanted lambda to be the highest suitable function
(if (> (car x) (cdr x))
(car x)
(cdr x))))
(define one-function (lambda (x) (+ x 1)))
(define second-function (lambda (x) (+ (* 2 x) 1))) ;my two functions
((max (cons one-function second-function)) 4)
And where are the functions being called? And you have two parameters called x, they must have different names. Try this:
(define (max f) ; you must use a different parameter name
(lambda (x)
(if (> ((car f) x) ((cdr f) x)) ; actually call the functions
((car f) x)
((cdr f) x))))
Now it'll work as expected:
((max (cons one-function second-function)) 4)
=> 9

How to write a macro that maintains local state?

This seems to work, it's a macro that expands to successive integers depending on how many times it has been expanded.
;; Library (test macro-state)
(library
(test macro-state)
(export get-count incr-count)
(import (rnrs))
(define *count* 0)
(define (get-count) *count*)
(define (incr-count) (set! *count* (+ *count* 1)))
)
;; Program
(import (rnrs) (for (test macro-state) expand))
(define-syntax m
(lambda (x)
(syntax-case x ()
((m) (begin (incr-count) (datum->syntax #'m (get-count)))))))
(write (list (m) (m) (m)))
(newline)
;; prints (1 2 3)
But it's clumsy to me because the macro state *count* and the macro m itself are in different modules. Is there a better way to do this in r6rs, preferably one that doesn't split the implementation over two modules?
EDIT
I should make it clear that although this example is just a single macro, in reality I'm looking for a method that works when multiple macros need to share state.
You can make the state local to the macro transformer:
(define-syntax m
(let ()
(define *count* 0)
(define (get-count) *count*)
(define (incr-count) (set! *count* (+ *count* 1)))
(lambda (x)
(syntax-case x ()
((m) (begin (incr-count) (datum->syntax #'m (get-count))))))))
Edited to add: In Racket, you can also do this:
(begin-for-syntax
(define *count* 0)
(define (get-count) *count*)
(define (incr-count) (set! *count* (+ *count* 1))))
(define-syntax m
(lambda (x)
(syntax-case x ()
((m) (begin (incr-count) (datum->syntax #'m (get-count)))))))
But I don't think R6RS has anything that corresponds to begin-for-syntax.

Why doesn't this Scheme sum-of-squares function work?

(define (square x)
(display (* x x)))
(define (sum-of-squares a b)
(+ (square a) (square b)))
I tested it, and the sum-of-squares function does not work. Why?
(display x) evaluates to void (could be seen as nothing). It is a function call that prints out the argument but doesn't return it. Instead you should define the square function to evaluate the value without displaying, that is:
(define (square x)
(* x x))

Resources