Skip to content

Commit b92d738

Browse files
committed
concChoiceSuffix
1 parent 8b7f615 commit b92d738

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

src/main/haskell/Toothpaste.hs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -350,19 +350,31 @@ concFromChoiceList ptl
350350
concFromSeqList :: (Ord a) => [PPTree a] -> [PPTree a]
351351
concFromSeqList ptl
352352
| length rs /= length hds
353-
= map (uncurry convertConcMapEntryToNode) (Map.toList rs)
353+
= map (uncurry convertConcMapEntryToNodePrefix) (Map.toList rs)
354354
| otherwise = ptl
355355
where (hds,tls) = unzip $ map (splitAt 2 . children) ptl
356356
rs = concMapFromSeqChildren hds tls
357357

358-
convertConcMapEntryToNode :: (Ord a) => [PPTree a] -> [[PPTree a]] -> PPTree a
359-
convertConcMapEntryToNode ptl tptl
358+
359+
360+
convertConcMapEntryToNode :: (Ord a) =>
361+
([PPTree a] -> [PPTree a]) -> [PPTree a] -> [[PPTree a]]
362+
-> PPTree a
363+
convertConcMapEntryToNode frc ptl tptl
360364
| onlySilent cr = concP ptl w
361-
| otherwise = seqP [concP ptl w, cr] w
365+
| otherwise = seqP (frc [concP ptl w, cr]) w
362366
where w = sum $ map weight ptl
363367
cc = mapMaybe convertConcTailEntry tptl
364368
cr = singleNodeOp $ choiceSim $ choiceP cc w
365369

370+
convertConcMapEntryToNodePrefix :: (Ord a) =>
371+
[PPTree a] -> [[PPTree a]] -> PPTree a
372+
convertConcMapEntryToNodePrefix = convertConcMapEntryToNode id
373+
374+
convertConcMapEntryToNodeSuffix :: (Ord a) =>
375+
[PPTree a] -> [[PPTree a]] -> PPTree a
376+
convertConcMapEntryToNodeSuffix = convertConcMapEntryToNode reverse
377+
366378
onlySilent :: PPTree a -> Bool
367379
onlySilent (Silent w) = True
368380
onlySilent pt = False
@@ -452,7 +464,7 @@ mergeConcPairT (ptx1,pty1) (ptx2,pty2)
452464

453465

454466
concFromChoiceSuff :: (Eq a, Ord a) => PRule a
455-
concFromChoiceSuff = choiceChildMR concFromChoiceList
467+
concFromChoiceSuff = choiceChildMR concFromChoiceListSuff
456468

457469
concFromChoiceListSuff :: (Eq a, Ord a) => LRule a
458470
concFromChoiceListSuff ptl
@@ -462,18 +474,19 @@ concFromChoiceListSuff ptl
462474
fl = filter (not . isNontrivSeq) ptl
463475
rs = concFromSeqListSuff sq
464476

465-
tail2 :: [a] -> [a]
466-
tail2 (x:y:xs) | null xs = [x,y]
467-
| otherwise = tail2 (y:xs)
468-
tail2 [x] = []
469-
tail2 [] = []
477+
tail2 :: [a] -> ([a],[a])
478+
tail2 (x:y:xs) | null xs = ([],[x,y])
479+
| otherwise = (x:nh,nt)
480+
where (nh,nt) = tail2 (y:xs)
481+
tail2 [x] = ([],[])
482+
tail2 [] = ([],[])
470483

471484
concFromSeqListSuff :: (Ord a) => [PPTree a] -> [PPTree a]
472485
concFromSeqListSuff ptl
473486
| length rs /= length hds
474-
= map (uncurry convertConcMapEntryToNode) (Map.toList rs)
487+
= map (uncurry convertConcMapEntryToNodeSuffix) (Map.toList rs)
475488
| otherwise = ptl
476-
where (hds,tls) = unzip $ map (splitAt 2 . children) ptl
489+
where (hds,tls) = unzip $ map (tail2 . children) ptl
477490
rs = concMapFromSeqChildren tls hds
478491

479492

src/test/haskell/ToothpasteTest.hs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ sba2 = NodeN Seq [lb2,la2] 2
3131
sbc2 = seqP [lb2,lc2] 2
3232
sbad2 = NodeN Seq [lb2,la2,ld2] 2
3333
sca = seqP [lc,la] 1
34-
sdb2 = seqP [ld2,lb2] 1
34+
scab = seqP [lc,la,lb] 1
35+
scb2 = seqP [lc2,lb2] 2
36+
sdba2 = seqP [ld2,lb2,la2] 2
37+
sdb2 = seqP [ld2,lb2] 2
3538

3639
ccab1 = NodeN Conc [la,lb] 2
3740

@@ -414,15 +417,14 @@ concFromChoicePrefTests = [
414417
~=? concFromChoice( NodeN Choice [sabc,sbad2] 3)
415418
]
416419

417-
-- TODO many more len > 2 cases at different levels
420+
-- more len > 2 cases at different levels wouldn't hurt
418421

419-
emptyPPTL :: [PPTree Int]
420-
emptyPPTL = []
421422

422-
tail2Tests = [ "empty" ~: emptyPPTL ~=? tail2 [],
423-
"l1" ~: [] ~=? tail2 [1],
424-
"l2" ~: [1,2] ~=? tail2 [1,2],
425-
"l3" ~: [2,3] ~=? tail2 [1,2,3]
423+
tail2Tests = [ "empty" ~: ([]::[Int],[]::[Int]) ~=? tail2 ([]::[Int] ),
424+
"l1" ~: ([],[]) ~=? tail2 [1],
425+
"l2" ~: ([],[1,2]) ~=? tail2 [1,2],
426+
"l3" ~: ([1],[2,3]) ~=? tail2 [1,2,3],
427+
"l4" ~: ([4,5],[7,8]) ~=? tail2 [4,5,7,8]
426428
]
427429

428430
concFromChoiceSuffTests = [
@@ -431,14 +433,12 @@ concFromChoiceSuffTests = [
431433
"cfcs1" ~: concP [la,lb] 2
432434
~=? concFromChoiceSuff( NodeN Choice [sab,sba] 2),
433435
"cfcs2" ~: concP [la,lb2] 3
434-
~=? concFromChoiceSuff( NodeN Choice [sab,sba2] 3)
435-
-- fail due to suff bug TODO
436-
-- "cfcs5" ~: seqP [ choiceP [la, Silent 2] 3,
437-
-- concP [lb,lc2] 3] 3
438-
-- ~=? concFromChoiceSuff( NodeN Choice [sabc,sbc2] 3),
439-
-- fail due to suff bug TODO
440-
-- "cfcs6" ~: seqP [ choiceP [lc,ld2] 3, concP [la,lb2 ] 3 ] 3
441-
-- ~=? concFromChoiceSuff( NodeN Choice [sca,sdb2] 3)
436+
~=? concFromChoiceSuff( NodeN Choice [sab,sba2] 3),
437+
"cfcs3" ~: seqP [ choiceP [la, Silent 2] 3,
438+
concP [lb,lc2] 3] 3
439+
~=? concFromChoiceSuff( NodeN Choice [sabc,scb2] 3),
440+
"cfcs4" ~: seqP [ choiceP [lc,ld2] 3, concP [la,lb2 ] 3 ] 3
441+
~=? concFromChoiceSuff( NodeN Choice [scab,sdba2] 3)
442442
]
443443

444444
concFromChoiceTests = concFromChoicePrefTests

0 commit comments

Comments
 (0)