|
| 1 | +-- Generate appendTree<0..4> and addDigits<1..4> for Data.Sequence |
| 2 | +module Main where |
| 3 | + |
| 4 | +main :: IO () |
| 5 | +main = putStr (compose [showAppend n | n <- [0..4]] "") |
| 6 | + |
| 7 | +showAppend :: Int -> ShowS |
| 8 | +showAppend n = |
| 9 | + showChar '\n' . |
| 10 | + showString "appendTree" . shows n . showString " :: " . |
| 11 | + showFunType |
| 12 | + ([fingertree] ++ replicate n tyarg ++ [fingertree]) fingertree . |
| 13 | + showString "\n" . |
| 14 | + appendTreeClause "EmptyT" "xs" (showCons (args n) (showString "xs")) . |
| 15 | + appendTreeClause "xs" "EmptyT" (showSnoc (showString "xs") (args n)) . |
| 16 | + appendTreeClause "(Single x)" "xs" |
| 17 | + (showCons ('x':args n) (showString "xs")) . |
| 18 | + appendTreeClause "xs" "(Single x)" |
| 19 | + (showSnoc (showString "xs") (args n++"x")) . |
| 20 | + appendTreeClause "(Deep s1 pr1 m1 sf1)" "(Deep s2 pr2 m2 sf2)" |
| 21 | + (showString "Deep (s1" . |
| 22 | + compose [showString " + size " . showChar v | v <- args n] . |
| 23 | + showString " + s2) pr1 (addDigits" . shows n . |
| 24 | + showString " m1 sf1" . showArgList (args n) . |
| 25 | + showString " pr2 m2) sf2") . |
| 26 | + showChar '\n' . |
| 27 | + showString "addDigits" . shows n . showString " :: " . |
| 28 | + showFunType |
| 29 | + ([fingertree_node, digit] ++ replicate n tyarg ++ [digit, fingertree_node]) |
| 30 | + fingertree_node . |
| 31 | + showString "\n" . |
| 32 | + compose [addDigitsClause n1 n2 | n1 <- [1..4], n2 <- [1..4]] |
| 33 | + where |
| 34 | + fingertree = tyapp "FingerTree" tyarg |
| 35 | + digit = tyapp "Digit" tyarg |
| 36 | + fingertree_node = tyapp "FingerTree" (tyapp "Node" tyarg) |
| 37 | + showFunType ts tr = |
| 38 | + compose [showString t . showString " -> " | t <- ts] . showString tr |
| 39 | + tyapp tc t = tc ++ " (" ++ t ++ ")" |
| 40 | + tyarg |
| 41 | + | n == 0 = "Elem a" |
| 42 | + | otherwise = "Node a" |
| 43 | + appendTreeClause t1 t2 rhs = |
| 44 | + showString "appendTree" . shows n . |
| 45 | + showChar ' ' . showString t1 . showArgList (args n) . |
| 46 | + showChar ' ' . showString t2 . |
| 47 | + showString " =\n " . rhs . showChar '\n' |
| 48 | + addDigitsClause n1 n2 = |
| 49 | + showString "addDigits" . shows n . |
| 50 | + showString " m1 (" . showDigit vs1 . showChar ')' . |
| 51 | + showArgList vsm . |
| 52 | + showString " (" . showDigit vs2 . showString ") m2" . |
| 53 | + showString " =\n " . |
| 54 | + showString "appendTree" . shows (length ns) . |
| 55 | + showString " m1" . |
| 56 | + compose [showString " (" . showNode node . showChar ')' | |
| 57 | + node <- ns] . |
| 58 | + showString " m2" . showChar '\n' |
| 59 | + where |
| 60 | + vs = args (n1+n+n2) |
| 61 | + vs1 = take n1 vs |
| 62 | + vsm = take n (drop n1 vs) |
| 63 | + vs2 = drop (n1+n) vs |
| 64 | + ns = nodes vs |
| 65 | + |
| 66 | +data Node a = Node2 a a | Node3 a a a |
| 67 | + |
| 68 | +nodes :: [a] -> [Node a] |
| 69 | +nodes [a, b] = [Node2 a b] |
| 70 | +nodes [a, b, c] = [Node3 a b c] |
| 71 | +nodes [a, b, c, d] = [Node2 a b, Node2 c d] |
| 72 | +nodes (a:b:c:xs) = Node3 a b c : nodes xs |
| 73 | + |
| 74 | +showNode (Node2 a b) = |
| 75 | + showString "node2 " . showChar a . showChar ' ' . showChar b |
| 76 | +showNode (Node3 a b c) = |
| 77 | + showString "node3 " . showChar a . showChar ' ' . showChar b . |
| 78 | + showChar ' ' . showChar c |
| 79 | + |
| 80 | +showDigit vs = |
| 81 | + showString (["One", "Two", "Three", "Four"]!!(length vs-1)) . |
| 82 | + showArgList vs |
| 83 | + |
| 84 | +showArgList :: [Char] -> ShowS |
| 85 | +showArgList vs = compose [showChar ' ' . showChar c | c <- vs] |
| 86 | + |
| 87 | +args :: Int -> [Char] |
| 88 | +args n = take n ['a'..] |
| 89 | + |
| 90 | +showCons xs sf = |
| 91 | + compose [showChar x . showString " `consTree` " | x <- xs] . sf |
| 92 | +showSnoc sf xs = |
| 93 | + sf . compose [showString " `snocTree` " . showChar x | x <- xs] |
| 94 | + |
| 95 | +compose :: [a -> a] -> a -> a |
| 96 | +compose = flip (foldr id) |
0 commit comments