Skip to content

Scheme #1183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 109 commits into
base: master
Choose a base branch
from
Open

Scheme #1183

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
ccb7dcd
First commit towards Scheme-to-CakeML compiler
myreen Dec 9, 2024
9efd210
More set up of Scheme-to-CakeML compiler
myreen Dec 17, 2024
09079cc
Shuffled Makefile configurations
pylasnier Dec 29, 2024
3f9f74b
READMEs
pylasnier Dec 29, 2024
0f09423
Start on unverified compiler
pylasnier Dec 29, 2024
1f576a5
Use local cake.S if exists
pylasnier Jan 4, 2025
9ca46d8
Unverified dynamic typing, conditionals, printing
pylasnier Jan 4, 2025
55adac2
Scheme arithmetic semantics
pylasnier Jan 14, 2025
d1cf844
exp_size dup
pylasnier Jan 14, 2025
e4604fc
Unverified lambdas/procedures (no parsing, compiler broken)
pylasnier Jan 15, 2025
3eda629
Small-step semantics
pylasnier Jan 15, 2025
8d9ae79
bools, conds, and rough let bindings implementation
pylasnier Jan 28, 2025
b49db3a
README
pylasnier Jan 28, 2025
81431ef
scoped lets
pylasnier Jan 31, 2025
4e3412f
lambdas
pylasnier Jan 31, 2025
bf89581
propagate exceptions
pylasnier Jan 31, 2025
e5030a2
1-step stack unwind
pylasnier Feb 3, 2025
419e5a5
begin
pylasnier Feb 3, 2025
754ce62
Merge branch 'begin' into handlers
pylasnier Feb 3, 2025
ea0798a
correction
pylasnier Feb 3, 2025
020f81b
cps transform, prims only
pylasnier Feb 7, 2025
1996602
fix build
pylasnier Feb 9, 2025
2a254c0
cpscheme to ml, missing prim defs
pylasnier Feb 9, 2025
e48e630
refunc termination proof
pylasnier Feb 9, 2025
b052b39
corrections, working translation with custom env
pylasnier Feb 14, 2025
fb65f42
lambdas properly, scoping with shared store
pylasnier Feb 14, 2025
f970e44
callcc
pylasnier Feb 15, 2025
be87593
Merge branch 'scheme_store' into scheme_callcc: callcc a bit useless …
pylasnier Feb 16, 2025
135396c
val
pylasnier Feb 16, 2025
1ed2d7c
pretty fupdate
pylasnier Feb 16, 2025
a0067e1
Merge branch 'scheme_store' into scheme_callcc
pylasnier Feb 16, 2025
5333f1d
set
pylasnier Feb 16, 2025
e79d78e
letrec
pylasnier Feb 16, 2025
da6453e
static scoping
pylasnier Feb 21, 2025
ffc20fa
removal of intermediate defunc cps compile step
pylasnier Feb 21, 2025
5324d9b
Merge branch 'scheme_compile1' into scheme_store
pylasnier Feb 21, 2025
55f80f6
Merge branch 'scheme_store' into scheme_callcc
pylasnier Feb 21, 2025
750163f
fix throw constructor
pylasnier Feb 21, 2025
f62f878
messing
pylasnier Feb 21, 2025
ccb9e9d
compile lambdas
pylasnier Feb 22, 2025
bfed75c
correction
pylasnier Feb 22, 2025
7ee20a5
sequencing
pylasnier Feb 22, 2025
c8bcaab
compile set, correct exception
pylasnier Feb 22, 2025
8708492
compile letrec
pylasnier Feb 22, 2025
9bbaeca
minus and signed ints
pylasnier Feb 22, 2025
4851bef
eqv
pylasnier Feb 22, 2025
ff623c6
cleanup after removal of intermediate step
pylasnier Feb 23, 2025
435804a
exceptions ditch continuation
pylasnier Feb 23, 2025
8d75339
Merge branch 'scheme_compile1' into scheme_compile2
pylasnier Feb 23, 2025
5966708
factorial example
pylasnier Feb 23, 2025
7e1ea84
Merge branch 'scheme_compile2' into scheme_compile3
pylasnier Feb 23, 2025
31a4bf8
redundant envs
pylasnier Feb 23, 2025
a5df19c
compile callcc
pylasnier Feb 23, 2025
ae6e726
factored out app ML-side
pylasnier Mar 1, 2025
8555aef
factor out cps transform for contexts, cond and app
pylasnier Mar 2, 2025
ce5e489
cps transform arbitrary continuations
pylasnier Mar 7, 2025
5b689f1
start at small subset proofs
pylasnier Mar 7, 2025
8b5dbe9
correction, more proving
pylasnier Mar 10, 2025
dac8cf3
stuff
pylasnier Mar 11, 2025
567499e
more stuff
pylasnier Mar 11, 2025
d33ddf0
testing translate
pylasnier Mar 11, 2025
3cf1c23
parser
pylasnier Mar 18, 2025
4d8de7f
translate parse
pylasnier Mar 19, 2025
36c0ad0
implode one more
pylasnier Mar 19, 2025
c5fa855
translates!
pylasnier Mar 20, 2025
03b1a40
bool bug fix
pylasnier Mar 20, 2025
374a753
better printing
pylasnier Mar 21, 2025
5fb682c
examples
pylasnier Mar 21, 2025
326389e
Merge branch 'scheme_compile3' into scheme_full_compile
pylasnier Mar 26, 2025
263ae94
separation of vals and lits
pylasnier Mar 28, 2025
28772f3
translate lits
pylasnier Mar 28, 2025
6e24002
semantic preservation of cond
pylasnier Apr 1, 2025
b174a2b
adjust cps def for proof
pylasnier Apr 2, 2025
462eeb8
semantic preservation of return to cond cont
pylasnier Apr 2, 2025
18ee71a
prove base case for compiled program
pylasnier Apr 4, 2025
bf2ac03
some more proving, bit of env theorem mess
pylasnier Apr 6, 2025
7545813
partial application step cases proven, some definition adjustment
pylasnier Apr 6, 2025
02bab82
messing
pylasnier Apr 7, 2025
27879c1
proven addition
pylasnier Apr 8, 2025
8a8b65a
env relation, some adjustments
pylasnier Apr 9, 2025
23457f3
proof of Scheme semantics progress, adjusted begin expression semanti…
pylasnier Apr 11, 2025
f60ece0
fix parser, compiler but cps transform termination not done
pylasnier Apr 11, 2025
dd4fdea
proven lambdas, fixed cps transform termination
pylasnier Apr 12, 2025
1bbd0bb
slight restructuring, proof dir
pylasnier Apr 12, 2025
c16f2d9
proven begin
pylasnier Apr 12, 2025
b474dad
proven set
pylasnier Apr 15, 2025
9cac5e8
proven letrec (a bit ugly)
pylasnier Apr 15, 2025
4717583
scheme divergence
pylasnier Apr 16, 2025
1bcdc8e
messing
pylasnier Apr 16, 2025
a06376c
compiler static checks
pylasnier Apr 16, 2025
87d1a5e
proven callcc
pylasnier Apr 16, 2025
1d04e69
proven eqv, narrowed eqv def a bit
pylasnier Apr 18, 2025
61e3a05
proven subtraction
pylasnier Apr 19, 2025
8109c11
step preservation proof complete!
pylasnier Apr 19, 2025
0806243
attempt at multistep
pylasnier Apr 19, 2025
c07add3
simplified arg names
pylasnier Apr 21, 2025
00172c9
forgot about letrec
pylasnier Apr 21, 2025
db2a5b0
ml var renames for consistency
pylasnier Apr 21, 2025
37ee40f
fix semantic minus bug
pylasnier Apr 25, 2025
8d8af5b
value termination and divergence
pylasnier Apr 30, 2025
db69075
tweak scheme semantics divergence theorem
pylasnier Apr 30, 2025
3c43493
Merge branch 'master' into scheme
pylasnier May 24, 2025
427d309
developer lint Scheme
pylasnier May 26, 2025
0a048c0
parse call/cc correctly
pylasnier May 26, 2025
f3bab93
nondet example simplified
pylasnier May 27, 2025
aa3c04f
letrec and letrec*
pylasnier May 29, 2025
7db068d
compilation
pylasnier May 29, 2025
21c22c5
letrec proof, missing validity
pylasnier May 30, 2025
2a9deab
letrec progress/validity
pylasnier May 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ Correctness proof for the CakeML compiler.
[repl](repl):
Some definitions and proofs used in the proof of the CakeML
and Candle read-eval-print loop (REPL).

