]> hydra-www.ietfng.org Git - dyna2/commitdiff
Start Dyna.Backend.Python; add Analysis.Aggregation; tweak ANF
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Mon, 10 Dec 2012 01:32:15 +0000 (20:32 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Mon, 10 Dec 2012 01:32:15 +0000 (20:32 -0500)
bin/utils.py
src/Dyna/Analysis/ANFSelftest.hs
src/Dyna/Analysis/Aggregation.hs [new file with mode: 0644]
src/Dyna/Analysis/RuleMode.hs
src/Dyna/Backend/Python.hs [new file with mode: 0644]

index e5c9d32ea23e9c77b8eabe18c49b172b97f53b9a..d7269c75c4ca5e30a8b1f0005ea277263c9209c8 100644 (file)
@@ -9,7 +9,7 @@ def toANF(code, f='/tmp/tmp.dyna'):
     with file(f, 'wb') as tmp:
         tmp.write(code)
     os.system('rm -f %s.anf' % f)  # clean up any existing ANF output
-    assert 0 == os.system("""ghc -isrc Dyna.Analysis.ANFSelftest -e 'normalizeFile "%s"' """ % f), \
+    assert 0 == os.system("""ghc -isrc Dyna.Backend.Python -e 'normalizeFile "%s"' """ % f), \
         'failed to convert file.'
     with file('%s.anf' % f) as h:
         return h.read()
index c711a79ef43b5b02ba603b392c5d2171b6d1c60b..4a153259ffeace9eace4caa06f1849ee760ca5c3 100644 (file)
@@ -48,11 +48,3 @@ e3 = "f(X,Y) += (g(X,\"str\",d) - h(X,X,Y) - c)^2 + f(Y,Z)/exp(3.0) whenever ?c,
 t3 = testNormRule e3
 p3 = printANF $ t3
 -}
