|
| 1 | +----------------------------------------------------------------------- |
| 2 | +-- Test for the Curry type class and curryN/uncurryN functions |
| 3 | +----------------------------------------------------------------------- |
| 4 | + |
| 5 | +package CurryTypeClass where |
| 6 | + |
| 7 | +import Vector |
| 8 | + |
| 9 | +----------------------------------------------------------------------- |
| 10 | +-- Test with Tuple2 |
| 11 | +----------------------------------------------------------------------- |
| 12 | + |
| 13 | +add2Tuple :: (Int 32, Int 32) -> Int 32 |
| 14 | +add2Tuple t = tpl_1 t + tpl_2 t |
| 15 | + |
| 16 | +add2 :: Int 32 -> Int 32 -> Int 32 |
| 17 | +add2 a b = a + b |
| 18 | + |
| 19 | +testCurry2 :: Bool |
| 20 | +testCurry2 = |
| 21 | + let curried :: Int 32 -> Int 32 -> Int 32 |
| 22 | + curried = curryN add2Tuple |
| 23 | + result1 :: Int 32 |
| 24 | + result1 = curried 5 10 |
| 25 | + uncurried :: (Int 32, Int 32) -> Int 32 |
| 26 | + uncurried = uncurryN add2 |
| 27 | + result2 :: Int 32 |
| 28 | + result2 = uncurried (5, 10) |
| 29 | + in result1 == 15 && result2 == 15 |
| 30 | + |
| 31 | + |
| 32 | +----------------------------------------------------------------------- |
| 33 | +-- Test with Tuple3 |
| 34 | +----------------------------------------------------------------------- |
| 35 | + |
| 36 | +add3Tuple :: (Int 32, Int 32, Int 32) -> Int 32 |
| 37 | +add3Tuple t = tpl_1 t + tpl_2 t + tpl_3 t |
| 38 | + |
| 39 | +add3 :: Int 32 -> Int 32 -> Int 32 -> Int 32 |
| 40 | +add3 a b c = a + b + c |
| 41 | + |
| 42 | +testCurry3 :: Bool |
| 43 | +testCurry3 = |
| 44 | + let curried :: Int 32 -> Int 32 -> Int 32 -> Int 32 |
| 45 | + curried = curryN add3Tuple |
| 46 | + result1 :: Int 32 |
| 47 | + result1 = curried 1 2 3 |
| 48 | + uncurried :: (Int 32, Int 32, Int 32) -> Int 32 |
| 49 | + uncurried = uncurryN add3 |
| 50 | + result2 :: Int 32 |
| 51 | + result2 = uncurried (1, 2, 3) |
| 52 | + in result1 == 6 && result2 == 6 |
| 53 | + |
| 54 | + |
| 55 | +----------------------------------------------------------------------- |
| 56 | +-- Test with Tuple4 |
| 57 | +----------------------------------------------------------------------- |
| 58 | + |
| 59 | +add4Tuple :: (Int 32, Int 32, Int 32, Int 32) -> Int 32 |
| 60 | +add4Tuple t = tpl_1 t + tpl_2 t + tpl_3 t + tpl_4 t |
| 61 | + |
| 62 | +add4 :: Int 32 -> Int 32 -> Int 32 -> Int 32 -> Int 32 |
| 63 | +add4 a b c d = a + b + c + d |
| 64 | + |
| 65 | +testCurry4 :: Bool |
| 66 | +testCurry4 = |
| 67 | + let curried :: Int 32 -> Int 32 -> Int 32 -> Int 32 -> Int 32 |
| 68 | + curried = curryN add4Tuple |
| 69 | + result1 :: Int 32 |
| 70 | + result1 = curried 1 2 3 4 |
| 71 | + uncurried :: (Int 32, Int 32, Int 32, Int 32) -> Int 32 |
| 72 | + uncurried = uncurryN add4 |
| 73 | + result2 :: Int 32 |
| 74 | + result2 = uncurried (1, 2, 3, 4) |
| 75 | + in result1 == 10 && result2 == 10 |
| 76 | + |
| 77 | + |
| 78 | +----------------------------------------------------------------------- |
| 79 | +-- Test roundtrip: curryN . uncurryN = id and uncurryN . curryN = id |
| 80 | +----------------------------------------------------------------------- |
| 81 | + |
| 82 | +testRoundtrip :: Bool |
| 83 | +testRoundtrip = |
| 84 | + -- Test that uncurrying then currying gets back the original behavior |
| 85 | + let temp :: (Int 32, Int 32, Int 32) -> Int 32 |
| 86 | + temp = uncurryN add3 |
| 87 | + f1 :: Int 32 -> Int 32 -> Int 32 -> Int 32 |
| 88 | + f1 = curryN temp |
| 89 | + result1 :: Int 32 |
| 90 | + result1 = f1 10 20 30 |
| 91 | + -- Test that currying then uncurrying gets back the original behavior |
| 92 | + f2 :: (Int 32, Int 32, Int 32) -> Int 32 |
| 93 | + f2 = uncurryN (curryN add3Tuple) |
| 94 | + result2 :: Int 32 |
| 95 | + result2 = f2 (10, 20, 30) |
| 96 | + in result1 == 60 && result2 == 60 |
| 97 | + |
| 98 | + |
| 99 | +----------------------------------------------------------------------- |
| 100 | +-- Test with unit tuple (special case) |
| 101 | +----------------------------------------------------------------------- |
| 102 | + |
| 103 | +fromUnitTuple :: () -> Int 32 |
| 104 | +fromUnitTuple _ = 42 |
| 105 | + |
| 106 | +testUnitTuple :: Bool |
| 107 | +testUnitTuple = |
| 108 | + let curried :: Int 32 |
| 109 | + curried = curryN fromUnitTuple |
| 110 | + in curried == 42 |
| 111 | + |
| 112 | + |
| 113 | +----------------------------------------------------------------------- |
| 114 | +-- Module for testing |
| 115 | +----------------------------------------------------------------------- |
| 116 | + |
| 117 | +{-# verilog sysCurryTypeClass #-} |
| 118 | +sysCurryTypeClass :: Module Empty |
| 119 | +sysCurryTypeClass = |
| 120 | + module |
| 121 | + rules |
| 122 | + "test": when True ==> |
| 123 | + action |
| 124 | + $display "Test Curry2: %b" testCurry2 |
| 125 | + $display "Test Curry3: %b" testCurry3 |
| 126 | + $display "Test Curry4: %b" testCurry4 |
| 127 | + $display "Test Roundtrip: %b" testRoundtrip |
| 128 | + $display "Test Unit Tuple: %b" testUnitTuple |
| 129 | + $finish 0 |
0 commit comments