-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modules.fold
561 lines (360 loc) · 8.47 KB
/
Modules.fold
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
--
-- Interfaces
--
{- # Type Module
To group together a type and a collection of functions that operate on that
type a _Type Module_ can be used. Type modules create both: a type and a
module for that type, with the same name. So the type declaration must have
exactly the same name as the module.
> Note: OCaml has no native support for type module but the equivalent model
> is widely used with the conventional type `Module.t`.
> Warning: The usage of the module's name may be replaced by `Self`.
Not only the declaration of the type uses the same name, but also all the
function and value signatures use it.
-}
module Person
type Self = { name :: String, age :: Int }
val name :: Self -> String
end
{-
This pattern makes it easier to extend types, since no changes have to be
performed to the existing declarations. Compared to OCaml, the beginners
will not be asking what the type `t` means, the proposed model is natural:
you had a type and now it moved to its own module.
> TODO: Review how the interface inclusion will work:
> include Monad with type t = t
> will become:
> include Monad for type List
-}
module List
type List a = [] | a & List a
let List (l: List a) = l
let List (v: Vec a) = Vec.to_list v
end
--
-- # Modules
--
-- * 1.0 Definition
-- * 2.0 Loading and Opening Modules
-- * 3.0 Functors
--
-- ## 2.0 Loading and Opening Modules
--
--
-- Load a module
--
-> load OS
-- Only the module can be used
-> user = OS.getenv "USER"
user :: String = "dmr"
--
-- Load a module using an alias
--
-> load OS as POSIX
-- Only the alias can be used
-> user = POSIX.getenv "USER"
user :: String = "dmr"
-- Using module name directly will not work
-> user = OS.getenv "USER"
* Error: Unbound module OS
--
-- Load multiple modules
--
-> load OS, Sys
-- Both modules can be used.
-> user = OS.getenv "USER"
user :: String = "dmr"
-> Sys.time ()
:: Float = 0.276656
--
-- Load a module and some symbols.
--
-> load OS with: [getenv, getuid as uid]
-- The `getenv` and `getuid` symbols can be used directly.
-> user, uid = getenv "USER", uid ()
user :: String = "dmr"
uid :: Int = 501
-- The `OS` was also loaded and can be used.
-> OS.getguid ()
:: Int = 20
--
-- Open all module symbols
--
-- The usage of `open` without selection is discouraged since conflicting
-- symbols may be introduced in the scope. In which case, for a conflicting
-- name, the last imported definition will be used.
--
-> open OS
-- All module symbols can be used directly.
-> getuid ()
:: Int = 501
-> getgid ()
:: Int = 20
-- Opened modules are not loaded.
-> OS.getuid ()
* Error Unbound module OS
--
-- Open only some module symbols
--
-> open OS only: [getcwd as cwd]
-- Only the symbols `cwd` was opened from the module.
-> cwd ()
:: String = "/home/dmr"
-- Open all module symbols with exceptions
open OS hide: [mkdir, mkfifo]
--
-- Interfaces
--
sig Animal T
init :: String -> T
name :: T -> String
talk :: T -> String
end
data Dog (name : String)
instance Animal Dog {
make :name = Dog name
name dog = dog.name
talk dog = "woof!"
}
laika : Dog = make name: "Laika Kosmonavt"
name laika == "Laika Kosmonavt"
talk laika == "woof!"
-- Module User
sig Show T
show :: T -> String
end
type User = (name: String, birthday: Date, profession: String)
ext Show User
let show (self::User) -> String = u.name
end
protocol Show T {
show : T -> String
}
instance Show {Eq T} :: (List a)
show
end
instance Show {Eq T} :: (Int -> String -> (Person, a))
show
end
show (u::User) = u.name
show (M::Show) (x::M a) = u.name
map `round [1, 2, 3]
alice :: User =
{ name: "Alice",
birthday: 14/08/2014,
profession: `cat }
show alice
type ordering = LT | EQ | GT
protocol Comparable a
function compare :: a -> a -> ordering
end
instance Comparable int
function compare a b
| a == b -> EQ
| a > b -> GT
| a < b -> LT
end
end
----
type ordering = LT | EQ | GT
protocol Comparable a:
compare :: a -> a -> ordering
instance Comparable Int:
compare x y =
if | x == y -> EQ
| x > y -> GT
| x < y -> LT
instance Comparable User =
compare x y = compare x.name y.name
module User =
type user = ...
compare x y =
-- // --
module JSONCodec n do
Log.info "Will calculate factorial of {n}"
(n == 0) ? 1 : n * factorial (n - 1)
end
interface Codec a =
encode :: a -> Result Data
decode :: Data -> Result a
end
module Codec Json =
encode = String x => "\"{x}\""
| Object x => surround x with ('{', '}')
| List xs => surround (join (map encode xs) with: ',')
with: '"'
end
module JSONCodec n =
Log.info "Will calculate factorial of {n}"
(n == 0) ? 1 : n * factorial (n - 1)
end
module JSONCodec n:
Log.info "Will calculate factorial of {n}"
(n == 0) ? 1 : n * factorial (n - 1)
end
JSONCodec n = module
Log.info "Will calculate factorial of {n}"
(n == 0) ? 1 : n * factorial (n - 1)
end
module JSONCodec n = {
Log.info "Will calculate factorial of {n}"
(n == 0) ? 1 : n * factorial (n - 1)
}
JSONCodec n = module {
Log.info "Will calculate factorial of {n}"
(n == 0) ? 1 : n * factorial (n - 1)
}
-- * --
module Option =
type Self a = Some a | None
self ? default =
case self of:
Some a -> a
None -> default
instance Functor Option =
map f self =
case self of:
Some a -> Some (f a)
None -> None
instance Monad Option =
pure = Some
bind f self =
case self of:
Some a -> f a
None -> None
module Option
type Self a = Some a | None
self ? default =
case self
Some a -> a
None -> default
end
end
instance Functor Option
map f self =
case self
Some a -> Some (f a)
None -> None
end
end
instance Monad Option
pure = Some
bind f self =
case self
Some a -> f a
None -> None
end
end
module Option {
type Self a = Some a | None
self ? default =
case self {
Some a -> a
None -> default
}
}
instance Functor Option {
map f self =
case self {
Some a -> Some (f a)
None -> None
}
}
instance Monad Option {
pure = Some
bind f self =
case self {
Some a -> f a
None -> None
}
}
-- * --
def data =
HTTP.get "http://data.io/data.json"
|> JSON.parse
def data :: Model
HTTP.get "http://data.io/data.json"
|> JSON.parse model
end
-- Expression notation
def hello name =
print "Hello, $name!"
-- Block notation
def hello name
print "Hello, $name!"
end
-- Expression notation
def hello name :: String -> () =
print "Hello, $name!"
-- Expression notation
def hello (name :: String) -> () =
print "Hello, $name!"
-- Block notation
def hello name :: String -> ()
print "Hello, $name!"
end
-- Expression notation
mod IntSet =
Set.Make Int
-- Block notation
mod IntSet
include (Set.Make Int)
end
-- * --
{- Provenance tracking. -}
interface Irmin_task
include Tc.S0
val create :: date: Int64 -> owner: String -> uid: Int64? -> String -> Self
val date :: Self -> Int64
val uid :: Self -> Int64
val owner :: Self -> String
val messages :: Self -> List String
val add :: Self -> String -> Unit
val empty :: Self
type f a = a -> self
val none :: f ()
end
-- • --
-- replicate :: forall f a. Unfoldable f => Int -> a -> f a
-- replicate n v = unfoldr step n
-- where
-- step :: Int -> Maybe (Tuple a Int)
-- step i =
-- if i <= 0 then Nothing
-- else Just (Tuple v (i - 1))
def replicate n x = unfold step n
where step i =
if i <= 0 then None
else Some (x, i - 1)
-- • --
-- Module Implemntations
-- Module definition
module M = { ... }
-- module M = struct ... end
-- Module definition with type
module M = { ... }
-- module M = struct ... end
interface M = { ... }
-- module type M = sig ... end
module M = Foo { ... }
-- module M = Foo (struct end)
module M = Foo int { ... }
-- module M = Foo.T0 (struct type nonrec t = int ... end)
include Foo { ... }
-- include Foo(struct ... end)
include Foo int { ... }
-- include Foo.T0(struct type nonrec t = int ... end)
include Foo int
-- include Foo.T0 with type t := int
-- Module Interfaces
module M :: I
-- module M : I
module M :: { ... }
-- module M : sig ... end
module M :: { ... }
-- module type M : sig ... end
-- • --
M : sig
M = mod { }
m : val int
f : fun int -> int