-
-normalizeFile file = do
-    contents <- B.readFile file
-    writeFile (file ++ ".anf")
-              (show $ vcat (map (\(P.LRule x T.:~ _) -> printANF $ runNormalize $ normRule x)
-                                (unsafeParse P.dlines contents))
-                      <> text "\n") -- add newline at end of file...
-    return ()
diff --git a/src/Dyna/Analysis/Aggregation.hs b/src/Dyna/Analysis/Aggregation.hs
new file mode 100644 (file)
index 0000000..e52049a
--- /dev/null
@@ -0,0 +1,50 @@
+---------------------------------------------------------------------------
+-- | Check that the aggregations in a program are well-founded.
+--
+-- Consumes "Dyna.Analysis.ANF" for a whole program.
+
+
+-- Header material                                                      {{{
+module Dyna.Analysis.Aggregation (
+    AggMap, buildAggMap
+) where
+
+import qualified Data.ByteString            as B
+import qualified Data.Map                   as M
+import           Dyna.Analysis.ANF
+import           Dyna.Term.TTerm
+import           Dyna.XXX.DataUtils
+
+------------------------------------------------------------------------}}}
+-- Preliminaries                                                        {{{
+
+type AggMap = M.Map DFunctAr DAgg
+
+------------------------------------------------------------------------}}}
+-- Processing                                                           {{{
+
+-- XXX These functions really would like to have span information, so they
+-- could report which line of the source caused an error.
+
+procANF :: (FDR, ANFState) -> Either String (DFunctAr, DAgg)
+procANF (FRule h a _ _, AS { as_unifs = us }) =
+  case M.lookup h us of
+    Nothing       -> Left $ "I can't process head-variables"
+    Just t -> case t of
+                TString _     -> Left $ "Malformed rule with string head"
+                TNumeric _    -> Left $ "Malformed rule with numeric head"
+                TFunctor f as -> Right ((f,length as),a)
+
+buildAggMap :: [(FDR, ANFState)] -> Either String AggMap
+buildAggMap = go (M.empty)
+ where
+  go m []      = Right m
+  go m (ar:xs) =
+    case procANF ar of
+      Left e -> Left e 
+      Right (d,a) ->
+        case mapUpsert d a m of
+          Left a' -> Left $ "Conflicting aggregators"
+          Right m' -> go m' xs
+
+------------------------------------------------------------------------}}}
index 3d8a09b7918c2dceab3baf9b72fd0d02a240b883..0ce62a4652dccb44f17e4c8f942d0bf3950c2140 100644 (file)
@@ -10,7 +10,9 @@
 {-# LANGUAGE Rank2Types #-}
 {-# LANGUAGE ScopedTypeVariables #-}
 
-module Dyna.Analysis.RuleMode where
+module Dyna.Analysis.RuleMode (
+    Det(..), DOpAMine(..), detOfDop, planEachEval
+) where
 
 import           Control.Monad
 import qualified Data.ByteString.Char8      as BC
@@ -287,35 +289,43 @@ unif_cruxes = M.foldrWithKey (\o i -> (crux o i :)) [] . as_unifs
 --   get a plan for doing so.
 plan :: (Crux ModedNT -> [Action])
      -> (PartialPlan -> Action -> Cost)
-     -> (FDR, ANFState)
+     -> ANFState
      -> Crux NTV
-     -> (Cost, Action)
-plan st sc (_, anfs) cr@(_,ci,co) = 
-  let cruxes =    eval_cruxes anfs
-               ++ unif_cruxes anfs
+     -> Maybe (Cost, Action)
+plan st sc anf cr@(_,ci,co) = 
+  let cruxes =    eval_cruxes anf
+               ++ unif_cruxes anf
       initPlan = PP { pp_cruxes = S.delete cr (S.fromList cruxes)
                     , pp_binds  = S.fromList $ filterNTs (co:ci)
                     , pp_score  = 0
                     , pp_plan   = []
                     }
-  in L.minimumBy (O.comparing fst) $ stepAgenda st sc [initPlan]
+  in case stepAgenda st sc [initPlan] of
+       [] -> Nothing
+       plans -> Just $ L.minimumBy (O.comparing fst) plans
+
+planEachEval anf =
+  map (\c -> (c, plan possible simpleCost anf c))
+    $ filter (\(f,_,_) -> case f of
+                            CFCall f' -> not $ isMath f'
+                            _         -> False )
+    $ eval_cruxes anf
 
 ------------------------------------------------------------------------}}}
 -- Experimental Detritus                                                {{{
 
-
 testPlanRule x =
- let (fr,anfs) = runNormalize $ normRule (unsafeParse DP.drule x)
-     updatePlans = map (\c -> (c, plan possible simpleCost (fr,anfs) c))
-       $ filter (\(f,_,_) -> case f of { CFCall f' -> not $ isMath f' ; _ -> False })
-       $ eval_cruxes anfs
-  in updatePlans
+ let (_,anf) = runNormalize $ normRule (unsafeParse DP.drule x)
+ in  planEachEval anf
 
 main :: IO ()
-main = mapM_ (\(c,(s,p)) -> do
+main = mapM_ (\(c,msp) -> do
                 putStrLn $ show c
-                putStrLn $ "SCORE: " ++ show s
-                forM_ p (putStrLn . show)
+                case msp of
+                  Just (s,p) -> do
+                     putStrLn $ "SCORE: " ++ show s
+                     forM_ p (putStrLn . show)
+                  Nothing -> putStrLn "NO PLAN"
                 putStrLn "")
        $ testPlanRule
        -- $ "fib(X) :- fib(X-1) + fib(X-2)"
diff --git a/src/Dyna/Backend/Python.hs b/src/Dyna/Backend/Python.hs
new file mode 100644 (file)
index 0000000..746a6c9
--- /dev/null
@@ -0,0 +1,70 @@
+---------------------------------------------------------------------------
+-- | Some week-before-the-deadline heroics to try to get something
+-- (anything) up and running.
+--
+-- XXX This is terrible.  Just terrible.
+
+-- Header material                                                      {{{
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE Rank2Types #-}
+{-# LANGUAGE ScopedTypeVariables #-}
+
+module Dyna.Backend.Python where
+
+import qualified Data.ByteString            as B
+import qualified Data.ByteString.Char8      as BC
+import           Data.Char
+-- import           Data.Either
+import qualified Data.List                  as L
+import qualified Data.Map                   as M
+import qualified Data.Maybe                 as MA
+import qualified Data.Ord                   as O
+import qualified Data.Set                   as S
+import qualified Debug.Trace                as XT
+import           Dyna.Analysis.ANF
+import           Dyna.Analysis.Aggregation
+import           Dyna.Analysis.RuleMode
+import           Dyna.Term.TTerm
+import qualified Dyna.ParserHS.Parser       as P
+import           Dyna.XXX.PPrint
+import           Dyna.XXX.TrifectaTest
+import           Text.PrettyPrint.Free
+import qualified Text.Trifecta              as T
+
+------------------------------------------------------------------------}}}
+-- Preliminaries                                                        {{{
+
+------------------------------------------------------------------------}}}
+-- Experimental Detritus                                                {{{
+
+processFile fileName = do
+  pr <- T.parseFromFileEx (P.dlines) fileName
+  case pr of
+    T.Failure td -> T.display td
+    T.Success rs ->
+      let urs  = map (\(P.LRule x T.:~ _) -> x) rs
+          anfs = map (runNormalize . normRule) urs
+          eaggm = buildAggMap anfs
+      in -- Ensure that we have an aggregator plan
+         case eaggm of
+           Left e -> print e >> putStrLn "while building aggregator map."
+           Right aggm -> print "Got an agg plan..."
+             -- XXX now, build an update plan for each rule
+             
+
+------------------------------------------------------------------------}}}
+-- Experimental Residuals?                                              {{{
+
+-- | Normalize all the rules in a file and emit S-exprs for the ANF
+-- normalized form.
+--
+-- NOTE: This is used by bin/prototype.py
+normalizeFile file = do
+    contents <- B.readFile file
+    writeFile (file ++ ".anf")
+              (show $ vcat (map (\(P.LRule x T.:~ _) ->
+                                printANF $ runNormalize $ normRule x)
+                                (unsafeParse P.dlines contents))
+                      <> line)
+    return ()
+------------------------------------------------------------------------}}}