| 1 | (******************************************************************************* |
|---|
| 2 | Generator-of-Generators Library is copyrighted free software by Kento Emoto |
|---|
| 3 | <emoto[at]ipl.t.u-tokyo.ac.jp> developed under the collaborative |
|---|
| 4 | research on Fortress Programming Language between Sun Microsystems, |
|---|
| 5 | Inc. and the University of Tokyo. |
|---|
| 6 | |
|---|
| 7 | You can redistribute it and/or modify it under the following |
|---|
| 8 | BSD-style license or the Sun Contributor Agreement that Kento Emoto signed. |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | Copyright 2009 by Kento Emoto |
|---|
| 12 | All rights reserved. |
|---|
| 13 | |
|---|
| 14 | Redistribution and use in source and binary forms, with or without |
|---|
| 15 | modification, are permitted provided that the following conditions |
|---|
| 16 | are met: |
|---|
| 17 | |
|---|
| 18 | * Redistributions of source code must retain the above copyright |
|---|
| 19 | notice, this list of conditions and the following disclaimer. |
|---|
| 20 | * Redistributions in binary form must reproduce the above copyright |
|---|
| 21 | notice, this list of conditions and the following disclaimer |
|---|
| 22 | in the documentation and/or other materials provided with the |
|---|
| 23 | distribution. |
|---|
| 24 | * Neither the name of Kento Emoto nor the names of its |
|---|
| 25 | contributors may be used to endorse or promote products derived |
|---|
| 26 | from this software without specific prior written permission. |
|---|
| 27 | |
|---|
| 28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|---|
| 29 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|---|
| 30 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 31 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|---|
| 32 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|---|
| 33 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|---|
| 34 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|---|
| 35 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 36 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 37 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 38 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | ******************************************************************************) |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | component Generator2Demo |
|---|
| 45 | |
|---|
| 46 | import List.{...} |
|---|
| 47 | import Set.{...} |
|---|
| 48 | import Map.{...} |
|---|
| 49 | (* |
|---|
| 50 | import Heap.{...} |
|---|
| 51 | *) |
|---|
| 52 | import Generator2.{...} |
|---|
| 53 | export Executable |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | id(x) = x |
|---|
| 57 | snd(x) = do (a, b) = x; b end |
|---|
| 58 | |
|---|
| 59 | run() : () = do |
|---|
| 60 | |
|---|
| 61 | (* arrays *) |
|---|
| 62 | xit = array[\Number\]( 400 ).fill(fn (x:ZZ32):Number => |\random(10)-5/|) |
|---|
| 63 | xs = array[\Number\]( 50 ).fill(fn (x:ZZ32):Number => |\random(10)-5/|) |
|---|
| 64 | xu = array[\Number\]( 5 ).fill(fn (x:ZZ32):Number => |\random(10)-5/|) |
|---|
| 65 | |
|---|
| 66 | (* lists *) |
|---|
| 67 | lxit = <|[\Number\] a | a <- xit|> |
|---|
| 68 | lxs = <|[\Number\] a | a <- xs|> |
|---|
| 69 | lxu = <|[\Number\] a | a <- xu|> |
|---|
| 70 | |
|---|
| 71 | (* sets *) |
|---|
| 72 | sxit = {[\Number\] a | a <- xit} |
|---|
| 73 | sxs = {[\Number\] a | a <- xs} |
|---|
| 74 | sxu = {[\Number\] a | a <- xu} |
|---|
| 75 | |
|---|
| 76 | (* maps *) |
|---|
| 77 | mxit = {[\Number,Number\] i |-> xit[i] | i <- (0#(|xit|)) } |
|---|
| 78 | mxs = {[\Number,Number\] i |-> xs[i] | i <- (0#(|xs|)) } |
|---|
| 79 | mxu = {[\Number,Number\] i |-> xu[i] | i <- (0#(|xu|)) } |
|---|
| 80 | |
|---|
| 81 | println("With optimization by dispatching in GGs (fast!)") |
|---|
| 82 | demo[\Number,Number\]("Array", xit, xs, xu, id) |
|---|
| 83 | demo[\Number,Number\]("List", lxit, lxs, lxu, id) |
|---|
| 84 | demo[\Number,Number\]("Set", sxit, sxs, sxu, id) |
|---|
| 85 | demo[\(Number,Number),Number\]("Map", mxit, mxs, mxu, snd) |
|---|
| 86 | switchDispatching(false) |
|---|
| 87 | println("") |
|---|
| 88 | println("") |
|---|
| 89 | println("Without optimization by dispatching in GGs (slow ;;)") |
|---|
| 90 | demo[\Number,Number\]("Array", xit, xs, xu, id) |
|---|
| 91 | demo[\Number,Number\]("List", lxit, lxs, lxu, id) |
|---|
| 92 | demo[\Number,Number\]("Set", sxit, sxs, sxu, id) |
|---|
| 93 | demo[\(Number,Number),Number\]("Map", mxit, mxs, mxu, snd) |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | end |
|---|
| 97 | |
|---|
| 98 | demo[\E,F\](str:String, xit:Generator[\E\], xs:Generator[\E\], xu:Generator[\E\], f : E -> F) = do |
|---|
| 99 | println("Applying GGs to " str) |
|---|
| 100 | testing[\E, F\]( xit, inits[\E\], f, "inits" ) |
|---|
| 101 | testing[\E, F\]( xit, tails[\E\], f, "tails" ) |
|---|
| 102 | testing[\E, F\]( xs, segs[\E\], f, "segs" ) |
|---|
| 103 | testing[\E, F\]( xu, subs[\E\], f, "subs" ) |
|---|
| 104 | end |
|---|
| 105 | |
|---|
| 106 | testing[\E,F\](x:Generator[\E\], gg : Generator[\E\] -> Generator2[\E\], f:E->F, ggstr : String) = do |
|---|
| 107 | |
|---|
| 108 | p1 = relationalPredicate[\E\](fn (a, b) => |f a - f b| < 4) |
|---|
| 109 | p2 = relationalPredicate[\E\](fn (a, b) => f a < f b) |
|---|
| 110 | |
|---|
| 111 | xi = gg(x) |
|---|
| 112 | xif = xi.filter(p1) |
|---|
| 113 | xiff = xif.filter(p2) |
|---|
| 114 | |
|---|
| 115 | (* |
|---|
| 116 | println("x = " x ) |
|---|
| 117 | println(ggstr "x = " ( list(xi) ) ) |
|---|
| 118 | println(ggstr "x filtered by flat4 = " ( list(xif) ) ) |
|---|
| 119 | println(ggstr "x filtered by flat4 and ascending = " ( list(xiff) ) ) |
|---|
| 120 | *) |
|---|
| 121 | psxi = SUM[\F\] <|[\F\] PROD[\F\] <|[\F\] f y | y <- ys |> | ys <- xi |> |
|---|
| 122 | println("product sum of " ggstr " of x = " psxi) |
|---|
| 123 | |
|---|
| 124 | psxif = SUM[\F\] <|[\F\] PROD[\F\] <|[\F\] f y | y <- ys |> | ys <- xi, p1 ys |> |
|---|
| 125 | println("product sum of " ggstr " of x filtered by flat4 = " psxif) |
|---|
| 126 | |
|---|
| 127 | psxiff = SUM[\F\] <|[\F\] PROD[\F\] <|[\F\] f y | y <- ys |> | ys <- xi, p1 ys, p2 ys |> |
|---|
| 128 | println("product sum of " ggstr " of x filtered by flat4 and ascending = " psxiff) |
|---|
| 129 | |
|---|
| 130 | msxi = BIG MAX[\F\] <|[\F\] SUM[\F\] <|[\F\] f y | y <- ys |> | ys <- xi |> |
|---|
| 131 | println("max sum of " ggstr " of x = " msxi) |
|---|
| 132 | |
|---|
| 133 | msxif = BIG MAX[\F\] <|[\F\] SUM[\F\] <|[\F\] f y | y <- ys |> | ys <- xi, p1 ys |> |
|---|
| 134 | println("max sum of " ggstr " of x filtered by flat4 = " msxif) |
|---|
| 135 | |
|---|
| 136 | msxiff = BIG MAX[\F\] <|[\F\] SUM[\F\] <|[\F\] f y | y <- ys |> | ys <- xi, p1 ys, p2 ys |> |
|---|
| 137 | println("max sum of " ggstr " of x filtered by flat4 and ascending = " msxiff) |
|---|
| 138 | |
|---|
| 139 | end |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | end |
|---|