File: syntax.fs

package info (click to toggle)
fsharp 4.0.0.4%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 58,824 kB
  • ctags: 1,395
  • sloc: cs: 2,983; ml: 1,098; makefile: 410; sh: 409; xml: 113
file content (1812 lines) | stat: -rw-r--r-- 51,129 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
// #Regression #Conformance #Operators #SyntacticSugar #Exceptions #ControlFlow #Arrays #Tuples #Lists #Classes #Constants #Records 
#if ALL_IN_ONE
module Core_syntax
#endif
#light
let failures = ref false
let report_failure () = 
  stderr.WriteLine " NO"; failures := true
let test s b = stderr.Write(s:string);  if b then stderr.WriteLine " OK" else report_failure() 

// Test the __LINE__ directive
test "line number test" (__LINE__ = "12")
#line 100
test "line number test" (__LINE__ = "100")

#line 102 "file.fs"
test "line number test" (__LINE__ = "102")
test "line number test" (__SOURCE_FILE__ = "file.fs")

#line 18 "original-test-file.fs"
test "line number test" (__LINE__ = "18")
test "line number test" (__SOURCE_FILE__ = "original-test-file.fs")

# 100
test "line number test" (__LINE__ = "100")

# 102 "file.fs"
test "line number test" (__LINE__ = "102")
test "line number test" (__SOURCE_FILE__ = "file.fs")

# 29 "original-test-file.fs"
test "line number test" (__LINE__ = "29")
test "line number test" (__SOURCE_FILE__ = "original-test-file.fs")

#if NetCore
#else
let argv = System.Environment.GetCommandLineArgs() 

let SetCulture() = 
  if argv.Length > 2 && argv.[1] = "--culture" then  
    let cultureString = argv.[2] 
    let culture = new System.Globalization.CultureInfo(cultureString) 
    stdout.WriteLine ("Running under culture "+culture.ToString()+"...");
    System.Threading.Thread.CurrentThread.CurrentCulture <-  culture
 

do SetCulture();;
#endif
  
let SimpleArithmetic( )
    = let x = 10 + 12 - 3
       in let y = x * 2 + 1 in
          let r1,r2 = x/3, x%3
           in printf "x = %d, y = %d, x/3 = %d, x%%3 = %d\n"
                           x       y        r1         r2;;

module CheckDynamicOperatorsOnTypes = 
    type Foo =
         val s : string
         new(s) = { s = s }
         static member (?) (foo : Foo, name : string) = foo.s + name
         static member (?<-) (foo : Foo, name : string, v : string) = ()

    let foo = Foo("hello, ")
    let hw  : string = foo ? world
    let hw2  : unit = foo ? world <- "3"
        

// Copyright (c) Microsoft Corporation 2005-2006.  .


open System

//--------------------------------------------------------

let _ = stdout.WriteLine "Test Starting"

let SampleArithmetic1() =
    let x = 10 + 12 - 3 
    let y = x * 2 + 1 
    let r1,r2 = x/3, x%3
    printf "x = %d, y = %d, r1 = %d, r2 = %d\n" x y r1 r2
  

let SampleArithmetic2() =
    let x = 10.0 + 12.0 - 3.0 
    let y = x * 2.0 + 1.0 
    let r1 = x/3.0
    printf "x = %g, y = %g, r1 = %g\n" x y r1

  
let SampleArithmetic3() =
    // Manipulating double-precision (64-bit) floating point numbers
    let pi1 = float   3 + 0.1415 
    let pi2 = float 3 + 0.1415   // identical - int32 and int are the same
    let pi3 = float 3 + 0.1415   // identical - A.to_B and B.of_A often both exist
    let pi4 = float 3          + 0.1415   // identical - 'float' is an addiitonal ahortcuts
    printf "pi1 = %f, pi2 = %f, pi3 = %f\n" pi1 pi2 pi3;
  
    let i1 = int   3.1415 
    let i2 = int32 3.1415   // identical - int32 and int are the same
    let i3 = int32 3.1415   // identical - A.to_B and B.of_A often both exist
    let i4 = float 3.1415         // identical 
    printf "pi1 = %f, pi2 = %f, pi3 = %f\n" pi1 pi2 pi3;
  
    // Manipulating single-precision (32-bit) floating point numbers
    let f32a = 2.1415f + 1.0f             // float32 (System.Single)
    let f32b = 2.1415f + float32 1 // float32 - identical 
    printf "f32a = %f, f32b = %G\n" (float f32a) (float f32b)
  
    // Manipulating bytes
    let byteA = byte (3+4)         // byte 
    let byteB = byte 0xFF          // byte 
    printf "byteA = %d, byteB = %d\n" (int byteA) (int byteB)
  

let Sample4() =
    // Operators over integers:
    let x1 = 0xAB7F3456 &&& 0xFFFF0000 
    let x2 = 0xAB7F3456 ||| 0xFFFF0000 
    let x3 = 0x12343456 ^^^ 0xFFFF0000 
    let x4 = 0x1234ABCD <<< 1 
    let x5 = 0x1234ABCD >>> 16 
    // Also over other integral types, e.g. Int64:
    let x6 = 0x0A0A0A0A012343456L &&& 0x00000000FFFF0000L 
    // Also over other integral types, e.g. unsigned Int64:
    let x6u = 0x0A0A0A0A012343456UL &&& 0x0000FFFF00000000UL 
    // And bytes:
    let x7 = 0x13uy &&& 0x11uy 
    // Now print the results:
    printf "x1 = 0x%08x\n" x1;
    printf "x2 = 0x%08x\n" x2;
    printf "x3 = 0x%08x\n" x3;
    printf "x4 = 0x%08x\n" x4;
    printf "x5 = 0x%08x\n" x5;
    printf "x6 = 0x%016x\n" x6;
    printf "x6u = 0x%016u\n" x6u;
    printf "x7 = 0x%02x\n" (int x7)
  
  
  
//--------------------------------------------------------


let FunctionSample1() =
    let twice x = x + x 
    printf "twice 2 = %d\n" (twice 2);
    printf "twice 4 = %d\n" (twice 4);
    printf "twice (twice 2) = %d\n" (twice (twice 2))

let FunctionSample2() =
    let even n = (n%2 = 0) 
    let tick x = printf "tick %d\n" x 
    let tock x = printf "tock %d\n" x 
    let choose f g h x = if f x then g x else h x 
    let ticktock = choose even tick tock  // ticktock is a function built out of other functions using 'choose'
    for i = 0 to 10 do
        ticktock i
    

let FunctionSample3() =
    let tick x = printf "tick %d\n" x 
    let tock x = printf "tock %d\n" x 
    let choose f g h x = if f x then g x else h x 
    for i = 0 to 10 do
        // This is like the previous sample, but uses an anonymous lambda expression for 
        // the function that decides whether to tick or tock.
        choose (fun n -> n%2 = 0) tick tock i 
    

//--------------------------------------------------------

let ExceptionSample1() : unit =
    failwith "Here's how to raise a simple 'Failure' exception"
  