[scheme](scheme):
A compiler from Scheme to CakeML
20 changes: 20 additions & 0 deletions compiler/scheme/Holmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
INCLUDES = $(CAKEMLDIR)/translator \
$(CAKEMLDIR)/basis \
$(CAKEMLDIR)/basis/pure \
$(CAKEMLDIR)/compiler/parsing \
$(CAKEMLDIR)/semantics \
$(CAKEMLDIR)/misc \
$(HOLDIR)/examples/formal-languages/context-free

all: $(DEFAULT_TARGETS) README.md
.PHONY: all

README_SOURCES = $(wildcard *Script.sml) $(wildcard *Lib.sml) $(wildcard *Syntax.sml)
# Filter out unverified/ (they don't have a readmePrefix)
DIRS = $(patsubst examples/,,$(patsubst unverified/,,$(wildcard */)))
README.md: $(CAKEMLDIR)/developers/readme_gen readmePrefix unverified/README.md examples/README.md $(patsubst %,%readmePrefix,$(DIRS)) $(README_SOURCES)
$(CAKEMLDIR)/developers/readme_gen $(README_SOURCES)

ifdef POLY
HOLHEAP = $(CAKEMLDIR)/misc/cakeml-heap
endif
34 changes: 34 additions & 0 deletions compiler/scheme/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
A compiler from Scheme to CakeML

