harp (empty) → 0.2.1
raw patch · 4 files changed
+252/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- Harp/Match.hs +147/−0
- LICENSE +77/−0
- Setup.hs +2/−0
- harp.cabal +26/−0
+ Harp/Match.hs view
@@ -0,0 +1,147 @@+-----------------------------------------------------------------------------+-- |+-- Module : Harp.Match+-- Copyright : (c) Niklas Broberg 2004,+-- License : BSD-style (see the file LICENSE.txt)+--+-- Maintainer : Niklas Broberg, d00nibro@dtek.chalmers.se+-- Stability : experimental+-- Portability : portable+--+-- Functions that simulate the behavior of regular patterns+-- using a Match monad for parsing lists.+-----------------------------------------------------------------------------++module Harp.Match (+ Match -- Match e a+ , runMatch -- Match e a -> [e] -> Maybe a+ , baseMatch -- (a -> Maybe b) -> Match a (a, b)+ , manyMatch -- Match e a -> Match e [a]+ , gManyMatch -- Match e a -> Match e [a]+ , foldComp -- [[a] -> [a]] -> ([a] -> [a])+ , unzip0, unzip1, unzip2, unzip3, unzip4, unzip5, unzip6, unzip7+ , (+++)+ ) where++import Data.List (unzip3, unzip4, unzip5, unzip6, unzip7)++--------------------------------------------------------------+-- | The Match monad++newtype Match e a = Match ([e] -> [(a, [e])])++(+++) :: Match e a -> Match e a -> Match e a+(Match f) +++ (Match g) = Match (\es -> let aes1 = f es+ aes2 = g es+ in aes1 ++ aes2)++instance Monad (Match e) where+ return x = Match (\es -> [(x, es)])++ (Match f) >>= k = Match (\es -> let aes = f es+ in concatMap help aes)+ where help (a, es) = let Match g = k a+ in g es+++mfail :: Match e a+mfail = Match $ \_ -> []++runM :: Match e a -> [e] -> [a]+runM (Match f) es = let aes = f es+ in map fst $ filter (null . snd) aes++getElement :: Match e e+getElement = Match $ \es -> case es of+ [] -> []+ (x:xs) -> [(x,x:xs)]++discard :: Match e ()+discard = Match $ \es -> case es of+ [] -> []+ (_:xs) -> [((), xs)]+++runMatch :: Match e a -> [e] -> Maybe a+runMatch m es = case runM m es of+ [] -> Nothing+ (a:_) -> Just a++baseMatch :: (a -> Maybe b) -> Match a (a, b)+baseMatch f = do e <- getElement+ case f e of+ Nothing -> mfail+ Just b -> do discard+ return (e, b)++gManyMatch :: Match e a -> Match e [a]+gManyMatch m = (do a <- m+ as <- gManyMatch m+ return (a:as))+ +++ (return [])++manyMatch :: Match e a -> Match e [a]+manyMatch m = (return []) ++++ (do a <- m+ as <- manyMatch m+ return (a:as))+++foldComp :: [[a] -> [a]] -> ([a] -> [a])+foldComp = foldl (.) id+++unzip0 :: [()] -> ()+unzip0 = const ()++unzip1 :: [a] -> [a]+unzip1 = id++unzip2 :: [(a,b)] -> ([a],[b])+unzip2 = unzip++{-+data M e a = Element (e -> M e a)+ | Fail+ | Return a (M e a)++instance Monad (M e) where+ return x = Return x Fail++ (Element f) >>= k = Element (\e -> f e >>= k)+ Fail >>= k = Fail+ (Return x m) >>= k = k x ++++ (m >>= k)+++infix 5 ++++++(++++) :: M e a -> M e a -> M e a+Fail ++++ n = n+m ++++ Fail = m+Return x m ++++ n = Return x (m ++++ n)+m ++++ Return x n = Return x (m ++++ n)+Element f ++++ Element g = Element (\e -> f e ++++ g e)+++runM :: M e a -> [e] -> [a]+runM (Element f) (e:es) = runM (f e) es+runM (Element _) [] = []+runM Fail _ = []+runM (Return x m) [] = x : runM m []+runM (Return _ m) es = runM m es++-- the continuation trick+newtype Match e a = Match ()++instance Monad (Match e) where+ return x = Match (\k -> k x)++ (Match f) >>= k = Match (\h -> f (\a -> let Match g = k a+ in g h))++runMatch :: Match e a -> [e] -> [a]+runMatch (Match f) = runM (f return)++mfail :: Match e a+mfail = Match $ \_ -> Fail+-}
+ LICENSE view
@@ -0,0 +1,77 @@+ [1]Return to the main page++Licence document for the haskell-harp package++ --------------------------------------------------------------------------++ Copyright 2004, Niklas Broberg. All rights reserved.++ Redistribution and use in source and binary forms, with or without+ modification, are permitted provided that the following conditions are+ met:++ * Redistributions of source code must retain the above copyright notice,+ this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.++ This software is provided by the copyright holders "as is" and any express+ or implied warranties, including, but not limited to, the implied+ warranties of merchantability and fitness for a particular purpose are+ disclaimed. In no event shall the copyright holders be liable for any+ direct, indirect, incidental, special, exemplary, or consequential damages+ (including, but not limited to, procurement of substitute goods or+ services; loss of use, data, or profits; or business interruption) however+ caused and on any theory of liability, whether in contract, strict+ liability, or tort (including negligence or otherwise) arising in any way+ out of the use of this software, even if advised of the possibility of+ such damage.++ --------------------------------------------------------------------------++ This library is based on code from the Glasgow Haskell Compiler, which is+ usable and redistributable under the following licence:++ The Glasgow Haskell Compiler License++ Copyright 2002, The University Court of the University of Glasgow. All+ rights reserved.++ Redistribution and use in source and binary forms, with or without+ modification, are permitted provided that the following conditions are+ met:++ - Redistributions of source code must retain the above copyright notice,+ this list of conditions and the following disclaimer.++ - Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.++ - Neither name of the University nor the names of its contributors may be+ used to endorse or promote products derived from this software without+ specific prior written permission.++ THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY COURT OF THE UNIVERSITY OF+ GLASGOW AND THE CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO+ EVENT SHALL THE UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW OR THE+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.++ --------------------------------------------------------------------------++ [2]Return to the main page++References++ Visible links+ 1. http://www.cs.chalmers.se/~d00nibro/harp/index.html+ 2. http://www.cs.chalmers.se/~d00nibro/harp/index.html
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ harp.cabal view
@@ -0,0 +1,26 @@+Name: harp+Version: 0.2.1+License: BSD4+License-File: LICENSE+Author: Niklas Broberg+Maintainer: Niklas Broberg <nibro@cs.chalmers.se>++Stability: Experimental+Category: Language+Synopsis: HaRP allows pattern-matching with regular expressions+Description: HaRP, or Haskell Regular Patterns, is a Haskell extension+ that extends the normal pattern matching facility with+ the power of regular expressions. This expressive power+ is highly useful in a wide range of areas, including text parsing+ and XML processing. Regular expression patterns in HaRP work over+ ordinary Haskell lists ([]) of arbitrary type. We have implemented+ HaRP as a pre-processor to ordinary Haskell.+ .+ For details on usage, please see the website.+Homepage: http://www.cs.chalmers.se/~d00nibro/harp/++Build-Depends: base+Build-Type: Simple+Tested-With: GHC==6.8.3++Exposed-modules: Harp.Match