Implementing R7RS on R6RS Scheme 
Takashi Kato 
@tk_riple 
AAutuotro:r : 1 91.91.11.11.414
AAutuotro:r : 1 91.91.11.11.414 
Introduction 
It's been a year since R7RS was standardised. 
Numbers of libraries must be created by now.
Keyword Github Google Code 
R6RS 59 18 
R7RS 12 8 
AAutuotro:r : 1 91.91.11.11.414 
Repositories 
Keyword Github Google Code 
R6RS 59 18 
R7RS 12 8 
*Searched in August 2014 
*The numbers are not accurate 
*Searched in August 2014 
*The numbers are not accurate
AAutuotro:r : 1 91.91.11.11.414 
We can expect... 
The number would grow in the near future 
R6RS libraries are also useful 
It would be nice to be able to use both libraries
AAutuotro:r : 1 91.91.11.11.414 
So we want to... 
● Use R6RS libraries in R7RS libraries 
● Use procedural macros in R7RS libraries 
● Use R7RS libraries in R6RS libraries 
● Use syntax-case in R7RS libraries 
… so on 
IImmpplleemmeenntt RR77RRSS oonn RR66RRSS!!
define-library vs library 
export phasing 
include 
include-ci 
cond-expand 
AAutuotro:r : 1 91.91.11.11.414 
What needs to be done... 
RReessoollvviinngg i innccoommppaattiibbiilliittiieess 
define-library vs library 
export phasing 
include 
include-ci 
cond-expand 
#u8() vs #vu8() #u8() vs #vu8() 
|symbol escape| vs symbolx20;escape |symbol escape| vs symbolx20;escape
AAutuotro:r : 1 91.91.11.11.414 
define-library vs library 
R7RS 
● define-library 
● Very flexible 
– import, export, begin, 
cond-expand and include 
related clauses. 
– These can be appear in 
any order and any number 
of times. 
● No phasing 
– R7RS has only syntax-rules 
R6RS 
● library 
● Rather fixed form 
– export and import with 
respective order 
● Phasing
AAutuotro:r : 1 91.91.11.11.414 
Lexical notations 
R7RS 
#u8( 1 2 3) 
| f oo bar | 
| f oo x20; bar | 
. f oo 
@f oo 
f oo x20; bar 
R6RS 
#vu8( 1 2 3) 
f oo x20; bar 
| f oo bar | 
. f oo 
@f oo
Handling lexical notations... 
● needs to be done by readers 
– Basically no other way :) 
● is easy but how should implementations write a 
datum in proper notations? 
– Using #! directives, like #! r 6r s? 
– Oops, R7RS doesn't have #! r 7r s 
● So using it would be implementation dependent 
AAutuotro:r : 1 91.91.11.11.414
Handling define-library forms... 
● is the problem 
– R7RS has extra keywords; include, include-ci, etc. 
– But it doesn't have phasing keyword 
● can be done in 2 ways; 
– Expander should expand it to l i br ar y form 
– Compiler should handle it 
AAutuotro:r : 1 91.91.11.11.414
Transforming R7RS library form... 
● May require resolving proper phase 
– Only for implementations adopted explicit phasing 
● Requires tracking include or include-ci 
– It is important to know where the source location is 
– What if a macro contains include expression? 
– What if include symbol is shadowed? 
AAutuotro:r : 1 91.91.11.11.414
AAutuotro:r : 1 91.91.11.11.414 
Recall: Phasing 
( l i br ar y ( f oo) 
( expor t f oo) 
( i mpor t ( f or ( r nr s) r un expand) ) 
( def i ne- synt ax f oo 
( l ambda ( x) 
( def i ne- synt ax name 
( synt ax- rul es ( ) 
( ( _ k) 
( dat um- >synt ax k ( s t ri ng- >symbol "bar" ) ) ) ) ) 
( synt ax- case x ( ) 
( ( k) 
( wi t h- synt ax ( ( def ( name #' k) ) ) 
#' ( def i ne def ' bar ) ) ) ) ) ) )
AAutuotro:r : 1 91.91.11.11.414 
Resolving phasing 
(foo) in R7RS 
( def i ne- l i br ar y ( f oo) 
( i mpor t …) 
( expor t …) 
( begi n 
( def i ne- synt ax f oo 
…) ) 
) 
After the transformation 
( l i br ar y ( f oo) 
( expor t …) 
( i mpor t ( f or …) …) 
( def i ne- synt ax f oo 
…) 
)
How should this be resolved? 
File hierarchy 
/ 
+ foo.sld 
+ impl/ 
+ bar.scm 
+ buzz.scm 
+ buzz.scm 
AAutuotro:r : 1 91.91.11.11.414 
; ; f oo. sl d 
( def i ne- l i br ar y ( f oo) 
( i mpor t ( scheme base) ) 
( expor t bar ) 
( i ncl ude " i mpl / bar . scm" ) ) 
; ; i mpl / bar . scm 
( i ncl ude " buzz. scm" ) 
; ; i mpl / buzz. scm 
( def i ne bar ' bar ) 
; ; buzz. scm 
( def i ne bar ' boo)
AAutuotro:r : 1 91.91.11.11.414 
Well... 
R7RS doesn't specify how to resolve the file paths... 
However, 
It would be nice if it's resolved as C preprocessor does. 
So symbol bar should be bound to f oo.
How should this be resolved? 
; ; f i l e- a. scm 
( def i ne- synt ax i ncl ude- i t 
( synt ax- r ul es ( ) 
( ( _ f i l e) 
( i ncl ude f i l e) ) ) ) 
; ; f i l e- b. scm 
( i ncl ude- i t " buzz. scm" ) 
AAutuotro:r : 1 91.91.11.11.414 
From where 'buzz.scm' 
should be resolved? 
From 'file-b.scm' should 
make more sense to users.
AAutuotro:r : 1 91.91.11.11.414 
To do that... 
● Expander should know where include expressions 
are evaluated. 
● Plus, result of macro expansion 
Thus expander needs to do not only transforming 
define-libray to library but also expand macros...
Implementing on Sagittarius 
● define-library as the built-in syntax 
– Avoid implementing macro expander twice 
– The compiler can use the compile time environment 
● include and include-ci are resolved 2 ways 
– As a library keyword, it's simply sliced 
● Paths are resolved from where the library is located 
– As a syntax, compiler considers bindings 
● Paths are resolved from where the current file is. 
AAutuotro:r : 1 91.91.11.11.414
Handling lexical notations (2) 
● The reader can read both R6RS and R7RS notations 
– Also providing strict modes 
● Writing R7RS bytevectors requires #!r7rs directive 
– The default mode writes in R6RS style 
AAutuotro:r : 1 91.91.11.11.414
AAutuotro:r : 1 91.91.11.11.414 
Interoperation 
; ; Expor t i ng pr ocedur al macr o f r om R6RS l i br ar y 
( l i br ar y ( ai f ) 
( expor t ai f ) 
( i mpor t ( r nr s) ) 
( def i ne- synt ax ai f 
( l ambda ( x) 
( synt ax- case x ( ) 
( ( ai f c t ) 
#' ( ai f c t ( i f #f #t ) ) ) 
( ( k c t e) 
( wi t h- synt ax ( ( i t ( dat um- >synt ax #' k ' i t ) ) ) 
#' ( l et ( ( i t c) ) 
( i f i t t e) ) ) ) ) ) ) )
AAutuotro:r : 1 91.91.11.11.414 
Interoperation 
; ; usi ng pr ocedur al macr o i n R7RS l i br ar y 
( def i ne- l i br ar y ( f oo) 
( i mpor t ( scheme base) ( ai f ) ) 
( expor t a) 
( begi n 
( def i ne a 
( l et ( ( l i s ' ( ( a . 0) ( b . 1) ( c . 2) ) ) ) 
( ai f ( assq ' a l i s) 
( cdr i t ) ) ) ) ) ) 
; ; bel ow can be bot h R6RS/ R7RS scr i pt 
( i mpor t ( f oo) ) 
a
AAutuotro:r : 1 91.91.11.11.414 
Conclusion 
● Making R7RS Scheme system on top of R6RS is not 
an easy task 
– Not so difficult either 
● Availability of both R6RS and R7RS can be a big 
advantage and good for future Scheme users
AAutuotro:r : 1 91.91.11.11.414 
Questions?

Implementing R7RS on R6RS Scheme

  • 1.
    Implementing R7RS onR6RS Scheme Takashi Kato @tk_riple AAutuotro:r : 1 91.91.11.11.414
  • 2.
    AAutuotro:r : 191.91.11.11.414 Introduction It's been a year since R7RS was standardised. Numbers of libraries must be created by now.
  • 3.
    Keyword Github GoogleCode R6RS 59 18 R7RS 12 8 AAutuotro:r : 1 91.91.11.11.414 Repositories Keyword Github Google Code R6RS 59 18 R7RS 12 8 *Searched in August 2014 *The numbers are not accurate *Searched in August 2014 *The numbers are not accurate
  • 4.
    AAutuotro:r : 191.91.11.11.414 We can expect... The number would grow in the near future R6RS libraries are also useful It would be nice to be able to use both libraries
  • 5.
    AAutuotro:r : 191.91.11.11.414 So we want to... ● Use R6RS libraries in R7RS libraries ● Use procedural macros in R7RS libraries ● Use R7RS libraries in R6RS libraries ● Use syntax-case in R7RS libraries … so on IImmpplleemmeenntt RR77RRSS oonn RR66RRSS!!
  • 6.
    define-library vs library export phasing include include-ci cond-expand AAutuotro:r : 1 91.91.11.11.414 What needs to be done... RReessoollvviinngg i innccoommppaattiibbiilliittiieess define-library vs library export phasing include include-ci cond-expand #u8() vs #vu8() #u8() vs #vu8() |symbol escape| vs symbolx20;escape |symbol escape| vs symbolx20;escape
  • 7.
    AAutuotro:r : 191.91.11.11.414 define-library vs library R7RS ● define-library ● Very flexible – import, export, begin, cond-expand and include related clauses. – These can be appear in any order and any number of times. ● No phasing – R7RS has only syntax-rules R6RS ● library ● Rather fixed form – export and import with respective order ● Phasing
  • 8.
    AAutuotro:r : 191.91.11.11.414 Lexical notations R7RS #u8( 1 2 3) | f oo bar | | f oo x20; bar | . f oo @f oo f oo x20; bar R6RS #vu8( 1 2 3) f oo x20; bar | f oo bar | . f oo @f oo
  • 9.
    Handling lexical notations... ● needs to be done by readers – Basically no other way :) ● is easy but how should implementations write a datum in proper notations? – Using #! directives, like #! r 6r s? – Oops, R7RS doesn't have #! r 7r s ● So using it would be implementation dependent AAutuotro:r : 1 91.91.11.11.414
  • 10.
    Handling define-library forms... ● is the problem – R7RS has extra keywords; include, include-ci, etc. – But it doesn't have phasing keyword ● can be done in 2 ways; – Expander should expand it to l i br ar y form – Compiler should handle it AAutuotro:r : 1 91.91.11.11.414
  • 11.
    Transforming R7RS libraryform... ● May require resolving proper phase – Only for implementations adopted explicit phasing ● Requires tracking include or include-ci – It is important to know where the source location is – What if a macro contains include expression? – What if include symbol is shadowed? AAutuotro:r : 1 91.91.11.11.414
  • 12.
    AAutuotro:r : 191.91.11.11.414 Recall: Phasing ( l i br ar y ( f oo) ( expor t f oo) ( i mpor t ( f or ( r nr s) r un expand) ) ( def i ne- synt ax f oo ( l ambda ( x) ( def i ne- synt ax name ( synt ax- rul es ( ) ( ( _ k) ( dat um- >synt ax k ( s t ri ng- >symbol "bar" ) ) ) ) ) ( synt ax- case x ( ) ( ( k) ( wi t h- synt ax ( ( def ( name #' k) ) ) #' ( def i ne def ' bar ) ) ) ) ) ) )
  • 13.
    AAutuotro:r : 191.91.11.11.414 Resolving phasing (foo) in R7RS ( def i ne- l i br ar y ( f oo) ( i mpor t …) ( expor t …) ( begi n ( def i ne- synt ax f oo …) ) ) After the transformation ( l i br ar y ( f oo) ( expor t …) ( i mpor t ( f or …) …) ( def i ne- synt ax f oo …) )
  • 14.
    How should thisbe resolved? File hierarchy / + foo.sld + impl/ + bar.scm + buzz.scm + buzz.scm AAutuotro:r : 1 91.91.11.11.414 ; ; f oo. sl d ( def i ne- l i br ar y ( f oo) ( i mpor t ( scheme base) ) ( expor t bar ) ( i ncl ude " i mpl / bar . scm" ) ) ; ; i mpl / bar . scm ( i ncl ude " buzz. scm" ) ; ; i mpl / buzz. scm ( def i ne bar ' bar ) ; ; buzz. scm ( def i ne bar ' boo)
  • 15.
    AAutuotro:r : 191.91.11.11.414 Well... R7RS doesn't specify how to resolve the file paths... However, It would be nice if it's resolved as C preprocessor does. So symbol bar should be bound to f oo.
  • 16.
    How should thisbe resolved? ; ; f i l e- a. scm ( def i ne- synt ax i ncl ude- i t ( synt ax- r ul es ( ) ( ( _ f i l e) ( i ncl ude f i l e) ) ) ) ; ; f i l e- b. scm ( i ncl ude- i t " buzz. scm" ) AAutuotro:r : 1 91.91.11.11.414 From where 'buzz.scm' should be resolved? From 'file-b.scm' should make more sense to users.
  • 17.
    AAutuotro:r : 191.91.11.11.414 To do that... ● Expander should know where include expressions are evaluated. ● Plus, result of macro expansion Thus expander needs to do not only transforming define-libray to library but also expand macros...
  • 18.
    Implementing on Sagittarius ● define-library as the built-in syntax – Avoid implementing macro expander twice – The compiler can use the compile time environment ● include and include-ci are resolved 2 ways – As a library keyword, it's simply sliced ● Paths are resolved from where the library is located – As a syntax, compiler considers bindings ● Paths are resolved from where the current file is. AAutuotro:r : 1 91.91.11.11.414
  • 19.
    Handling lexical notations(2) ● The reader can read both R6RS and R7RS notations – Also providing strict modes ● Writing R7RS bytevectors requires #!r7rs directive – The default mode writes in R6RS style AAutuotro:r : 1 91.91.11.11.414
  • 20.
    AAutuotro:r : 191.91.11.11.414 Interoperation ; ; Expor t i ng pr ocedur al macr o f r om R6RS l i br ar y ( l i br ar y ( ai f ) ( expor t ai f ) ( i mpor t ( r nr s) ) ( def i ne- synt ax ai f ( l ambda ( x) ( synt ax- case x ( ) ( ( ai f c t ) #' ( ai f c t ( i f #f #t ) ) ) ( ( k c t e) ( wi t h- synt ax ( ( i t ( dat um- >synt ax #' k ' i t ) ) ) #' ( l et ( ( i t c) ) ( i f i t t e) ) ) ) ) ) ) )
  • 21.
    AAutuotro:r : 191.91.11.11.414 Interoperation ; ; usi ng pr ocedur al macr o i n R7RS l i br ar y ( def i ne- l i br ar y ( f oo) ( i mpor t ( scheme base) ( ai f ) ) ( expor t a) ( begi n ( def i ne a ( l et ( ( l i s ' ( ( a . 0) ( b . 1) ( c . 2) ) ) ) ( ai f ( assq ' a l i s) ( cdr i t ) ) ) ) ) ) ; ; bel ow can be bot h R6RS/ R7RS scr i pt ( i mpor t ( f oo) ) a
  • 22.
    AAutuotro:r : 191.91.11.11.414 Conclusion ● Making R7RS Scheme system on top of R6RS is not an easy task – Not so difficult either ● Availability of both R6RS and R7RS can be a big advantage and good for future Scheme users
  • 23.
    AAutuotro:r : 191.91.11.11.414 Questions?