[compilation](compilation):
Compilation scripts for the Scheme-to-CakeML compiler.

[examples](examples):
Example Scheme programs compiled using the Scheme compiler

[proofs](proofs):
Proofs for Scheme to CakeML compiler

[scheme_astScript.sml](scheme_astScript.sml):
AST of Scheme

[scheme_compilerScript.sml](scheme_compilerScript.sml):
Definition of a compiler from Scheme to CakeML

[scheme_parsingScript.sml](scheme_parsingScript.sml):
Parser for Scheme

[scheme_semanticsScript.sml](scheme_semanticsScript.sml):
Semantics of Scheme

[scheme_to_cakeScript.sml](scheme_to_cakeScript.sml):
Code generator for Scheme to CakeML compiler

[scheme_valuesScript.sml](scheme_valuesScript.sml):
Definition of Scheme values

[translation](translation):
CakeML translation of Scheme-to-CakeML compiler

[unverified](unverified):
An unverified compiler from Scheme to ML written in Haskell
12 changes: 12 additions & 0 deletions compiler/scheme/compilation/Holmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
INCLUDES = $(CAKEMLDIR)/compiler/scheme/translation $(CAKEMLDIR)/compiler $(CAKEMLDIR)/cv_translator $(CAKEMLDIR)/developers/bin

all: $(DEFAULT_TARGETS) README.md
.PHONY: all
README_SOURCES = $(wildcard *Script.sml) $(wildcard *Lib.sml) $(wildcard *Syntax.sml)
DIRS = $(wildcard */)
README.md: $(CAKEMLDIR)/developers/readme_gen readmePrefix $(patsubst %,%readmePrefix,$(DIRS)) $(README_SOURCES)
$(protect $(CAKEMLDIR)/developers/readme_gen) $(README_SOURCES)

ifdef POLY
HOLHEAP = $(CAKEMLDIR)/cv_translator/cake_compile_heap
endif
4 changes: 4 additions & 0 deletions compiler/scheme/compilation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Compilation scripts for the Scheme-to-CakeML compiler.

[scheme_compilerCompileScript.sml](scheme_compilerCompileScript.sml):
In-logic compilation of the Scheme-to-CakeML compiler
1 change: 1 addition & 0 deletions compiler/scheme/compilation/readmePrefix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Compilation scripts for the Scheme-to-CakeML compiler.
13 changes: 13 additions & 0 deletions compiler/scheme/compilation/scheme_compilerCompileScript.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(*
In-logic compilation of the Scheme-to-CakeML compiler
*)

open preamble scheme_compilerProgTheory eval_cake_compile_x64Lib;

val _ = new_theory "scheme_compilerCompile";

Theorem scheme_compiler_compiled =
eval_cake_compile_x64 "" scheme_compiler_prog_def
"scheme_compiler.S";

val _ = export_theory ();
58 changes: 58 additions & 0 deletions compiler/scheme/examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
.PHONY: all clean

OS ?= $(shell uname)

ifeq ($(OS),Windows_NT)
PREF =
SUFF = .exe
EVALFLAG =
else
PREF = ./
SUFF =
EVALFLAG = -DEVAL
endif

CAKEOPT = --sexp=true

ifeq ($(OS),Darwin)
# These options avoid linker warnings on macOS
LDFLAGS += -Wl,-no_pie
EVALFLAG =
endif

ALL = $(patsubst %.scm,%.cake$(SUFF),$(wildcard *.scm))

CFLAGS+=-O2
LDLIBS+=-lm

ARCH=x64
WORD_SIZE=64

all: $(ALL)

%.cake.S: %.scm cake scheme_compiler$(SUFF)
cat $< | $(PREF)scheme_compiler$(SUFF) | $(PREF)cake$(SUFF) $(CAKEOPT) > $@

%.cake$(SUFF) : %.cake.S ../../../basis/basis_ffi.c
$(CC) $< ../../../basis/basis_ffi.c $(LOADLIBES) $(LDLIBS) -o $@ $(LDFLAGS)

scheme_compiler.S: ../compilation/scheme_compiler.S
cp $< $@

scheme_compiler$(SUFF): scheme_compiler.S ../../../basis/basis_ffi.c
$(CC) -o $@ scheme_compiler.S ../../../basis/basis_ffi.c $(LDLIBS)

cake.S:
@if [ ! -f "../../bootstrap/compilation/x64/64/$(@F)" ] ; then $(MAKE) download ; else cp ../../bootstrap/compilation/x64/64/$(@F) $@ ; fi

download:
@echo "$(red)Could not find \`cake.S\`. Downloading the latest version from CakeML's GitHub releases.$(reset)"
curl -LO https://github.com/CakeML/cakeml/releases/latest/download/cake-$(ARCH)-$(WORD_SIZE).tar.gz
@tar -zxf cake-$(ARCH)-$(WORD_SIZE).tar.gz --strip-components 1 cake-$(ARCH)-$(WORD_SIZE)/cake.S
@rm cake-$(ARCH)-$(WORD_SIZE).tar.gz

cake$(SUFF): cake.S ../../../basis/basis_ffi.c
$(CC) $(CFLAGS) $< ../../../basis/basis_ffi.c $(LOADLIBES) $(EVALFLAG) -o $@ $(LDFLAGS) $(LDLIBS)

