-
Notifications
You must be signed in to change notification settings - Fork 1
/
traits.rkt
130 lines (96 loc) · 3.97 KB
/
traits.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
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
#lang racket
;; ===================================================================================================
;; representing Evolution traits (mechanisms for specifying, rendering, and comparing traits)
;; EXTERNAL SERVICES
(provide
;; type Trait
;; Any -> Boolean : Trait
trait?
;; Trait -> String
trait->string
;; JSExpr -> String
string->trait
;; Trait
carnivore
attack-traits ;; [Listof Trait]
ambush pack-hunting
defensive-traits ;; [Listof Trait]
burrowing warning-call hard-shell herding horns
action-trait ;; [Listof Trait]
climbing
common-traits ;; [Listof Trait]
cooperation fat-tissue fertile foraging long-neck scavenger symbiosis
all-but-carnivore ;; [Listof Trait]
;; Trait Trait -> Boolean
<-trait
;; (-> Any Boolean) : Specific Trait
carnivore?
ambush? warning-call? burrowing? climbing? hard-shell? herding? horns? pack-hunting?
cooperation? fat-tissue? fertile? foraging? long-neck? scavenger? symbiosis?)
;; ===================================================================================================
;; DEPENDENCIES
(module+ test
(require rackunit))
;; ===================================================================================================
;; IMPLEMENTATION
;; syntax: use singleton-pattern to represent traits via structs
;; (define-traits n:id t1:id ... tn:id)
;; creates the traits t1 through tn and also collects them a list, which is then named n
;; EFFECT remember the new traits in *rendering
(define *trait-x-string-representation '())
(define-syntax (define-traits stx)
(syntax-case stx ()
[(_ name n ...)
(with-syntax ([(n? ...) (map mk-pred (syntax->list #'(n ...)))])
#'(begin
;; (provide n n?) ... ;; why not here? I don't want readers to know how it is implemented
(define-values (n n?)
(let ()
(struct n ())
(define x (n))
(set! *trait-x-string-representation
(cons `(,x ,(symbol->string 'n)) *trait-x-string-representation))
(values x n?)))
...
(define name (list n ...))))]))
;; Identifier -> Identifier
;; create the predicate identifier for n; example: (mk-pred #'carnivore) == #'carnivore?
(define-for-syntax (mk-pred n)
(define n:symbol (syntax-e n))
(define n:string (symbol->string n:symbol))
(define r:symbol (string->symbol (string-append n:string "?")))
(datum->syntax n r:symbol))
;; ---------------------------------------------------------------------------------------------------
(define-traits tester* tester) ;; define a special tester trait
(module+ test
(check-equal? (string->trait (trait->string tester)) tester))
(define (trait->string x)
(define r (assq x *trait-x-string-representation))
(if r (second r) (error 'trait->string "~e" x)))
(module+ test
(check-false (string->trait "x") "bad string"))
(define (string->trait j)
(define r (argmax (lambda (x) (if (string=? (second x) j) 1 0)) *trait-x-string-representation))
(if (and r (string=? (second r) j)) (first r) #false))
;; ---------------------------------------------------------------------------------------------------
;; specifying traits
(define-traits _ carnivore)
(define-traits attack-traits
ambush pack-hunting)
(define-traits defensive-traits
burrowing hard-shell herding horns warning-call)
(define-traits action-trait
climbing)
(define-traits common-traits
cooperation fat-tissue fertile foraging long-neck scavenger symbiosis)
(define all-but-carnivore (append attack-traits defensive-traits action-trait common-traits))
(define (trait? x)
(or (carnivore? x) (member x all-but-carnivore)))
;; ---------------------------------------------------------------------------------------------------
;; comparing traits
(module+ test
(check-true (<-trait ambush foraging))
(check-true (<-trait herding horns))
(check-false (<-trait herding hard-shell)))
(define (<-trait t1 t2)
(string<? (trait->string t1) (trait->string t2)))