annotate src/list_util.sml @ 88:7bab29834cd6

Constraints in modules
author Adam Chlipala <adamc@hcoop.net>
date Tue, 01 Jul 2008 15:58:02 -0400
parents 02f42e9a1825
children 3739af9e727a
rev   line source
adamc@5 1 (* Copyright (c) 2008, Adam Chlipala
adamc@5 2 * All rights reserved.
adamc@5 3 *
adamc@5 4 * Redistribution and use in source and binary forms, with or without
adamc@5 5 * modification, are permitted provided that the following conditions are met:
adamc@5 6 *
adamc@5 7 * - Redistributions of source code must retain the above copyright notice,
adamc@5 8 * this list of conditions and the following disclaimer.
adamc@5 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@5 10 * this list of conditions and the following disclaimer in the documentation
adamc@5 11 * and/or other materials provided with the distribution.
adamc@5 12 * - The names of contributors may not be used to endorse or promote products
adamc@5 13 * derived from this software without specific prior written permission.
adamc@5 14 *
adamc@5 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@5 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@5 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@5 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@5 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@5 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@5 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@5 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@5 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@5 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@5 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@5 26 *)
adamc@5 27
adamc@5 28 structure ListUtil :> LIST_UTIL = struct
adamc@5 29
adamc@6 30 structure S = Search
adamc@6 31
adamc@6 32 fun mapfold f =
adamc@6 33 let
adamc@6 34 fun mf ls s =
adamc@6 35 case ls of
adamc@6 36 nil => S.Continue (nil, s)
adamc@6 37 | h :: t =>
adamc@6 38 case f h s of
adamc@6 39 S.Return x => S.Return x
adamc@6 40 | S.Continue (h', s) =>
adamc@6 41 case mf t s of
adamc@6 42 S.Return x => S.Return x
adamc@6 43 | S.Continue (t', s) => S.Continue (h' :: t', s)
adamc@6 44 in
adamc@6 45 mf
adamc@6 46 end
adamc@6 47
adamc@34 48 fun mapfoldB f =
adamc@34 49 let
adamc@34 50 fun mf ctx ls s =
adamc@34 51 case ls of
adamc@34 52 nil => S.Continue (nil, s)
adamc@34 53 | h :: t =>
adamc@34 54 let
adamc@34 55 val (ctx, r) = f (ctx, h)
adamc@34 56 in
adamc@34 57 case r s of
adamc@34 58 S.Return x => S.Return x
adamc@34 59 | S.Continue (h', s) =>
adamc@34 60 case mf ctx t s of
adamc@34 61 S.Return x => S.Return x
adamc@34 62 | S.Continue (t', s) => S.Continue (h' :: t', s)
adamc@34 63 end
adamc@34 64 in
adamc@34 65 mf
adamc@34 66 end
adamc@34 67
adamc@26 68 fun foldlMap f s =
adamc@26 69 let
adamc@26 70 fun fm (ls', s) ls =
adamc@26 71 case ls of
adamc@26 72 nil => (rev ls', s)
adamc@26 73 | h :: t =>
adamc@26 74 let
adamc@26 75 val (h', s') = f (h, s)
adamc@26 76 in
adamc@26 77 fm (h' :: ls', s') t
adamc@26 78 end
adamc@26 79 in
adamc@26 80 fm ([], s)
adamc@26 81 end
adamc@26 82
adamc@39 83 fun foldlMapConcat f s =
adamc@39 84 let
adamc@39 85 fun fm (ls', s) ls =
adamc@39 86 case ls of
adamc@39 87 nil => (rev ls', s)
adamc@39 88 | h :: t =>
adamc@39 89 let
adamc@39 90 val (h', s') = f (h, s)
adamc@39 91 in
adamc@39 92 fm (List.revAppend (h', ls'), s') t
adamc@39 93 end
adamc@39 94 in
adamc@39 95 fm ([], s)
adamc@39 96 end
adamc@39 97
adamc@39 98 fun foldlMapPartial f s =
adamc@39 99 let
adamc@39 100 fun fm (ls', s) ls =
adamc@39 101 case ls of
adamc@39 102 nil => (rev ls', s)
adamc@39 103 | h :: t =>
adamc@39 104 let
adamc@39 105 val (h', s') = f (h, s)
adamc@39 106 val ls' = case h' of
adamc@39 107 NONE => ls'
adamc@39 108 | SOME h' => h' :: ls'
adamc@39 109 in
adamc@39 110 fm (ls', s') t
adamc@39 111 end
adamc@39 112 in
adamc@39 113 fm ([], s)
adamc@39 114 end
adamc@39 115
adamc@23 116 fun search f =
adamc@23 117 let
adamc@23 118 fun s ls =
adamc@23 119 case ls of
adamc@23 120 [] => NONE
adamc@23 121 | h :: t =>
adamc@23 122 case f h of
adamc@23 123 NONE => s t
adamc@23 124 | v => v
adamc@23 125 in
adamc@23 126 s
adamc@23 127 end
adamc@23 128
adamc@5 129 end