From de2c4a51e524108bac86a3328e27d744da60d9a6 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Sat, 8 Dec 2012 23:07:12 -0500 Subject: [PATCH] Parameterize Analysis.ANF.NT on the contents of a variable --- src/Dyna/Analysis/ANF.hs | 33 +++++++++++++++++--------------- src/Dyna/Analysis/ANFSelftest.hs | 2 +- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/Dyna/Analysis/ANF.hs b/src/Dyna/Analysis/ANF.hs index 0a58a4b..28611ca 100644 --- a/src/Dyna/Analysis/ANF.hs +++ b/src/Dyna/Analysis/ANF.hs @@ -69,7 +69,7 @@ {-# LANGUAGE OverloadedStrings #-} module Dyna.Analysis.ANF ( - ANFState(..), NT(..), FDT, EVF, FDR, + ANFState(..), NT(..), NTV, FDT, EVF, FDR, normTerm, normRule, runNormalize, printANF ) where @@ -127,18 +127,24 @@ mergeDispositions = md md SDQuote (ECExplicit,ADEval) = ADEval md SDQuote (_,_) = ADQuote +-- | A Normalized Term, parametric in the variable case +-- -- The Ord instance is solely for Data.Set's use -data NT = NTNumeric (Either Integer Double) - | NTString B.ByteString - | NTVar DVar +data NT v = NTNumeric (Either Integer Double) + | NTString B.ByteString + | NTVar v deriving (Eq,Ord,Show) -type FDT = TermF DVar NT + +-- | Normalized Term over 'DVar' (that is, either a primitive or a variable) +type NTV = NT DVar + +-- | Flat Dyna Term (that is, either a primitive or a term built up from a +-- functor over primitives and variables) +type FDT = TermF DVar NTV + +-- | Either a 'DVar' or a flat Dyna term type EVF = Either DVar FDT -{- This stage of ANF does not actually link evaluations to - - their semantic interpretation. That is, we have not yet - - resolved foreign function calls. - -} data ANFState = AS { as_next :: !Int , as_evals :: M.Map DVar EVF @@ -213,14 +219,11 @@ dynaFunctorSelfDispositions x = case x of -- Normalize a Term {{{ -- | Convert a syntactic term into ANF; while here, move to a --- Control.Unification term representation. +-- flattened representation. -- -- The ANFState ensures that variables are unique; we additionally give them -- \"semi-meaningful\" prefixes, but these should not be relied upon. -- --- XXX On second thought, we should just move to a @TermF B.ByteString --- Var@ representation, since we want everything flattened. --- -- XXX This sheds span information entirely, which is probably not what we -- actually want. Note that we're careful to keep a stack of contexts -- around, so we should probably do something clever like attach them to @@ -229,7 +232,7 @@ normTerm_ :: (Functor m, MonadState ANFState m, MonadReader ANFDict m) => EvalCtx -- ^ In an evaluation context? -> [T.Span] -- ^ List of spans traversed -> P.Term -- ^ Term being digested - -> m NT + -> m NTV -- Variables only evaluate in explicit context -- @@ -301,7 +304,7 @@ normTerm_ c ss (P.TFunctor f as) = do normTerm :: (Functor m, MonadState ANFState m, MonadReader ANFDict m) => Bool -- ^ In an evaluation context? -> T.Spanned P.Term -- ^ Term to digest - -> m NT + -> m NTV normTerm c (t T.:~ s) = normTerm_ (ECFunctor,if c then ADEval else ADQuote) [s] t diff --git a/src/Dyna/Analysis/ANFSelftest.hs b/src/Dyna/Analysis/ANFSelftest.hs index c3b6613..26970b2 100644 --- a/src/Dyna/Analysis/ANFSelftest.hs +++ b/src/Dyna/Analysis/ANFSelftest.hs @@ -26,7 +26,7 @@ import Dyna.Term.TTerm import Dyna.XXX.TrifectaTest -testNormTerm :: Bool -> B.ByteString -> (NT, ANFState) +testNormTerm :: Bool -> B.ByteString -> (NTV, ANFState) testNormTerm c = runNormalize . normTerm c . unsafeParse P.dterm testNormRule :: B.ByteString -> (FDR, ANFState) -- 2.50.1