-
Notifications
You must be signed in to change notification settings - Fork 15
/
blog.smli
1276 lines (1188 loc) · 46.3 KB
/
blog.smli
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Licensed to Julian Hyde under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Julian Hyde licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*
* This script contains Morel fragments that are used in Morel's
* web site, documentation, and blog posts. Just, you know, to keep
* us honest.
*)
Sys.set ("printDepth", ~1);
> val it = () : unit
Sys.set ("lineWidth", 78);
> val it = () : unit
Sys.set ("stringDepth", ~1);
> val it = () : unit
(*) === README.md ===================================================
(*) Auxiliary declarations
val e = {deptno = 10, name = "Fred"};
> val e = {deptno=10,name="Fred"} : {deptno:int, name:string}
val d = 10;
> val d = 10 : int
val filter = List.filter;
> val filter = fn : ('a -> bool) -> 'a list -> 'a list
"Hello, world!";
> val it = "Hello, world!" : string
(*) In Morel, you can omit label = if the expression is an identifier,
(*) label application, or field reference. Thus
{#deptno e, e.name, d};
> val it = {d=10,deptno=10,name="Fred"} : {d:int, deptno:int, name:string}
(*) is shorthand for
{deptno = #deptno e, name = e.name, d = d};
> val it = {d=10,deptno=10,name="Fred"} : {d:int, deptno:int, name:string}
(*) In a sense, from is syntactic sugar. For example, given emps and
(*) depts, relations defined as lists of records as follows
val emps =
[{id = 100, name = "Fred", deptno = 10},
{id = 101, name = "Velma", deptno = 20},
{id = 102, name = "Shaggy", deptno = 30},
{id = 103, name = "Scooby", deptno = 30}];
> val emps =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
val depts =
[{deptno = 10, name = "Sales"},
{deptno = 20, name = "Marketing"},
{deptno = 30, name = "Engineering"},
{deptno = 40, name = "Support"}];
> val depts =
> [{deptno=10,name="Sales"},{deptno=20,name="Marketing"},
> {deptno=30,name="Engineering"},{deptno=40,name="Support"}]
> : {deptno:int, name:string} list
(*) the expression
from e in emps where e.deptno = 30 yield e.id;
> val it = [102,103] : int list
(*) is equivalent to standard ML
map (fn e => (#id e)) (filter (fn e => (#deptno e) = 30) emps);
> val it = [102,103] : int list
(*) You can iterate over more than one collection, and therefore
(*) generate a join or a cartesian product:
from e in emps, d in depts
where e.deptno = d.deptno
yield {e.id, e.deptno, ename = e.name, dname = d.name};
> val it =
> [{deptno=10,dname="Sales",ename="Fred",id=100},
> {deptno=20,dname="Marketing",ename="Velma",id=101},
> {deptno=30,dname="Engineering",ename="Shaggy",id=102},
> {deptno=30,dname="Engineering",ename="Scooby",id=103}]
> : {deptno:int, dname:string, ename:string, id:int} list
(*) As in any ML expression, you can define functions within a from
(*) expression, and those functions can operate on lists. Thus we can
(*) implement equivalents of SQL's IN and EXISTS operators:
(* FIXME
let
fun in_ e [] = false
| in_ e (h :: t) = e = h orelse (in_ e t)
in
from e in emps
where in_ e.deptno (from d in depts
where d.name = "Engineering"
yield d.deptno)
yield e.name
end;
*)
(* FIXME
let
fun exists [] = false
| exists hd :: tl = true
in
from e in emps
where exists (from d in depts
where d.deptno = e.deptno
andalso d.name = "Engineering")
yield e.name
end;
*)
(*) === Screen cast =================================================
(*) Now we're in morel's shell, for interactive commands.
(*) First of all, we need to talk about comments.
(* This is a block comment, which can span multiple lines... *)
(*) ... and this is a single-line comment.
(*) Now, the basics.
(*) Everything in ML is an expression.
"a string literal";
> val it = "a string literal" : string
1 + 2;
> val it = 3 : int
(*) The Morel shell deduces the type of each expression,
(*) and assigns it to a variable called "it".
(*) We can use "it" in the next expression...
it + 4;
> val it = 7 : int
(*) We just saw string and int expressions.
(*) There are also boolean, list, record and tuple types:
1 = 2;
> val it = false : bool
[1, 2, 3];
> val it = [1,2,3] : int list
{id = 10, name = "Alex"};
> val it = {id=10,name="Alex"} : {id:int, name:string}
(1, true, "yes");
> val it = (1,true,"yes") : int * bool * string
(*) You can assign values to variables.
val x = 7;
> val x = 7 : int
val y = x mod 3;
> val y = 1 : int
(*) Functions are expressions, too.
(*) "fn" makes a lambda expression.
val plusOne = fn x => x + 1;
> val plusOne = fn : int -> int
plusOne 2;
> val it = 3 : int
(*) Functions are widely used, so they have a shorthand.
(*) "fun" is short for "val ... = fn".
fun plusOne x = x + 1;
> val plusOne = fn : int -> int
plusOne 1000;
> val it = 1001 : int
(*) Functions can have multiple arguments, separated by spaces.
fun plus x y = x + y;
> val plus = fn : int -> int -> int
plus 3 4;
> val it = 7 : int
(*) If we supply too few arguments, we get a closure that captures
(*) the argument value and can be applied later.
val plusTen = plus 10;
> val plusTen = fn : int -> int
plusTen 2;
> val it = 12 : int
(*) Functions can be recursive.
fun fact n = if n = 1 then 1 else n * fact (n - 1);
> val fact = fn : int -> int
fact 1;
> val it = 1 : int
fact 5;
> val it = 120 : int
(*) A higher-order function is a function that operates on other
(*) functions. Here are a couple.
(*) "map" applies another function to each element of a list
let
fun map f [] = []
| map f (head :: tail) = (f head) :: (map f tail)
fun double n = n * 2
in
map double [1, 2, 3, 4]
end;
> val it = [2,4,6,8] : int list
(*) "filter" keeps only those elements of a list for which
(*) a predicate evaluates to true.
let
fun filter p [] = []
| filter p (head :: tail) =
if (p head) then
(head :: (filter p tail))
else
(filter p tail)
fun even n = n mod 2 = 0
in
filter even [1, 2, 3, 4]
end;
> val it = [2,4] : int list
(*) You may notice that "map" and "filter" are very similar to the
(*) "select" and "where" clauses of a SQL statement.
(*)
(*) This is no surprise: relational algebra, which underlies SQL, is
(*) basically a collection of higher-order functions applied to
(*) lists of records (relations).
(*)
(*) Can we extend ML syntax to make it easier to write relational
(*) algebra expressions? You bet!
(*) Let's start by defining "emp" and "dept" relations as lists of
(*) records.
val emps =
[{id = 100, name = "Fred", deptno = 10},
{id = 101, name = "Velma", deptno = 20},
{id = 102, name = "Shaggy", deptno = 30},
{id = 103, name = "Scooby", deptno = 30}];
> val emps =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
val depts =
[{deptno = 10, name = "Sales"},
{deptno = 20, name = "HR"},
{deptno = 30, name = "Engineering"},
{deptno = 40, name = "Support"}];
> val depts =
> [{deptno=10,name="Sales"},{deptno=20,name="HR"},
> {deptno=30,name="Engineering"},{deptno=40,name="Support"}]
> : {deptno:int, name:string} list
(*) Now our first query, equivalent to "select * from emps as e".
from e in emps yield e;
> val it =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
(*) Now "select e.id from emps as e where e.deptno = 30"
from e in emps where (#deptno e) = 30 yield (#id e);
> val it = [102,103] : int list
(*) Join two relations
from e in emps, d in depts
where (#deptno e) = (#deptno d)
yield {id = (#id e), deptno = (#deptno e),
ename = (#name e), dname = (#name d)};
> val it =
> [{deptno=10,dname="Sales",ename="Fred",id=100},
> {deptno=20,dname="HR",ename="Velma",id=101},
> {deptno=30,dname="Engineering",ename="Shaggy",id=102},
> {deptno=30,dname="Engineering",ename="Scooby",id=103}]
> : {deptno:int, dname:string, ename:string, id:int} list
(*) A query with "exists" and a correlated sub-query.
(*) We define the "exists" function ourselves: no need for a
(*) built-in!
let
fun exists [] = false
| exists (head :: tail) = true
in
from e in emps
where exists (from d in depts
where (#deptno d) = (#deptno e)
andalso (#name d) = "Engineering")
yield (#name e)
end;
> val it = ["Shaggy","Scooby"] : string list
(*) That's all, folks!
(*) To recap, Morel has:
(*) * expressions of int, string, boolean, float, char, list,
(*) tuple and record types;
(*) * lambda expressions and recursive functions;
(*) * algebraic datatypes and pattern-matching;
(*) * polymorphism and powerful type-inference;
(*) * relational expressions (an extension to Standard ML).
(*)
(*) Follow our progress at https://github.com/hydromatic/morel.
(*) This is only release 0.1, so there's more to come!
(*) === 2020/02/25: Morel: A functional language for data ===========
(*) Auxiliary declarations
val hr = {
emps = [
{id = 100, deptno = 10, name = "SCOTT"}],
depts = [
{deptno = 10, name = "SALES"}]};
> val hr =
> {depts=[{deptno=10,name="SALES"}],emps=[{deptno=10,id=100,name="SCOTT"}]}
> : {depts:{deptno:int, name:string} list,
> emps:{deptno:int, id:int, name:string} list}
(*) here is a query in Morel:
from e in hr.emps,
d in hr.depts
where e.deptno = d.deptno
yield {e.id, e.deptno, ename = e.name, dname = d.name};
> val it = [{deptno=10,dname="SALES",ename="SCOTT",id=100}]
> : {deptno:int, dname:string, ename:string, id:int} list
(*) === 2020/03/03: Morel: The basic language =======================
(* As a functional language, everything in Morel is an expression.
The basic types are `bool`, `int`, `float`, `string` and `char`. Here
are literals in each. *)
false;
> val it = false : bool
10;
> val it = 10 : int
~4.5;
> val it = ~4.5 : real
"morel";
> val it = "morel" : string
#"a";
> val it = #"a" : char
();
> val it = () : unit
(* As you'd expect, there are built-in operators for each data
type. Here are a few examples: *)
true andalso false;
> val it = false : bool
true orelse false;
> val it = true : bool
not false;
> val it = true : bool
1 + 2;
> val it = 3 : int
~(5 - 2);
> val it = ~3 : int
10 mod 3;
> val it = 1 : int
"mo" ^ "rel";
> val it = "morel" : string
(* You can assign values to variables. *)
val x = 7;
> val x = 7 : int
val y = x mod 3;
> val y = 1 : int
x + y;
> val it = 8 : int
(* The shell deduces the type of each expression,
and assigns it to a variable called `it`.
We can use `it` in the next expression. *)
"morel";
> val it = "morel" : string
String.size it;
> val it = 5 : int
it + 4;
> val it = 9 : int
(* A let expression binds one or more values and evaluates an expression *)
let
val x = 3
val y = 2
in
x + y
end;
> val it = 5 : int
(* In addition to primitive types, there are list, record and tuple
types. *)
[1, 2, 3];
> val it = [1,2,3] : int list
{id = 10, name = "Scooby"};
> val it = {id=10,name="Scooby"} : {id:int, name:string}
(1, true, "yes");
> val it = (1,true,"yes") : int * bool * string
(* Tuples are actually just records with fields named "1", "2",
etc.: *)
(1, true, "yes");
> val it = (1,true,"yes") : int * bool * string
{1 = 1, 2 = true, 3 = "yes"};
> val it = (1,true,"yes") : int * bool * string
(1, true, "yes") = {1 = 1, 2 = true, 3 = "yes"};
> val it = true : bool
(* The empty record and empty tuple are equal, and are the only value
of the type unit. *)
{};
> val it = () : unit
();
> val it = () : unit
{} = ();
> val it = true : bool
(* Functions are expressions, too. `fn` makes a lambda expression.
After we have bound the lambda value to `plusOne`, we can use
`plusOne` as a function. *)
val plusOne = fn x => x + 1;
> val plusOne = fn : int -> int
plusOne 2;
> val it = 3 : int
(* Functions declarations are common, so the `fun` keyword provides a
shorthand. *)
fun plusOne x = x + 1;
> val plusOne = fn : int -> int
plusOne 2;
> val it = 3 : int
(* Functions can have multiple arguments, separated by spaces. *)
fun plus x y = x + y;
> val plus = fn : int -> int -> int
plus 3 4;
> val it = 7 : int
(* If we supply too few arguments, we get a closure that captures the
argument value and can be applied later. *)
val plusTen = plus 10;
> val plusTen = fn : int -> int
plusTen 2;
> val it = 12 : int
(* Functions can be recursive. *)
fun factorial n =
if n = 1 then
1
else
n * factorial (n - 1);
> val factorial = fn : int -> int
factorial 1;
> val it = 1 : int
factorial 5;
> val it = 120 : int
(* A higher-order function is a function that operates on other
functions. Here are a couple of examples.
The map function applies a given function `f` to each element of a
list, returning a list. *)
fun map f [] = []
| map f (head :: tail) = (f head) :: (map f tail);
> val map = fn : ('a -> 'b) -> 'a list -> 'b list
fun double n = n * 2;
> val double = fn : int -> int
map double [1, 2, 3, 4];
> val it = [2,4,6,8] : int list
(* The filter function keeps only those elements of a list for which a
predicate `p` evaluates to true. *)
fun filter p [] = []
| filter p (head :: tail) =
if (p head) then
(head :: (filter p tail))
else
(filter p tail);
> val filter = fn : ('a -> bool) -> 'a list -> 'a list
fun even n = n mod 2 = 0;
> val even = fn : int -> bool
filter even [1, 2, 3, 4];
> val it = [2,4] : int list
(* Let’s start by defining emps and depts relations as lists of
records. *)
val emps =
[{id = 100, name = "Fred", deptno = 10},
{id = 101, name = "Velma", deptno = 20},
{id = 102, name = "Shaggy", deptno = 30},
{id = 103, name = "Scooby", deptno = 30}];
> val emps =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
val depts =
[{deptno = 10, name = "Sales"},
{deptno = 20, name = "HR"},
{deptno = 30, name = "Engineering"},
{deptno = 40, name = "Support"}];
> val depts =
> [{deptno=10,name="Sales"},{deptno=20,name="HR"},
> {deptno=30,name="Engineering"},{deptno=40,name="Support"}]
> : {deptno:int, name:string} list
(* Now let's run our first query. *)
from e in emps yield e;
> val it =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
(* There is no difference between a query, a table and a list-valued
expression, so we could have instead written just `emps`. *)
emps;
> val it =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
(* A where clause filters out rows. *)
from e in emps
where #deptno e = 30
yield {id = #id e};
> val it = [{id=102},{id=103}] : {id:int} list
(* The following is equivalent. *)
from e in emps
where e.deptno = 30
yield {e.id};
> val it = [{id=102},{id=103}] : {id:int} list
(* If you omit 'yield' you get the raw values of 'e'. *)
from e in emps
where #deptno e = 30;
> val it = [{deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
(* Shorthand. The following 3 queries are equivalent. *)
from e in emps
yield {e = #id e};
> val it = [{e=100},{e=101},{e=102},{e=103}] : {e:int} list
from e in emps
yield {e = e.id};
> val it = [{e=100},{e=101},{e=102},{e=103}] : {e:int} list
from e in emps
yield {e.id};
> val it = [{id=100},{id=101},{id=102},{id=103}] : {id:int} list
(* Joins and sub-queries. *)
from e in emps,
d in depts
where e.deptno = d.deptno
yield {e.id, e.deptno, ename = e.name, dname = d.name};
> val it =
> [{deptno=10,dname="Sales",ename="Fred",id=100},
> {deptno=20,dname="HR",ename="Velma",id=101},
> {deptno=30,dname="Engineering",ename="Shaggy",id=102},
> {deptno=30,dname="Engineering",ename="Scooby",id=103}]
> : {deptno:int, dname:string, ename:string, id:int} list
(* The following query would, in SQL, be described as having 'EXISTS
and a correlated sub-query'. But 'exists' is not a built-in keyword
in Morel, just a function that we define in the query, and a
sub-query is just an expression that happens to return a list. *)
let
fun exists [] = false
| exists (head :: tail) = true
in
from e in emps
where exists (from d in depts
where d.deptno = e.deptno
andalso d.name = "Engineering")
yield e.name
end;
> val it = ["Shaggy","Scooby"] : string list
(*) === 2020/03/03: Morel: The basic language =======================
(*) WordCount in Standard ML
(* Note: The blog post used Standard ML. Here, to accommodate missing
language features in Morel, we have changed "(op +)" to
"(fn (x, y) => x + y)". *)
fun mapReduce mapper reducer list =
let
fun update (key, value, []) = [(key, [value])]
| update (key, value, ((key2, values) :: tail)) =
if key = key2 then
(key, (value :: values)) :: tail
else
(key2, values) :: (update (key, value, tail))
fun dedup ([], dict) = dict
| dedup ((key, value) :: tail, dict) =
dedup (tail, update (key, value, dict))
fun flatMap f list = List.foldl List.at [] (List.map f list)
val keyValueList = flatMap mapper list
val keyValuesList = dedup (keyValueList, [])
in
List.map (fn (key, values) => (key, reducer (key, values))) keyValuesList
end;
> val mapReduce = fn
> : ('a -> ('b * 'c) list)
> -> ('b * 'c list -> 'd) -> 'a list -> ('b * 'd) list
fun wc_mapper line =
let
fun split0 [] word words = word :: words
| split0 (#" " :: s) word words = split0 s "" (word :: words)
| split0 (c :: s) word words = split0 s (word ^ (String.str c)) words
fun split s = List.rev (split0 (String.explode s) "" [])
in
List.map (fn w => (w, 1)) (split line)
end;
> val wc_mapper = fn : string -> (string * int) list
fun wc_reducer (key, values) = List.foldl (fn (x, y) => x + y) 0 values;
> val wc_reducer = fn : 'a * int list -> int
(*) Check that they work on discrete values
wc_mapper "a skunk sat on a stump";
> val it = [("a",1),("skunk",1),("sat",1),("on",1),("a",1),("stump",1)]
> : (string * int) list
wc_reducer ("hello", [1, 4, 2]);
> val it = 7 : int
(*) Bind them to mapReduce, and run
fun wordCount lines = mapReduce wc_mapper wc_reducer lines;
> val wordCount = fn : string list -> (string * int) list
from p in wordCount ["a skunk sat on a stump",
"and thunk the stump stunk",
"but the stump thunk the skunk stunk"]
order p.word;
> stdIn:4.7 Error: no field 'word' in type 'string * int'
> raised at: stdIn:4.7
from p in wordCount ["a skunk sat on a stump",
"and thunk the stump stunk",
"but the stump thunk the skunk stunk"]
order #1 p;
> val it =
> [("a",2),("and",1),("but",1),("on",1),("sat",1),("skunk",2),("stump",3),
> ("stunk",2),("the",3),("thunk",2)] : (string * int) list
(*) WordCount in Morel
val lines = ["a skunk sat on a stump",
"and thunk the stump stunk",
"but the stump thunk the skunk stunk"];
> val lines =
> ["a skunk sat on a stump","and thunk the stump stunk",
> "but the stump thunk the skunk stunk"] : string list
fun split s =
let
fun split0 [] word words = word :: words
| split0 (#" " :: s) word words = split0 s "" (word :: words)
| split0 (c :: s) word words = split0 s (word ^ (String.str c)) words
in
List.rev (split0 (String.explode s) "" [])
end;
> val split = fn : string -> string list
from line in lines,
word in split line
group word compute count
order word;
> val it =
> [{count=2,word="a"},{count=1,word="and"},{count=1,word="but"},
> {count=1,word="on"},{count=1,word="sat"},{count=2,word="skunk"},
> {count=3,word="stump"},{count=2,word="stunk"},{count=3,word="the"},
> {count=2,word="thunk"}] : {count:int, word:string} list
(*) A more complete solution
fun wordCount lines =
let
fun split0 [] word words = word :: words
| split0 (#" " :: s) word words = split0 s "" (word :: words)
| split0 (c :: s) word words = split0 s (word ^ (String.str c)) words
fun split s = List.rev (split0 (String.explode s) "" [])
in
from line in lines,
word in split line
group word compute count
end;
> val wordCount = fn : string list -> {count:int, word:string} list
from p in wordCount lines order p.word;
> val it =
> [{count=2,word="a"},{count=1,word="and"},{count=1,word="but"},
> {count=1,word="on"},{count=1,word="sat"},{count=2,word="skunk"},
> {count=3,word="stump"},{count=2,word="stunk"},{count=3,word="the"},
> {count=2,word="thunk"}] : {count:int, word:string} list
(*) === Aggregate functions =========================================
val emps = scott.emp;
> val emps = <relation>
> :
> {comm:real, deptno:int, empno:int, ename:string, hiredate:string,
> job:string, mgr:int, sal:real} list
val depts = scott.dept;
> val depts = <relation> : {deptno:int, dname:string, loc:string} list
from e in emps
group e.deptno compute sumSal = sum of e.sal
order deptno;
> val it =
> [{deptno=10,sumSal=8750.0},{deptno=20,sumSal=10875.0},
> {deptno=30,sumSal=9400.0}] : {deptno:int, sumSal:real} list
from e in emps,
d in depts
where e.deptno = d.deptno
group e.deptno, d.dname, e.job
compute sumSal = sum of e.sal,
minRemuneration = min of e.sal + e.comm
order deptno, job;
> val it =
> [
> {deptno=10,dname="ACCOUNTING",job="CLERK",minRemuneration=1300.0,
> sumSal=1300.0},
> {deptno=10,dname="ACCOUNTING",job="MANAGER",minRemuneration=2450.0,
> sumSal=2450.0},
> {deptno=10,dname="ACCOUNTING",job="PRESIDENT",minRemuneration=5000.0,
> sumSal=5000.0},
> {deptno=20,dname="RESEARCH",job="ANALYST",minRemuneration=3000.0,
> sumSal=6000.0},
> {deptno=20,dname="RESEARCH",job="CLERK",minRemuneration=800.0,sumSal=1900.0},
> {deptno=20,dname="RESEARCH",job="MANAGER",minRemuneration=2975.0,
> sumSal=2975.0},
> {deptno=30,dname="SALES",job="CLERK",minRemuneration=950.0,sumSal=950.0},
> {deptno=30,dname="SALES",job="MANAGER",minRemuneration=2850.0,sumSal=2850.0},
> {deptno=30,dname="SALES",job="SALESMAN",minRemuneration=1500.0,
> sumSal=5600.0}]
> : {deptno:int, dname:string, job:string, minRemuneration:real, sumSal:real} list
(*) In this example, we define our own version of the `sum` function:
let
fun my_sum [] = 0
| my_sum (head :: tail) = head + (my_sum tail)
in
from e in emps
group e.deptno
compute sumEmpno = my_sum of e.empno
order deptno
end;
> val it =
> [{deptno=10,sumEmpno=23555},{deptno=20,sumEmpno=38501},
> {deptno=30,sumEmpno=46116}] : {deptno:int, sumEmpno:int} list
(*) The equivalent of SQL's COLLECT aggregate function is trivial
from e in emps
group e.deptno
compute names = (fn x => x) of e.ename
order deptno;
> val it =
> [{deptno=10,names=["CLARK","KING","MILLER"]},
> {deptno=20,names=["SMITH","JONES","SCOTT","ADAMS","FORD"]},
> {deptno=30,names=["ALLEN","WARD","MARTIN","BLAKE","TURNER","JAMES"]}]
> : {deptno:int, names:string list} list
(*) === StrangeLoop 2021 talk =======================================
(*) Standard ML: values
"Hello, world!";
> val it = "Hello, world!" : string
1 + 2;
> val it = 3 : int
~1.5;
> val it = ~1.5 : real
[1, 1, 2, 3, 5];
> val it = [1,1,2,3,5] : int list
fn i => i mod 2 = 1;
> val it = fn : int -> bool
(1, "a");
> val it = (1,"a") : int * string
{name = "Fred", empno = 100};
> val it = {empno=100,name="Fred"} : {empno:int, name:string}
(*) Standard ML: types
true;
> val it = true : bool
#"a";
> val it = #"a" : char
~1;
> val it = ~1 : int
3.14;
> val it = 3.14 : real
"foo";
> val it = "foo" : string
();
> val it = () : unit
String.size;
> val it = fn : string -> int
fn (x, y) => x + y * y;
> val it = fn : int * int -> int
(10, "Fred");
> val it = (10,"Fred") : int * string
{empno=10, name="Fred"};
> val it = {empno=10,name="Fred"} : {empno:int, name:string}
[1, 2, 3];
> val it = [1,2,3] : int list
[(true, fn i => i + 1)];
> val it = [(true,fn)] : (bool * (int -> int)) list
List.length;
> val it = fn : 'a list -> int
(*) Standard ML: variables and functions
val x = 1;
> val x = 1 : int
val isOdd = fn i => i mod 2 = 1;
> val isOdd = fn : int -> bool
fun isOdd i = i mod 2 = 0;
> val isOdd = fn : int -> bool
isOdd x;
> val it = false : bool
let
val x = 6
fun isOdd i = i mod 2 = 1
in
isOdd x
end;
> val it = false : bool
(*) Algebraic data types, case, and recursion
(* TODO fix bug in parameterized datatype, and remove intTree below
datatype 'a tree =
EMPTY
| LEAF of 'a
| NODE of ('a * 'a tree * 'a tree);
*)
datatype intTree =
EMPTY
| LEAF of int
| NODE of (int * intTree * intTree);
> datatype intTree = EMPTY | LEAF of int | NODE of int * intTree * intTree
fun sumTree EMPTY = 0
| sumTree (LEAF i) = i
| sumTree (NODE (i, l, r)) =
i + sumTree l + sumTree r;
> val sumTree = fn : intTree -> int
val t = NODE (1, LEAF 2, NODE (3, EMPTY, LEAF 7));
> val t = NODE (1,LEAF 2,NODE (3,EMPTY,LEAF 7)) : intTree
sumTree t;
> val it = 13 : int
val rec sumTree = fn t =>
case t of EMPTY => 0
| LEAF i => i
| NODE (i, l, r) => i + sumTree l + sumTree r;
> val sumTree = fn : intTree -> int
(*) Relations and higher-order functions
val emps = [
{id = 100, name = "Fred", deptno = 10},
{id = 101, name = "Velma", deptno = 20},
{id = 102, name = "Shaggy", deptno = 30},
{id = 103, name = "Scooby", deptno = 30}];
> val emps =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
List.filter (fn e => #deptno e = 30) emps;
> val it = [{deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
(*) Implementing Join using higher-order functions
val depts = [
{deptno = 10, name = "Sales"},
{deptno = 20, name = "Marketing"},
{deptno = 30, name = "R&D"}];
> val depts =
> [{deptno=10,name="Sales"},{deptno=20,name="Marketing"},
> {deptno=30,name="R&D"}] : {deptno:int, name:string} list
fun flatMap f xs = List.concat (List.map f xs);
> val flatMap = fn : ('a -> 'b list) -> 'a list -> 'b list
List.map
(fn (d, e) => {deptno = #deptno d, name = #name e})
(List.filter
(fn (d, e) => #deptno d = #deptno e)
(flatMap
(fn e => (List.map (fn d => (d, e)) depts))
emps));
> val it =
> [{deptno=10,name="Fred"},{deptno=20,name="Velma"},{deptno=30,name="Shaggy"},
> {deptno=30,name="Scooby"}] : {deptno:int, name:string} list
(*) Implementing Join in Morel using `from`
from e in emps,
d in depts
where e.deptno = d.deptno
yield {d.deptno, e.name};
> val it =
> [{deptno=10,name="Fred"},{deptno=20,name="Velma"},{deptno=30,name="Shaggy"},
> {deptno=30,name="Scooby"}] : {deptno:int, name:string} list
from e in emps,
d in depts
where #deptno e = #deptno d
yield {deptno = #deptno d, name = #name e};
> val it =
> [{deptno=10,name="Fred"},{deptno=20,name="Velma"},{deptno=30,name="Shaggy"},
> {deptno=30,name="Scooby"}] : {deptno:int, name:string} list
(*) WordCount
let
fun split0 [] word words = word :: words
| split0 (#" " :: s) word words = split0 s "" (word :: words)
| split0 (c :: s) word words = split0 s (word ^ (String.str c)) words
fun split s = List.rev (split0 (String.explode s) "" [])
in
from line in lines,
word in split line
group word compute c = count
order word
end;
> val it =
> [{c=2,word="a"},{c=1,word="and"},{c=1,word="but"},{c=1,word="on"},
> {c=1,word="sat"},{c=2,word="skunk"},{c=3,word="stump"},{c=2,word="stunk"},
> {c=3,word="the"},{c=2,word="thunk"}] : {c:int, word:string} list
from p in wordCount ["a skunk sat on a stump",
"and thunk the stump stunk",
"but the stump thunk the skunk stunk"]
order p.word;
> val it =
> [{count=2,word="a"},{count=1,word="and"},{count=1,word="but"},
> {count=1,word="on"},{count=1,word="sat"},{count=2,word="skunk"},
> {count=3,word="stump"},{count=2,word="stunk"},{count=3,word="the"},
> {count=2,word="thunk"}] : {count:int, word:string} list
(*) Functions as views, functions as values
fun emps2 () =
from e in emps
yield {e.id,
e.name,
e.deptno,
comp = fn revenue => case e.deptno of
30 => e.id + revenue / 2
| _ => e.id};
> val emps2 = fn
> : unit -> {comp:int -> int, deptno:int, id:int, name:string} list
from e in emps2 ()
yield {e.name, e.id, c = e.comp 1000};
> val it =
> [{c=100,id=100,name="Fred"},{c=101,id=101,name="Velma"},
> {c=602,id=102,name="Shaggy"},{c=603,id=103,name="Scooby"}]
> : {c:int, id:int, name:string} list
(*) Chaining relational operators
from e in emps;
> val it =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
from e in emps
order e.deptno, e.id desc;
> val it =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=103,name="Scooby"},{deptno=30,id=102,name="Shaggy"}]
> : {deptno:int, id:int, name:string} list
from e in emps
order e.deptno, e.id desc
yield {e.name, nameLength = String.size e.name, e.id, e.deptno};
> val it =
> [{deptno=10,id=100,name="Fred",nameLength=4},
> {deptno=20,id=101,name="Velma",nameLength=5},
> {deptno=30,id=103,name="Scooby",nameLength=6},
> {deptno=30,id=102,name="Shaggy",nameLength=6}]
> : {deptno:int, id:int, name:string, nameLength:int} list
from e in emps
order e.deptno, e.id desc
yield {e.name, nameLength = String.size e.name, e.id, e.deptno}
where nameLength > 4;
> val it =
> [{deptno=20,id=101,name="Velma",nameLength=5},
> {deptno=30,id=103,name="Scooby",nameLength=6},
> {deptno=30,id=102,name="Shaggy",nameLength=6}]
> : {deptno:int, id:int, name:string, nameLength:int} list
from e in emps
order e.deptno, e.id desc
yield {e.name, nameLength = String.size e.name, e.id, e.deptno}
where nameLength > 4
group deptno compute c = count, s = sum of nameLength;
> val it = [{c=1,deptno=20,s=5},{c=2,deptno=30,s=12}]
> : {c:int, deptno:int, s:int} list
from e in emps
order e.deptno, e.id desc
yield {e.name, nameLength = String.size e.name, e.id, e.deptno}
where nameLength > 4
group deptno compute c = count, s = sum of nameLength
where s > 10;
> val it = [{c=2,deptno=30,s=12}] : {c:int, deptno:int, s:int} list
from e in emps
order e.deptno, e.id desc
yield {e.name, nameLength = String.size e.name, e.id, e.deptno}
where nameLength > 4
group deptno compute c = count, s = sum of nameLength
where s > 10
yield c + s;
> val it = [14] : int list
(*) Integration with Apache Calcite - schemas
foodmart;
> val it =
> {account=<relation>,agg_c_10_sales_fact_1997=<relation>,
> agg_c_14_sales_fact_1997=<relation>,
> agg_c_special_sales_fact_1997=<relation>,
> agg_g_ms_pcat_sales_fact_1997=<relation>,
> agg_l_03_sales_fact_1997=<relation>,agg_l_04_sales_fact_1997=<relation>,
> agg_l_05_sales_fact_1997=<relation>,agg_lc_06_sales_fact_1997=<relation>,
> agg_lc_100_sales_fact_1997=<relation>,agg_ll_01_sales_fact_1997=<relation>,
> agg_pl_01_sales_fact_1997=<relation>,category=<relation>,
> currency=<relation>,customer=<relation>,days=<relation>,
> department=<relation>,employee=<relation>,employee_closure=<relation>,
> expense_fact=<relation>,inventory_fact_1997=<relation>,
> inventory_fact_1998=<relation>,position=<relation>,product=<relation>,