-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix computation of commonPrefix from Agda
It's been a lengthy process but I finally nailed down the issue with commonPrefix computation, shaving a few yaks related to Read/Show instances consistency in the process.
- Loading branch information
Showing
16 changed files
with
186 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,44 @@ | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
module Peras.Chain where | ||
|
||
import Peras.Block (Block) | ||
|
||
data Chain t | ||
= Genesis | ||
| Cons (Block t) (Chain t) | ||
deriving (Eq) | ||
data Chain t = Genesis | ||
| Cons (Block t) (Chain t) | ||
deriving (Eq) | ||
|
||
foldl1Maybe :: (a -> a -> a) -> [a] -> Maybe a | ||
foldl1Maybe f xs = | ||
foldl | ||
( \m y -> | ||
Just | ||
( case m of | ||
Nothing -> y | ||
Just x -> f x y | ||
) | ||
) | ||
Nothing | ||
xs | ||
|
||
asList :: forall t. Eq t => Chain t -> [Block t] | ||
foldl1Maybe f xs | ||
= foldl | ||
(\ m y -> | ||
Just | ||
(case m of | ||
Nothing -> y | ||
Just x -> f x y)) | ||
Nothing | ||
xs | ||
|
||
asList :: forall t . Eq t => Chain t -> [Block t] | ||
asList Genesis = [] | ||
asList (Cons x c) = x : asList c | ||
|
||
asChain :: forall t. Eq t => [Block t] -> Chain t | ||
asChain :: forall t . Eq t => [Block t] -> Chain t | ||
asChain [] = Genesis | ||
asChain (x : bs) = Cons x (asChain bs) | ||
|
||
prefix :: forall t. Eq t => [Block t] -> [Block t] -> [Block t] | ||
prefix (x : xs) (y : ys) = if x == y then x : prefix xs ys else [] | ||
prefix _ _ = [] | ||
|
||
commonPrefix :: forall t. Eq t => [Chain t] -> Chain t | ||
commonPrefix chains = | ||
case listPrefix of | ||
Nothing -> Genesis | ||
Just bs -> asChain bs | ||
where | ||
listPrefix :: Maybe [Block t] | ||
listPrefix = foldl1Maybe prefix (reverse (map asList chains)) | ||
prefix :: | ||
forall t . Eq t => [Block t] -> [Block t] -> [Block t] -> [Block t] | ||
prefix acc (x : xs) (y : ys) | ||
= if x == y then prefix (x : acc) xs ys else reverse acc | ||
prefix acc _ _ = reverse acc | ||
|
||
commonPrefix :: forall t . Eq t => [Chain t] -> Chain t | ||
commonPrefix chains | ||
= case listPrefix of | ||
Nothing -> Genesis | ||
Just bs -> asChain (reverse bs) | ||
where | ||
listPrefix :: Maybe [Block t] | ||
listPrefix | ||
= foldl1Maybe (prefix []) (map (\ l -> reverse (asList l)) chains) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.