summaryrefslogtreecommitdiff
path: root/prototypes
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2015-12-18 21:37:13 +0100
committerPéter Diviánszky <divipp@gmail.com>2015-12-18 21:37:13 +0100
commit105f4644f203a371d703b3ff1013ab5736e58bc3 (patch)
tree78ce31b7598d9e497bca42693c1ff4b0ab4edd6b /prototypes
parentaf68dd00ffc329baebce5d29798ee17dba706c38 (diff)
cleanup
Diffstat (limited to 'prototypes')
-rw-r--r--prototypes/CGExp.hs37
-rw-r--r--prototypes/Infer.hs68
2 files changed, 23 insertions, 82 deletions
diff --git a/prototypes/CGExp.hs b/prototypes/CGExp.hs
index d10e04bb..2a0f0fa2 100644
--- a/prototypes/CGExp.hs
+++ b/prototypes/CGExp.hs
@@ -4,14 +4,13 @@
4{-# LANGUAGE FlexibleContexts #-} 4{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE FlexibleInstances #-} 5{-# LANGUAGE FlexibleInstances #-}
6{-# LANGUAGE NoMonomorphismRestriction #-} 6{-# LANGUAGE NoMonomorphismRestriction #-}
7{-# LANGUAGE OverloadedStrings #-}
8{-# LANGUAGE DeriveFunctor #-} 7{-# LANGUAGE DeriveFunctor #-}
9{-# LANGUAGE DeriveFoldable #-} 8{-# LANGUAGE DeriveFoldable #-}
10{-# LANGUAGE DeriveTraversable #-} 9{-# LANGUAGE DeriveTraversable #-}
11{-# LANGUAGE RecursiveDo #-} 10{-# LANGUAGE RecursiveDo #-}
12module CGExp 11module CGExp
13 ( module CGExp 12 ( module CGExp
14 , module Infer 13 , Lit(..), Export(..), ModuleR(..)
15 ) where 14 ) where
16 15
17import Control.Monad.Reader 16import Control.Monad.Reader
@@ -26,12 +25,12 @@ import Text.Parsec.Pos
26 25
27import Pretty 26import Pretty
28import qualified Infer as I 27import qualified Infer as I
29import Infer (Binder(..), SName, Lit(..), Visibility(..), FunName(..), CaseFunName(..), ConName(..), TyConName(..), Export(..), ModuleR(..)) 28import Infer (SName, Lit(..), Visibility(..), Export(..), ModuleR(..))
30 29
31-------------------------------------------------------------------------------- 30--------------------------------------------------------------------------------
32 31
33data Exp_ a 32data Exp_ a
34 = Pi_ Visibility SName a a -- TODO: prohibit meta binder here 33 = Pi_ Visibility SName a a
35 | Lam_ Visibility Pat a a 34 | Lam_ Visibility Pat a a
36 | Con_ (SName, a) [a] 35 | Con_ (SName, a) [a]
37 | ELit_ Lit 36 | ELit_ Lit
@@ -61,32 +60,30 @@ newtype Exp = Exp (Exp_ Exp)
61 60
62type ConvM a = StateT [SName] (Reader [SName]) a 61type ConvM a = StateT [SName] (Reader [SName]) a
63 62
63newName = gets head <* modify tail
64
64toExp :: I.Exp -> Exp 65toExp :: I.Exp -> Exp
65toExp = flip runReader [] . flip evalStateT freshTypeVars . f 66toExp = flip runReader [] . flip evalStateT freshTypeVars . f
66 where 67 where
67 f = \case 68 f = \case
68 I.FunN "swizzvector" [_, _, _, exp, getSwizzVec -> Just (concat -> s)] -> do 69 I.FunN "swizzvector" [_, _, _, exp, getSwizzVec -> Just (concat -> s)] -> newName >>= \n -> do
69 e <- f exp 70 e <- f exp
70 let sty = tyOf e 71 return $ app' (EFieldProj (Pi Visible n (tyOf e) (TVec (length s) TFloat)) s) e
71 dty = TVec (length s) TFloat 72 I.FunN "swizzscalar" [_, _, exp, mkSwizzStr -> Just s] -> newName >>= \n -> do
72 (gets head <* modify tail) >>= \n -> return $ app' (EFieldProj (Pi Visible n sty dty) s) e
73 I.FunN "swizzscalar" [_, _, exp, mkSwizzStr -> Just s] -> do
74 e <- f exp 73 e <- f exp
75 let sty = tyOf e 74 return $ app' (EFieldProj (Pi Visible n (tyOf e) TFloat) s) e
76 dty = TFloat
77 (gets head <* modify tail) >>= \n -> return $ app' (EFieldProj (Pi Visible n sty dty) s) e
78 I.Var i -> asks $ uncurry Var . (!!! i) 75 I.Var i -> asks $ uncurry Var . (!!! i)
79 I.Pi b x y -> (gets head <* modify tail) >>= \n -> do 76 I.Pi b x y -> newName >>= \n -> do
80 t <- f x 77 t <- f x
81 Pi b n t <$> local ((n, t):) (f y) 78 Pi b n t <$> local ((n, t):) (f y)
82 I.Lam b x y -> (gets head <* modify tail) >>= \n -> do 79 I.Lam b x y -> newName >>= \n -> do
83 t <- f x 80 t <- f x
84 Lam b (PVar t n) t <$> local ((n, t):) (f y) 81 Lam b (PVar t n) t <$> local ((n, t):) (f y)
85 I.Con (ConName s _ _ t) xs -> con s <$> f t <*> mapM f xs 82 I.Con (I.ConName s _ _ t) xs -> con s <$> f t <*> mapM f xs
86 I.TyCon (TyConName s _ _ t _ _) xs -> con s <$> f t <*> mapM f xs 83 I.TyCon (I.TyConName s _ _ t _ _) xs -> con s <$> f t <*> mapM f xs
87 I.ELit l -> pure $ ELit l 84 I.ELit l -> pure $ ELit l
88 I.Fun (FunName s _ t) xs -> fun s <$> f t <*> mapM f xs 85 I.Fun (I.FunName s _ t) xs -> fun s <$> f t <*> mapM f xs
89 I.CaseFun (CaseFunName s t _) xs -> fun s <$> f t <*> mapM f xs 86 I.CaseFun (I.CaseFunName s t _) xs -> fun s <$> f t <*> mapM f xs
90 I.App a b -> app' <$> f a <*> f b 87 I.App a b -> app' <$> f a <*> f b
91 I.Label x _ -> f x 88 I.Label x _ -> f x
92 I.TType -> pure TType 89 I.TType -> pure TType
@@ -201,10 +198,6 @@ pattern Prim3 n a b c <- PrimN n [a, b, c]
201pattern Prim4 n a b c d <- PrimN n [a, b, c, d] 198pattern Prim4 n a b c d <- PrimN n [a, b, c, d]
202pattern Prim5 n a b c d e <- PrimN n [a, b, c, d, e] 199pattern Prim5 n a b c d e <- PrimN n [a, b, c, d, e]
203 200
204--pattern EFieldProj :: Exp -> SName -> Exp
205--pattern EFieldProj a b = Prim2 "EFieldProj" a (ELit (LString b))
206
207
208-- todo: remove 201-- todo: remove
209hackType = \case 202hackType = \case
210 "Output" -> TType 203 "Output" -> TType
diff --git a/prototypes/Infer.hs b/prototypes/Infer.hs
index 2e6c3364..c8b62651 100644
--- a/prototypes/Infer.hs
+++ b/prototypes/Infer.hs
@@ -34,15 +34,12 @@ import Control.Applicative hiding (optional)
34import Control.Exception hiding (try) 34import Control.Exception hiding (try)
35 35
36import Text.Parsec hiding (parse, label, Empty, State, (<|>), many, optional) 36import Text.Parsec hiding (parse, label, Empty, State, (<|>), many, optional)
37--import Text.Parsec.Token hiding (makeTokenParser, operator)
38import qualified Text.Parsec.Token as Pa 37import qualified Text.Parsec.Token as Pa
39import Text.Parsec.Pos 38import Text.Parsec.Pos
40import Text.Parsec.Indentation hiding (Any) 39import Text.Parsec.Indentation hiding (Any)
41import Text.Parsec.Indentation.Char 40import Text.Parsec.Indentation.Char
42import Text.Parsec.Indentation.Token 41import Text.Parsec.Indentation.Token
43 42
44import System.Environment
45import System.Directory
46import Debug.Trace 43import Debug.Trace
47 44
48import qualified Pretty as P 45import qualified Pretty as P
@@ -52,14 +49,16 @@ import qualified Pretty as P
52type SName = String 49type SName = String
53 50
54data Stmt 51data Stmt
55 = TypeAnn SName SExp -- intermediate 52 = Let SName MFixity (Maybe SExp) [Visibility]{-source arity-} SExp
56 | Let SName MFixity (Maybe SExp) [Visibility]{-source arity-} SExp
57 | Data SName [(Visibility, SExp)]{-parameters-} SExp{-type-} [(SName, SExp)]{-constructor names and types-} 53 | Data SName [(Visibility, SExp)]{-parameters-} SExp{-type-} [(SName, SExp)]{-constructor names and types-}
58 | Primitive (Maybe Bool{-Just True: type constructor; Just False: constructor; Nothing: function-}) SName SExp{-type-} 54 | Primitive (Maybe Bool{-Just True: type constructor; Just False: constructor; Nothing: function-}) SName SExp{-type-}
59 | PrecDef SName Fixity 55 | PrecDef SName Fixity
60 | Wrong [Stmt] 56 | Wrong [Stmt]
61 | FunAlt SName [((Visibility, SExp), Pat)] (Maybe SExp) SExp -- eliminated during parsing
62 | ValueDef ([SName], Pat) SExp 57 | ValueDef ([SName], Pat) SExp
58
59 -- eliminated during parsing
60 | TypeAnn SName SExp -- intermediate
61 | FunAlt SName [((Visibility, SExp), Pat)] (Maybe SExp) SExp
63 deriving (Show) 62 deriving (Show)
64 63
65data SExp 64data SExp
@@ -345,26 +344,9 @@ type ElabStmtM m = StateT GlobalEnv (ExceptT String m)
345getFunName (fst . getApps' -> Fun f _) = Just f 344getFunName (fst . getApps' -> Fun f _) = Just f
346getFunName _ = Nothing 345getFunName _ = Nothing
347 346
348label b c = {-trace ("label: " ++ n) $ -} label_ b c 347label x (LabelEnd y) = y
349 where 348label x y = Label x y
350 label_ x (LabelEnd y) = y
351 label_ x y = Label x y
352{-
353 label_ ac@(getFunName -> Just (FunName n _ _)) d | labellable d = {-trace ("Label: " ++ n) $ -} Label ac d
354 label_ _ d = d
355
356 labellable (Lam' _) = True
357 labellable (Fun f _) = labellableName f
358 labellable (CaseFun f _) = True
359 labellable _ = False
360
361 labellableName (FunName n _ _) = n `elem` ["matchInt", "matchList"] --False
362-}
363--unLabel (Label _ _ x) = x
364--unLabel x = x
365 349
366--pattern UnLabel a <- (unLabel -> a) where UnLabel a = a
367--pattern UPrim a b = UnLabel (Con a b)
368pattern UBind a b c = {-UnLabel-} (Bind a b c) -- todo: review 350pattern UBind a b c = {-UnLabel-} (Bind a b c) -- todo: review
369pattern UApp a b = {-UnLabel-} (App a b) -- todo: review 351pattern UApp a b = {-UnLabel-} (App a b) -- todo: review
370pattern UVar n = Var n 352pattern UVar n = Var n
@@ -2259,41 +2241,7 @@ infer env = fmap (forceGE . snd) . runExcept . flip runStateT (initEnv <> env) .
2259forceGE x = length (concatMap (uncurry (++) . (showExp *** showExp)) $ Map.elems x) `seq` x 2241forceGE x = length (concatMap (uncurry (++) . (showExp *** showExp)) $ Map.elems x) `seq` x
2260 2242
2261fromRight ~(Right x) = x 2243fromRight ~(Right x) = x
2262{- 2244
2263main = do
2264 args <- getArgs
2265 let name = head $ args ++ ["tests/accept/DepPrelude"]
2266 f = name ++ ".lc"
2267 f' = name ++ ".lci"
2268
2269 s <- readFile f
2270 let parseAndInfer = do
2271 p <- definitions <$> parse f s
2272 infer initEnv $ removePreExps (mkGlobalEnv' p) p
2273 case parseAndInfer of
2274 Left e -> putStrLn_ e
2275 Right (fmap (showExp *** showExp) -> s_) -> do
2276 putStrLn_ "----------------------"
2277 b <- doesFileExist f'
2278 if b then do
2279 s' <- Map.fromList . read <$> readFile f'
2280 bs <- sequence $ Map.elems $ Map.mapWithKey (\k -> either (\x -> False <$ putStrLn_ (either (const "missing") (const "new") x ++ " definition: " ++ k)) id) $ Map.unionWithKey check (Left . Left <$> s') (Left . Right <$> s_)
2281 when (not $ and bs) $ do
2282 putStr "write changes? (Y/N) "
2283 x <- getChar
2284 when (x `elem` ("yY" :: String)) $ do
2285 writeFile f' $ show $ Map.toList s_
2286 putStrLn_ "Changes written."
2287 else do
2288 writeFile f' $ show $ Map.toList s_
2289 putStrLn_ $ f' ++ " was written."
2290 putStrLn_ $ maybe "!main was not found" fst $ Map.lookup "main" s_
2291 where
2292 check k (Left (Left (x, t))) (Left (Right (x', t')))
2293 | t /= t' = Right $ False <$ putStrLn_ ("!!! type diff: " ++ k ++ "\n old: " ++ t ++ "\n new: " ++ t')
2294 | x /= x' = Right $ False <$ putStrLn_ ("!!! def diff: " ++ k)
2295 | otherwise = Right $ return True
2296-}
2297-------------------------------------------------------------------------------- utils 2245-------------------------------------------------------------------------------- utils
2298 2246
2299dropNth i xs = take i xs ++ drop (i+1) xs 2247dropNth i xs = take i xs ++ drop (i+1) xs