Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore-this: c276879bc5a84833980ff7bbf49c7cb0 darcs-hash:20111027101641-e29d1-fab00102b29463d92b491051ed18dc14ea96eb8a.gz
- Loading branch information
1 parent
83bc1df
commit cab2a2d
Showing
13 changed files
with
134 additions
and
229 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,51 +1,71 @@ | ||
{- | ||
Purely Functional Queue with Amortised Linear Cost | ||
Based on section 3 of | ||
Christoph Herrmann, Edwin Brady and Kevin Hammond. | ||
"Dependently-typed Programming by Composition from Functional | ||
Building Blocks." | ||
In Draft Proceedings of the 12th International Symposium on | ||
Trends in Functional Programming, TFP 2011. | ||
Tech. Rep. SIC-07/11, Dept. Computer Systems and Computing, | ||
Universidad Complutense de Madrid | ||
-} | ||
|
||
{-# OPTIONS_GHC -F -pgmF inch #-} | ||
{-# LANGUAGE RankNTypes, GADTs, KindSignatures, ScopedTypeVariables, | ||
NPlusKPatterns #-} | ||
|
||
module Queue where | ||
|
||
data Pair :: * -> * -> * where | ||
Pair :: forall a b . a -> b -> Pair a b | ||
|
||
p0 (Pair a b) = a | ||
p1 (Pair a b) = b | ||
|
||
data Vec :: * -> Num -> * where | ||
Nil :: forall a . Vec a 0 | ||
Cons :: forall (n :: Nat) a . a -> Vec a n -> Vec a (n+1) | ||
Nil :: forall a . Vec a 0 | ||
Cons :: forall (n :: Nat) a . a -> Vec a n -> Vec a (n+1) | ||
deriving Show | ||
|
||
|
||
data Queue :: * -> Num -> * where | ||
Q :: forall elem . pi (a b c :: Nat) . Vec elem a -> Vec elem b -> Queue elem (c + 3*a + b) | ||
Q :: forall elem . pi (a b c :: Nat) . | ||
Vec elem a -> Vec elem b -> Queue elem (c + 3*a + b) | ||
deriving Show | ||
|
||
initQueue = Q {0} {0} {0} Nil Nil | ||
|
||
enqueue :: forall elem (paid :: Nat) . elem -> Queue elem paid -> Queue elem (paid + 4) | ||
enqueue :: forall elem (paid :: Nat) . | ||
elem -> Queue elem paid -> Queue elem (paid + 4) | ||
enqueue x (Q {a} {b} {c} sA sB) = Q {a+1} {b} {c+1} (Cons x sA) sB | ||
|
||
reverseS :: forall elem (paid :: Nat) . Queue elem paid -> Queue elem paid | ||
reverseS :: forall elem (paid :: Nat) . | ||
Queue elem paid -> Queue elem paid | ||
reverseS (Q {0} {b} {c} Nil sB) = Q {0} {b} {c} Nil sB | ||
reverseS (Q {a+1} {b} {c} (Cons x sA) sB) = reverseS (Q {a} {b+1} {c+2} sA (Cons x sB)) | ||
|
||
dequeue :: forall elem (paid :: Nat) . Queue elem paid -> Pair elem (Queue elem paid) | ||
dequeue (Q {a} {b+1} {c} sA (Cons x sB)) = Pair x (Q {a} {b} {c+1} sA sB) | ||
dequeue :: forall elem (paid :: Nat) . | ||
Queue elem paid -> (elem, Queue elem paid) | ||
dequeue (Q {a} {b+1} {c} sA (Cons x sB)) = (x, Q {a} {b} {c+1} sA sB) | ||
dequeue (Q {a+1} {0} {c} sA Nil) = dequeue (reverseS (Q {a+1} {0} {c} sA Nil)) | ||
|
||
|
||
|
||
data Queue2 :: * -> Num -> * where | ||
Q2 :: forall elem (a b c :: Nat) . Vec elem a -> Vec elem b -> Queue2 elem (c + 3*a + b) | ||
|
||
Q2 :: forall elem (a b c :: Nat) . | ||
Vec elem a -> Vec elem b -> Queue2 elem (c + 3*a + b) | ||
deriving Show | ||
|
||
initQueue2 :: forall elem . Queue2 elem 0 | ||
initQueue2 = Q2 Nil Nil | ||
|
||
enqueue2 :: forall elem (paid :: Nat) . elem -> Queue2 elem paid -> Queue2 elem (paid + 4) | ||
enqueue2 :: forall elem (paid :: Nat) . | ||
elem -> Queue2 elem paid -> Queue2 elem (paid + 4) | ||
enqueue2 x (Q2 sA sB) = Q2 (Cons x sA) sB | ||
|
||
reverseS2 :: forall elem (paid :: Nat) . Queue2 elem paid -> Queue2 elem paid | ||
reverseS2 :: forall elem (paid :: Nat) . | ||
Queue2 elem paid -> Queue2 elem paid | ||
reverseS2 (Q2 Nil sB) = Q2 Nil sB | ||
reverseS2 (Q2 (Cons x sA) sB) = reverseS2 (Q2 sA (Cons x sB)) | ||
|
||
dequeue2 :: forall elem (paid :: Nat) . Queue2 elem paid -> Pair elem (Queue2 elem paid) | ||
dequeue2 (Q2 sA (Cons x sB)) = Pair x (Q2 sA sB) | ||
dequeue2 :: forall elem (paid :: Nat) . | ||
Queue2 elem paid -> (elem, Queue2 elem paid) | ||
dequeue2 (Q2 sA (Cons x sB)) = (x, Q2 sA sB) | ||
dequeue2 (Q2 sA Nil) = dequeue2 (reverseS2 (Q2 sA Nil)) |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.