clean:
rm cake$(SUFF) cake.S scheme_compiler$(SUFF) scheme_compiler.S *.cake.S *.cake$(SUFF)
1 change: 1 addition & 0 deletions compiler/scheme/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Example Scheme programs compiled using the Scheme compiler
4 changes: 4 additions & 0 deletions compiler/scheme/examples/facimp.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(letrec ((fac (lambda (x)
(letrec ((st 0) (acc 1)) (begin
(call/cc (lambda (k) (set! st k)))
(if (eqv? x 0) acc (st (begin (set! acc ( * acc x)) (set! x (- x 1)))))))))) (fac 6))
7 changes: 7 additions & 0 deletions compiler/scheme/examples/fib.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(letrec
((fib (lambda (n)
(if (eqv? n 0) n
(if (eqv? n 1) n
(+ (fib (- n 1))
(fib (- n 2))))))))
(fib 30))
9 changes: 9 additions & 0 deletions compiler/scheme/examples/list.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(letrec (
(cons (lambda (car cdr) (lambda (b) (if b car cdr))))
(car (lambda (cons) (cons #t)))
(cdr (lambda (cons) (cons #f)))
(nil 999)
(nil? (lambda (l) (eqv? l nil)))
(length (lambda (l) (if (nil? l) 0 (+ 1 (length (cdr l))))))
(index (lambda (n l) (if (eqv? n 0) (car l) (index (- n 1) (cdr l)))))
) (index 1 (cons 10 (cons 20 (cons 30 nil)))))
35 changes: 35 additions & 0 deletions compiler/scheme/examples/nondet.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(letrec
((nondet
(lambda (fun)
(call/cc
(lambda (cc)
(letrec ((k cc))
(fun
(lambda (f n m)
(letrec ((i n)
(last k))
(begin
(call/cc
(lambda (cc) (set! k (lambda (v) (begin (set! i (+ i 1)) (cc v))))))
(if (eqv? i m)
(begin (set! k last) (k (- 1)))
(f i)))))
(lambda () (k (- 1)))))))))

(triangle
(lambda (n)
(if (eqv? n 0) n
(+ n (triangle (- n 1))))))
(fib
(lambda (n)
(if (eqv? n 0) n
(if (eqv? n 1) n
(+ (fib (- n 1))
(fib (- n 2))))))))

;(display
(nondet
(lambda (choose fail)
(letrec ((x (choose triangle 3 10))
(y (choose fib 3 10)))
(if (eqv? x y) x (fail))))));)
1 change: 1 addition & 0 deletions compiler/scheme/examples/print.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(letrec ((touch 0)) (begin (set! touch 1) touch))
5 changes: 5 additions & 0 deletions compiler/scheme/examples/tailfib.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(letrec
((fib (lambda (n a b)
(if (eqv? n 0) a
(fib (- n 1) (+ a b) a)))))
(fib 35 0 1))
21 changes: 21 additions & 0 deletions compiler/scheme/proofs/Holmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
INCLUDES = $(CAKEMLDIR)/translator \
$(CAKEMLDIR)/basis \
$(CAKEMLDIR)/basis/pure \
$(CAKEMLDIR)/compiler/parsing \
$(CAKEMLDIR)/semantics \
$(CAKEMLDIR)/misc \
$(HOLDIR)/examples/formal-languages/context-free \
$(CAKEMLDIR)/compiler/scheme

all: $(DEFAULT_TARGETS) README.md
.PHONY: all

README_SOURCES = $(wildcard *Script.sml) $(wildcard *Lib.sml) $(wildcard *Syntax.sml)
# Filter out tests/ (they don't have a readmePrefix)
DIRS = $(patsubst tests/,,$(wildcard */))
README.md: $(CAKEMLDIR)/developers/readme_gen readmePrefix $(patsubst %,%readmePrefix,$(DIRS)) $(README_SOURCES)
$(CAKEMLDIR)/developers/readme_gen $(README_SOURCES)

ifdef POLY
HOLHEAP = $(CAKEMLDIR)/misc/cakeml-heap
endif
7 changes: 7 additions & 0 deletions compiler/scheme/proofs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Proofs for Scheme to CakeML compiler

[scheme_semanticsPropsScript.sml](scheme_semanticsPropsScript.sml):
Proofs of Scheme semantics properties

[scheme_to_cakeProofScript.sml](scheme_to_cakeProofScript.sml):
Proof of semantic preservation from Scheme to CakeML
1 change: 1 addition & 0 deletions compiler/scheme/proofs/readmePrefix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Proofs for Scheme to CakeML compiler
Loading