forked from jashkenas/coffeescript
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsyntax.coffee
164 lines (132 loc) · 3.64 KB
/
syntax.coffee
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
158
159
160
161
162
163
164
# basic functions
id :: (Num) -> Num
id = (x) -> x
# multi args
adder :: (Num, Str, Bool) -> Str
adder = (a, b, c) ->
a + b + c
# simple flat combinators
or :: (Num or Str) -> Bool
and :: (Num and Str) -> Bool # not a very accepting contract :)
# want not but this conflicts with contract expression escape operator: !(x) -> x > 4
not :: (not Num) -> Num
~~>
not :: (!Num) -> Num # conflict!
# optional args
opt :: (Num, Str, Str?) -> Str
# higher order
app :: ((Num) -> Bool, Bool) -> Bool
# call only
f :: (Num) --> Num # can't be used with 'new'
# new only
f :: (Num) ==> Any # can't be used without 'new'
# dependent
inc :: (Num, Num) -> !(result) -> result > $1 and $1 > $2
# this contract
f :: (Str, @{name: Str}) -> Str
# objects
o :: { a : Num, b: Str, f: (Any) -> Any }
# implicit curlies
o ::
a: Num
b: Str?
f: (Any) -> Any
oo:
z: Num
y: Str
# this contract
o ::
a: Num,
b: Str,
f: (Any, @{a: Num, b: Str}) -> Any
# pre/post/invariant
o ::
a: Num,
b: Str,
f: (Any, @{name: Str, age: Num}) -> Any |
pre: (obj) -> obj.a > 42
post: (obj) -> obj.b is not "bad"
| invariant: ->
@.a > 0 and @.b is "foo"
# recursive contracts
o ::
a: Num
b: Str
f: (Num) -> Self
# arrays
arr :: [Str, Num, Bool, [Bool, Bool]]
arr :: [...Bool]
arr :: [Str, Num, ...Bool]
# naming contracts
NumId = ?(Num) -> Num
NumObj = ?{a: Num, b: Num}
NumArr = ?[Num, Num]
# escaping from a contract expression
f :: (!(x) -> typeof x is 'number') -> Num
retPrim :: (Num) -> !isPrime
f :: (Num, !(x) -> isPrime x) -> Bool
NumId = ?(Num) -> Num
id :: NumId
MyEven = ?!(x) -> x % 2 is 0
idEven :: (MyEven) -> MyEven
MyEven = (x) -> x % 2 is 0
idEven :: (!MyEven) -> !MyEven
# note on combinators...racket provides a bunch of flat contracts
# like >, <, between, etc. The only one we're providing are 'and' and 'or'.
# I think the escape syntax is sufficient and potentially less confusing (also less
# work for me :-). So where racket would use the flat combinators we have:
f :: (Num and (!(x) -> x > 4)) -> (Num and (!(x) -> x < 10 and x > 1)
# modules for browser
# server.coffee
obj ::
a: Str
b: Num
obj = {a: "foo", b: 42}
# client-browser.coffee
obj.a # ERROR
o = obj.use()
o.a = "bar" # works fine
o.a = 42 # contract failure
# client-node.coffee (not implemented...no node with proxies yet)
s = require "server"
o = s.obj
# some example blame messages
# (note that a cleaned up stack trace (contract library frames removed) is available on
# the error object that gets thrown when a contract is violated
id :: (Num) -> Num
id "foo"
# Error: Contract violation: expected <Number>, actual: "foo"
# Value guarded in: blametest.js:31 -- blame is on: blametest.js:32
# Parent contracts:
# (Number) -> Number
f :: ( (Str) -> Str ) -> Num
f = (fun) ->
fun "foo"
42
f (s) -> s
# Error: Contract violation: expected <String>, actual: 42
# Value guarded in: blametest.js:108 (server) -- blame is on: blametest.js:108 (client)
# Parent contracts:
# (String) -> String
# ((String) -> String ) -> Number
o :: {a: Num, b: Str}
o = {a: 42}
# Error: Contract violation: expected <{a : Number, b : String}>, actual: "[missing property: b]"
# Value guarded in: blametest.js:164 (server) -- blame is on: blametest.js:164 (server)
# Parent contracts:
# {a : Number, b : String}
o ::
a: Num
f: (Num) -> Num |
pre: (obj) -> obj.a > 0
o =
a: -1
f: ...
o.f 42
# Error: Contract violation: expected <precondition: function (obj) {
# return obj.a > 0;
# }>, actual: "[failed precondition]"
# Value guarded in: blametest.js:282 (server) -- blame is on: blametest.js:282 (client)
# Parent contracts:
# (Number) -> Number
# {f : (Number) -> Number , a : Number}