Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. -- uniqコマンド
  2. --
  3. -- $ agda -c -i. -i/usr/share/agda-stdlib/ uniq.agda
  4. -- $ ./uniq
  5. module uniq where
  6.  
  7. open import Data.Colist using (Colist; [])
  8. open import Data.String using (String; Costring)
  9. open import Data.Maybe
  10. open import Data.Product
  11. open import Coinduction
  12. open import Category.Monad.Partiality
  13. open import Category.Monad.State
  14. open import Category.Monad
  15. module S = RawIMonadState (StateTMonadState (∞ Costring) (Category.Monad.Partiality.monad))
  16. module M = RawMonad S.monad
  17.  
  18. takeLine : StateT (∞ Costring) _⊥ (Maybe String)
  19. takeLine = proj₁ M.<$> ((LS.get LM.>>= go ∘ ♭) "") where
  20. open import Function
  21. open import Data.List using ([_])
  22. open import Data.Colist using (Colist; _∷_)
  23. open import Data.String using (_++_; fromList)
  24. module SS = RawIMonadState (StateTMonadState String S.monad)
  25. module LS = RawIMonadState (LiftMonadState String (StateTMonadState (∞ Costring) (Category.Monad.Partiality.monad)))
  26. module LM = RawMonad LS.monad
  27. go : Costring → StateT String (StateT (∞ Costring) _⊥) (Maybe String)
  28. go [] = LM.return nothing
  29. go ('\n' ∷ xs) = LS.put xs LM.>>= λ _ →
  30. SS.get LM.>>= λ acc →
  31. SS.put "" LM.>>= λ _ →
  32. LM.return (just acc)
  33. go ( x ∷ xs) = λ acc ss → later (♯ go (♭ xs) (acc ++ fromList [ x ]) xs) -- 相性が悪い
  34. -- LS.put xs LM.>>= λ _ →
  35. -- SS.get LM.>>= λ acc →
  36. -- SS.put (acc ++ fromList [ x ]) LM.>>= λ _ →
  37. -- go (♭ xs) -- termination check にひっかかる
  38. -- でも結局関数合成なので,_>>=_で合成してしまうと適切に再帰をlaterで包めない!
  39.  
  40. uniq : Costring → Colist (Maybe String)
  41. uniq = go nothing ∘ takeLine ∘ ♯_ where
  42. open import Function
  43. open import Data.Colist using ([]; _∷_)
  44. open import Relation.Binary
  45. open DecSetoid (Data.Maybe.decSetoid Data.String.decSetoid)
  46. open import Relation.Nullary
  47. go : Maybe String → (Maybe String × ∞ Costring) ⊥ → Colist (Maybe String)
  48. go _ (now (nothing , _)) = []
  49. go p (now (just line , xs)) with p ≟ just line
  50. ... | yes _ = nothing ∷ ♯ go p (takeLine xs)
  51. ... | no _ = just line ∷ ♯ go (just line) (takeLine xs)
  52. go p (later x) = nothing ∷ ♯ go p (♭ x)
  53.  
  54. main = run (♯ getContents >>= ♯_ ∘ mapM (maybe coloredPutStrLn (return tt)) ∘ uniq) where
  55. open import IO
  56. open import Function
  57. open import Data.Unit
  58. open import Data.String using (_++_)
  59. coloredPutStrLn : String → IO ⊤
  60. coloredPutStrLn ss = putStrLn ("\x1b[32m" ++ ss ++ "\x1b[39m") -- 出力わかりやすいように色付け
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement