-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial.rkt
71 lines (57 loc) · 1.6 KB
/
tutorial.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#lang s-exp rosette/safe
; These are the code snippets from the Rosette tutorial
(require rosette/lib/meta/meta
rosette/lib/tools/render)
(current-bitwidth 8)
(define (polynomial x)
(+ (* x x x x) (* 6 x x x) (* 11 x x) (* 6 x)))
(define (factored x)
(begin
(define-symbolic a b c number?)
(assert (not (< a 0)))
(assert (not (< b 0)))
(assert (not (< c 0)))
(* (+ x a) (+ x 1) (+ x b) (+ x c))))
(define-symbolic i number?)
(define factor-synthesis
(synthesize #:forall (list i)
#:guarantee (assert (eq? (polynomial i) (factored i)))))
(define (!= x y)
(not (= x y)))
(define (bound x min max)
(and (> x min) (< x max)))
(define (constrain x)
(bound x 0 (expt 2 5)))
(define-symbolic a b c d e number?)
(assert (constrain a))
(assert (constrain b))
(assert (constrain c))
(assert (constrain d))
(assert (constrain e))
(assert (= (/ (* d e) e) d))
(assert (= (/ (* d e) d) e))
(assert (= b (+ d e)))
(assert (= c (* d e)))
(define (f x)
(+ (* x x a) (* x b) c))
(define (g x)
(* (+ x d) (+ x e)))
(define-symbolic x number?)
(define coefficient-synthesis
(synthesize #:forall (list x)
#:guarantee (assert (= (f x) (g x)))))
(define (symlist n)
(if (= n 0)
null
(begin
(define-symbolic x number?)
(cons x (symlist (- n 1))))))
(define (symlist* n) ; a list of distinct symbolic values
(if (= n 0)
null
(begin
(define-symbolic* x number?)
(cons x (symlist* (- n 1))))))
(define good-list (symlist* 4))
(define good-solution
(solve (assert (equal? good-list (list 1 2 3 4)))))