let ExceptionSample2() =
    try 
        printf "About to raise a simple 'Failure' exception...\n"
        failwith "Whoa!"
    with 
        Failure msg -> 
            printf "Caught a simple 'Failure' exception, msg = '%s'\n" msg
  
let ExceptionSample3() =
    try 
        printf "About to raise an exception...\n"
        match DateTime.Now.DayOfWeek with 
        | DayOfWeek.Monday -> raise (System.Collections.Generic.KeyNotFoundException())
        | _                -> raise (Failure "it's not Monday")
    with 
        | :? System.Collections.Generic.KeyNotFoundException -> 
            printf "Caught a 'Not_found' exception, it must be Monday\n"
        | Failure msg -> 
            printf "Caught a 'Failure' exception: %s\n" msg

let ExceptionSample4() =
    try 
        printf "About to raise an ArgumentException exception...\n"
        if DateTime.Now.DayOfWeek = DayOfWeek.Tuesday then 
            raise (new System.ArgumentException("Not today, it's Tuesday"))
        else
            raise (new System.InvalidOperationException("Hey, it's not Tuesday..."))
    with 
        | :? System.ArgumentException as e -> 
            printf "Caught an ArgumentException, e.Message = %s\n" e.Message
        | :? System.InvalidOperationException as e -> 
            printf "Caught an InvalidOperationException, e.Message = %s\n" e.Message
        | _ -> 
            printf "Some other exception was caught\n"
  
//--------------------------------------------------------

  
let SampleForLoop1() =
    for i = 1 to 10 do 
        printf "In a for-loop, i = %d\n" i
    
  
let SampleForLoop2() =
    for i = 0 to 9 do 
        for j = 0 to i-1 do 
            printf " "
        for j = i to 9 do 
            printf "%d" j
        printf "\n"
    
  

let SampleWhileLoop1() =
    let count = ref 0
    while (!count < 10) do 
        printf "Counting, skipping by 2, count = %d...\n" !count;
        count := !count + 2
    printf "Done counting!\n"
  


let SampleWhileLoop2() =
    let start = DateTime.Now 
    let duration = System.TimeSpan.FromMilliseconds(8.0)
    let diff (a:DateTime) (b:DateTime) = System.DateTime.op_Subtraction(System.DateTime.Now,b) 
    printf "Waiting...\n"
    // Here's the loop
    while diff DateTime.Now start < duration do
        printf "."
    // OK, we're ...
    let span = diff DateTime.Now start 
    printf "\nAttempted to busy-wait 8ms, actually waited %dms\n" span.Milliseconds
  

//--------------------------------------------------------


let SampleRec1() =
    let rec fib n = if n < 2 then 1 else fib (n-1) + fib (n-2) 
    for i = 1 to 10 do
        printf "fib %d = %d\n" i (fib i)

//--------------------------------------------------------



let SampleArray1() =
    let size = 1000 
    let arr = Array.create size 0 
    for i = 1 to size - 1 do 
        arr.[i] <- i + arr.[i-1]
    for i = 1 to size - 1 do 
        printf "arr.[%4d] = %d\n" i arr.[i]
    
  


let SampleArray2() =
    let numLetters = 26 
    let results = Array.create numLetters 0 
    let data = "The quick brown fox jumps over the lazy dog" 
    for i = 0 to data.Length - 1 do 
        let c = data.Chars(i) 
        let c = Char.ToUpper(c)  
        if c >= 'A' && c <= 'Z' then 
            let i = int c - int 'A' 
            results.[i] <- results.[i] + 1
    for i = 0 to numLetters - 1 do 
        printf "Number of '%c' characters = %d\n" (char (i + int 'A')) results.[i]
    
  


let SampleHashtbl3() =
    let tab = new System.Collections.Generic.Dictionary<char,int>(30) 
    let data = "The quick brown fox jumps over the lazy dog" 
    for i = 0 to data.Length - 1 do 
        let c = data.Chars(i) 
        if tab.ContainsKey(c) then 
            let v = tab.[c] 
            let _ = tab.Remove(c) 
            tab.Add(c,v+1)
        else 
            tab.Add(c,1)
    ;
    tab |> Seq.iter (fun kvp -> printf "Number of '%c' characters = %d\n" kvp.Key kvp.Value ) 

  

//--------------------------------------------------------


let TupleSample1() =
    let data = 1,2,3
    printf "data = %A\n" data;
    let f (a,b,c) = (a+b,b+c,c+a) 
    let res = f(f(f(data))) 
    printf "res = %A\n" res;
    let r1,r2,r3 = res 
    printf "r1 = %d, r2 = %d, r3 = %d\n" r1 r2 r3;
    let r4,r5,r6 = f(res) 
    printf "r4 = %d, r5 = %d, r6 = %d\n" r4 r5 r6
  


let ListSample1() =
    let data = [1;2;3;4]
    printf "data = %A\n" data;
    printf "head data = %d\n" (List.head data);
    printf "tail data = %A\n" (List.tail data);
    printf "length data = %d\n" (List.length data);
    printf "nonempty data = %b\n" (List.isEmpty data);
    let consume data = 
        match data with 
        | 1::rest    -> printf "matched a 1\n";       rest
        | 2::3::rest -> printf "matched a 2 and 3\n"; rest 
        | [4]        -> printf "matched a 4\n";       []
        | _          -> printf "unexpected!";         [] 
    let data = consume data 
    let data = consume data 
    let data = consume data 
    printf "At end of list? %b\n" (data = [])
  

let ListSample2() =
    let data = [1;2;3;4]
    let r1 = List.map (fun x -> x + 1) data 
    printf "Adding '1' using map = %A\n" r1
    let r2 = List.map string data 
    printf "Converting to strings using map = %A\n" r2
    let r3 = List.map (fun x -> (x,x)) data 
    printf "Tupling up using map = %A\n" r3
 

let ListSample3() =
    let data = ["Cats";"Dogs";"Mice";"Elephants"]
    data |> List.iter (fun x -> printf "item: %s\n" x)
 

let ListSample4() =
    let data = ["Cats";"Dogs";"Mice";"Elephants"]
    data |> List.iteri (fun i x -> printf "item %d: %s\n" i x)
 


let ListSample5() =
    let data = [("Cats",4);
                ("Dogs",5);
                ("Mice",3);
                ("Elephants",2)]
    let count = List.fold (fun acc (nm,x) -> acc+x) 0 data
    printf "Total number of animals: %d\n" count
 

let ListSample6() =
    let data = [("Cats",4);
                ("Dogs",5);
                ("Mice",3);
                ("Elephants",2)]
    let res = List.filter (fun (nm,x) -> String.length nm <= 4) data
    printf "Animals with short names: %A\n" res
 


let ListSample7() =
    let data = [("Cats",4);
                ("Dogs",5);
                ("Mice",3);
                ("Elephants",2)]
    let res = List.choose (fun (nm,x) -> if String.length nm <= 4 then Some(x) else None) data
    printf "Counts of animals with short names: %A\n" res
 
  

