-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-record-types.scm
157 lines (130 loc) · 4.87 KB
/
example-record-types.scm
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
;; ===================
;; Example of Transforming Scheme Record-Types
;; ===================
(load "load.scm")
(load "main.scm")
(define (printable-string? x) (string? x))
(register-predicate! printable-string?)
(register-super! string? printable-string?)
;; ===================
;; Example: Printing data about a person
;; ===================
(define (first-name? x) (printable-string? x))
(define (last-name? x) (printable-string? x))
(define (formal-title? x) (printable-string? x))
(define (person:age? x) (number? x))
;; Define the Person record type
(define-record-type Person
(make-person first-name last-name age)
person?
(first-name person:first-name)
(last-name person:last-name)
(age person:age))
(register-predicate! person?)
(register-predicate! first-name?)
(register-super! first-name? printable-string?)
(register-predicate! last-name?)
(register-super! last-name? printable-string?)
(register-predicate! person:age?)
(register-super! person:age? integer?)
(register-type-transform! person? first-name?
person:first-name)
(register-type-transform! person? last-name?
person:last-name)
(register-type-transform! person? person:age?
person:age)
;; Define the FullName record type
(define-record-type FullName
(make-full-name first-name last-name)
full-name?
(first-name full-name:first-name)
(last-name full-name:last-name))
(register-predicate! full-name?)
(register-type-transform! full-name? first-name? full-name:first-name)
(register-type-transform! full-name? last-name? full-name:last-name)
(register-type-transform! (list first-name? last-name?) full-name? make-full-name)
(define (print-full-name fn)
(string-append
(full-name:first-name fn)
" "
(full-name:last-name fn)))
(register-type-transform!
full-name?
printable-string?
print-full-name )
;; Define the FormalTitleName record type
(define-record-type FormalTitleName
(make-formal-title-name formal-title last-name)
formal-title-name?
(formal-title formal-title-name:formal-title)
(last-name formal-title-name:last-name))
(register-predicate! formal-title-name?)
(register-predicate! formal-title?)
(register-type-transform! formal-title-name? formal-title? formal-title-name:formal-title)
(register-type-transform! formal-title-name? last-name? formal-title-name:last-name)
(register-type-transform! (list formal-title? last-name?) formal-title-name? make-formal-title-name)
;; Define a transformation from FirstName to FormalTitle
(define (is-female-first-name? x) #f) ;; In a real system, we would look this up in a database...
(define (first-name-to-formal-title fn)
(if (is-female-first-name? fn) "Mrs." "Mr."))
(register-type-transform!
first-name?
formal-title?
first-name-to-formal-title)
;; Define a transformation from FormalTitleName to PrintableString
(define (print-formal-title-name ftn)
(string-append
(formal-title-name:formal-title ftn)
" "
(formal-title-name:last-name ftn)))
(register-type-transform!
formal-title-name?
printable-string?
print-formal-title-name )
;; Tests
(define gs (make-person "Gerald" "Sussman" 18))
(define fullname (make-full-name "Gerald" "Sussman"))
(debug-transform person? first-name? gs)
;; "Found 1 paths: "
;; "------"
;; "Code Gen:"
;; (define (person?-to-first-name? input)
;; (person:first-name input))
;; "Output value:"
;; "Gerald"
;; "Transforms:"
;; (person:first-name)
;; "Predicates:"
;; (first-name?)
;; "Values:"
;; (#[person 12] . "Gerald")
;; ""
(debug-transform person? full-name? gs)
;; "Found 2 paths: "
;; "------"
;; "Code Gen:"
;; (define (person?-to-full-name? input)
;; (make-full-name (person:first-name input) (formal-title-name:last-name (make-formal-title-name (first-name-to-formal-title (person:first-name input)) (person:last-name input)))))
;; "Output value:"
;; #[fullname 13]
;; "Transforms:"
;; (((person:first-name) (((person:first-name first-name-to-formal-title) (person:last-name)) (make-formal-title-name formal-title-name:last-name))) (make-full-name))
;; "Predicates:"
;; (((first-name?) (((first-name? formal-title?) (last-name?)) (formal-title-name? last-name?))) (full-name?))
;; "Values:"
;; (((#[person 12] . "Gerald") (((#[person 12] "Gerald" . "Mr.") (#[person 12] . "Sussman")) (("Mr." "Sussman") #[formaltitlename 15] . "Sussman"))) (("Gerald" "Sussman") . #[fullname 14]))
;; "------"
;; "Code Gen:"
;; (define (person?-to-full-name? input)
;; (make-full-name (person:first-name input) (person:last-name input)))
;; "Output value:"
;; #[fullname 16]
;; "Transforms:"
;; (((person:first-name) (person:last-name)) (make-full-name))
;; "Predicates:"
;; (((first-name?) (last-name?)) (full-name?))
;; "Values:"
;; (((#[person 12] . "Gerald") (#[person 12] . "Sussman")) (("Gerald" "Sussman") . #[fullname 17]))
;; ""
(debug-transform person? printable-string? gs)
;; Outputs values including "Gerald", "Sussman", "Mr. Sussman", and "Gerald Sussman"