packages feed

slot-lambda (empty) → 0.1.0.0

raw patch · 7 files changed

+119/−0 lines, 7 filesdep +basedep +containersdep +haskell-src-extssetup-changed

Dependencies added: base, containers, haskell-src-exts, haskell-src-meta, syb, template-haskell, vector

Files

+ LICENSE view
@@ -0,0 +1,20 @@+The MIT License (MIT)++Copyright (c) 2014 ducis.++Permission is hereby granted, free of charge, to any person obtaining a copy of+this software and associated documentation files (the "Software"), to deal in+the Software without restriction, including without limitation the rights to+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of+the Software, and to permit persons to whom the Software is furnished to do so,+subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Makefile view
@@ -0,0 +1,4 @@+try:+	for i in $$(ls examples/*.hs); do cat $$i; runghc $$i; done+cb:+	cabal build
+ README.md view
@@ -0,0 +1,7 @@+pa\_slot+==========++Write lambdas without naming the parameters.++	[s| ı : ı : _ı : ı : _ı : _ı : _0 : [] |] 'a' 'b' 'c' = "abbccca"+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ Syntax/Slot.hs view
@@ -0,0 +1,57 @@+{-# LANGUAGE LambdaCase, ViewPatterns,ScopedTypeVariables, NoMonomorphismRestriction #-}+module Syntax.Slot(slot,s) where++import Language.Haskell.TH+import Language.Haskell.TH.Quote+import qualified Language.Haskell.Meta as M+import Language.Haskell.Exts.Annotated as A+import Language.Haskell.Exts.Annotated.Simplify+import Language.Haskell.Exts.SrcLoc+import Data.Generics+import Data.Data+import Control.Applicative+import Text.Read+import Data.Either+import Text.Printf+import qualified Data.Vector as V+import Data.List+import qualified Data.Map as M+import Debug.Trace(trace)++--tr x = trace (show x) x+tr x = x++slot = s++s = QuasiQuoter onExp e e e where+	e _ = fail "Not here"+	onExp s = case parseExp s of +		ParseOk a -> transform a+		ParseFailed _ s -> fail s+	transform exp = do+		let +			f = \case+				Var _ (UnQual _ (A.Ident (l::SrcSpanInfo) x)) -> case x of+					"ı" -> [Left (0,l)]+					"_ı" -> [Left (1,l)]+					'_':(readMaybe -> Just (n::Int)) -> [Right (n,l)]+				_ -> []+			((groupBy (\(a,_) (b,_)->a+b==1)->gs), ns) = partitionEithers $ everything (++) (mkQ [] f) exp+			ms = [0..(maximum $ tr (length gs-1): map fst ns)]+		names <- (`mapM` ms) $ (show <$>). newName.printf "slot_%02d"+		let namesV = V.fromList names+		let namesI = M.fromList $ sort $ concat $ zipWith (\n g->[(l,n)|(_,l)<-g]) names gs+		return $ LamE (map (VarP . mkName) names) $ M.toExp $ sExp $ (`everywhere` exp) $ mkT $ \case+			Var l0 (UnQual l1 (A.Ident (l::SrcSpanInfo) x)) -> Var l0 $ UnQual l1 $ A.Ident l $ case x of+				"ı" -> fromI+				"_ı" -> fromI+				'_':(readMaybe -> Just (n::Int)) -> namesV V.! n+				x -> x+				where +				fromI = namesI M.! l+			x -> x+			--Var l (UnQual _ (show->x)) -> case x of++branch f r = case r of+	Right e -> f e+	Left err -> fail err
+ examples/t0.hs view
@@ -0,0 +1,5 @@+{-# LANGUAGE TemplateHaskell,QuasiQuotes #-}+import Syntax.Slot+a = [s| ı : ı : _ı : ı : _ı : _ı : _0 : [] |] 'a' 'b' 'c'+main = print a+
+ slot-lambda.cabal view
@@ -0,0 +1,24 @@+name:                slot-lambda+version:             0.1.0.0+synopsis:            Write lambdas without naming the parameters. --\_ + _  =  \x y -> x+y+description:         Write lambdas without naming the parameters. +                     e.g. [s| ı : ı : _ı : ı : _ı : _ı : _0 : [] |] 'a' 'b' 'c' +							=    \x y z -> x:y:y:z:z:z:x:[]+							=    "abbccca"+							Just `import Syntax.Slot` and use either [s| ... |] or [slot| ... |], which are the same thing given two names just for convenience.+license:             MIT+license-file:        LICENSE+author:              ducis+maintainer:          ducis_cn@126.com+-- copyright:           +category:            Language+build-type:          Simple+extra-source-files:  README.md, examples/*.hs, Makefile+cabal-version:       >=1.10++library+  exposed-modules:     Syntax.Slot+  other-extensions:    TemplateHaskell, QuasiQuotes+  build-depends:       base >=4.6 && < 5, template-haskell >=2.8, haskell-src-meta>=0.6.0.5, syb>=0.4.1, vector>=0.10.9.1, haskell-src-exts >= 1.14.0.1, containers >= 0.5.0.0+  default-language:    Haskell2010+