let OptionsSample2() =
    let data = Some(1,3)
    printf "data = %A\n" data;
    printf "Option.isSome data = %b\n" (Option.isSome data);
    printf "Option.isNone data = %b\n" (Option.isNone data);
    //printf "Option.length data = %d\n"  (Option.length data);
    printf "Option.get data = %A\n" (Option.get data);
    let data2 = None
    printf "Option.isSome data = %b\n" (Option.isSome data2);
    printf "Option.isNone data = %b\n" (Option.isNone data2);
    //printf "Option.length data = %d\n"  (Option.length data2)


let OptionsSample3() =
    let openingHours day = 
        match day with 
        | DayOfWeek.Monday 
        | DayOfWeek.Tuesday 
        | DayOfWeek.Thursday 
        | DayOfWeek.Friday    -> Some(9,17)
        | DayOfWeek.Wednesday -> Some(9,19) // extended hours on Wednesday
        | _ -> None 
    let today = DateTime.Now.DayOfWeek 
    match openingHours today with 
    | None -> printf "The shop's not open today\n"
    | Some(s,f) -> printf "The shop's open today from %02d:00-%d:00\n" s f

//--------------------------------------------------------

  

let ComparisonSample1() =
    let show a b = 
        printf "%A < %A: %b\n" a b (a < b)
        printf "%A = %A: %b\n" a b (a = b)
        printf "%A > %A: %b\n" a b (a > b)
    
    show 1 2;
    show 2 2;
    show "1" "2"
    show "abb" "abc" 
    show "aBc" "ABB" // case-sensitive
    show None (Some 1);
    show None None;
    show (Some 0) (Some 1);
    show (Some 1) (Some 1);
    show [1;2;3] [1;2;2];
    show [] [1;2;2]
  

let HashingSample2() =
    let show a = printf "hash(%A) : %d\n" a (hash a) 
    show 1;
    show 2;
    show "1"
    show "2"
    show "abb" 
    show "aBc" // case-sensitive
    show None;
    show (Some 1);
    show (Some 0);
    show [1;2;3];
    show [1;2;3;4;5;6;7;8];
    show [1;2;3;4;5;6;7;8;9;10;11];
    show [1;2;3;4;5;6;7;8;9;10;11;12;13;14;15]

  
//--------------------------------------------------------  


let dummy() = ()
type wheel = Wheel of float  // radius of wheel, inches
type cycle = 
    | Unicycle of wheel
    | Bicycle of wheel * wheel 

let veryBigWheel = Wheel(26.0)
let bigWheel     = Wheel(13.0)
let smallWheel   = Wheel(6.0)

let pennyFarthing = Bicycle(veryBigWheel,smallWheel)
let racer         = Bicycle(bigWheel    ,bigWheel)
let kidsBike      = Bicycle(smallWheel  ,smallWheel)

let UnionSample1() =
    let show bike = 
        match bike with 
        | Unicycle (Wheel r) -> printf "Unicycle, one wheel, radius = %f\n" r
        | Bicycle (Wheel r1,Wheel r2) -> printf "Bicycle, two wheels, front = %f, back = %f\n" r1 r2 
    show pennyFarthing;
    show racer;
    show kidsBike



let dummy2() = ()
type point = { x: float; y: float}
type triangle = { p1: point; p2: point; p3: point }
type vector = { dx: float; dy: float}

let origin = { x = 0.0; y = 0.0 }
let onex = { x = 1.0; y = 0.0 }
let oney = { x = 0.0; y = 1.0 }
let diff p1 p2 = { dx = p2.x - p1.x; dy = p2.y - p1.y }
let sides tri = 
    diff tri.p2 tri.p1, 
    diff tri.p3 tri.p2, 
    diff tri.p1 tri.p3


let RecordSample1() =
    let triangle1 = { p1=origin;p2=onex;p3=oney } 
    printf "triangle1 = %A\n" triangle1;
    printf "sides(triangle1) = %A\n" (sides triangle1)
  
  

let dummy3() = ()
type Point = { x: float; y: float}
    with 
        member p.VectorFromOrigin = { dx = p.x; dy = p.y }
        static member Origin = { x = 0.0; y = 0.0 }
        static member (+) ((p:Point),(v:Vector)) = { x = p.x + v.dx; y = p.y + v.dy }
    end
 
and Vector = { dx: float; dy: float}
    with 
        static member Zero = { dx = 0.0; dy = 0.0 }
        static member OneX = { dx = 1.0; dy = 0.0 }
        static member OneY = { dx = 0.0; dy = 1.0 }
        static member (+) ((v1:Vector),(v2:Vector)) = { dx = v1.dx + v2.dx; dy = v1.dy + v2.dy }
    end



let MemberSample1() =
    printf "Point.Origin = %A\n" Point.Origin;
    printf "Point.Origin + Vector.OneX = %A\n" (Point.Origin + Vector.OneX);
    printf "Vector.OneX + Vector.OneY = %A\n" (Vector.OneX + Vector.OneY)

  


let InterfaceSample1() =
    let disposableObject1 = 
        { new IDisposable  with 
              member __.Dispose() = printf "disposed!\n" } 
    let disposableObject2 = 
        let disposed = ref false 
        { new IDisposable with 
            member __.Dispose() = if not !disposed then (disposed := true; printf "disposed!\n") } 
    disposableObject1.Dispose();
    disposableObject1.Dispose();
    disposableObject2.Dispose();
    disposableObject2.Dispose()

let dummy5() = ()
type WrapOneStream = 
    // some owned objects that need to be disposed
    { myManagedResource: IO.MemoryStream; }
    with 
        // Here is the standard machinery to implement disposability.
        // This tends to be replicated each type that must support the
        // disposal paradigm.
        interface IDisposable with
            member x.Dispose() = 
                 x.Dispose(true); 
                 GC.SuppressFinalize(x)
        end
        // We override Finalize the case this class has native resources of its own.
        // In this sample it doesn't, but we have included the full pattern 
        // here for completeness.
        override x.Finalize() = x.Dispose(false)
        member x.Dispose(deep: bool) =  
            printf "disposing, deep = %b!\n" deep;
            if deep then x.myManagedResource.Close()
  end 

let dummy4() = ()
type IPoint = 
    interface
        abstract X : float
        abstract Y : float
    end

/// This interface is really just a function, but we give it a name
/// here as an example. It represents a function from some variable to (X,Y)
type ILine = 
    interface
        abstract GetY : float -> IPoint
    end

/// A function representing a line, where the parameter governing the line is the
/// X coordinate itself
type Line = 
    class
        val a: float
        val c: float
        new (a,c) = { a=a;c=c}
        interface ILine with 
            member l.GetY(x) = { new IPoint with 
                                    member __.X=x 
                                    member __.Y= l.a * x + l.c }
        end
end

let InterfaceSample3() =
    let line = new Line(2.0,1.5) :> ILine 
    let origin =  { new IPoint with 
                         member __.X=0.0 
                         member __.Y= 0.0 }
    let point1 = line.GetY(-1.0)
    let point2 = line.GetY(0.0)
    let point3 = line.GetY(1.0)
    let output_point os (p:IPoint) = fprintf os "(%f,%f)" p.X p.Y 
    printf "origin = %a\n" output_point origin;
    printf "point1 = %a\n" output_point point1;
    printf "point2 = %a\n" output_point point2;
    printf "point3 = %a\n" output_point point3
  
let LineDirectedInputSample1() = 
    
    // Write a test file
    let outputChannel = System.IO.File.CreateText @"test.txt" 
    outputChannel.Write "This is a test file.\r\nIt is easy to read.";
    outputChannel.Close();
    
    // Now read the test file.
    let inputChannel = System.IO.File.OpenText @"test.txt" 
    let line1 = inputChannel.ReadLine() 
    let line2 = inputChannel.ReadLine() 
    // Don't forget to close the channel
    inputChannel.Close();
    printf "line1=%s\nline2=%s\n" line1 line2

module InfixTokenIndentationExamples = begin


    let f x =
       let y = 1
       -1

end

let LineDirectedInputSample2() = 
    // Write a test file
    System.IO.File.WriteAllLines(@"test.txt", [| "This is a test file."; 
                                                 "It is easy to read." |]);

    // Now read it.  We use using to ensure the file is closed even if an exception occurs
    // during reading.  
    let line1,line2 = 
        using (System.IO.File.OpenText @"test.txt") (fun sr -> 
            let line1 = sr.ReadLine() 
            let line2 = sr.ReadLine() 
            (line1,line2))  
    printf "line1=%s\nline2=%s\n" line1 line2

let LineDirectedInputSample3() = 
    // Write a test file
    System.IO.File.WriteAllLines(@"test.txt", [| "This is a test file."; 
                                                 "It is easy to read." |]);
    // Now read it 
    let lines = System.IO.File.ReadAllLines @"test.txt"  
    printf "%s" (sprintf "%A" lines)

let EntireFileInputSample2() = 
    // Write a test file
    System.IO.File.WriteAllLines(@"test.txt", [| "This is a test file."; 
                                                 "It is easy to read." |]);
    // Now read it 
    let res = System.IO.File.ReadAllText(@"test.txt")  
    printf "%s" res

module String = 
    let split (c : char list) =
        let ca = Array.ofList c 
        fun (s:string) ->
            Array.toList(s.Split(ca, System.StringSplitOptions.RemoveEmptyEntries))

let ReadCSVFile1() = 
    // Write a test file
    System.IO.File.WriteAllLines(@"test.csv", [| "Desmond, Barrow, Market Place, 2"; 
                                                 "Molly, Singer, Band, 12" |]);
    // Now read it 
    let linesSplitIntoWords = 
        System.IO.File.ReadAllLines(@"test.csv")
        |> Array.map (String.split [',';' ';'\t'])
        |> Array.map List.toArray  
    printf "%A"  linesSplitIntoWords
        
let EnumerateCSVFile1() = 

    // Write a test file
    System.IO.File.WriteAllLines(@"test.csv", [| "Desmond, Barrow, Market Place, 2"; 
                                                 "Molly, Singer, Band, 12" |]);

    /// This function builds an IEnumerable<string list> object that enumerates the CSV-split
    /// lines of the given file on-demand 
    let CSVFileEnumerator(fileName) = 
        // The function is implemented using RuntimeHelpers.GenerateUsing, which is the standard
        // function for building enumerators for sources such as files and database
        // connections.  This function takes two function parameters: one function to generate 
        // a handle to the resource, and one to incrementally read new results from the handle.  
        // The function guarantees to cleanup the resources associated with the handle when 
        // each individual enumeration is complete. Multiple concurrent enumerations can be 
        // in progress since each will use a different stream handle and/or database connection.
        seq { use sr = System.IO.File.OpenText(fileName)
              while not sr.EndOfStream  do
                yield sr.ReadLine() |> String.split [',';' ';'\t'] }

    // Now test this out on our test file, iterating the entire file
    let test = CSVFileEnumerator(@"test.csv")  
    printf "-------Enumeration 1------\n";
    test |> Seq.iter (sprintf "%A" >> printf "line %s\n");
    // Now do it again, this time determining the numer of entries on each line.
    // Note how the file is read from the start again, since each enumeration is 
    // independent.
    printf "-------Enumeration 2------\n";
    test |> Seq.iter (List.length >> printf "line has %d entries\n");
    // Now do it again, this time determining the numer of entries on each line.
    // Note how the file is read from the start again, since each enumeration is 
    // independent.
    printf "-------Enumeration 3------\n";
    test |> Seq.iter (List.map String.length >> sprintf "%A" >> printf "lengths of entries: %s\n")

let test27832() = 
    if true then
      3
    else 
      if true then 
        4
      else 
        if true then
          5
        else 
          6

let test27834() = 
    if true then
      3
    else 
      if true then printf "hello";
      4 

let test278343() = 
    if true then
      3
    else if true then 
      4
    else if true then
      5
    else 
      6
 
let test273343() = 
    if true then
      3
    else 
      if true then printf "hello";
      4
 
let test278342() = 
    if true then
      3
    elif true then 
      4
    elif true then
      5
    else 
      6

open System.IO
let findDLLs (dir:string) =
    if (Directory.Exists dir) then
        let files = Directory.GetFiles(dir, "*.dll") 
        Array.toList files
    else
        if true then 
            eprintf "Directory %s does not exist!\n" dir
        []
 
do SimpleArithmetic( );;

type recd = {x : int; y:int; z:int }
let r = { x = 3; y = 4;z=6 }
let r2 = { r with 
             x = 4 }

let r3 = { r with 
             x = 4;
             y = 6 }
let r4 = { r with 
            x = 4;
            y = 6 }
let r4b = { r with x = 4;
                   y = 6 }
let r4c = { r with   x = 4;
                     y = 6 }
let r5 = { new recd 
             with x = 4 
             and  y = 5
             and  z = 6}

let testTryFinallySyntaxOnOneLine () = 
    try () finally ()


type SampleForm = 
  class
  #if Portable
    inherit System.Object
  #else
    inherit System.Windows.Forms.Form
  #endif
    new () as this =
       { }
       
       then 
        ()
  end

