├── .gitignore ├── Code ├── 2015 │ ├── 001 │ │ ├── ListPair.ok │ │ ├── README.md │ │ ├── tests.cm │ │ └── tests.sml │ ├── 002 │ │ ├── Either.ok │ │ ├── Either.results │ │ ├── either.sig │ │ ├── either.sml │ │ ├── sources.cm │ │ ├── sources.mlb │ │ ├── tests.cm │ │ └── tests.sml │ ├── 003 │ │ ├── array.sig │ │ ├── array.sml │ │ ├── list-pair.sig │ │ ├── list-pair.sml │ │ ├── list.sig │ │ ├── list.sml │ │ ├── mono-array.fun │ │ ├── mono-array.sig │ │ ├── mono-vector.fun │ │ ├── mono-vector.sig │ │ ├── option.sig │ │ ├── option.sml │ │ ├── sources.cm │ │ ├── sources.mlb │ │ ├── string.sig │ │ ├── string.sml │ │ ├── vector.sig │ │ └── vector.sml │ ├── 004 │ │ ├── BUFFER.sig │ │ └── Buffer.sml │ ├── 005 │ │ ├── fn.sig │ │ ├── fn.sml │ │ ├── sources.cm │ │ └── sources.mlb │ └── 007 │ │ ├── Ref.ok │ │ ├── ref.sig │ │ ├── ref.sml │ │ ├── sources.cm │ │ ├── sources.mlb │ │ ├── tests.cm │ │ └── tests.sml ├── 2016 │ └── 001 │ │ ├── sources.cm │ │ ├── sources.mlb │ │ ├── word.sig │ │ └── word.sml ├── 2018 │ ├── 001 │ │ ├── CharBuffer.ok │ │ ├── mono-buffer.fun │ │ ├── mono-buffer.sig │ │ ├── mono-buffer.sml │ │ ├── sources.cm │ │ ├── sources.mlb │ │ ├── tests.cm │ │ └── tests.sml │ └── 002 │ │ ├── array-slice.sig │ │ ├── array-slice.sml │ │ ├── mono-array-slice.fun │ │ ├── mono-array-slice.sig │ │ ├── mono-vector-slice.fun │ │ ├── mono-vector-slice.sig │ │ ├── sources.cm │ │ ├── sources.mlb │ │ ├── tests.cm │ │ ├── tests.sml │ │ ├── vector-slice.sig │ │ └── vector-slice.sml ├── 2020 │ └── 001 │ │ ├── universal.sig │ │ └── universal.sml ├── example │ ├── Counter.ok │ ├── README.md │ ├── counter.sig │ ├── counter.sml │ ├── example-proposal.md │ ├── sources.cm │ ├── sources.mlb │ ├── tests.cm │ └── tests.sml └── testing │ ├── README.md │ ├── check.sml │ └── sources.cm └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # files generated by CM # 2 | ######################### 3 | 4 | **/.cm 5 | 6 | # OS generated files # 7 | ###################### 8 | .DS_Store 9 | .DS_Store? 10 | 11 | -------------------------------------------------------------------------------- /Code/2015/001/ListPair.ok: -------------------------------------------------------------------------------- 1 | ** Testing ListPair 2 | -- ListPair.appEq-1 ok 3 | -- ListPair.appEq-2 ok 4 | -- ListPair.appEq-3 ok 5 | -- ListPair.appEq-4 ok 6 | -- ListPair.appEq-5 ok 7 | -- ListPair.appEq-6 ok 8 | -- ListPair.appEq-7 ok 9 | -- ListPair.mapEq-1 ok 10 | -- ListPair.mapEq-2 ok 11 | -- ListPair.mapEq-3 ok 12 | -- ListPair.mapEq-4 ok 13 | -- ListPair.mapEq-5 ok 14 | -- ListPair.mapEq-6 ok 15 | -- ListPair.mapEq-7 ok 16 | -- ListPair.foldlEq-1 ok 17 | -- ListPair.foldlEq-2 ok 18 | -- ListPair.foldlEq-3 ok 19 | -- ListPair.foldlEq-4 ok 20 | -- ListPair.foldlEq-5 ok 21 | -- ListPair.foldlEq-6 ok 22 | -- ListPair.foldlEq-7 ok 23 | -- ListPair.foldrEq-1 ok 24 | -- ListPair.foldrEq-2 ok 25 | -- ListPair.foldrEq-3 ok 26 | -- ListPair.foldrEq-4 ok 27 | -- ListPair.foldrEq-5 ok 28 | -- ListPair.foldrEq-6 ok 29 | -- ListPair.foldrEq-7 ok 30 | ** Summary for ListPair: 28 tests; 28 passed 31 | -------------------------------------------------------------------------------- /Code/2015/001/README.md: -------------------------------------------------------------------------------- 1 | This directory contains tests for the SML Basis Library proposal 2 | [2015-001](https://github.com/SMLFamily/BasisLibrary/wiki/2015-001-Correction-to-ListPair). 3 | Which is a clarification of the ordering of effects for some of the `ListPair` combinators. 4 | The tests check that an implementation does indeed respect the specified order. 5 | -------------------------------------------------------------------------------- /Code/2015/001/tests.cm: -------------------------------------------------------------------------------- 1 | (* tests.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for testing the SML Basis Library Proposal 2015-001. 26 | *) 27 | 28 | Library 29 | 30 | structure Tests 31 | 32 | is 33 | 34 | $/basis.cm 35 | ../../testing/sources.cm 36 | 37 | tests.sml 38 | -------------------------------------------------------------------------------- /Code/2015/001/tests.sml: -------------------------------------------------------------------------------- 1 | (* tests.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Test suite for SML Basis Library Proposal 2015-001. 26 | *) 27 | 28 | structure Tests = 29 | struct 30 | 31 | local 32 | open Check 33 | open ListPair 34 | exception TestExn 35 | fun expectUnequal tst = expectExn (fn UnequalLengths => true | _ => false) tst 36 | fun expectTestExn tst = expectExn (fn TestExn => true | _ => false) tst 37 | fun raiseTestExn _ = raise TestExn 38 | val expectIntPairList = 39 | expectList (fn ((x1 : int, y1 : int), (x2, y2)) => (x1 = x2) andalso (y1 = y2)) 40 | val l1 = [1, 2, 3] 41 | val l2 = [1, 2, 3, 4] 42 | fun consp (x, y, xys) = (x, y) :: xys 43 | in 44 | 45 | fun run () = runSuite "ListPair" (fn test => ( 46 | (* test appEq *) 47 | test "appEq-1" ( 48 | expectIntPairList [(3, 3), (2, 2), (1, 1)] (withRef [] (fn r => 49 | appEq (fn (a, b) => r := (a, b) :: !r) (l1, l1)))); 50 | test "appEq-2" ( 51 | expectUnequal (withRef [] (fn r => 52 | appEq (fn (a, b) => r := (a, b) :: !r) (l1, l2)))); 53 | test "appEq-3" ( 54 | expectUnequal (withRef [] (fn r => 55 | appEq (fn (a, b) => r := (a, b) :: !r) (l2, l1)))); 56 | test "appEq-4" (expectTestExn (fn () => appEq raiseTestExn (l1, l2))); 57 | test "appEq-5" (expectTestExn (fn () => appEq raiseTestExn (l2, l1))); 58 | test "appEq-6" (expectUnequal (fn () => appEq raiseTestExn ([], l2))); 59 | test "appEq-7" (expectUnequal (fn () => appEq raiseTestExn (l2, []))); 60 | (* test mapEq *) 61 | test "mapEq-1" (expectIntPairList [(1, 1), (2, 2), (3, 3)] 62 | (fn () => mapEq (fn xy => xy) (l1, l1))); 63 | test "mapEq-2" (expectUnequal (fn () => mapEq (fn xy => xy) (l1, l2))); 64 | test "mapEq-3" (expectUnequal (fn () => mapEq (fn xy => xy) (l2, l1))); 65 | test "mapEq-4" (expectTestExn (fn () => mapEq raiseTestExn (l1, l2))); 66 | test "mapEq-5" (expectTestExn (fn () => mapEq raiseTestExn (l2, l1))); 67 | test "mapEq-6" (expectUnequal (fn () => mapEq raiseTestExn ([], l2))); 68 | test "mapEq-7" (expectUnequal (fn () => mapEq raiseTestExn (l2, []))); 69 | (* test foldlEq *) 70 | test "foldlEq-1" ( 71 | expectIntPairList [(3, 3), (2, 2), (1, 1)] (fn () => foldlEq consp [] (l1, l1))); 72 | test "foldlEq-2" (expectUnequal (fn () => foldlEq consp [] (l1, l2))); 73 | test "foldlEq-3" (expectUnequal (fn () => foldlEq consp [] (l2, l1))); 74 | test "foldlEq-4" (expectTestExn (fn () => foldlEq raiseTestExn [] (l1, l2))); 75 | test "foldlEq-5" (expectTestExn (fn () => foldlEq raiseTestExn [] (l2, l1))); 76 | test "foldlEq-6" (expectUnequal (fn () => foldlEq raiseTestExn [] ([], l2))); 77 | test "foldlEq-7" (expectUnequal (fn () => foldlEq raiseTestExn [] (l2, []))); 78 | (* test foldrEq *) 79 | test "foldrEq-1" ( 80 | expectIntPairList [(1, 1), (2, 2), (3, 3)] (fn () => foldrEq consp [] (l1, l1))); 81 | test "foldrEq-2" (expectUnequal (fn () => foldrEq consp [] (l1, l2))); 82 | test "foldrEq-3" (expectUnequal (fn () => foldrEq consp [] (l2, l1))); 83 | test "foldrEq-4" (expectUnequal (fn () => foldrEq raiseTestExn [] (l1, l2))); 84 | test "foldrEq-5" (expectUnequal (fn () => foldrEq raiseTestExn [] (l2, l1))); 85 | test "foldrEq-6" (expectUnequal (fn () => foldrEq raiseTestExn [] ([], l2))); 86 | test "foldrEq-7" (expectUnequal (fn () => foldrEq raiseTestExn [] (l2, []))); 87 | ())) 88 | 89 | end (* local *) 90 | 91 | end 92 | -------------------------------------------------------------------------------- /Code/2015/002/Either.ok: -------------------------------------------------------------------------------- 1 | ** Testing Either 2 | -- Either.isLeft-1 ok 3 | -- Either.isLeft-2 ok 4 | -- Either.isRight-1 ok 5 | -- Either.isRight-2 ok 6 | -- Either.asLeft-1 ok 7 | -- Either.asLeft-2 ok 8 | -- Either.asRight-1 ok 9 | -- Either.asRight-2 ok 10 | -- Either.map-1 ok 11 | -- Either.map-2 ok 12 | -- Either.map-3 ok 13 | -- Either.map-4 ok 14 | -- Either.app-1 ok 15 | -- Either.app-2 ok 16 | -- Either.app-3 ok 17 | -- Either.app-4 ok 18 | -- Either.fold-1 ok 19 | -- Either.fold-2 ok 20 | -- Either.fold-3 ok 21 | -- Either.fold-4 ok 22 | -- Either.proj-1 ok 23 | -- Either.proj-2 ok 24 | -- Either.partition-1 ok 25 | -- Either.partition-2 ok 26 | -- Either.partition-3 ok 27 | -- Either.partition-4 ok 28 | -- Either.partition-5 ok 29 | -- Either.partition-6 ok 30 | ** Summary for Either: 28 tests; 28 passed 31 | -------------------------------------------------------------------------------- /Code/2015/002/Either.results: -------------------------------------------------------------------------------- 1 | ** Testing Either 2 | -- Either.isLeft-1 ok 3 | -- Either.isLeft-2 ok 4 | -- Either.isRight-1 ok 5 | -- Either.isRight-2 ok 6 | -- Either.asLeft-1 ok 7 | -- Either.asLeft-2 ok 8 | -- Either.asRight-1 ok 9 | -- Either.asRight-2 ok 10 | -- Either.map-1 ok 11 | -- Either.map-2 ok 12 | -- Either.map-3 ok 13 | -- Either.map-4 ok 14 | -- Either.mapLeft-1 ok 15 | -- Either.mapLeft-2 ok 16 | -- Either.mapRight-1 ok 17 | -- Either.mapRight-2 ok 18 | -- Either.app-1 ok 19 | -- Either.app-2 ok 20 | -- Either.app-3 ok 21 | -- Either.app-4 ok 22 | -- Either.appLeft-1 ok 23 | -- Either.appLeft-2 ok 24 | -- Either.appRight-1 ok 25 | -- Either.appRight-2 ok 26 | -- Either.fold-1 ok 27 | -- Either.fold-2 ok 28 | -- Either.fold-3 ok 29 | -- Either.fold-4 ok 30 | -- Either.proj-1 ok 31 | -- Either.proj-2 ok 32 | -- Either.partition-1 ok 33 | -- Either.partition-2 ok 34 | -- Either.partition-3 ok 35 | -- Either.partition-4 ok 36 | -- Either.partition-5 ok 37 | -- Either.partition-6 ok 38 | ** Summary for Either: 36 tests; 36 passed 39 | -------------------------------------------------------------------------------- /Code/2015/002/either.sig: -------------------------------------------------------------------------------- 1 | (* either.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-002. 26 | *) 27 | 28 | signature EITHER = 29 | sig 30 | 31 | datatype ('left, 'right) either = INL of 'left | INR of 'right 32 | 33 | val isLeft : ('left, 'right) either -> bool 34 | val isRight : ('left, 'right) either -> bool 35 | 36 | val asLeft : ('left, 'right) either -> 'left option 37 | val asRight : ('left, 'right) either -> 'right option 38 | 39 | val map : ('ldom -> 'lrng) * ('rdom -> 'rrng) 40 | -> ('ldom, 'rdom) either 41 | -> ('lrng, 'rrng) either 42 | 43 | val app : ('left -> unit) * ('right -> unit) 44 | -> ('left, 'right) either 45 | -> unit 46 | 47 | val fold : ('left * 'b -> 'b) * ('right * 'b -> 'b) 48 | -> 'b -> ('left, 'right) either -> 'b 49 | 50 | val proj : ('a, 'a) either -> 'a 51 | 52 | val partition : (('left, 'right) either) list -> ('left list * 'right list) 53 | 54 | (* added 2016-08-10 *) 55 | val mapLeft : ('ldom -> 'lrng) -> ('ldom, 'rdom) either -> ('lrng, 'rdom) either 56 | val mapRight : ('rdom -> 'rrng) -> ('ldom, 'rdom) either -> ('ldom, 'rrng) either 57 | 58 | val appLeft : ('left -> unit) -> ('left, 'right) either -> unit 59 | val appRight : ('right -> unit) -> ('left, 'right) either -> unit 60 | 61 | end 62 | -------------------------------------------------------------------------------- /Code/2015/002/either.sml: -------------------------------------------------------------------------------- 1 | (* either.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-002. 26 | *) 27 | 28 | structure Either : EITHER = 29 | struct 30 | 31 | datatype ('left, 'right) either = INL of 'left | INR of 'right 32 | 33 | fun isLeft (INL _) = true 34 | | isLeft (INR _) = false 35 | fun isRight (INL _) = false 36 | | isRight (INR _) = true 37 | 38 | fun asLeft (INL x) = SOME x 39 | | asLeft (INR _) = NONE 40 | fun asRight (INL _) = NONE 41 | | asRight (INR x) = SOME x 42 | 43 | fun map (fl, fr) sum = (case sum 44 | of INL x => INL(fl x) 45 | | INR x => INR(fr x) 46 | (* end case *)) 47 | 48 | fun mapLeft fl sum = map (fl, fn x => x) sum 49 | fun mapRight fr sum = map (fn x => x, fr) sum 50 | 51 | fun app (fl, fr) sum = (case sum 52 | of INL x => fl x 53 | | INR x => fr x 54 | (* end case *)) 55 | 56 | fun appLeft fl sum = app (fl, fn x => ()) sum 57 | fun appRight fr sum = app (fn x => (), fr) sum 58 | 59 | fun fold (fl, fr) init sum = (case sum 60 | of INL x => fl (x, init) 61 | | INR x => fr (x, init) 62 | (* end case *)) 63 | 64 | fun proj (INL x) = x 65 | | proj (INR x) = x 66 | 67 | fun partition sums = let 68 | fun lp ([], ls, rs) = (List.rev ls, List.rev rs) 69 | | lp ((INL x)::sums, ls, rs) = lp (sums, x::ls, rs) 70 | | lp ((INR x)::sums, ls, rs) = lp (sums, ls, x::rs) 71 | in 72 | lp (sums, [], []) 73 | end 74 | 75 | end 76 | -------------------------------------------------------------------------------- /Code/2015/002/sources.cm: -------------------------------------------------------------------------------- 1 | (* sources.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for SML Basis Library Proposal 2015-002. 26 | *) 27 | 28 | Library 29 | 30 | signature EITHER 31 | 32 | structure Either 33 | 34 | is 35 | 36 | (* use the original Basis to compile this proposal *) 37 | #if ((SMLNJ_VERSION = 110) andalso (SMLNJ_MINOR_VERSION >= 79)) 38 | $/basis-2004.cm 39 | #else 40 | $/basis.cm 41 | #endif 42 | 43 | either.sig 44 | either.sml 45 | -------------------------------------------------------------------------------- /Code/2015/002/sources.mlb: -------------------------------------------------------------------------------- 1 | (* sources.mlb 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * MLB file for SML Basis Library Proposal 2015-002. 26 | *) 27 | 28 | local 29 | 30 | $(SML_LIB)/basis/basis.mlb 31 | 32 | either.sig 33 | either.sml 34 | 35 | in 36 | 37 | signature EITHER 38 | 39 | structure Either 40 | 41 | end 42 | -------------------------------------------------------------------------------- /Code/2015/002/tests.cm: -------------------------------------------------------------------------------- 1 | (* tests.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for testing the reference implementation of 26 | * SML Basis Library Proposal 2015-002. 27 | *) 28 | 29 | Library 30 | 31 | structure Tests 32 | 33 | signature EITHER 34 | structure Either 35 | 36 | is 37 | 38 | $/basis.cm 39 | ../../testing/sources.cm 40 | 41 | tests.sml 42 | 43 | either.sig 44 | either.sml 45 | -------------------------------------------------------------------------------- /Code/2015/002/tests.sml: -------------------------------------------------------------------------------- 1 | (* tests.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Test suite for SML Basis Library Proposal 2015-002. 26 | *) 27 | 28 | structure Tests = 29 | struct 30 | 31 | local 32 | open Check 33 | open Either 34 | fun ident x = x 35 | val itos = Int.toString 36 | in 37 | 38 | fun run () = runSuite "Either" (fn test => ( 39 | (* test isLeft *) 40 | test "isLeft-1" (expectTrue (fn () => isLeft (INL 1))); 41 | test "isLeft-2" (expectFalse (fn () => isLeft (INR 1))); 42 | (* test isRight *) 43 | test "isRight-1" (expectTrue (fn () => isRight (INR 1))); 44 | test "isRight-2" (expectFalse (fn () => isRight (INL 1))); 45 | (* test asLeft *) 46 | test "asLeft-1" (expectSome' 1 (fn () => asLeft (INL 1))); 47 | test "asLeft-2" (expectNone (fn () => asLeft (INR 1))); 48 | (* test asRight *) 49 | test "asRight-1" (expectSome' 1 (fn () => asRight (INR 1))); 50 | test "asRight-2" (expectNone (fn () => asRight (INL 1))); 51 | (* test map *) 52 | test "map-1" (expectVal' (INL "17") (fn () => map (itos, itos) (INL 17))); 53 | test "map-2" (expectVal' (INR "17") (fn () => map (itos, itos) (INR 17))); 54 | test "map-3" (expectVal' (INL 17) (fn () => map (ident, itos) (INL 17))); 55 | test "map-4" (expectVal' (INR "17") (fn () => map (ident, itos) (INR 17))); 56 | (* test mapLeft *) 57 | test "mapLeft-1" (expectVal' (INL "17") (fn () => mapLeft itos (INL 17))); 58 | test "mapLeft-2" (expectVal' (INR 17) (fn () => mapLeft itos (INR 17))); 59 | (* test mapRight *) 60 | test "mapRight-1" (expectVal' (INL 17) (fn () => mapRight itos (INL 17))); 61 | test "mapRight-2" (expectVal' (INR "17") (fn () => mapRight itos (INR 17))); 62 | (* test app *) 63 | test "app-1" (expectVal' "17" 64 | (withRef "" (fn r => app (fn x => r := itos x, fn x => r := itos x) (INL 17)))); 65 | test "app-2" (expectVal' "17" 66 | (withRef "" (fn r => app (fn x => r := itos x, fn x => r := itos x) (INR 17)))); 67 | test "app-3" (expectVal' "16" 68 | (withRef "" (fn r => app (fn x => r := itos(x-1), fn x => r := itos(x+1)) (INL 17)))); 69 | test "app-4" (expectVal' "18" 70 | (withRef "" (fn r => app (fn x => r := itos(x-1), fn x => r := itos(x+1)) (INR 17)))); 71 | (* test appLeft *) 72 | test "appLeft-1" (expectVal' "17" 73 | (withRef "%%" (fn r => appLeft (fn x => r := itos x) (INL 17)))); 74 | test "appLeft-2" (expectVal' "%%" 75 | (withRef "%%" (fn r => appLeft (fn x => r := itos x) (INR 17)))); 76 | (* test appRight *) 77 | test "appRight-1" (expectVal' "%%" 78 | (withRef "%%" (fn r => appRight (fn x => r := itos(x+1)) (INL 17)))); 79 | test "appRight-2" (expectVal' "18" 80 | (withRef "%%" (fn r => appRight (fn x => r := itos(x+1)) (INR 17)))); 81 | (* test fold *) 82 | test "fold-1" (expectInt 16 (fn () => fold (Int.-, Int.+) 1 (INL 17))); 83 | test "fold-2" (expectInt 18 (fn () => fold (Int.-, Int.+) 1 (INR 17))); 84 | test "fold-3" (expectInt 18 (fn () => fold (Int.+, Int.-) 1 (INL 17))); 85 | test "fold-4" (expectInt 16 (fn () => fold (Int.+, Int.-) 1 (INR 17))); 86 | (* test proj *) 87 | test "proj-1" (expectInt 17 (fn () => proj (INL 17))); 88 | test "proj-2" (expectInt 42 (fn () => proj (INR 42))); 89 | (* test partition *) 90 | test "partition-1" (expect (fn ([], []) => true | _ => false) (fn () => partition [])); 91 | test "partition-2" (expect (fn ([17], []) => true | _ => false) (fn () => partition [INL 17])); 92 | test "partition-3" (expect (fn ([], [42]) => true | _ => false) (fn () => partition [INR 42])); 93 | test "partition-4" (expect (fn ([17], [42]) => true | _ => false) (fn () => partition [INL 17, INR 42])); 94 | test "partition-5" (expect (fn ([42], [17]) => true | _ => false) (fn () => partition [INR 17, INL 42])); 95 | test "partition-6" (expect 96 | (fn ([13, 42], [17, 99]) => true | _ => false) 97 | (fn () => partition [INL 13, INR 17, INR 99, INL 42])); 98 | ())) 99 | 100 | end (* local *) 101 | 102 | end 103 | -------------------------------------------------------------------------------- /Code/2015/003/array.sig: -------------------------------------------------------------------------------- 1 | (* array.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | signature ARRAY_EXT = 29 | sig 30 | 31 | include ARRAY 32 | 33 | val toList : 'a array -> 'a list 34 | 35 | val fromVector : 'a vector -> 'a array 36 | val toVector : 'a array -> 'a vector 37 | 38 | end 39 | -------------------------------------------------------------------------------- /Code/2015/003/array.sml: -------------------------------------------------------------------------------- 1 | (* array.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | structure ArrayExt : ARRAY_EXT = 29 | struct 30 | 31 | open Array 32 | 33 | fun toList arr = List.tabulate(length arr, fn i => sub(arr, i)) 34 | 35 | fun fromVector vec = tabulate (Vector.length vec, fn i => Vector.sub(vec, i)) 36 | 37 | val toVector = vector 38 | 39 | end 40 | -------------------------------------------------------------------------------- /Code/2015/003/list-pair.sig: -------------------------------------------------------------------------------- 1 | (* list-pair.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | signature LIST_PAIR_EXT = 29 | sig 30 | 31 | include LIST_PAIR 32 | 33 | val appi : (int * 'a * 'b -> unit) -> 'a list * 'b list -> unit 34 | val appiEq : (int * 'a * 'b -> unit) -> 'a list * 'b list -> unit 35 | val mapi : (int * 'a * 'b -> 'c) -> 'a list * 'b list -> 'c list 36 | val mapiEq : (int * 'a * 'b -> 'c) -> 'a list * 'b list -> 'c list 37 | val mapPartial : ('a * 'b -> 'c option) -> 'a list * 'b list -> 'c list 38 | val mapPartialEq : ('a * 'b -> 'c option) -> 'a list * 'b list -> 'c list 39 | val mapPartiali : (int * 'a * 'b -> 'c option) -> 'a list * 'b list -> 'c list 40 | val mapPartialiEq : (int * 'a * 'b -> 'c option) -> 'a list * 'b list -> 'c list 41 | val foldli : (int * 'a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c 42 | val foldliEq : (int * 'a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c 43 | val foldri : (int * 'a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c 44 | val foldriEq : (int * 'a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c 45 | 46 | (* added 2015-10-24 *) 47 | val unzipMap : ('a -> 'b * 'c) -> 'a list -> 'b list * 'c list 48 | val unzipMapi : (int * 'a -> 'b * 'c) -> 'a list -> 'b list * 'c list 49 | 50 | (* added 2015-10-27 *) 51 | val find : ('a * 'b -> bool) -> 'a list * 'b list -> ('a * 'b) option 52 | val findi : (int * 'a * 'b -> bool) -> 'a list * 'b list -> (int * 'a * 'b) option 53 | 54 | end 55 | -------------------------------------------------------------------------------- /Code/2015/003/list-pair.sml: -------------------------------------------------------------------------------- 1 | (* list-pair.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | structure ListPairExt : LIST_PAIR_EXT = 29 | struct 30 | 31 | open ListPair 32 | 33 | fun appi f (l1, l2) = let 34 | fun appf (i, x::xs, y::ys) = (f(i, x, y); appf(i+1, xs, ys)) 35 | | appf _ = () 36 | in 37 | appf (0, l1, l2) 38 | end 39 | 40 | fun appiEq f (l1, l2) = let 41 | fun appf (i, x::xs, y::ys) = (f(i, x, y); appf(i+1, xs, ys)) 42 | | appf (_, [], []) = () 43 | | appf _ = raise UnequalLengths 44 | in 45 | appf (0, l1, l2) 46 | end 47 | 48 | fun mapi f (l1, l2) = let 49 | fun mapf (i, x::xs, y::ys, zs) = (mapf(i+1, xs, ys, f(i, x, y) :: zs)) 50 | | mapf (_, _, _, zs) = List.rev zs 51 | in 52 | mapf (0, l1, l2, []) 53 | end 54 | 55 | fun mapiEq f (l1, l2) = let 56 | fun mapf (i, x::xs, y::ys, zs) = (mapf(i+1, xs, ys, f(i, x, y) :: zs)) 57 | | mapf (_, [], [], zs) = List.rev zs 58 | | mapf _ = raise UnequalLengths 59 | in 60 | mapf (0, l1, l2, []) 61 | end 62 | 63 | fun mapPartial f (l1, l2) = let 64 | fun mapf (x::xs, y::ys, acc) = (case f(x, y) 65 | of SOME z => mapf(xs, ys, z::acc) 66 | | NONE => mapf(xs, ys, acc) 67 | (* end case *)) 68 | | mapf (_, _, acc) = List.rev acc 69 | in 70 | mapf (l1, l2, []) 71 | end 72 | 73 | fun mapPartialEq f (l1, l2) = let 74 | fun mapf (x::xs, y::ys, acc) = (case f(x, y) 75 | of SOME z => mapf(xs, ys, z::acc) 76 | | NONE => mapf(xs, ys, acc) 77 | (* end case *)) 78 | | mapf ([], [], acc) = List.rev acc 79 | | mapf _ = raise UnequalLengths 80 | in 81 | mapf (l1, l2, []) 82 | end 83 | 84 | fun mapPartiali f (l1, l2) = let 85 | fun mapf (i, x::xs, y::ys, acc) = (case f(i, x, y) 86 | of SOME z => mapf(i+1, xs, ys, z::acc) 87 | | NONE => mapf(i+1, xs, ys, acc) 88 | (* end case *)) 89 | | mapf (_, _, _, acc) = List.rev acc 90 | in 91 | mapf (0, l1, l2, []) 92 | end 93 | 94 | fun mapPartialiEq f (l1, l2) = let 95 | fun mapf (i, x::xs, y::ys, acc) = (case f(i, x, y) 96 | of SOME z => mapf(i+1, xs, ys, z::acc) 97 | | NONE => mapf(i+1, xs, ys, acc) 98 | (* end case *)) 99 | | mapf (_, [], [], acc) = List.rev acc 100 | | mapf _ = raise UnequalLengths 101 | in 102 | mapf (0, l1, l2, []) 103 | end 104 | 105 | fun foldli f init (l1, l2) = let 106 | fun foldf (i, x::xs, y::ys, acc) = foldf (i+1, xs, ys, f(i, x, y, acc)) 107 | | foldf (_, _, _, acc) = acc 108 | in 109 | foldf (0, l1, l2, init) 110 | end 111 | 112 | fun foldliEq f init (l1, l2) = let 113 | fun foldf (i, x::xs, y::ys, acc) = foldf (i+1, xs, ys, f(i, x, y, acc)) 114 | | foldf (_, [], [], acc) = acc 115 | | foldf _ = raise UnequalLengths 116 | in 117 | foldf (0, l1, l2, init) 118 | end 119 | 120 | fun foldri f init (l1, l2) = let 121 | fun lp (i, x::xs, y::ys) = f (i, x, y, lp (i+1, xs, ys)) 122 | | lp (_, _, _) = init 123 | in 124 | lp (0, l1, l2) 125 | end 126 | 127 | fun foldriEq f init (l1, l2) = let 128 | fun lp (i, x::xs, y::ys) = f (i, x, y, lp (i+1, xs, ys)) 129 | | lp (_, [], []) = init 130 | | lp _ = raise UnequalLengths 131 | in 132 | lp (0, l1, l2) 133 | end 134 | 135 | fun unzipMap f l = let 136 | fun mapf ([], l1, l2) = (List.rev l1, List.rev l2) 137 | | mapf (x::xs, l1, l2) = let 138 | val (y1, y2) = f x 139 | in 140 | mapf (xs, y1::l1, y2::l2) 141 | end 142 | in 143 | mapf (l, [], []) 144 | end 145 | 146 | fun unzipMapi f l = let 147 | fun mapf ([], _, l1, l2) = (List.rev l1, List.rev l2) 148 | | mapf (x::xs, i, l1, l2) = let 149 | val (y1, y2) = f (i, x) 150 | in 151 | mapf (xs, i+1, y1::l1, y2::l2) 152 | end 153 | in 154 | mapf (l, 0, [], []) 155 | end 156 | 157 | fun find pred = let 158 | fun findp ([], _) = NONE 159 | | findp (_, []) = NONE 160 | | findp (x::xs, y::ys) = if pred(x, y) then SOME(x, y) else findp(xs, ys) 161 | in 162 | findp 163 | end 164 | 165 | fun findi pred (l1, l2) = let 166 | fun findp (_, [], _) = NONE 167 | | findp (_, _, []) = NONE 168 | | findp (i, x::xs, y::ys) = if pred(i, x, y) 169 | then SOME(i, x, y) 170 | else findp(i+1, xs, ys) 171 | in 172 | findp (0, l1, l2) 173 | end 174 | 175 | end 176 | -------------------------------------------------------------------------------- /Code/2015/003/list.sig: -------------------------------------------------------------------------------- 1 | (* list.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | signature LIST_EXT = 29 | sig 30 | 31 | include LIST 32 | 33 | val unfoldl : ('strm -> ('a * 'strm) option) -> 'strm -> 'a list 34 | val unfoldr : ('strm -> ('a * 'strm) option) -> 'strm -> 'a list 35 | 36 | val reduce : ('a * 'a -> 'a) -> 'a -> 'a list -> 'a 37 | 38 | val appi : (int * 'a -> unit) -> 'a list -> unit 39 | val mapi : (int * 'a -> 'b) -> 'a list -> 'b list 40 | val mapPartiali : (int * 'a -> 'b option) -> 'a list -> 'b list 41 | val foldli : (int * 'a * 'b -> 'b) -> 'b -> 'a list -> 'b 42 | val foldri : (int * 'a * 'b -> 'b) -> 'b -> 'a list -> 'b 43 | val findi : (int * 'a -> bool) -> 'a list -> (int * 'a) option 44 | 45 | val revMap : ('a -> 'b) -> 'a list -> 'b list 46 | val revMapi : (int * 'a -> 'b) -> 'a list -> 'b list 47 | val revMapPartial : ('a -> 'b option) -> 'a list -> 'b list 48 | val revMapPartiali : (int * 'a -> 'b option) -> 'a list -> 'b list 49 | 50 | val concatMap : ('a -> 'b list) -> 'a list -> 'b list 51 | val concatMapi : (int * 'a -> 'b list) -> 'a list -> 'b list 52 | 53 | val foldMapl : ('b * 'c -> 'c) -> ('a -> 'b) -> 'c -> 'a list -> 'c 54 | val foldMapr : ('b * 'c -> 'c) -> ('a -> 'b) -> 'c -> 'a list -> 'c 55 | 56 | val splitAt : 'a list * int -> 'a list * 'a list 57 | val update : 'a list * int * 'a -> 'a list 58 | val sub : 'a list * int -> 'a 59 | 60 | end 61 | -------------------------------------------------------------------------------- /Code/2015/003/list.sml: -------------------------------------------------------------------------------- 1 | (* list.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | structure ListExt : LIST_EXT = 29 | struct 30 | 31 | open List 32 | 33 | fun unfoldr getNext strm = let 34 | fun lp (strm, items) = (case getNext strm 35 | of NONE => items 36 | | SOME(x, rest) => lp(rest, x::items) 37 | (* end case *)) 38 | in 39 | lp (strm, []) 40 | end 41 | 42 | fun unfoldl getNext strm = rev(unfoldr getNext strm) 43 | 44 | fun reduce f id [] = id 45 | | reduce f _ (x::xs) = foldl f x xs 46 | 47 | fun appi f l = let 48 | fun appf (_, []) = () 49 | | appf (i, x::xs) = (f(i, x); appf(i+1, xs)) 50 | in 51 | appf (0, l) 52 | end 53 | 54 | fun mapi f l = let 55 | fun mapf (_, []) = [] 56 | | mapf (i, x::xs) = (f(i, x) :: mapf(i+1, xs)) 57 | in 58 | mapf (0, l) 59 | end 60 | 61 | fun mapPartiali f l = let 62 | fun mapf (_, []) = [] 63 | | mapf (i, x::xs) = (case f(i, x) 64 | of NONE => mapf (i+1, xs) 65 | | SOME y => y :: mapf(i+1, xs) 66 | (* end case *)) 67 | in 68 | mapf (0, l) 69 | end 70 | 71 | fun foldli f init l = let 72 | fun lp (_, [], acc) = acc 73 | | lp (i, x::xs, acc) = lp (i+1, xs, f(i, x, acc)) 74 | in 75 | lp (0, l, init) 76 | end 77 | 78 | fun foldri f init l = let 79 | fun lp (_, []) = init 80 | | lp (i, x::xs) = f (i, x, lp (i+1, xs)) 81 | in 82 | lp (0, l) 83 | end 84 | 85 | fun findi f l = let 86 | fun lp (_, []) = NONE 87 | | lp (i, x::xs) = if (f(i, x)) then SOME(i, x) else lp(i+1, xs) 88 | in 89 | lp (0, l) 90 | end 91 | 92 | fun revMap f l = let 93 | fun mapf (x::xs, ys) = mapf (xs, f x :: ys) 94 | | mapf ([], ys) = ys 95 | in 96 | mapf (l, []) 97 | end 98 | fun revMapi f l = let 99 | fun mapf (i, x::xs, ys) = mapf (i+1, xs, f(i, x) :: ys) 100 | | mapf (_, [], ys) = ys 101 | in 102 | mapf (0, l, []) 103 | end 104 | 105 | fun revMapPartial f l = rev (mapPartial f l) 106 | fun revMapPartiali f l = rev (mapPartiali f l) 107 | 108 | fun concatMap f l = concat(map f l) 109 | fun concatMapi f l = concat(mapi f l) 110 | 111 | fun foldMapl reduceFn mapFn init l = 112 | foldl (fn (x, acc) => reduceFn(mapFn x, acc)) init l 113 | 114 | fun foldMapr reduceFn mapFn init l = 115 | foldr (fn (x, acc) => reduceFn(mapFn x, acc)) init l 116 | 117 | fun splitAt (xs, i) = (List.take (xs, i), List.drop (xs, i)) 118 | 119 | fun update (xs, i, y) = let 120 | fun upd (x::xs, 0, prefix) = revAppend(prefix, y::xs) 121 | | upd ([], _, _) = raise Subscript 122 | | upd (x::xs, i, prefix) = upd (xs, i-1, x::prefix) 123 | in 124 | if (i < 0) then raise Subscript else upd(xs, i, []) 125 | end 126 | 127 | val sub = nth 128 | 129 | end 130 | -------------------------------------------------------------------------------- /Code/2015/003/mono-array.fun: -------------------------------------------------------------------------------- 1 | (* mono-array.fun 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | functor MonoArrayExtFn ( 29 | structure A : MONO_ARRAY 30 | structure V : MONO_VECTOR 31 | sharing type A.elem = V.elem 32 | sharing type A.vector = V.vector 33 | ) : MONO_ARRAY_EXT = 34 | struct 35 | 36 | open A 37 | 38 | fun toList arr = List.tabulate(length arr, fn i => sub(arr, i)) 39 | 40 | fun fromVector vec = tabulate (V.length vec, fn i => V.sub(vec, i)) 41 | 42 | val toVector = vector 43 | 44 | end 45 | -------------------------------------------------------------------------------- /Code/2015/003/mono-array.sig: -------------------------------------------------------------------------------- 1 | (* mono-array.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | signature MONO_ARRAY_EXT = 29 | sig 30 | 31 | include MONO_ARRAY 32 | 33 | val toList : array -> elem list 34 | 35 | val fromVector : vector -> array 36 | val toVector : array -> vector 37 | 38 | end 39 | -------------------------------------------------------------------------------- /Code/2015/003/mono-vector.fun: -------------------------------------------------------------------------------- 1 | (* mono-vector.fun 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | functor MonoVectorExtFn (V : MONO_VECTOR) : MONO_VECTOR_EXT = 29 | struct 30 | 31 | open V 32 | 33 | fun toList v = List.tabulate(length v, fn i => sub(v, i)) 34 | 35 | fun append (v, x) = concat[v, fromList[x]] 36 | 37 | fun prepend (x, v) = concat[fromList[x], v] 38 | 39 | end 40 | -------------------------------------------------------------------------------- /Code/2015/003/mono-vector.sig: -------------------------------------------------------------------------------- 1 | (* mono-vector.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | signature MONO_VECTOR_EXT = 29 | sig 30 | 31 | include MONO_VECTOR 32 | 33 | val toList : vector -> elem list 34 | 35 | val append : vector * elem -> vector 36 | val prepend : elem * vector -> vector 37 | 38 | end 39 | -------------------------------------------------------------------------------- /Code/2015/003/option.sig: -------------------------------------------------------------------------------- 1 | (* option.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | signature OPTION_EXT = 29 | sig 30 | 31 | include OPTION 32 | 33 | val isNone : 'a option -> bool 34 | val fold : ('a * 'b -> 'b) -> 'b -> 'a option -> 'b 35 | 36 | end 37 | -------------------------------------------------------------------------------- /Code/2015/003/option.sml: -------------------------------------------------------------------------------- 1 | (* option.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | structure OptionExt : OPTION_EXT = 29 | struct 30 | 31 | open Option 32 | 33 | fun isNone NONE = true 34 | | isNone _ = false 35 | 36 | fun fold f init NONE = init 37 | | fold f init (SOME x) = f(x, init); 38 | 39 | end 40 | -------------------------------------------------------------------------------- /Code/2015/003/sources.cm: -------------------------------------------------------------------------------- 1 | (* sources.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for SML Basis Library Proposal 2015-003. 26 | * 27 | * The Basis signatures and stuctures in this reference implementation 28 | * have the suffix "_EXT" (resp. Ext) on their names. The monomorphic 29 | * array and vector types are supported by functors that extend the 30 | * existing implementation. 31 | *) 32 | 33 | Library 34 | 35 | signature ARRAY_EXT 36 | signature LIST_EXT 37 | signature LIST_PAIR_EXT 38 | signature MONO_ARRAY_EXT 39 | signature MONO_VECTOR_EXT 40 | signature OPTION_EXT 41 | signature STRING_EXT 42 | signature VECTOR_EXT 43 | 44 | structure ArrayExt 45 | structure ListExt 46 | structure ListPairExt 47 | structure OptionExt 48 | structure StringExt 49 | structure VectorExt 50 | 51 | functor MonoArrayExtFn 52 | functor MonoVectorExtFn 53 | 54 | is 55 | 56 | (* use the original Basis to compile this proposal *) 57 | #if ((SMLNJ_VERSION = 110) andalso (SMLNJ_MINOR_VERSION >= 79)) 58 | $/basis-2004.cm 59 | #else 60 | $/basis.cm 61 | #endif 62 | 63 | array.sig 64 | array.sml 65 | 66 | list-pair.sig 67 | list-pair.sml 68 | 69 | list.sig 70 | list.sml 71 | 72 | mono-array.sig 73 | mono-array.fun 74 | 75 | mono-vector.sig 76 | mono-vector.fun 77 | 78 | option.sig 79 | option.sml 80 | 81 | string.sig 82 | string.sml 83 | 84 | vector.sig 85 | vector.sml 86 | -------------------------------------------------------------------------------- /Code/2015/003/sources.mlb: -------------------------------------------------------------------------------- 1 | (* sources.mlb 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * MLB file for SML Basis Library Proposal 2015-003. 26 | * 27 | * The Basis signatures and stuctures in this reference implementation 28 | * have the suffix "_EXT" (resp. Ext) on their names. The monomorphic 29 | * array and vector types are supported by functors that extend the 30 | * existing implementation. 31 | *) 32 | 33 | local 34 | 35 | $(SML_LIB)/basis/basis.mlb 36 | 37 | array.sig 38 | array.sml 39 | 40 | list-pair.sig 41 | list-pair.sml 42 | 43 | list.sig 44 | list.sml 45 | 46 | mono-array.sig 47 | mono-array.fun 48 | 49 | mono-vector.sig 50 | mono-vector.fun 51 | 52 | option.sig 53 | option.sml 54 | 55 | string.sig 56 | string.sml 57 | 58 | vector.sig 59 | vector.sml 60 | 61 | in 62 | 63 | signature ARRAY_EXT 64 | signature LIST_EXT 65 | signature LIST_PAIR_EXT 66 | signature MONO_ARRAY_EXT 67 | signature MONO_VECTOR_EXT 68 | signature OPTION_EXT 69 | signature STRING_EXT 70 | signature VECTOR_EXT 71 | 72 | structure ArrayExt 73 | structure ListExt 74 | structure ListPairExt 75 | structure OptionExt 76 | structure StringExt 77 | structure VectorExt 78 | 79 | functor MonoArrayExtFn 80 | functor MonoVectorExtFn 81 | 82 | end 83 | -------------------------------------------------------------------------------- /Code/2015/003/string.sig: -------------------------------------------------------------------------------- 1 | (* string.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | signature STRING_EXT = 29 | sig 30 | 31 | include STRING 32 | 33 | val rev : string -> string 34 | 35 | val implodeRev : char list -> string 36 | 37 | val concatWithMap : string -> ('a -> string) -> 'a list -> string 38 | 39 | end 40 | -------------------------------------------------------------------------------- /Code/2015/003/string.sml: -------------------------------------------------------------------------------- 1 | (* string.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | structure StringExt : STRING_EXT = 29 | struct 30 | 31 | open String 32 | 33 | val rev = implode o List.rev o explode 34 | val implodeRev = implode o List.rev 35 | fun concatWithMap sep f l = concatWith sep (List.map f l) 36 | 37 | end 38 | -------------------------------------------------------------------------------- /Code/2015/003/vector.sig: -------------------------------------------------------------------------------- 1 | (* vector.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | signature VECTOR_EXT = 29 | sig 30 | 31 | include VECTOR 32 | 33 | val toList : 'a vector -> 'a list 34 | 35 | val append : 'a vector * 'a -> 'a vector 36 | val prepend : 'a * 'a vector -> 'a vector 37 | 38 | end 39 | -------------------------------------------------------------------------------- /Code/2015/003/vector.sml: -------------------------------------------------------------------------------- 1 | (* vector.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-003. 26 | *) 27 | 28 | structure VectorExt : VECTOR_EXT = 29 | struct 30 | 31 | open Vector 32 | 33 | fun toList v = List.tabulate(length v, fn i => sub(v, i)) 34 | 35 | fun append (v, x) = concat[v, fromList[x]] 36 | 37 | fun prepend (x, v) = concat[fromList[x], v] 38 | 39 | end 40 | -------------------------------------------------------------------------------- /Code/2015/004/BUFFER.sig: -------------------------------------------------------------------------------- 1 | signature BUFFER = 2 | sig 3 | type buf 4 | val new : int -> buf 5 | val contents : buf -> string 6 | val size : buf -> int 7 | val clear : buf -> unit 8 | val reset : buf -> unit 9 | 10 | val addChar : buf -> char -> unit 11 | val addString : buf -> string -> unit 12 | val addSubString : buf -> substring -> unit 13 | end 14 | 15 | (* [buf] is the type of mutable string buffers that allows efficient 16 | concatenation at the end and automatically expand as necessary. It 17 | provides accumulative concatenation of strings in quasi-linear time 18 | (instead of quadratic time when strings are concatenated pairwise). 19 | 20 | [new hint] creates a new empty buffer. Raises Size if hint <= 0 or 21 | hint > String.maxSize. 22 | The argument hint is used as the initial size of the internal 23 | string that holds the buffer contents. The internal string is 24 | automatically reallocated as contents is stored in the buffer. For 25 | best performance, hint should be of the same order of magnitude as 26 | the number of characters that are expected to be stored in the 27 | buffer (for instance, 80 for a buffer that holds one output line). 28 | Nothing bad will happen if the buffer grows beyond that limit, 29 | however. In doubt, take hint = 16 for instance. 30 | 31 | [contents buf] returns the contents of buf. 32 | 33 | [size buf] returns the size of the contents of buf. 34 | 35 | [clear buf] emptys buf. 36 | 37 | [reset buf] emptys buf and shrink the internal string to the 38 | initial hint. 39 | 40 | [addChar buf c] appends c at the end of buf. 41 | 42 | [addString buf s] appends s at the end of buf. 43 | 44 | [addSubString buf ss] appends ss at the end of buf. 45 | 46 | *) 47 | 48 | -------------------------------------------------------------------------------- /Code/2015/004/Buffer.sml: -------------------------------------------------------------------------------- 1 | (* String buffers heavily inspired by the Buffer module in OCaml *) 2 | (* Ken Friis Larsen 2001-07-31 *) 3 | structure Buffer :> BUFFER = 4 | struct 5 | 6 | datatype buf = BUF of {elts : string ref, 7 | size : int ref, 8 | initial : int} 9 | 10 | local 11 | prim_val create_ : int -> string = 1 "create_string"; 12 | prim_val update_ : string -> int -> char -> unit = 3 "set_nth_char"; 13 | prim_val blit_ : string -> int -> string -> int -> int -> unit 14 | = 5 "blit_string"; 15 | in 16 | fun new bufSize = 17 | if bufSize > 0 andalso bufSize <= String.maxSize 18 | then BUF{elts = ref (create_ bufSize), 19 | size = ref 0, 20 | initial = bufSize} 21 | else raise Size 22 | 23 | fun clear (BUF{size, ...}) = size := 0 24 | 25 | fun size (BUF{size as ref s, ...}) = s 26 | 27 | fun reset (BUF{initial, elts, size, ...}) = 28 | ( elts := create_ initial 29 | ; size := 0 ) 30 | 31 | fun contents (BUF{elts as ref arr, size as ref s, ...}) = 32 | let val newstr = create_ s 33 | in blit_ arr 0 newstr 0 s; newstr end 34 | 35 | fun resize (BUF{elts as ref arr, size as ref s, ...}) more = 36 | let val newSize = s + more 37 | val () = if newSize > String.maxSize then raise Size else () 38 | fun roundUp new = if new < newSize then roundUp(2*new) 39 | else if new > String.maxSize then String.maxSize 40 | else new 41 | val len = String.size arr 42 | val newLen = roundUp(2*len) 43 | handle Overflow => String.maxSize 44 | val arr' = create_ newLen 45 | in blit_ arr 0 arr' 0 s 46 | ; elts := arr' end 47 | 48 | 49 | fun addChar (buf as BUF{elts as ref arr, size as ref s, ...}) c = 50 | ( if s = String.size arr then resize buf 1 else () 51 | ; update_ (!elts) s c 52 | ; size := s+1 53 | ) 54 | 55 | fun addSubString (buf as BUF{elts as ref arr, size as ref s, ...}) x = 56 | let val (ss, off, len) = Substring.base x 57 | val newSize = s+len 58 | in if newSize >= String.size arr then resize buf len else () 59 | ; blit_ ss off (!elts) s len 60 | ; size := newSize end 61 | 62 | fun addString (buf as BUF{elts as ref arr, size as ref s, ...}) x = 63 | let val len = String.size x 64 | val newSize = s+len 65 | in if newSize >= String.size arr then resize buf len else () 66 | ; blit_ x 0 (!elts) s len 67 | ; size := newSize end 68 | 69 | end (*end local*) 70 | end 71 | 72 | 73 | (* Implementation in pure SML (using slices) *) 74 | (* 75 | structure Buffer :> Buffer = 76 | struct 77 | structure CA = CharArray 78 | structure CAS = CharArraySlice 79 | structure CVS = CharVectorSlice 80 | 81 | datatype buf = BUF of {elts : CA.array ref, 82 | size : int ref, 83 | initial : int} 84 | 85 | fun contents (BUF{elts as ref arr, size as ref s, ...}) = 86 | CAS.vector(CAS.slice(arr, 0, SOME s)) 87 | 88 | fun clear (BUF{size, ...}) = size := 0 89 | 90 | fun size (BUF{size as ref s, ...}) = s 91 | 92 | fun new bufSize = 93 | if bufSize > 0 then BUF{elts = ref (CA.array (bufSize, #"\000")), 94 | size = ref 0, 95 | initial = bufSize} 96 | else raise Size 97 | 98 | fun reset (BUF{initial, elts, size, ...}) = 99 | ( elts := CA.array (initial, #"\000") 100 | ; size := 0 ) 101 | 102 | 103 | fun resize (BUF{elts as ref arr, size as ref s, ...}) more = 104 | let val newSize = s + more 105 | val () = if newSize > CA.maxLen then raise Size else () 106 | fun roundUp new = if new < newSize then roundUp(2*new) else new 107 | val len = CA.length arr 108 | val newLen = roundUp(2*len) 109 | handle Overflow => CA.maxLen 110 | val arr' = CA.array (newLen, #"\000") 111 | handle Size => CA.array (CA.maxLen, #"\000") 112 | in CAS.copy{src=CAS.slice(arr, 0, SOME s), dst=arr', di=0} 113 | ; elts := arr' end 114 | 115 | 116 | fun addChar (buf as BUF{elts as ref arr, size as ref s, ...}) x = 117 | ( if s = CA.length arr then resize buf 1 else () 118 | ; CA.update (!elts, s, x) 119 | ; size := s+1) 120 | 121 | fun addSubString (buf as BUF{elts as ref arr, size as ref s, ...}) x = 122 | let val len = CVS.length x 123 | val newSize = s+len 124 | in if newSize >= CA.length arr then resize buf len else () 125 | ; CAS.copyVec{src = x, dst = !elts, di = s} 126 | ; size := newSize end 127 | 128 | fun addString (buf as BUF{elts as ref arr, size as ref s, ...}) x = 129 | let val len = String.size x 130 | val newSize = s+len 131 | in if newSize >= CA.length arr then resize buf len else () 132 | ; CA.copyVec{src = x, dst = !elts, di = s} 133 | ; size := newSize end 134 | end 135 | *) 136 | -------------------------------------------------------------------------------- /Code/2015/005/fn.sig: -------------------------------------------------------------------------------- 1 | (* fn.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-005. 26 | *) 27 | 28 | signature FN = 29 | sig 30 | 31 | val id : 'a -> 'a 32 | val const : 'a -> 'b -> 'a 33 | val apply : ('a -> 'b) * 'a -> 'b 34 | val o : ('b -> 'c) * ('a -> 'b) -> ('a -> 'c) 35 | val curry : ('a * 'b -> 'c) -> ('a -> 'b -> 'c) 36 | val uncurry : ('a -> 'b -> 'c) -> ('a * 'b -> 'c) 37 | val flip : ('a * 'b -> 'c) -> ('b * 'a -> 'c) 38 | val repeat : int -> ('a -> 'a) -> ('a -> 'a) 39 | val equal : ''a -> ''a -> bool 40 | val notEqual : ''a -> ''a -> bool 41 | 42 | end 43 | -------------------------------------------------------------------------------- /Code/2015/005/fn.sml: -------------------------------------------------------------------------------- 1 | (* fn.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-005. 26 | *) 27 | 28 | structure Fn : FN = 29 | struct 30 | 31 | fun id x = x 32 | 33 | fun const x y = x 34 | 35 | fun apply (f, x) = f x 36 | 37 | val op o = General.o 38 | 39 | fun curry f x y = f(x, y) 40 | 41 | fun uncurry f (x, y) = f x y 42 | 43 | fun flip f (x, y) = f (y, x) 44 | 45 | fun repeat n = if (n < 0) 46 | then raise Domain 47 | else (fn f => let 48 | fun repeatF (0, x) = x 49 | | repeatF (n, x) = repeatF (n-1, f x) 50 | in 51 | fn x => repeatF (n, x) 52 | end) 53 | 54 | fun equal a b = (a = b) 55 | 56 | fun notEqual a b = (a <> b) 57 | 58 | end 59 | -------------------------------------------------------------------------------- /Code/2015/005/sources.cm: -------------------------------------------------------------------------------- 1 | (* sources.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * CM file for SML Basis Library Proposal 2015-005. 26 | *) 27 | 28 | Library 29 | 30 | signature FN 31 | 32 | structure Fn 33 | 34 | is 35 | 36 | (* use the original Basis to compile this proposal *) 37 | #if ((SMLNJ_VERSION = 110) andalso (SMLNJ_MINOR_VERSION >= 79)) 38 | $/basis-2004.cm 39 | #else 40 | $/basis.cm 41 | #endif 42 | 43 | fn.sig 44 | fn.sml 45 | -------------------------------------------------------------------------------- /Code/2015/005/sources.mlb: -------------------------------------------------------------------------------- 1 | (* sources.mlb 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * MLB file for SML Basis Library Proposal 2015-005. 26 | *) 27 | 28 | local 29 | 30 | $(SML_LIB)/basis/basis.mlb 31 | 32 | fn.sig 33 | fn.sml 34 | 35 | in 36 | 37 | signature FN 38 | 39 | structure Fn 40 | 41 | end 42 | -------------------------------------------------------------------------------- /Code/2015/007/Ref.ok: -------------------------------------------------------------------------------- 1 | ** Testing Ref 2 | -- Ref.exchange ok 3 | -- Ref.swap ok 4 | -- Ref.app ok 5 | -- Ref.map ok 6 | -- Ref.modify ok 7 | ** Summary for Ref: 5 tests; 5 passed 8 | -------------------------------------------------------------------------------- /Code/2015/007/ref.sig: -------------------------------------------------------------------------------- 1 | (* ref.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-007. 26 | *) 27 | 28 | signature REF = 29 | sig 30 | datatype ref = datatype ref 31 | 32 | val ! : 'a ref -> 'a 33 | val := : 'a ref * 'a -> unit 34 | val exchange : 'a ref * 'a -> 'a 35 | val swap : 'a ref * 'a ref -> unit 36 | 37 | val app : ('a -> unit) -> 'a ref -> unit 38 | val map : ('a -> 'b) -> 'a ref -> 'b ref 39 | val modify : ('a -> 'a) -> 'a ref -> unit 40 | end 41 | -------------------------------------------------------------------------------- /Code/2015/007/ref.sml: -------------------------------------------------------------------------------- 1 | (* ref.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2015-007. 26 | *) 27 | 28 | structure Ref : REF = 29 | struct 30 | 31 | datatype ref = datatype ref 32 | 33 | val ! = General.! 34 | val op := = General.:= 35 | 36 | fun exchange (r as ref x, y) = (r := y; x) 37 | fun swap (rx as ref x, ry as ref y) = (rx := y; ry := x) 38 | 39 | fun app f (ref x) = f x 40 | 41 | fun map f (ref x) = ref (f x) 42 | 43 | fun modify f (r as ref x) = r := f x 44 | 45 | end 46 | -------------------------------------------------------------------------------- /Code/2015/007/sources.cm: -------------------------------------------------------------------------------- 1 | (* sources.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for SML Basis Library Proposal 2015-007. 26 | *) 27 | 28 | Library 29 | 30 | signature REF 31 | 32 | structure Ref 33 | 34 | is 35 | 36 | (* use the original Basis to compile this proposal *) 37 | #if ((SMLNJ_VERSION = 110) andalso (SMLNJ_MINOR_VERSION >= 79)) 38 | $/basis-2004.cm 39 | #else 40 | $/basis.cm 41 | #endif 42 | 43 | ref.sig 44 | ref.sml 45 | -------------------------------------------------------------------------------- /Code/2015/007/sources.mlb: -------------------------------------------------------------------------------- 1 | (* sources.mlb 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * MLB file for SML Basis Library Proposal 2015-007. 26 | *) 27 | 28 | local 29 | 30 | $(SML_LIB)/basis/basis.mlb 31 | 32 | ref.sig 33 | ref.sml 34 | 35 | in 36 | 37 | signature REF 38 | 39 | structure Ref 40 | 41 | end 42 | -------------------------------------------------------------------------------- /Code/2015/007/tests.cm: -------------------------------------------------------------------------------- 1 | (* tests.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2020 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for testing the SML Basis Library Proposal 2015-007. 26 | *) 27 | 28 | Library 29 | 30 | structure Tests 31 | 32 | is 33 | 34 | $/basis.cm 35 | ../../testing/sources.cm 36 | 37 | tests.sml 38 | -------------------------------------------------------------------------------- /Code/2015/007/tests.sml: -------------------------------------------------------------------------------- 1 | (* tests.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2020 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Test suite for SML Basis Library Proposal 2015-007. 26 | *) 27 | 28 | structure Tests = 29 | struct 30 | 31 | local 32 | open Check 33 | open Ref 34 | fun expectIntInt (p : (int * int)) = expectVal (op =) p 35 | fun expectIntString (p : (int * string)) = expectVal (op =) p 36 | fun withRefPair (init1, init2) tst () = let 37 | val r1 = ref init1 38 | val r2 = ref init2 39 | in 40 | tst (r1, r2); 41 | (!r1, !r2) 42 | end 43 | in 44 | 45 | fun run () = runSuite "Ref" (fn test => ( 46 | (* test exchange *) 47 | test "exchange" (expectIntInt (42, 17) (fn () => let 48 | val r = ref 17 49 | val x = exchange(r, 42) 50 | in 51 | (!r, x) 52 | end)); 53 | (* test swap *) 54 | test "swap" (expectIntInt (42, 17) (withRefPair (17, 42) swap)); 55 | (* test app : ('a -> unit) -> 'a ref -> unit *) 56 | test "app" (expectIntString (42, "42") (withRefPair (42, "") 57 | (fn (r, res) => app (fn n => res := Int.toString n) r))); 58 | (* test map : ('a -> 'b) -> 'a ref -> 'b ref *) 59 | test "map" (expectIntString (42, "42") (fn () => let 60 | val r = ref 42 61 | val res = map Int.toString r 62 | in 63 | (!r, !res) 64 | end)); 65 | (* test modify *) 66 | test "modify" (expectInt 18 (withRef 17 (fn r => modify (fn x => x+1) r))); 67 | ())) 68 | 69 | end (* local *) 70 | 71 | end 72 | -------------------------------------------------------------------------------- /Code/2016/001/sources.cm: -------------------------------------------------------------------------------- 1 | (* sources.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2016 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for SML Basis Library Proposal 2016-001. 26 | *) 27 | 28 | Library 29 | 30 | signature WORD_EXT 31 | 32 | structure WordExt 33 | 34 | is 35 | 36 | (* use the original Basis to compile this proposal *) 37 | #if ((SMLNJ_VERSION = 110) andalso (SMLNJ_MINOR_VERSION >= 80)) 38 | $/basis-2004.cm 39 | #else 40 | $/basis.cm 41 | #endif 42 | 43 | word.sig 44 | word.sml 45 | -------------------------------------------------------------------------------- /Code/2016/001/sources.mlb: -------------------------------------------------------------------------------- 1 | (* sources.mlb 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2016 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * MLB file for SML Basis Library Proposal 2016-001. 26 | *) 27 | 28 | local 29 | 30 | $(SML_LIB)/basis/basis.mlb 31 | 32 | word.sig 33 | word.sml 34 | 35 | in 36 | 37 | signature WORD_EXT 38 | 39 | structure WordExt 40 | 41 | end 42 | -------------------------------------------------------------------------------- /Code/2016/001/word.sig: -------------------------------------------------------------------------------- 1 | (* word.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2016 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2016-001. 26 | *) 27 | 28 | signature WORD_EXT = 29 | sig 30 | 31 | include WORD 32 | 33 | val popCount : word -> int 34 | 35 | end 36 | -------------------------------------------------------------------------------- /Code/2016/001/word.sml: -------------------------------------------------------------------------------- 1 | (* word.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2016 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2016-001. 26 | *) 27 | 28 | structure WordExt : WORD_EXT = 29 | struct 30 | 31 | open Word 32 | 33 | (* population count. See below for more efficient straight-line implementations 34 | * for specific word sizes. 35 | *) 36 | fun popCount w = let 37 | fun cnt (0w0, i) = i 38 | | cnt (w, i) = cnt (>>(w, 0w1), if (andb(w, 0w1) <> 0w0) then i+1 else i) 39 | in 40 | cnt (w, 0) 41 | end 42 | 43 | (* the following straight-line version works for 8-bit words and only uses addition, 44 | * logical and, and shift operations 45 | fun popCount (w : Word8.word) = let 46 | (* pop count of each 2 bits into those 2 bits *) 47 | val w = w - Word8.andb(Word8.>>(w, 0w1), 0wx55) 48 | (* pop count of each 4 bits into those 4 bits *) 49 | val w = Word8.andb(w, 0wx33) + Word8.andb(Word8.>>(w, 0w2), 0wx33) 50 | (* pop count of each 8 bits into those 8 bits *) 51 | val w = w + Word8.>>(w, 0w4) 52 | in 53 | (* mask out result *) 54 | Word8.toIntX (Word8.andb(w, 0wx0F)) 55 | end 56 | *) 57 | 58 | (* the following straight-line version works for 32-bit words and only uses addition, 59 | * logical and, and shift operations 60 | fun popCount (w : Word32.word) = let 61 | (* pop count of each 2 bits into those 2 bits *) 62 | val w = w - Word32.andb(Word32.>>(w, 0w1), 0wx55555555) 63 | (* pop count of each 4 bits into those 4 bits *) 64 | val w = Word32.andb(w, 0wx33333333) + Word32.andb(Word32.>>(w, 0w2), 0wx33333333) 65 | (* pop count of each 8 bits into those 8 bits *) 66 | val w = Word32.andb(w + Word32.>>(w, 0w4), 0wx0F0F0F0F) 67 | (* pop count of each 16 bits into their lowest 8 bits *) 68 | val w = w + Word32.>>(w, 0w8) 69 | (* pop count of each 32 bits into their lowest 8 bits *) 70 | val w = w + Word32.>>(w, 0w16) 71 | in 72 | (* mask out result *) 73 | Word32.toIntX (Word32.andb(w, 0wx3F)) 74 | end 75 | *) 76 | 77 | (* the following straight-line version works for 32-bit words, but requires a fast 78 | * multiply instruction 79 | fun popCount (w : Word32.word) = let 80 | (* pop count of each 2 bits into those 2 bits *) 81 | val w = w - Word32.andb(Word32.>>(w, 0w1), 0wx55555555) 82 | (* pop count of each 4 bits into those 4 bits *) 83 | val w = Word32.andb(w, 0wx33333333) + Word32.andb(Word32.>>(w, 0w2), 0wx33333333) 84 | (* pop count of each 8 bits into those 8 bits *) 85 | val w = Word32.andb(w + Word32.>>(w, 0w4), 0wx0F0F0F0F) 86 | in 87 | (* return leftmost 8 bits of w + (w<<8) + (w<<16) + (w<<24) *) 88 | Word32.toIntX (Word32.>>(w * 0wx01010101, 0w24)) 89 | end 90 | *) 91 | 92 | (* the following straight-line version works for 64-bit words and only uses addition, 93 | * logical and, and shift operations 94 | fun popCount (w : Word64.word) = let 95 | (* pop count of each 2 bits into those 2 bits *) 96 | val w = w - Word64.andb(Word64.>>(w, 0w1), 0wx5555555555555555) 97 | (* pop count of each 4 bits into those 4 bits *) 98 | val w = Word64.andb(w, 0wx3333333333333333) 99 | + Word64.andb(Word64.>>(w, 0w2), 0wx3333333333333333) 100 | (* pop count of each 8 bits into those 8 bits *) 101 | val w = Word64.andb(w + Word64.>>(w, 0w4), 0wx0F0F0F0F0F0F0F0F) 102 | (* pop count of each 16 bits into their lowest 8 bits *) 103 | val w = w + Word64.>>(w, 0w8) 104 | (* pop count of each 32 bits into their lowest 8 bits *) 105 | val w = w + Word64.>>(w, 0w16) 106 | (* pop count of each 64 bits into their lowest 8 bits *) 107 | val w = w + Word64.>>(w, 0w32) 108 | in 109 | (* mask out result *) 110 | Word64.toIntX (Word64.andb(w, 0wx7F)) 111 | end 112 | *) 113 | 114 | 115 | (* the following straight-line version works for 64-bit words, but requires a fast 116 | * multiply instruction 117 | fun popCount (w : Word64.word) = let 118 | (* pop count of each 2 bits into those 2 bits *) 119 | val w = w - Word64.andb(Word64.>>(w, 0w1), 0wx5555555555555555) 120 | (* pop count of each 4 bits into those 4 bits *) 121 | val w = Word64.andb(w, 0wx3333333333333333) 122 | + Word64.andb(Word64.>>(w, 0w2), 0wx3333333333333333) 123 | (* pop count of each 8 bits into those 8 bits *) 124 | val w = Word64.andb(w + Word64.>>(w, 0w4), 0wx0F0F0F0F0F0F0F0F) 125 | in 126 | (* return leftmost 8 bits of w + (w<<8) + (w<<16) + (w<<24) + ... + (w<<56) *) 127 | Word64.toIntX (Word64.>>(w * 0wx0101010101010101, 0w56)) 128 | end 129 | *) 130 | 131 | end 132 | -------------------------------------------------------------------------------- /Code/2018/001/CharBuffer.ok: -------------------------------------------------------------------------------- 1 | ** Testing CharBuffer 2 | -- CharBuffer.new-1 ok 3 | -- CharBuffer.new-2 ok 4 | -- CharBuffer.new-3 ok 5 | -- CharBuffer.new-4 ok 6 | -- CharBuffer.contents-1 ok 7 | -- CharBuffer.contents-2 ok 8 | -- CharBuffer.contents-3 ok 9 | -- CharBuffer.length-1 ok 10 | -- CharBuffer.length-2 ok 11 | -- CharBuffer.length-3 ok 12 | -- CharBuffer.clear-1 ok 13 | -- CharBuffer.clear-2 ok 14 | -- CharBuffer.clear-3 ok 15 | -- CharBuffer.reset-1 ok 16 | -- CharBuffer.reset-2 ok 17 | -- CharBuffer.reset-3 ok 18 | -- CharBuffer.reserve-1 ok 19 | -- CharBuffer.reserve-2 ok 20 | -- CharBuffer.add1-1 ok 21 | -- CharBuffer.add1-2 ok 22 | -- CharBuffer.addVec-1 ok 23 | -- CharBuffer.addVec-2 ok 24 | -- CharBuffer.addSlice-1 ok 25 | -- CharBuffer.addSlice-2 ok 26 | -- CharBuffer.addArr-1 ok 27 | -- CharBuffer.addArrSlice-1 ok 28 | -- CharBuffer.addArrSlice-2 ok 29 | ** Summary for CharBuffer: 27 tests; 27 passed 30 | -------------------------------------------------------------------------------- /Code/2018/001/mono-buffer.fun: -------------------------------------------------------------------------------- 1 | (* mono-buffer.fun 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2018-001. 26 | * 27 | * This proposal supersedes 2015-004. 28 | *) 29 | 30 | functor MonoBufferFn ( 31 | 32 | structure V : MONO_VECTOR 33 | structure VS : MONO_VECTOR_SLICE 34 | where type elem = V.elem and type vector = V.vector 35 | structure A : MONO_ARRAY 36 | where type elem = V.elem and type vector = V.vector 37 | structure AS : MONO_ARRAY_SLICE 38 | where type elem = V.elem and type array = A.array 39 | and type vector = V.vector and type vector_slice = VS.slice 40 | val defaultElem : V.elem 41 | 42 | ) : MONO_BUFFER = struct 43 | 44 | type elem = V.elem 45 | type vector = V.vector 46 | type slice = VS.slice 47 | type array = A.array 48 | type array_slice = AS.slice 49 | 50 | datatype buf = BUF of { 51 | content : array ref, (* array for holding content *) 52 | len : int ref, (* current length of content *) 53 | initLen : int (* initial size *) 54 | } 55 | 56 | (* default initial size *) 57 | val defaultInitLen = 4096 58 | 59 | val maxLen = Int.min(V.maxLen, A.maxLen) 60 | 61 | fun new hint = let 62 | val n = if (hint < 0) orelse (V.maxLen < hint) 63 | then raise Size 64 | else if (hint = 0) then defaultInitLen 65 | else hint 66 | in 67 | BUF{ 68 | content = ref(A.array(n, defaultElem)), 69 | len = ref 0, 70 | initLen = n 71 | } 72 | end 73 | 74 | fun contents (BUF{content=ref arr, len=ref n, ...}) = 75 | AS.vector (AS.slice (arr, 0, SOME n)) 76 | 77 | fun copy {src=BUF{content=ref arr, len=ref n, ...}, dst, di} = 78 | AS.copy {src = AS.slice (arr, 0, SOME n), dst = dst, di = di} 79 | 80 | fun length (BUF{len=ref n, ...}) = n 81 | 82 | fun sub (BUF{content=ref arr, len=ref n, ...}, i) = 83 | if (i < 0) orelse (n <= i) 84 | then raise Subscript 85 | else A.sub(arr, i) 86 | 87 | fun clear (BUF{len, ...}) = (len := 0) 88 | 89 | fun reset (BUF{content, len, initLen}) = ( 90 | len := 0; 91 | if (A.length(!content) <> initLen) 92 | then content := A.array(initLen, defaultElem) 93 | else ()) 94 | 95 | (* ensure that the content array has space for amt elements *) 96 | fun ensureCapacity (content, len, amt) = let 97 | val curCap = A.length(!content) 98 | val amt = Int.max(curCap - len + curCap, amt) 99 | val capacity = (len + amt) handle Overflow => maxLen 100 | in 101 | if (curCap < capacity) 102 | then let 103 | val newArr = A.array(capacity, defaultElem) 104 | in 105 | AS.copy{dst = newArr, di = 0, src = AS.slice(!content, 0, SOME len)}; 106 | content := newArr 107 | end 108 | else () 109 | end 110 | 111 | fun reserve (_, 0) = () 112 | | reserve (BUF{content, len=ref len, ...}, n) = 113 | if (n < 0) then raise Size 114 | else ensureCapacity (content, len, n) 115 | 116 | fun add1 (BUF{content, len, ...}, elem) = ( 117 | ensureCapacity(content, !len, 1); 118 | A.update(!content, !len, elem); 119 | len := !len + 1) 120 | 121 | fun addVec (BUF{content, len, ...}, vec) = ( 122 | ensureCapacity(content, !len, V.length vec); 123 | A.copyVec{dst = !content, di = !len, src = vec}; 124 | len := !len + V.length vec) 125 | 126 | fun addSlice (BUF{content, len, ...}, slice) = ( 127 | ensureCapacity(content, !len, VS.length slice); 128 | AS.copyVec{dst = !content, di = !len, src = slice}; 129 | len := !len + VS.length slice) 130 | 131 | fun addArr (BUF{content, len, ...}, arr) = ( 132 | ensureCapacity(content, !len, A.length arr); 133 | A.copy{dst = !content, di = !len, src = arr}; 134 | len := !len + A.length arr) 135 | 136 | fun addArrSlice (BUF{content, len, ...}, slice) = ( 137 | ensureCapacity(content, !len, AS.length slice); 138 | AS.copy{dst = !content, di = !len, src = slice}; 139 | len := !len + AS.length slice) 140 | 141 | end 142 | -------------------------------------------------------------------------------- /Code/2018/001/mono-buffer.sig: -------------------------------------------------------------------------------- 1 | (* mono-buffer.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2018-001. 26 | * 27 | * This proposal supersedes 2015-004. 28 | *) 29 | 30 | signature MONO_BUFFER = 31 | sig 32 | 33 | type buf 34 | 35 | type elem 36 | type vector 37 | type slice 38 | type array 39 | type array_slice 40 | 41 | val maxLen : int 42 | 43 | val new : int -> buf 44 | 45 | val contents : buf -> vector 46 | 47 | val copy : {src : buf, dst : array, di : int} -> unit 48 | 49 | val length : buf -> int 50 | 51 | val sub : buf * int -> elem 52 | 53 | val clear : buf -> unit 54 | 55 | val reset : buf -> unit 56 | 57 | val reserve : buf * int -> unit 58 | 59 | val add1 : buf * elem -> unit 60 | val addVec : buf * vector -> unit 61 | val addSlice : buf * slice -> unit 62 | val addArr : buf * array -> unit 63 | val addArrSlice : buf * array_slice -> unit 64 | 65 | end 66 | -------------------------------------------------------------------------------- /Code/2018/001/mono-buffer.sml: -------------------------------------------------------------------------------- 1 | (* mono-buffer.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2018-001. 26 | * 27 | * This proposal supersedes 2015-004. 28 | *) 29 | 30 | structure Word8Buffer :> MONO_BUFFER 31 | where type elem = Word8.word 32 | and type vector = Word8Vector.vector 33 | and type slice = Word8VectorSlice.slice 34 | and type array = Word8Array.array 35 | and type array_slice = Word8ArraySlice.slice 36 | = MonoBufferFn ( 37 | structure V = Word8Vector 38 | structure VS = Word8VectorSlice 39 | structure A = Word8Array 40 | structure AS = Word8ArraySlice 41 | val defaultElem : Word8.word = 0w0 42 | ); 43 | 44 | structure CharBuffer :> MONO_BUFFER 45 | where type elem = Char.char 46 | and type vector = CharVector.vector 47 | and type slice = CharVectorSlice.slice 48 | and type array = CharArray.array 49 | and type array_slice = CharArraySlice.slice 50 | = MonoBufferFn ( 51 | structure V = CharVector 52 | structure VS = CharVectorSlice 53 | structure A = CharArray 54 | structure AS = CharArraySlice 55 | val defaultElem : Char.char = #"\000" 56 | ); 57 | -------------------------------------------------------------------------------- /Code/2018/001/sources.cm: -------------------------------------------------------------------------------- 1 | (* sources.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for SML Basis Library Proposal 2018-001. 26 | *) 27 | 28 | Library 29 | 30 | signature MONO_BUFFER 31 | 32 | structure CharBuffer 33 | structure Word8Buffer 34 | 35 | is 36 | 37 | (* use the original Basis to compile this proposal *) 38 | #if ((SMLNJ_VERSION = 110) andalso (SMLNJ_MINOR_VERSION >= 79)) 39 | $/basis-2004.cm 40 | #else 41 | $/basis.cm 42 | #endif 43 | 44 | mono-buffer.sig 45 | mono-buffer.fun 46 | mono-buffer.sml 47 | -------------------------------------------------------------------------------- /Code/2018/001/sources.mlb: -------------------------------------------------------------------------------- 1 | (* sources.mlb 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * MLB file for SML Basis Library Proposal 2018-001. 26 | *) 27 | 28 | local 29 | 30 | $(SML_LIB)/basis/basis.mlb 31 | 32 | mono-buffer.sig 33 | mono-buffer.fun 34 | mono-buffer.sml 35 | 36 | in 37 | 38 | signature MONO_BUFFER 39 | 40 | structure CharBuffer 41 | structure Word8Buffer 42 | 43 | end 44 | -------------------------------------------------------------------------------- /Code/2018/001/tests.cm: -------------------------------------------------------------------------------- 1 | (* tests.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for testing the reference implementation of 26 | * SML Basis Library Proposal 2018-001. 27 | *) 28 | 29 | Library 30 | 31 | structure Tests 32 | 33 | signature MONO_BUFFER 34 | 35 | structure CharBuffer 36 | structure Word8Buffer 37 | 38 | is 39 | 40 | $/basis.cm 41 | ../../testing/sources.cm 42 | sources.cm 43 | 44 | tests.sml 45 | -------------------------------------------------------------------------------- /Code/2018/001/tests.sml: -------------------------------------------------------------------------------- 1 | (* tests.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Test suite for SML Basis Library Proposal 2018-001. 26 | *) 27 | 28 | structure Tests = 29 | struct 30 | 31 | local 32 | open Check 33 | open CharBuffer 34 | fun ident x = x 35 | val itos = Int.toString 36 | fun withBuf n f () = let 37 | val buf = new n 38 | in 39 | f buf 40 | end 41 | fun withBufContents n f () = let 42 | val buf = new n 43 | in 44 | f buf; contents buf 45 | end 46 | structure SS = Substring 47 | in 48 | 49 | fun run () = runSuite "CharBuffer" (fn test => ( 50 | (* test new *) 51 | test "new-1" (expectSize (fn () => new ~1)); 52 | test "new-2" (expectSize (fn () => new(maxLen+1))); 53 | test "new-3" (expect (fn _ => true) (fn () => new 0)); 54 | test "new-4" (expect (fn _ => true) (fn () => new 2)); 55 | (* test contents *) 56 | test "contents-1" (expectStr "" (withBuf 0 contents)); 57 | test "contents-2" (expectStr "" (withBuf 2 contents)); 58 | test "contents-3" (expectStr "x" 59 | (withBuf 8 (fn buf => (add1(buf, #"x"); contents buf)))); 60 | (* test length *) 61 | test "length-1" (expectInt 0 (withBuf 0 length)); 62 | test "length-2" (expectInt 0 (withBuf 2 length)); 63 | test "length-3" (expectInt 1 64 | (withBuf 8 (fn buf => (add1(buf, #"x"); length buf)))); 65 | (* test clear *) 66 | test "clear-1" (expectStr "" (withBufContents 8 clear)); 67 | test "clear-2" (expectStr "" (withBufContents 8 68 | (fn buf => (add1(buf, #"x"); clear buf)))); 69 | test "clear-3" (expectStr "ab" (withBufContents 8 70 | (fn buf => (addVec(buf, "xyz"); clear buf; addVec(buf, "ab"))))); 71 | (* test reset *) 72 | test "reset-1" (expectStr "" (withBufContents 8 reset)); 73 | test "reset-2" (expectStr "" (withBufContents 8 74 | (fn buf => (add1(buf, #"x"); reset buf)))); 75 | test "reset-3" (expectStr "ab" (withBufContents 8 76 | (fn buf => (addVec(buf, "xyz"); reset buf; addVec(buf, "ab"))))); 77 | (* test reserve *) 78 | test "reserve-1" (expectSize (withBuf 8 (fn buf => reserve(buf, ~1)))); 79 | test "reserve-2" (expectStr "xy" 80 | (withBufContents 4 81 | (fn buf => (add1(buf, #"x"); reserve(buf, 1024); add1(buf, #"y"))))); 82 | (* test add1 *) 83 | test "add1-1" (expectStr "xyz" 84 | (withBufContents 1 85 | (fn buf => (add1(buf, #"x"); add1(buf, #"y"); add1(buf, #"z"))))); 86 | test "add1-2" (expectStr "xyz" 87 | (withBufContents 8 88 | (fn buf => (add1(buf, #"x"); add1(buf, #"y"); add1(buf, #"z"))))); 89 | (* test addVec *) 90 | test "addVec-1" (expectStr "xyz" 91 | (withBufContents 8 (fn buf => addVec(buf, "xyz")))); 92 | test "addVec-2" (expectStr "xyzabc" 93 | (withBufContents 8 (fn buf => (addVec(buf, "xyz"); addVec(buf, "abc"))))); 94 | (* test addSlice *) 95 | test "addSlice-1" (expectStr "xyz" 96 | (withBufContents 8 (fn buf => addSlice(buf, SS.full "xyz")))); 97 | test "addSlice-2" (expectStr "xyz" 98 | (withBufContents 8 99 | (fn buf => (addSlice(buf, SS.substring("012xyzabc", 3, 3)))))); 100 | (* test addArr *) 101 | test "addArr-1" (expectStr "xyz" 102 | (withBufContents 8 (fn buf => let 103 | val arr = Array.fromList(String.explode "xyz") 104 | in 105 | addSlice(buf, SS.full "xyz") 106 | end))); 107 | (* test addArrSlice *) 108 | test "addArrSlice-1" (expectStr "xyz" 109 | (withBufContents 8 (fn buf => let 110 | val arr = CharArray.fromList(String.explode "xyz") 111 | in 112 | addArrSlice(buf, CharArraySlice.full arr) 113 | end))); 114 | test "addArrSlice-2" (expectStr "xyz" 115 | (withBufContents 8 (fn buf => let 116 | val arr = CharArray.fromList(String.explode "012xyzabc") 117 | in 118 | addArrSlice(buf, CharArraySlice.slice(arr, 3, SOME 3)) 119 | end))); 120 | ())) 121 | 122 | end (* local *) 123 | 124 | end 125 | -------------------------------------------------------------------------------- /Code/2018/002/array-slice.sig: -------------------------------------------------------------------------------- 1 | (* array-slice.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2018-002. 26 | *) 27 | 28 | signature ARRAY_SLICE_EXT = 29 | sig 30 | 31 | include ARRAY_SLICE 32 | 33 | val triml : int -> 'a slice -> 'a slice 34 | val trimr : int -> 'a slice -> 'a slice 35 | val splitAt : 'a slice * int -> 'a slice * 'a slice 36 | val getVec : 'a slice * int -> ('a vector * 'a slice) option 37 | 38 | end 39 | -------------------------------------------------------------------------------- /Code/2018/002/array-slice.sml: -------------------------------------------------------------------------------- 1 | (* array-slice.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2018-002. 26 | *) 27 | 28 | structure ArraySliceExt : ARRAY_SLICE_EXT = 29 | struct 30 | 31 | open ArraySlice 32 | 33 | fun triml n s = if (n < length s) 34 | then subslice(s, n, NONE) 35 | else subslice(s, length s, NONE) 36 | 37 | fun trimr n s = if (n < length s) 38 | then subslice(s, 0, SOME(length s - n)) 39 | else subslice(s, 0, SOME 0) 40 | 41 | fun splitAt (s, i) = (subslice(s, 0, SOME i), subslice(s, i, NONE)) 42 | 43 | fun getVec (s, n) = if (n < length s) 44 | then let 45 | val (s1, s2) = splitAt (s, n) 46 | in 47 | SOME(vector s1, s2) 48 | end 49 | else NONE 50 | 51 | end 52 | -------------------------------------------------------------------------------- /Code/2018/002/mono-array-slice.fun: -------------------------------------------------------------------------------- 1 | (* mono-array-slice.fun 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2018-002. 26 | *) 27 | 28 | functor MonoArraySliceExtFn (MS : MONO_ARRAY_SLICE) : MONO_ARRAY_SLICE_EXT = 29 | struct 30 | 31 | open MS 32 | 33 | fun triml n s = if (n < length s) 34 | then subslice(s, n, NONE) 35 | else subslice(s, length s, NONE) 36 | 37 | fun trimr n s = if (n < length s) 38 | then subslice(s, 0, SOME(length s - n)) 39 | else subslice(s, 0, SOME 0) 40 | 41 | fun splitAt (s, i) = (subslice(s, 0, SOME i), subslice(s, i, NONE)) 42 | 43 | fun getVec (s, n) = if (n < length s) 44 | then let 45 | val (s1, s2) = splitAt (s, n) 46 | in 47 | SOME(vector s1, s2) 48 | end 49 | else NONE 50 | 51 | end 52 | -------------------------------------------------------------------------------- /Code/2018/002/mono-array-slice.sig: -------------------------------------------------------------------------------- 1 | (* mono-array-slice.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2018-002. 26 | *) 27 | 28 | signature MONO_ARRAY_SLICE_EXT = sig 29 | 30 | include MONO_ARRAY_SLICE 31 | 32 | val triml : int -> slice -> slice 33 | val trimr : int -> slice -> slice 34 | val splitAt : slice * int -> slice * slice 35 | val getVec : slice * int -> (vector * slice) option 36 | 37 | end -------------------------------------------------------------------------------- /Code/2018/002/mono-vector-slice.fun: -------------------------------------------------------------------------------- 1 | (* mono-vector-slice.fun 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2018-002. 26 | *) 27 | 28 | functor MonoVectorSliceExtFn (MS : MONO_VECTOR_SLICE) : MONO_VECTOR_SLICE_EXT = 29 | struct 30 | 31 | open MS 32 | 33 | fun triml n s = if (n < length s) 34 | then subslice(s, n, NONE) 35 | else subslice(s, length s, NONE) 36 | 37 | fun trimr n s = if (n < length s) 38 | then subslice(s, 0, SOME(length s - n)) 39 | else subslice(s, 0, SOME 0) 40 | 41 | fun splitAt (s, i) = (subslice(s, 0, SOME i), subslice(s, i, NONE)) 42 | 43 | fun getVec (s, n) = if (n < length s) 44 | then let 45 | val (s1, s2) = splitAt (s, n) 46 | in 47 | SOME(vector s1, s2) 48 | end 49 | else NONE 50 | 51 | end 52 | -------------------------------------------------------------------------------- /Code/2018/002/mono-vector-slice.sig: -------------------------------------------------------------------------------- 1 | (* mono-vector-slice.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | *) 25 | 26 | signature MONO_VECTOR_SLICE_EXT = sig 27 | 28 | include MONO_VECTOR_SLICE 29 | 30 | val triml : int -> slice -> slice 31 | val trimr : int -> slice -> slice 32 | val splitAt : slice * int -> slice * slice 33 | val getVec : slice * int -> (vector * slice) option 34 | 35 | end -------------------------------------------------------------------------------- /Code/2018/002/sources.cm: -------------------------------------------------------------------------------- 1 | (* sources.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for SML Basis Library Proposal 2018-002. 26 | *) 27 | 28 | Library 29 | 30 | signature ARRAY_SLICE_EXT 31 | signature MONO_ARRAY_SLICE_EXT 32 | signature MONO_VECTOR_SLICE_EXT 33 | signature VECTOR_SLICE_EXT 34 | 35 | structure ArraySliceExt 36 | structure VectorSliceExt 37 | 38 | functor MonoArraySliceExtFn 39 | functor MonoVectorSliceExtFn 40 | 41 | is 42 | 43 | (* use the original Basis to compile this proposal *) 44 | #if ((SMLNJ_VERSION = 110) andalso (SMLNJ_MINOR_VERSION >= 79)) 45 | $/basis-2004.cm 46 | #else 47 | $/basis.cm 48 | #endif 49 | 50 | array-slice.sig 51 | array-slice.sml 52 | mono-array-slice.fun 53 | mono-array-slice.sig 54 | mono-vector-slice.fun 55 | mono-vector-slice.sig 56 | vector-slice.sig 57 | vector-slice.sml 58 | -------------------------------------------------------------------------------- /Code/2018/002/sources.mlb: -------------------------------------------------------------------------------- 1 | (* sources.mlb 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * MLB file for SML Basis Library Proposal 2018-002. 26 | *) 27 | 28 | local 29 | 30 | $(SML_LIB)/basis/basis.mlb 31 | 32 | array-slice.sig 33 | array-slice.sml 34 | mono-array-slice.fun 35 | mono-array-slice.sig 36 | mono-vector-slice.fun 37 | mono-vector-slice.sig 38 | vector-slice.sig 39 | vector-slice.sml 40 | 41 | in 42 | 43 | signature ARRAY_SLICE_EXT 44 | signature MONO_ARRAY_SLICE_EXT 45 | signature MONO_VECTOR_SLICE_EXT 46 | signature VECTOR_SLICE_EXT 47 | 48 | structure ArraySliceExt 49 | structure VectorSliceExt 50 | 51 | functor MonoArraySliceExtFn 52 | functor MonoVectorSliceExtFn 53 | 54 | end 55 | -------------------------------------------------------------------------------- /Code/2018/002/tests.cm: -------------------------------------------------------------------------------- 1 | (* tests.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for testing the reference implementation of 26 | * SML Basis Library Proposal 2018-002. 27 | *) 28 | 29 | Library 30 | 31 | structure Tests 32 | 33 | signature ARRAY_SLICE_EXT 34 | signature MONO_ARRAY_SLICE_EXT 35 | signature MONO_VECTOR_SLICE_EXT 36 | signature VECTOR_SLICE_EXT 37 | 38 | structure ArraySliceExt 39 | structure VectorSliceExt 40 | 41 | structure CharArraySliceExt 42 | structure CharVectorSliceExt 43 | 44 | is 45 | 46 | $/basis.cm 47 | ../../testing/sources.cm 48 | sources.cm 49 | 50 | tests.sml 51 | -------------------------------------------------------------------------------- /Code/2018/002/tests.sml: -------------------------------------------------------------------------------- 1 | (* tests.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Test suite for SML Basis Library Proposal 2018-002. 26 | *) 27 | 28 | structure CharArraySliceExt = MonoArraySliceExtFn(CharArraySlice) 29 | 30 | structure CharVectorSliceExt = MonoVectorSliceExtFn(CharVectorSlice) 31 | 32 | structure Tests = 33 | struct 34 | 35 | local 36 | open Check 37 | structure SS = Substring 38 | fun triml (v, n) () = SS.string(CharVectorSlice.triml n (SS.full v)) 39 | fun trimr (v, n) () = SS.string(CharVectorSlice.trimr n (SS.full v)) 40 | fun splitAt (v, n) () = let 41 | val (vs1, vs2) = CharVectorSlice.splitAt(SS.full v, n) 42 | in 43 | (SS.string vs1, SS.string vs2) 44 | end 45 | fun getVec (v, n) () = Option.map 46 | (fn (v, r) => (v, SS.string r)) 47 | (CharVectorSlice.getVec (SS.full v, n)) 48 | in 49 | 50 | fun testCVS () = runSuite "CharVectorSlice" (fn test => ( 51 | (* test triml *) 52 | test "triml-1" (expectSubscript (triml ("abc", ~1))); 53 | test "triml-2" (expectStr "" (triml ("", 0))); 54 | test "triml-3" (expectStr "abc" (triml ("abc", 0))); 55 | test "triml-4" (expectStr "bc" (triml ("abc", 1))); 56 | test "triml-5" (expectStr "" (triml ("abc", 3))); 57 | test "triml-6" (expectStr "" (triml ("abc", 4))); 58 | (* test trimr *) 59 | test "trimr-1" (expectSubscript (trimr ("abc", ~1))); 60 | test "trimr-2" (expectStr "" (trimr ("", 0))); 61 | test "trimr-3" (expectStr "abc" (trimr ("abc", 0))); 62 | test "trimr-4" (expectStr "ab" (trimr ("abc", 1))); 63 | test "trimr-5" (expectStr "" (trimr ("abc", 3))); 64 | test "trimr-6" (expectStr "" (trimr ("abc", 4))); 65 | (* test splitAt *) 66 | test "splitAt-1" (expectSubscript (splitAt ("abcxyz", ~1))); 67 | test "splitAt-2" (expectSubscript (splitAt ("abcxyz", 7))); 68 | test "splitAt-3" (expectVal' ("", "") (splitAt ("", 0))); 69 | test "splitAt-4" (expectVal' ("", "abcxyz") (splitAt ("abcxyz", 0))); 70 | test "splitAt-5" (expectVal' ("a", "bcxyz") (splitAt ("abcxyz", 1))); 71 | test "splitAt-6" (expectVal' ("abc", "xyz") (splitAt ("abcxyz", 3))); 72 | test "splitAt-7" (expectVal' ("abcxy", "z") (splitAt ("abcxyz", 5))); 73 | test "splitAt-8" (expectVal' ("abcxyz", "") (splitAt ("abcxyz", 6))); 74 | (* test getVec *) 75 | test "getVec-1" (expectSubscript (getVec ("abcdef", ~1))); 76 | test "getVec-2" (expectNone (getVec ("abcxyz", 7))); 77 | test "getVec-3" (expectSome' ("", "") (getVec ("", 0))); 78 | test "getVec-4" (expectSome' ("", "abcxyz") (getVec ("abcxyz", 0))); 79 | test "getVec-5" (expectSome' ("a", "bcxyz") (getVec ("abcxyz", 1))); 80 | test "getVec-6" (expectSome' ("abc", "xyz") (getVec ("abcxyz", 3))); 81 | test "getVec-7" (expectSome' ("abcxy", "z") (getVec ("abcxyz", 5))); 82 | test "getVec-8" (expectSome' ("abcxyz", "") (getVec ("abcxyz", 6))); 83 | ())) 84 | 85 | end (* local *) 86 | 87 | local 88 | open Check 89 | fun slice v = CharArraySlice.full (CharArray.fromVector v) 90 | val vector = CharArraySlice.vector 91 | fun triml (v, n) () = vector(CharArraySlice.triml n (slice v)) 92 | fun trimr (v, n) () = vector(CharArraySlice.trimr n (slice v)) 93 | fun splitAt (v, n) () = let 94 | val (vs1, vs2) = CharArraySlice.splitAt(slice v, n) 95 | in 96 | (vector vs1, vector vs2) 97 | end 98 | fun getVec (v, n) () = Option.map 99 | (fn (v, r) => (v, vector r)) 100 | (CharArraySlice.getVec (slice v, n)) 101 | fun eq ((a : string, b : string), (a', b')) = 102 | if (a = a') andalso (b = b') then true 103 | else (print(concat["(\"", a, "\", \"", b, "\") <> (\"", a', "\", \"", b', "\")\n"]); false) 104 | in 105 | 106 | fun testCAS () = runSuite "CharArraySlice" (fn test => ( 107 | (* test triml *) 108 | test "triml-1" (expectSubscript (triml ("abc", ~1))); 109 | test "triml-2" (expectStr "" (triml ("", 0))); 110 | test "triml-3" (expectStr "abc" (triml ("abc", 0))); 111 | test "triml-4" (expectStr "bc" (triml ("abc", 1))); 112 | test "triml-5" (expectStr "" (triml ("abc", 3))); 113 | test "triml-6" (expectStr "" (triml ("abc", 4))); 114 | (* test trimr *) 115 | test "trimr-1" (expectSubscript (trimr ("abc", ~1))); 116 | test "trimr-2" (expectStr "" (trimr ("", 0))); 117 | test "trimr-3" (expectStr "abc" (trimr ("abc", 0))); 118 | test "trimr-4" (expectStr "ab" (trimr ("abc", 1))); 119 | test "trimr-5" (expectStr "" (trimr ("abc", 3))); 120 | test "trimr-6" (expectStr "" (trimr ("abc", 4))); 121 | (* test splitAt *) 122 | test "splitAt-1" (expectSubscript (splitAt ("abcxyz", ~1))); 123 | test "splitAt-2" (expectSubscript (splitAt ("abcxyz", 7))); 124 | test "splitAt-3" (expectVal eq ("", "") (splitAt ("", 0))); 125 | test "splitAt-4" (expectVal eq ("", "abcxyz") (splitAt ("abcxyz", 0))); 126 | test "splitAt-5" (expectVal eq ("a", "bcxyz") (splitAt ("abcxyz", 1))); 127 | test "splitAt-6" (expectVal eq ("abc", "xyz") (splitAt ("abcxyz", 3))); 128 | test "splitAt-7" (expectVal eq ("abcxy", "z") (splitAt ("abcxyz", 5))); 129 | test "splitAt-8" (expectVal eq ("abcxyz", "") (splitAt ("abcxyz", 6))); 130 | (* test getVec *) 131 | test "getVec-1" (expectSubscript (getVec ("abcdef", ~1))); 132 | test "getVec-2" (expectNone (getVec ("abcxyz", 7))); 133 | test "getVec-3" (expectSome' ("", "") (getVec ("", 0))); 134 | test "getVec-4" (expectSome' ("", "abcxyz") (getVec ("abcxyz", 0))); 135 | test "getVec-5" (expectSome' ("a", "bcxyz") (getVec ("abcxyz", 1))); 136 | test "getVec-6" (expectSome' ("abc", "xyz") (getVec ("abcxyz", 3))); 137 | test "getVec-7" (expectSome' ("abcxy", "z") (getVec ("abcxyz", 5))); 138 | test "getVec-8" (expectSome' ("abcxyz", "") (getVec ("abcxyz", 6))); 139 | ())) 140 | 141 | end (* local *) 142 | 143 | (* 144 | fun testVS () = runSuite "VectorSlice" (fn test => ( 145 | (* test triml *) 146 | (* test trimr *) 147 | (* test splitAt *) 148 | (* test getVec *) 149 | ())) 150 | 151 | fun testAS () = runSuite "ArraySlice" (fn test => ( 152 | (* test triml *) 153 | (* test trimr *) 154 | (* test splitAt *) 155 | (* test getVec *) 156 | ())) 157 | *) 158 | 159 | end 160 | -------------------------------------------------------------------------------- /Code/2018/002/vector-slice.sig: -------------------------------------------------------------------------------- 1 | (* vector-slice.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2018-002. 26 | *) 27 | 28 | signature VECTOR_SLICE_EXT = 29 | sig 30 | 31 | include VECTOR_SLICE 32 | 33 | val triml : int -> 'a slice -> 'a slice 34 | val trimr : int -> 'a slice -> 'a slice 35 | val splitAt : 'a slice * int -> 'a slice * 'a slice 36 | val getVec : 'a slice * int -> ('a vector * 'a slice) option 37 | 38 | end 39 | -------------------------------------------------------------------------------- /Code/2018/002/vector-slice.sml: -------------------------------------------------------------------------------- 1 | (* vector-slice.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2018 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2018-002. 26 | *) 27 | 28 | structure VectorSliceExt : VECTOR_SLICE_EXT = 29 | struct 30 | 31 | open VectorSlice 32 | 33 | fun triml n s = if (n < length s) 34 | then subslice(s, n, NONE) 35 | else subslice(s, length s, NONE) 36 | 37 | fun trimr n s = if (n < length s) 38 | then subslice(s, 0, SOME(length s - n)) 39 | else subslice(s, 0, SOME 0) 40 | 41 | fun splitAt (s, i) = (subslice(s, 0, SOME i), subslice(s, i, NONE)) 42 | 43 | fun getVec (s, n) = if (n < length s) 44 | then let 45 | val (s1, s2) = splitAt (s, n) 46 | in 47 | SOME(vector s1, s2) 48 | end 49 | else NONE 50 | 51 | end 52 | -------------------------------------------------------------------------------- /Code/2020/001/universal.sig: -------------------------------------------------------------------------------- 1 | (* universal.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2020 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2020-001. 26 | * 27 | * This interface is copied from signature for the Poly/ML Universal 28 | * structure. 29 | *) 30 | 31 | signature UNIVERSAL = sig 32 | 33 | type universal 34 | 35 | type 'a tag 36 | 37 | val tag : unit -> 'a tag 38 | val tagInject : 'a tag -> 'a -> universal 39 | val tagIs : 'a tag -> universal -> bool 40 | val tagProject : 'a tag -> universal -> 'a 41 | 42 | end 43 | -------------------------------------------------------------------------------- /Code/2020/001/universal.sml: -------------------------------------------------------------------------------- 1 | (* universal.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2020 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Reference code for SML Basis Library Proposal 2020-001. 26 | *) 27 | 28 | structure Universal :> UNIVERSAL = 29 | struct 30 | 31 | type universal = exn 32 | 33 | type 'a tag = { inj : 'a -> universal, prj : universal -> 'a option } 34 | 35 | fun 'a tag () = let 36 | exception TAG of 'a 37 | in { 38 | inj = TAG, 39 | prj = fn (TAG x) => SOME x | _ => NONE 40 | } end 41 | 42 | fun tagInject (tag : 'a tag) = #inj tag 43 | fun tagIs (tag : 'a tag) univ = Option.isSome(#prj tag univ) 44 | fun tagProject (tag : 'a tag) univ = (case #prj tag univ 45 | of SOME x => x 46 | | NONE => raise Match 47 | (* end case *)) 48 | 49 | end 50 | -------------------------------------------------------------------------------- /Code/example/Counter.ok: -------------------------------------------------------------------------------- 1 | ** Testing Counter 2 | -- Counter.new-1 ok 3 | ** Summary for Counter: 1 tests; 1 passed 4 | -------------------------------------------------------------------------------- /Code/example/README.md: -------------------------------------------------------------------------------- 1 | This directory contains an example of how a proposal should be structured. 2 | The example proposes adding a `Counter` module to the Basis Library and 3 | consists of the following files are included: 4 | 5 | * `example-proposal.md` 6 | This file is a github markdown template for the proposal wiki page. 7 | 8 | * `counter.sig` 9 | An example signature file. 10 | 11 | * `counter.sml` 12 | An example reference implementation of the proposal. 13 | 14 | * `sources.cm` 15 | An SML/NJ CM file for compiling the reference implementation. 16 | 17 | * `sources.mlb` 18 | A MLB file for compiling the reference implementation. 19 | 20 | * `tests.sml` 21 | The tests for the reference implementation. 22 | 23 | * `tests.cm` 24 | An SML/NJ CM file for compiling the tests. 25 | 26 | * `Counter.ok` 27 | The expected output from the tests. 28 | 29 | -------------------------------------------------------------------------------- /Code/example/counter.sig: -------------------------------------------------------------------------------- 1 | (* counter.sig 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | *) 25 | 26 | signature COUNTER = 27 | sig 28 | 29 | type cntr 30 | 31 | datatype oper = INC | DEC 32 | 33 | exception Underflow 34 | 35 | val new : unit -> cntr 36 | 37 | val update : cntr * oper -> unit 38 | 39 | val read : cntr -> IntInf.int 40 | 41 | end 42 | -------------------------------------------------------------------------------- /Code/example/counter.sml: -------------------------------------------------------------------------------- 1 | (* counter.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | *) 25 | 26 | structure Counter :> COUNTER = 27 | struct 28 | 29 | datatype cntr = C of IntInf.int ref 30 | 31 | datatype oper = INC | DEC 32 | 33 | exception Underflow 34 | 35 | fun new () = C(ref 0) 36 | 37 | fun update (C(r as ref n), INC) = r := n + 1 38 | | update (C(ref 0), DEC) = raise Underflow 39 | | update (C(r as ref n), DEC) = r := n - 1 40 | 41 | fun read (C(ref n)) = n 42 | 43 | end 44 | -------------------------------------------------------------------------------- /Code/example/example-proposal.md: -------------------------------------------------------------------------------- 1 | ## Proposal yyyy-ddd 2 | ### Addition of `Counter` module 3 | 4 | **Author:** Some Author
5 | **Managing editor:** Some Editor
6 | **Last revised:** August 25, 2015
7 | **Status:** proposed
8 | **Discussion:** [issue #XX](https://github.com/SMLFamily/BasisLibrary/issues/XX)
9 | 10 | *** 11 | 12 | #### Synopsis 13 | ```sml 14 | signature COUNTER 15 | structure Counter :> COUNTER 16 | ``` 17 | 18 | \[_Here we include a short summary of the proposed API and what its purpose is._] 19 | 20 | This module provides stateful counters for counting things. 21 | 22 | #### Interface 23 | 24 | ```sml 25 | type cntr 26 | 27 | datatype oper = INC | DEC 28 | 29 | exception Underflow 30 | 31 | val new : unit -> cntr 32 | val update : cntr * oper -> unit 33 | val read : cntr -> IntInf.int 34 | ``` 35 | 36 | #### Description 37 | 38 | * `type cntr`
39 | is the abstract type of counters. 40 | 41 | * `datatype oper = INC | DEC`
42 | defines the operations that can be performed on a counter: 43 | * `INC` is the increment operator 44 | * `DEC` is the decrement operator 45 | 46 | * `exception Underflow`
47 | is raised if performing a decrement on a counter would cause it to go below zero. 48 | 49 | * `new ()`
50 | returns a new counter. 51 | 52 | * `update (cntr, oper)`
53 | modifies the state of the counter `cntr` by applying the operation `oper` to the counter. 54 | The `Underflow` exception is raised when a decrement operation is applied to a counter 55 | whose value is zero. 56 | 57 | * `read cntr`
58 | returns the current value of the counter. 59 | 60 | #### Discussion 61 | 62 | \[_A discussion of the design choices and possible alternatives._] 63 | 64 | It might be useful to provide `inc` and `dec` functions. 65 | 66 | #### Impact 67 | 68 | \[_What impact will this proposal have on existing code?_] 69 | 70 | Adding this module will not affect existing code. 71 | 72 | #### Rationale 73 | 74 | \[_Why is the proposed API a candidate for the Basis Library (as opposed to some other 75 | library). Why do we need it?_] 76 | 77 | Counting things is important; this module will help support that common task. 78 | 79 | *** 80 | 81 | ### History 82 | 83 | * [2015-08-25] Proposed 84 | 85 | *** -------------------------------------------------------------------------------- /Code/example/sources.cm: -------------------------------------------------------------------------------- 1 | (* sources.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | *) 25 | 26 | Library 27 | 28 | signature COUNTER 29 | 30 | structure Counter 31 | 32 | is 33 | 34 | $/basis.cm 35 | 36 | counter.sig 37 | counter.sml 38 | -------------------------------------------------------------------------------- /Code/example/sources.mlb: -------------------------------------------------------------------------------- 1 | (* sources.mlb 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | *) 25 | 26 | local 27 | 28 | $(SML_LIB)/basis/basis.mlb 29 | 30 | counter.sig 31 | counter.sml 32 | 33 | in 34 | 35 | signature COUNTER 36 | 37 | structure Counter 38 | 39 | end 40 | -------------------------------------------------------------------------------- /Code/example/tests.cm: -------------------------------------------------------------------------------- 1 | (* tests.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | *) 25 | 26 | Library 27 | 28 | signature COUNTER 29 | structure Counter 30 | 31 | structure Tests 32 | 33 | is 34 | 35 | $/basis.cm 36 | ../testing/sources.cm (* path will need to be adjusted *) 37 | sources.cm 38 | 39 | tests.sml 40 | -------------------------------------------------------------------------------- /Code/example/tests.sml: -------------------------------------------------------------------------------- 1 | (* tests.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | *) 25 | 26 | structure Tests = 27 | struct 28 | 29 | local 30 | open Check (* the testing framework *) 31 | open Counter (* the module being tested *) 32 | in 33 | 34 | fun run () = runSuite "Counter" (fn test => ( 35 | (* test new *) 36 | test "new-1" (expectIntInf 0 (fn () => read (new ()))); 37 | (* test update *) 38 | (* test read *) 39 | ())) 40 | 41 | end (* local *) 42 | 43 | end 44 | -------------------------------------------------------------------------------- /Code/testing/README.md: -------------------------------------------------------------------------------- 1 | This directory contains some support code for writing Basis Library tests. 2 | Read the comments in `check.sml` for more information. 3 | -------------------------------------------------------------------------------- /Code/testing/check.sml: -------------------------------------------------------------------------------- 1 | (* check.sml 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * Support code for testing Basis Library reference implementations. 26 | *) 27 | 28 | structure Check : sig 29 | 30 | (* the checked result of a test. Either ok, failure because of the wrong value 31 | * being returned, or failure because of an unexpected exception being raised. 32 | *) 33 | datatype result = OK | FAIL | RAISE of exn 34 | 35 | (* convert a result to a string *) 36 | val toString : result -> string 37 | 38 | (* `expect pred tst` takes a predicate and a test. It runs the test and 39 | * returns 40 | * OK if the function returns a value that satisfies the predicate 41 | * FAIL if the function returns a value that fails the predicate 42 | * RAISE ex if the function raises the exception ex 43 | *) 44 | val expect : ('a -> bool) -> (unit -> 'a) -> unit -> result 45 | 46 | (* `expectVal eq v tst` takes an equality test, an expected value, and a test. 47 | * It runs the test and checks the result against the expected value. 48 | *) 49 | val expectVal : ('a * 'a -> bool) -> 'a -> (unit -> 'a) -> unit -> result 50 | 51 | (* like expectVal, except that it only works on equality types *) 52 | val expectVal' : ''a -> (unit -> ''a) -> unit -> result 53 | 54 | (* expects for common types *) 55 | val expectTrue : (unit -> bool) -> unit -> result 56 | val expectFalse : (unit -> bool) -> unit -> result 57 | val expectStr : string -> (unit -> string) -> unit -> result 58 | val expectInt : int -> (unit -> int) -> unit -> result 59 | val expectIntInf : IntInf.int -> (unit -> IntInf.int) -> unit -> result 60 | val expectWrd : word -> (unit -> word) -> unit -> result 61 | val expectNone : (unit -> 'a option) -> unit -> result 62 | val expectSome : ('a * 'a -> bool) -> 'a -> (unit -> 'a option) -> unit -> result 63 | val expectSome' : ''a -> (unit -> ''a option) -> unit -> result 64 | val expectNil : (unit -> 'a list) -> unit -> result 65 | val expectList : ('a * 'a -> bool) -> 'a list -> (unit -> 'a list) -> unit -> result 66 | 67 | (* `expectExn isExn tst` takes a predicate on exceptions and a test. 68 | * It runs the test and returns 69 | * OK if the function raises an exception that satisfies the predicate 70 | * FAIL if the function returns a value 71 | * RAISE ex if the function raises an exception ex that does not satisfy the predicate 72 | * 73 | * It runs the test and returns "ok" if an exception was raised that 74 | * satisfies the predicate. Otherwise it returns "fail". 75 | *) 76 | val expectExn : (exn -> bool) -> (unit -> 'a) -> unit -> result 77 | 78 | (* expects for many of the basis exceptions *) 79 | val expectSubscript : (unit -> 'a) -> unit -> result 80 | val expectSize : (unit -> 'a) -> unit -> result 81 | val expectOverflow : (unit -> 'a) -> unit -> result 82 | val expectChr : (unit -> 'a) -> unit -> result 83 | val expectDiv : (unit -> 'a) -> unit -> result 84 | val expectDomain : (unit -> 'a) -> unit -> result 85 | val expectSpan : (unit -> 'a) -> unit -> result 86 | val expectOption : (unit -> 'a) -> unit -> result 87 | 88 | (* a wrapper for testing imperative functions using a reference as the state. 89 | * 90 | * withRef init tst 91 | * 92 | * builds a test is equivalent to 93 | * 94 | * let val r = ref init in tst r; !r end 95 | *) 96 | val withRef : 'a -> ('a ref -> unit) -> unit -> 'a 97 | 98 | (* a suite provides a context for the tests associated with a given module *) 99 | type suite 100 | 101 | (* create a suite for the named module that reports results to standard output *) 102 | val new : string -> suite 103 | 104 | (* create a suite for the named module that reports results to the file module.results. 105 | * The boolean flag controls the verbosity of the output; if true, then only summary 106 | * results are reported. 107 | *) 108 | val new' : string * bool -> suite 109 | 110 | (* report the results for the suite; if the suite has an associated output file, then 111 | * the file is closed after reporting the results. 112 | *) 113 | val report : suite -> unit 114 | 115 | (* a general function for running a test in a given suite. Some typical uses are 116 | * 117 | * test "Option" "getOpt-1" (expectInt 42 (fn () => getOpt(SOME 42, 17))) 118 | * test "Option" "getOpt-2" (expectInt 17 (fn () => getOpt(NONE, 17))) 119 | * test "Option" "valOf-1" (expectOption (fn () => valOf NONE)) 120 | * 121 | * which will produce the output 122 | * 123 | * Option.getOpt-1 ok 124 | * Option.getOpt-2 ok 125 | * Option.valOf-1 ok 126 | *) 127 | val test : suite -> string -> (unit -> result) -> unit 128 | 129 | (* `runSuite module runTests` creates a suite `s`, applies `runTests` to `test s`, and 130 | * then reports the results. 131 | *) 132 | val runSuite : string -> ((string -> (unit -> result) -> unit) -> unit) -> unit 133 | 134 | end = struct 135 | 136 | datatype result = OK | FAIL | RAISE of exn 137 | 138 | fun toString OK = "ok" 139 | | toString FAIL = "fail" 140 | | toString (RAISE ex) = String.concat["fail[", General.exnName ex, "]"] 141 | 142 | datatype 'a return = VAL of 'a | EXN of exn 143 | 144 | (* run a test returning a result *) 145 | fun run tst = VAL(tst()) handle ex => EXN ex 146 | 147 | fun expect chk tst () = (case run tst 148 | of VAL v => if chk v then OK else FAIL 149 | | EXN ex => RAISE ex 150 | (* end case *)) 151 | 152 | (* some expects for common types *) 153 | fun expectVal eq v = expect (fn v' => eq(v, v')) 154 | fun expectVal' v = expect (fn v' => (v = v')) 155 | val expectTrue = expect (fn x => x) 156 | val expectFalse = expect Bool.not 157 | fun expectStr (s : string) = expectVal (op =) s 158 | fun expectInt (i : int) = expectVal (op =) i 159 | fun expectIntInf (i : IntInf.int) = expectVal (op =) i 160 | fun expectWrd (w : word) = expectVal (op =) w 161 | fun expectNone tst = expect (fn NONE => true | _ => false) tst 162 | fun expectSome eq v = expect (fn SOME v' => eq(v, v') | _ => false) 163 | fun expectSome' v = expect (fn SOME v' => (v = v') | _ => false) 164 | fun expectNil tst = expect (fn [] => true | _ => false) tst 165 | fun expectList eq = expectVal (ListPair.allEq eq) 166 | 167 | fun expectExn chk tst () = (case run tst 168 | of VAL v => FAIL 169 | | EXN ex => if chk ex then OK else RAISE ex 170 | (* end case *)) 171 | 172 | (* expects for many of the basis exceptions *) 173 | fun expectSubscript tst = expectExn (fn General.Subscript => true | _ => false) tst 174 | fun expectSize tst = expectExn (fn General.Size => true | _ => false) tst 175 | fun expectOverflow tst = expectExn (fn General.Overflow => true | _ => false) tst 176 | fun expectChr tst = expectExn (fn General.Chr => true | _ => false) tst 177 | fun expectDiv tst = expectExn (fn General.Div => true | _ => false) tst 178 | fun expectDomain tst = expectExn (fn General.Domain => true | _ => false) tst 179 | fun expectSpan tst = expectExn (fn General.Span => true | _ => false) tst 180 | fun expectOption tst = expectExn (fn Option.Option => true | _ => false) tst 181 | 182 | (* support for testing imperative operations *) 183 | fun withRef init tst () = let val r = ref init in tst r; !r end 184 | 185 | (* a suite provides a context for the tests associated with a given module *) 186 | datatype suite = Suite of { 187 | module : string, (* the module name *) 188 | outS : TextIO.outstream option, (* NONE is stdOut *) 189 | quiet : bool, (* if true, then only summary results are reported *) 190 | nTests : int ref, (* the number of tests run *) 191 | nFails : int ref, (* the number of tests that returned the wrong result *) 192 | nExns : int ref (* the number of tests that raised an exception *) 193 | } 194 | 195 | fun streamOf strmOpt = getOpt (strmOpt, TextIO.stdOut) 196 | 197 | fun pr (strmOpt, s) = TextIO.output (streamOf strmOpt, s) 198 | 199 | fun flush strmOpt = TextIO.flushOut (streamOf strmOpt) 200 | 201 | fun create (module, strm, q) = ( 202 | if q then () else pr (strm, String.concat["** Testing ", module, "\n"]); 203 | Suite{ 204 | module = module, 205 | outS = strm, 206 | quiet = q, 207 | nTests = ref 0, 208 | nFails = ref 0, 209 | nExns = ref 0 210 | }) 211 | 212 | (* create a suite for the named module that reports results to standard output *) 213 | fun new module = create (module, NONE, false) 214 | 215 | (* create a suite for the named module that reports results to the file module.results *) 216 | fun new' (module, quiet) = create ( 217 | module, 218 | SOME(TextIO.openOut(OS.Path.joinBaseExt{base=module, ext=SOME "results"})), 219 | quiet) 220 | 221 | (* report the results for the suite; if the suite has an associated output file, then 222 | * the file is closed after reporting the results. 223 | *) 224 | fun report (Suite{module, outS, nTests, nFails, nExns, ...}) = let 225 | val nBad = !nFails + !nExns 226 | in 227 | if !nTests = 0 228 | then pr(outS, "!! no tests\n") 229 | else ( 230 | pr(outS, String.concat[ 231 | "** Summary for ", module, ": ", Int.toString(!nTests), " tests; ", 232 | Int.toString(!nTests - nBad), " passed" 233 | ]); 234 | if nBad > 0 235 | then pr(outS, String.concat["; ", Int.toString nBad, " falures\n"]) 236 | else pr(outS, "\n")); 237 | Option.app TextIO.closeOut outS 238 | end 239 | 240 | (* run a named test and report the result *) 241 | fun test suite = let 242 | val Suite{module, outS, quiet, nTests, nFails, nExns, ...} = suite 243 | fun inc (r as ref n) = r := n+1 244 | fun test' name tst = ( 245 | if quiet 246 | then () 247 | else ( 248 | pr(outS, StringCvt.padRight #" " 40 (String.concat["-- ", module, ".", name, " "])); 249 | flush outS); 250 | inc nTests; 251 | case tst () 252 | of OK => if quiet then () else pr(outS, "ok\n") 253 | | FAIL => (inc nFails; if quiet then () else pr(outS, "fail\n")) 254 | | (RAISE ex) => ( 255 | inc nExns; 256 | if quiet then () else pr(outS, String.concat["fail[", General.exnName ex, "]\n"]))) 257 | in 258 | test' 259 | end 260 | 261 | fun runSuite module runTests = let 262 | val suite = new' (module, false) 263 | in 264 | runTests (test suite); 265 | report suite 266 | end 267 | 268 | end 269 | -------------------------------------------------------------------------------- /Code/testing/sources.cm: -------------------------------------------------------------------------------- 1 | (* sources.cm 2 | * 3 | * ============================================================================ 4 | * Copyright (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ============================================================================ 24 | * 25 | * SML/NJ CM file for testing support code. 26 | *) 27 | 28 | Library 29 | structure Check 30 | is 31 | $/basis.cm 32 | check.sml 33 | 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Standard ML Basis Library 2 | 3 | This repository and associated [wiki](https://github.com/SMLFamily/BasisLibrary/wiki) provides a platform for managing changes to the [Standard ML Basis Library](http://sml-family.org/Basis/) specification. 4 | 5 | ## Scope 6 | The SML Basis Library was purposefully designed to be limited in its scope. Specifically, it included features that satisified one of the following properties: 7 | 8 | - features that required special compiler support, such as strings and arrays. 9 | - features that require an interface to the host operating system, such as sockets and file-system access. 10 | - features that are more concise or efficient than an equivalent combination of other features. 11 | - features that have clear or proven utility or were part of previous versions of the SML Basis (*e.g.*, the SML'90 Basis). 12 | 13 | These properties are meant to serve as guidelines for what belongs in the Basis Library, instead of in an utility or application-specific library. 14 | 15 | ## Conventions 16 | Proposals for new Basis Library modules should follow the coding conventions used in the published version of the original specification. These conventions are 17 | 18 | - Alphanumeric value identifiers are in mixed-case, with a leading lowercase letter; *e.g.*, `map` and `openIn`. 19 | - Type identifiers are all lowercase, with words separated by underscores; *e.g.*, `word` and `file_desc`. 20 | - Signature identifiers are in all capitals, with words separated by underscores; *e.g.*, `PACK_WORD` and `OS_PATH`. We refer to this convention as the *signature* convention. 21 | - Structure and functor identifiers are in mixed-case, with initial letters of words capitalized; *e.g.*, `General` and `WideChar`. We refer to this convention as the *structure* convention. 22 | - Alphanumeric datatype constructors follow the signature convention; *e.g.*, `SOME`, `A_READ`, and `FOLLOW_ALL`. In certain cases, where external usage or aesthetics dictates otherwise, the structure convention can be used. Within the Basis library, the only use of the latter convention occurs with the months and weekdays in `Date`, *e.g.*, `Jan` and `Mon`. 23 | The one exception to this rule are the traditional constructors `nil`, `true`, and `false`. 24 | - Exception identifiers follow the structure convention; *e.g.*, `Domain` and `SysErr`. 25 | 26 | ## Process 27 | We need to develop a process for how to handling proposals. Issues include 28 | 29 | - what should a proposal contain 30 | - how should proposals be submitted (is there an editor?) 31 | - how to resolve discussion and approve a proposal 32 | 33 | One possible model is the [SRFI](http://srfi.schemers.org) process. 34 | --------------------------------------------------------------------------------