Skip to content

Commit 5382745

Browse files
SamuelSchlesingerSamuel Schlesinger
andauthored
Add mkappend.hs (#945)
This is the code that was originally used to generate the code to append sequences. Adding it to the repo for history and education. Closes #908. Co-authored-by: Samuel Schlesinger <[email protected]>
1 parent 7b27da8 commit 5382745

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

containers/containers.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cabal-version: >=1.10
2424
extra-source-files:
2525
include/containers.h
2626
changelog.md
27+
mkappend.hs
2728

2829
tested-with: GHC==9.6.1, GHC==9.4.2, GHC==9.2.2, GHC==9.0.2, GHC==8.10.7, GHC==8.8.4, GHC==8.6.5, GHC==8.4.4, GHC==8.2.2, GHC==8.0.2
2930

containers/mkappend.hs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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)

containers/src/Data/Sequence/Internal.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,8 @@ snocTree' (Deep s pr m (One a)) b =
18881888
(><) :: Seq a -> Seq a -> Seq a
18891889
Seq xs >< Seq ys = Seq (appendTree0 xs ys)
18901890

1891-
-- The appendTree/addDigits gunk below is machine generated
1891+
-- The appendTree/addDigits gunk below was originally machine generated via mkappend.hs,
1892+
-- but has since been manually edited to include strictness annotations.
18921893

18931894
appendTree0 :: FingerTree (Elem a) -> FingerTree (Elem a) -> FingerTree (Elem a)
18941895
appendTree0 EmptyT xs =

0 commit comments

Comments
 (0)