(* check do introduces a #light block *)
begin 
   [ for x in [1;2;3] do
       let z = x + 1 
       let y = x + 1 
       yield y ]
end

(* check do introduces a #light block *)
begin 
   [ for x in [1;2;3] do
       let z = x + 1 
       let y = x + 1 
       yield (let x = 3 
              x+x) ]
end 


begin 
   [ for x in [1;2;3] do 
        let z = x + 1 
        let y = x + 1 
        yield y ]
end

begin 
   [ for x in [1;2;3] do 
        let z = x + 1 in 
        let y = x + 1 in 
        yield! [y] ]
end

begin 
   [ for x in [1;2;3] do
       let z = x + 1  
       let y = x + 1 
       yield y ]
end

(* check do introduces a #light block *)
begin 
   [ for x in [1;2;3] do
       let z = x + 1 in 
       let y = x + 1 
       yield y ]
end

(* check do introduces a #light block *)
begin 
   [ for x in [1;2;3] do
       if (x > x + 1) then
         let y = x + 1 
         yield y ]
end

(* check d introduces a #light block *)
begin 
   [ for x in [1;2;3] do
       if (x > x + 1) then
         yield (let y = x + 1 
                y) ]
end

begin 
   [ for x in [1;2;3]  
       -> let y = x + 1 
          y ]
end
begin 
   [ for x in [1;2;3]  
       -> let y = x + 1 
          y ]
end
begin 
   [ while false  do
       yield (let y = 2 + 1 
              y) ]
end
begin 
   [ while false  do
       yield 3 ]
end
begin 
   [ while false  do
       if false then
         yield 4 ]
end
begin 
   [ while false  do
       if false then
         do printfn "hello"
         yield 5 ]
end
begin 
   [ while false  do
       let x = 1
       if false then
         do printfn "hello"
         yield 5 ]
end
begin 
   [ let x = 1
     while false  do
       let x = 1
       if false then
         do printfn "hello"
         yield 5 ]
end
begin 
   [ let x = 1
     while false  do
       let x = 1
       while false  do
         if false then
           do printfn "hello"
           yield 5 ]
end
begin 
   [ for x in [1;2;3] 
       -> 1 ]
end

begin 
   [ for x in [1;2;3] do 
      if (x > x + 1) then
       let z = x + 1 in 
       let y = x + 1 
       yield y ]
end

begin 
   [ for x in [1;2;3] do 
        if (x > x + 1) then
         let z = x + 1 in 
         let y = x + 1 
         yield y ]
end
begin 
   [ for x in [1;2;3] do 
       if (x > x + 1) then
         let z = x + 1 
         let y = x + 1 
         yield y ]
end

let rand = new Random()

type EasyOptionInt =
| None
| Some of int
with
    member x.ToNullableInt =
        match x with
        | EasyOptionInt.None -> new Nullable<int>(-1) 
        | EasyOptionInt.Some x -> new Nullable<int>(x)
end

let getPoistiveInt2() = 
    let i = rand.Next(10)
    match i with
    | 0 -> EasyOptionInt.None
    | i -> EasyOptionInt.Some(i)

do test "vliwe9a" (-1-2 = -3)
do test "vliwe9a" (2 - -5 = 7)
do test "vliwe9a" (2- - 5 = 7)
do test "vliwe9a" (2 - - 5 = 7)
do test "vliwe9a" (2- -5 = 7)
do test "vliwe9s" (let n = -1 in let m = -2 in -n-m = 3)
do test "vliwe9s" (let n = -1 in let m = -2 in -n-m = +3)
do test "vliwe9s" (let n = -1 in let m = -2 in -n - m = +3)

do test "vliwe9d" ((1)-2 = -1)

do test "vliwe9f" ((match 1,1 with -1,-1 -> 1 | _ -> 2) = 2)
do test "vliwe9g" ((match -1,-1 with -1,-1 -> 1 | _ -> 2) = 1)

let f x = x

do test "vliwe91"   (f -2y = - 2y)
do test "vliwe92"   (f -2s = - 2s)
do test "vliwe93"   (f -2 = - 2)
do test "vliwe94"   (f -2L = - 2L)
do test "vliwe95"   (f -2n = - 2n)
do test "vliwe96"   (f -2.0 = - 2.0)
do test "vliwe97"   (f -2.0f = - 2.0f)
#if Portable
#else
do test "vliwe99"   (f -2I = - 2I)
#endif
do test "vliwe9q"   ((function -2y -> true | _ -> false) (- 2y))
do test "vliwe9w"   ((function -2s -> true | _ -> false) (- 2s))
do test "vliwe9e"   ((function -2 -> true | _ -> false) (- 2))
do test "vliwe9r"   ((function -2L -> true | _ -> false) (- 2L))
do test "vliwe9t"   ((function -2n -> true | _ -> false) (- 2n))
do test "vliwe9y"   ((function -2.0 -> true | _ -> false) (- 2.0))
do test "vliwe9u"   ((function -2.0f -> true | _ -> false) (- 2.0f))
//do test "vliwe9i"   ((function -2I -> true | _ -> false) (- 2I))
//do test "vliwe9o"   ((function -2N -> true | _ -> false) (- 2N))

module NegativePrecedence = begin
    let x = 1
    let R x = x
    let R2 x y = x
    let v1 = R 3
    let v2 = -x
    let v3 = R -x
    let v3b = R2 -x -x
    let v3c = R2 x -x
    let v3d = R2 -(R x) -x
    let v3e = R2 -(R -x) -x
    let v4 = -R(3)
    let v5 = -R (3)
    let v6 = -R 3
end

module UnicodeChars = begin

    let some_unicode_char = '\u00D6' 

    let another_unicode_char = '\U000007FF'
end



module EscapeChars = begin
    do test "cwjnecew90" ("\a" = "\007")
    do test "cwjnecew91" ('\a' = '\007')
    do test "cwjnecew91b" ('\a'B = '\007'B)
    do test "cwjnecew92" ("\f" = "\012")
    do test "cwjnecew93b" ('\f' = '\012')
    do test "cwjnecew93" ('\f'B = '\012'B)
    do test "cwjnecew94" ("\v" = "\011")
    do test "cwjnecew95" ('\v' = '\011')
    do test "cwjnecew95b" ('\v'B = '\011'B)
end

module NoParensNeededForHighPrecedenceTypeApplications = begin

    let f x = x

    let ty = f typeof<int>
end

module TypeArgsCanContainRightArrow = begin
    let (f_ok : unit -> int) = Unchecked.defaultof<(unit->int)>
    let (f : unit -> int) = Unchecked.defaultof<unit->int>
    let (g : unit -> int) = if true then Unchecked.defaultof<unit->int> else fun() -> 0

end

module QuoteDotParsing = begin
  let _ = <@ 1 @>.Raw 
  let _ = <@@ 1 @@>.ToString() 
end
  
module MappableNumericLiteralsG = begin
    module NumericLiteralG =
        let inline FromZero() =  LanguagePrimitives.GenericZero<_>
        let inline FromOne() = LanguagePrimitives.GenericOne<_>
    do test "Gvrer90a" (0G + 1G + 1G = 2)
    do test "Gvrer90b" (0G + 1G + 1G = 2.0)
    do test "Gvrer90c" (0G + 1G + 1G = 2.0f)
    do test "Gvrer90d" (0G + 1G + 1G = 2uy)
end
module MappableNumericLiteralsN = begin

    module NumericLiteralN =
        let FromZero() =  0
        let FromOne() = 1
        let FromInt32(x:int32) =  x
        let FromInt64(x:int64) =  int32 x
        let FromString(s:string) =  -2

    do test "Nvrer90e" (0N = 0)
    do test "Nvrer90f" (1N = 1)
    do test "Nvrer90g" (-1N = -1)
    do test "Nvrer90h" (2N = 2)
    do test "Nvrer90i" (2147483647N = System.Int32.MaxValue)
    do test "Nvrer90j" (-2147483648N = System.Int32.MinValue)
    do test "Nvrer90k" (2147483648N = int32 2147483648L)
    do test "Nvrer90m" (-2147483649N = int32 -2147483649L)
    do test "Nvrer90m" (9223372036854775807N = int32 9223372036854775807L)
    do test "Nvrer90m" (-9223372036854775808N = int32 -9223372036854775808L)
    do test "Nvrer90m" (9223372036854775808N = -2)
    do test "Nvrer90m" (-9223372036854775809N = -2)
    do test "Nvrer90m" (10000000000000000000000000000000000000000000000000000000000000000000000N = -2)

end

module MappableNumericLiteralsNToString = begin

    module NumericLiteralN =
        let FromZero() =  "0"
        let FromOne() = "1"
        let FromInt32(x:int32) =  string x
        let FromInt64(x:int64) =  string x
        let FromString(s:string) =  s

    do test "N2vrer90e" (0N = "0")
    do test "N2vrer90f" (1N = "1")
    do test "N2vrer90g" (-1N = "-1")
    do test "N2vrer90h" (2N = "2")
    do test "N2vrer90i" (2147483647N = "2147483647")
    do test "N2vrer90j" (-2147483648N = "-2147483648")
    do test "N2vrer90k" (2147483648N = "2147483648")
    do test "N2vrer90m" (-2147483649N = "-2147483649")
    do test "N2vrer90m" (9223372036854775807N = "9223372036854775807")
    do test "N2vrer90m" (-9223372036854775808N = "-9223372036854775808")
    do test "N2vrer90m" (9223372036854775808N = "9223372036854775808")
    do test "N2vrer90m" (-9223372036854775809N = "-9223372036854775809")
    do test "N2vrer90m" (10000000000000000000000000000000000000000000000000000000000000000000000N = "10000000000000000000000000000000000000000000000000000000000000000000000")

end

module MappableNumericLiteralsZToString = begin

    module NumericLiteralZ =
        let FromZero() =  "0"
        let FromOne() = "1"
        let FromInt32(x:int32) =  string x
        let FromInt64(x:int64) =  string x
        let FromString(s:string) =  s

    do test "Zvrer90e" (0Z = "0")
    do test "Zvrer90f" (1Z = "1")
    do test "Zvrer90g" (-1Z = "-1")
    do test "Zvrer90h" (2Z = "2")
    do test "Zvrer90i" (2147483647Z = "2147483647")
    do test "Zvrer90j" (-2147483648Z = "-2147483648")
    do test "Zvrer90k" (2147483648Z = "2147483648")
    do test "Zvrer90m" (-2147483649Z = "-2147483649")
    do test "Zvrer90m" (9223372036854775807Z = "9223372036854775807")
    do test "Zvrer90m" (-9223372036854775808Z = "-9223372036854775808")
    do test "Zvrer90m" (9223372036854775808Z = "9223372036854775808")
    do test "Zvrer90m" (-9223372036854775809Z = "-9223372036854775809")
    do test "Zvrer90m" (10000000000000000000000000000000000000000000000000000000000000000000000Z = "10000000000000000000000000000000000000000000000000000000000000000000000")

end

module MappableNumericLiteralsQToString = begin

    module NumericLiteralQ =
        let FromZero() =  "0"
        let FromOne() = "1"
        let FromInt32(x:int32) =  string x
        let FromInt64(x:int64) =  string x
        let FromString(s:string) =  s

    do test "Qvrer90e" (0Q = "0")
    do test "Qvrer90f" (1Q = "1")
    do test "Qvrer90g" (-1Q = "-1")
    do test "Qvrer90h" (2Q = "2")
    do test "Qvrer90i" (2147483647Q = "2147483647")
    do test "Qvrer90j" (-2147483648Q = "-2147483648")
    do test "Qvrer90k" (2147483648Q = "2147483648")
    do test "Qvrer90m" (-2147483649Q = "-2147483649")
    do test "Qvrer90m" (9223372036854775807Q = "9223372036854775807")
    do test "Qvrer90m" (-9223372036854775808Q = "-9223372036854775808")
    do test "Qvrer90m" (9223372036854775808Q = "9223372036854775808")
    do test "Qvrer90m" (-9223372036854775809Q = "-9223372036854775809")
    do test "Qvrer90m" (10000000000000000000000000000000000000000000000000000000000000000000000Q = "10000000000000000000000000000000000000000000000000000000000000000000000")

end

module MappableNumericLiteralsRToString = begin

    module NumericLiteralR =
        let FromZero() =  "0"
        let FromOne() = "1"
        let FromInt32(x:int32) =  string x
        let FromInt64(x:int64) =  string x
        let FromString(s:string) =  s

    do test "Rvrer90e" (0R = "0")
    do test "Rvrer90f" (1R = "1")
    do test "Rvrer90g" (-1R = "-1")
    do test "Rvrer90h" (2R = "2")
    do test "Rvrer90i" (2147483647R = "2147483647")
    do test "Rvrer90j" (-2147483648R = "-2147483648")
    do test "Rvrer90k" (2147483648R = "2147483648")
    do test "Rvrer90m" (-2147483649R = "-2147483649")
    do test "Rvrer90m" (9223372036854775807R = "9223372036854775807")
    do test "Rvrer90m" (-9223372036854775808R = "-9223372036854775808")
    do test "Rvrer90m" (9223372036854775808R = "9223372036854775808")
    do test "Rvrer90m" (-9223372036854775809R = "-9223372036854775809")
    do test "Rvrer90m" (10000000000000000000000000000000000000000000000000000000000000000000000R = "10000000000000000000000000000000000000000000000000000000000000000000000")

end

module MappableNumericLiteralsIToString = begin

    module NumericLiteralI =
        let FromZero() =  "0"
        let FromOne() = "1"
        let FromInt32(x:int32) =  string x
        let FromInt64(x:int64) =  string x
        let FromString(s:string) =  s

    do test "Ivrer90e" (0I = "0")
    do test "Ivrer90f" (1I = "1")
    do test "Ivrer90g" (-1I = "-1")
    do test "Ivrer90h" (2I = "2")
    do test "Ivrer90i" (2147483647I = "2147483647")
    do test "Ivrer90j" (-2147483648I = "-2147483648")
    do test "Ivrer90k" (2147483648I = "2147483648")
    do test "Ivrer90m" (-2147483649I = "-2147483649")
    do test "Ivrer90m" (9223372036854775807I = "9223372036854775807")
    do test "Ivrer90m" (-9223372036854775808I = "-9223372036854775808")
    do test "Ivrer90m" (9223372036854775808I = "9223372036854775808")
    do test "Ivrer90m" (-9223372036854775809I = "-9223372036854775809")
    do test "Ivrer90m" (10000000000000000000000000000000000000000000000000000000000000000000000I = "10000000000000000000000000000000000000000000000000000000000000000000000")

end

module OperatorNamesAddressOf = begin

    let op_AddressOf (x:int) = string x
    do test "vrnoe09" (&3 = "3")

end

module OperatorNamesIntegerAddressOf = begin

    let op_IntegerAddressOf (x:int) = string x
    do test "vrnoe09" (&&3 = "3")

end
    
module OperatorBooleanAnd = begin

    let op_BooleanAnd (x:string) (y:string) = string x + string y
    do test "vrnoe09" ( ("3" && "4") =  "34")

end
    
module OperatorBooleanOr = begin

    let op_BooleanOr (x:string) (y:string) = string x + string y
    do test "vrnoe09" ( ("3" || "4") =  "34")

end


module StartInCOmments = begin
    //This works
    (*
    let a1 = ( * )
    *)
    let b1 = ()


    //This should work
    (*
    let a3 = (*)
    *)
    let b3 = ()
end


module PrecedenceOfTypeOperators = begin
    // Check :> operator has precedence lower than '|>'
    let _ : obj  = ([1] |> List.map id) :> obj // this is how it should parse
    let _ : obj = [1] |> List.map id :> obj
    // Check :?> operator has precedence lower than '|>'
    let _ : list<int> = ([1] |> box) :?> list<int>  // this is how it should parse
    let _ : list<int> = [1] |> box :?> list<int>   // this is the real test
    let _ : list<int> = [1] |> box :?> int list  // this is the real test

    // Check :> operator has LEFT precedence 
    let _ : obj  = (([1] |> List.map id) :> System.IComparable) :> obj // this is how it should parse
    let _ : obj = [1] |> List.map id :> System.IComparable :> obj  // this is the real test


    // Check :?> operator has LEFT precedence 
    let _ : list<int> = ([1] |> box) :?> System.IComparable :?> list<int>  // this is how it should parse
    let _ : list<int> = [1] |> box :?> System.IComparable :?> list<int>  // this is the real test
    let _ : list<int> = [1] |> box :?> System.IComparable :?> int list  // this is the real test

    // Check :> operator has precedence greater than ','
    let _, _ = (([1] |> List.map id) :> obj), (([1] |> List.map id) :> obj) // this is how it should parse
    let _, _ = [1] |> List.map id :> obj, [1] |> List.map id :> obj  // this is the real test
    // Check :?> operator has precedence greater than ','
    let _, _ = (([1] |> box) :?> list<int>), (([1] |> box) :?> list<int>) // this is how it should parse
    let _, _ = [1] |> box :?> list<int>, [1] |> box :?> list<int>  // this is the real test


    // Check :? operator has precedence lower than '++'
    let _ = 
       let (++) a b = box (List.length (a @ b))
       let (_ : bool)  = ([1] ++ [1]) :? int  // this is how it should parse
       let (_ : bool)  = [1] ++ [1] :? int  // this is the real test
       1

    // Check :? operator has precedence greater than '|>'
    let _ = 
       let (|>) a b = (a,b)
       let b1 = box 1
       let (_ : int list), (_ : bool) = [1] |> (b1 :? int) // this is how it should parse
       let (_ : int list), (_ : bool) = [1] |> b1 :? int  // this is the real test
       1



end


module MultiLineTypeParameterTests = 

    type C<'T,
           'U>() = 
         static let x =  1

    type C2<[<System.CLSCompliantAttribute(true)>] 'T,
            [<System.CLSCompliantAttribute(true)>] 'U>() = 
         static let x =  1

    type C3<[<System.CLSCompliantAttribute(true)>] 'T,
               [<System.CLSCompliantAttribute(true)>] 'U>() = 
         static let x =  1

    type C4<[<System.CLSCompliantAttribute(true)>] 'T1,
            [<System.CLSCompliantAttribute(true)>] 'T2,
            [<System.CLSCompliantAttribute(true)>] 'T3,
            [<System.CLSCompliantAttribute(true)>] 'T4,
            [<System.CLSCompliantAttribute(true)>] 'T5,
               [<System.CLSCompliantAttribute(true)>] 'U>() = 
         static let x =  1

    type C5<[<System.CLSCompliantAttribute(true)>] 'T1,
              [<System.CLSCompliantAttribute(true)>] 'T2,
                [<System.CLSCompliantAttribute(true)>] 'T3,
                  [<System.CLSCompliantAttribute(true)>] 'T4,
                      [<System.CLSCompliantAttribute(true)>] 'T5,
                      [<System.CLSCompliantAttribute(true)>] 'U>() = 
         static let x =  1

module AdhocIndentationTests = 

    let fffffffffffffffff x = 3
    let mutable z = 3
    let f0 = 
        z <- fffffffffffffffff (3,
                         3
                      )

    let f1() = 
         fffffffffffffffff (false &&
           true && 
           false)

    let f2() = 
         fffffffffffffffff (
           printf "hello"
           printf "hello"
           printf "hello"
           true)



module TypeApplicationDisambiguation = 
    let (||) a b = 1 // this gives a warning - ignore
    let (&&) a b = 1 // this gives a warning - ignore
    let (<) a b = 1 // this gives a warning - ignore
    let (>) a b = 1 // this gives a warning - ignore
    let (@) a b = 1
    let (@@) a b = 1
    let (^^) a b = 1
    let ( ** ) a b = 1
    let ( *** ) a b = 1
    let ( % ) a b = 1
    let ( %%% ) a b = 1
    let ( <<< ) a b = 1
    let ( >>> ) a b = 1
    let ( &&& ) a b = 1
    let ( => ) a b = 1
    let ( ? ) a b = 1
    
    let f0 x = id<int> // this is considered a type application
    
    // Check that the following expressions are NOT considered type applications
    let f1 x = x<x ||| x>x
    let f2 x = x<x &&& x>x
    let f3 x = x<x @ x>x
    let f3b x = x<x @@ x>x
    let f4 x = x<x ^^ x>x
 //   let f5 x = x<x = x>x
    let f6 x = x<x ** x>x
    let f7 x = x<x *** x>x
    let f8 x = x<x % x>x
    let f9 x = x<x %%% x>x
    let f10 x = x<x <<< x>x
    let f13 x = x<x ? x, x>x
    let f14 x = x<x &&& x>x
    let f16 x = x<x => x>x
    let f17 x = x<x || x>x
    let f18 x = x<x && x>x
    // We explicitly DO consider these to be type applications 
    //      f<x >>> x>x
    //      f<int>x
    //      f<x * x>x
    //      f<x , x>x
    //      f<x ^ x>x
    //      f<x ^- x>x
    //      f<x / x>x
    //      f<x -> x>x
    //      f<x # x>x
    //      f<x ' x>x

module MultiLineWhenInPatternMatchSyntaxTests = 
    let test1 x =
        match x with
        | _ when true &&
                 false -> 
                 let x = 1 
                 x


    let test2 x =
        match x with
        | _ when true &&
                 false ->    
         let x = 1 
         x



    let test3 x =
        match x with
        | _ when true &&
                 false ->    
                    1
        | _ when (false &&
                  true) ->   
             0
        | _ -> failwith "Shouldn't get here."

     
    type IntType = Negative | Zero | Positive

    let test x =
        match x with
        | _ when x = 0 -> 
         Zero
        | _ when x < 0 &&
                 x < 0 -> 
         Negative
        | _ when (x > 0 &&
                  x > 0) ->
             Positive
        | _ -> failwith "Shouldn't get here."


module PostiveWithWithoutSemicolonTests =

    type r = { a : int; b : int } 
    let r = { a = 1; b = 2 }

    let v1 = { r with a = 1
                      b = 3 } 
                     
    let v1b = { r with 
                    a = 1
                    b = 3 } 
                     
                     
                     
    let v2 = { r with a = 
                        let x = 1
                        x + x;
                      } 
    let v3 = {v1 with a=30
                      r.b=40}

    let v4 = {v1 with r.a=30
                      b=0}


module LeftAssignmentWithStructuredRightHandSide =

    let mutable x = 1

    let f1() = 
      x <- 
          let y = 1
          y + y

      x <- let y = 1
           y + y


      x <- while false do
             ()
           1 + 1

      x <- try 
             3
           with _ -> 2

      x <- try 
             3
           finally 
             ()

      x <- use x = { new System.IDisposable with member __.Dispose() = () }
           3

      x <- if true then () else ()
           4

      x <- 
          printfn "hello"
          2 + 2

module RecordExpressionsWithStructuredRightHandSide =
    let f3a() = 
      { contents = 
          printfn "hello"
          1 + 1 }

    let f3b() = 
      { contents = let y = 1
                   y + y }

    let f3c() = 
      { contents = while false do
                     ()
                   1 + 1 }

    let f3d() = 
      { contents = try 
                     3
                   with _ -> 
                     2 }

    let f3e() = 
      { contents = try 
                     3
                   finally () }

    let f3f() = 
      { contents = use x = { new System.IDisposable with member __.Dispose() = () }
                   3 }

    let f3g() = 
      { contents = if true then () else ()
                   3 }

module RecordExpressionsWithStructuredRightHandSide2 =

    type r = { contents : int; name : string }
    let f3a() = 
      { name = "1" 
        contents = 
          printfn "hello"
          1 + 1 }

    let f3b() = 
      { name = "1" 
        contents = let y = 1
                   y + y }

    let f3c() = 
      { name = "1" 
        contents = while false do
                     ()
                   1 + 1 }

    let f3d() = 
      { name = "1" 
        contents = try 
                     3
                   with _ -> 
                     2 }

    let f3e() = 
      { name = "1" 
        contents = try 
                     3
                   finally () }

    let f3f() = 
      { name = "1" 
        contents = use x = { new System.IDisposable with member __.Dispose() = () }
                   3 }

    let f3g() = 
      { name = "1" 
        contents = if true then () else ()
                   3 }

module RecordExpressionsWithStructuredRightHandSide3 =

    type r = { contents : int; name : string }
    let f3a() = 
      { contents = 
          printfn "hello"
          1 + 1
        name = "1" }

    let f3b() = 
      { contents = let y = 1
                   y + y
        name = "1" }

    let f3c() = 
      { contents = while false do
                     ()
                   1 + 1 
        name = "1"}

    let f3d() = 
      { contents = try 
                     3
                   with _ -> 
                     2
        name = "1" }

    let f3e() = 
      { contents = try 
                     3
                   finally ()
        name = "1" }

    let f3f() = 
      { contents = use x = { new System.IDisposable with member __.Dispose() = () }
                   3
        name = "1" }

    let f3g() = 
      { contents = if true then () else ()
                   3 
        name = "1" }
  
module ActualReproAndVariations = 
     // problem?? See feat binding...

    type fields = { docId  : int64;
                    rating : int;
                    qid:     int;
                    feat:    float[];
                  }

    let mkFields1 (cols:string[]) =
      {
        docId  = cols.[0] |> int64;
        rating = cols.[1] |> int32;
        qid    = cols.[2] |> int32;
        feat   = let arr = Array.create 25 0.0
                 for i = 0 to 25 do
                   arr.[i] <- (cols.[i+3] |> float)
                 arr                          
      }

    let mkFields2 (cols:string[]) =
      {
        docId  = cols.[0] |> int64;
        rating = cols.[1] |> int32;
        feat   = let arr = Array.create 25 0.0
                 for i = 0 to 25 do
                   arr.[i] <- (cols.[i+3] |> float)
                 arr                          
        qid    = cols.[2] |> int32;
      }

    let mkFields3 (cols:string[]) =
      {
        docId  = cols.[0] |> int64;
        rating = cols.[1] |> int32;
        feat   = 
           let arr = Array.create 25 0.0
           for i = 0 to 25 do
             arr.[i] <- (cols.[i+3] |> float)
           arr                          
        qid    = cols.[2] |> int32;
      }

    let mkFields4 (cols:string[]) =
      {
        docId  = 
          cols.[0] 
          |> int64;
        rating = 
          cols.[1] 
          |> int32;
        feat   
         = 
           let arr = Array.create 25 0.0
           for i = 0 to 25 do
             arr.[i] <- (cols.[i+3] |> float)
           arr                          
        qid    = 
          cols.[2] 
          |> int32;
      }

module MiscRecordTEsts = 
    let _ = 
        { contents = 
            printfn "hello"
            1 + 1 }

    let rrrrrr = ref 1
    let _ = 
        { rrrrrr with 
           contents = 
            let x = 1
            x + x }

module AdHocTests = 
    let f4() = 
      { contents = fun _ ->
          printfn "hello"
          1 + 1 }


    let f6() = 
        let fffffffffffffffffffffffff x = x
        let g1 () = fffffffffffffffffffffffff (
                     3)
        let g1 () = async { 
            let x = 2 + 1
            return x
        }
        ()
        
    let f7() = 
        let r1 = ref 1
        { r1 with contents = match 1 with 1 -> 2 | _ -> 3 }



    let f9 = function
      | [] -> 0
      | _ -> 1 
    let f10() = 
        let y = ref 0
        y :=
            let (b, _, _) = (1,2,3)
            b      

    let f12() = 
        let y = 0
        y ::
            let (b, _, _) = (1,2,3)
            [b]      

    let f12b() = 
        let y = ref [0]
        y := 
            1 ::
                let (b, _, _) = (1,2,3)
                [b]      

    let f13() = 
        let y = ref [0]
        y := 
            1 ::
             let (b, _, _) = (1,2,3)
             [b]      

    let f14() = 
        let y = ref [0]
        y := 
         1 ::
          let (b, _, _) = (1,2,3)
          [b]      
    
    let f15() = 
        let y = ref [0]
        y 
         := 
           1 ::
            let (b, _, _) = (1,2,3)
            [b]      

    let f16() = 
        let y = ref [0]
        y 
         := 
           1 
            ::
              let (b, _, _) = (1,2,3)
              [b]      
    
#if ALL_IN_ONE
let RUN() = !failures
#else
let aa =
  match !failures with 
  | false -> 
      stdout.WriteLine "Test Passed"
      System.IO.File.WriteAllText("test.ok","ok")
      exit 0
  | _ -> 
      stdout.WriteLine "Test Failed"
      exit 1
#endif