Skip to content

Commit

Permalink
Merge branch 'main' into SamB-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
robrix authored Mar 25, 2024
2 parents c0f11bf + 3baf0b4 commit 36c6eae
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 245 deletions.
1 change: 1 addition & 0 deletions .github/workflows/haskell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
run: |
cabal v2-update
cabal v2-configure --project-file=cabal.project.ci --disable-optimization --enable-tests --write-ghc-environment-files=always -j2
cd semantic-source && cabal v2-configure --project-file=cabal.project.ci --disable-optimization --enable-tests --write-ghc-environment-files=always -j2
- name: Restore from cache
run: ./cabal-cache sync-from-archive --threads=2 --archive-uri=dist-cache || true
Expand Down
1 change: 1 addition & 0 deletions semantic-analysis/cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

-- Local packages
packages: .
../semantic-source
6 changes: 3 additions & 3 deletions semantic-analysis/script/ghci-flags
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ function flags {
echo "-Wno-name-shadowing"
echo "-Wno-safe"
echo "-Wno-unsafe"
[[ "$ghc_version" = 8.8.* ]] || [[ "$ghc_version" = 8.10.* ]] || [[ "$ghc_version" = 9.2.* ]] && echo "-Wno-missing-deriving-strategies" || true
[[ "$ghc_version" = 8.10.* ]] || [[ "$ghc_version" = 9.2.* ]] && echo "-Wno-missing-safe-haskell-mode" && echo "-Wno-prepositive-qualified-module" && echo "-Wno-unused-packages" || true
[[ "$ghc_version" = 9.2.* ]] && echo "-Wno-missing-kind-signatures" || true
[[ "$ghc_version" = 9.4.* ]] || [[ "$ghc_version" = 8.8.* ]] || [[ "$ghc_version" = 8.10.* ]] || [[ "$ghc_version" = 9.2.* ]] && echo "-Wno-missing-deriving-strategies" || true
[[ "$ghc_version" = 9.4.* ]] || [[ "$ghc_version" = 8.10.* ]] || [[ "$ghc_version" = 9.2.* ]] && echo "-Wno-missing-safe-haskell-mode" && echo "-Wno-prepositive-qualified-module" && echo "-Wno-unused-packages" || true
[[ "$ghc_version" = 9.4.* ]] || [[ "$ghc_version" = 9.2.* ]] && echo "-Wno-missing-kind-signatures" || true
}

flags > "$output_file"
5 changes: 3 additions & 2 deletions semantic-analysis/semantic-analysis.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ library
hs-source-dirs: src
exposed-modules:
Analysis.Analysis.Concrete
Analysis.Analysis.DeadCode
Analysis.Analysis.Exception
Analysis.Analysis.Typecheck
Analysis.Blob
Expand All @@ -68,15 +69,15 @@ library
Analysis.Project
Analysis.Reference
Analysis.Syntax
Analysis.Syntax.Python
Analysis.VM
build-depends:
, aeson >= 1.4 && < 3
, base >= 4.13 && < 5
, bytestring >= 0.10.8.2 && < 0.13
, containers ^>= 0.6
, filepath
, fused-effects ^>= 1.1
, hashable
, semantic-source ^>= 0.2
, text ^>= 1.2.3.1
, transformers ^>= 0.5
, vector ^>= 0.12.3
120 changes: 120 additions & 0 deletions semantic-analysis/src/Analysis/Analysis/DeadCode.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Analysis.Analysis.DeadCode
( deadCodeFlowInsensitive
) where

import Analysis.Carrier.Fail.WithLoc
import qualified Analysis.Carrier.Statement.State as A
import qualified Analysis.Carrier.Store.Monovariant as A
import Analysis.Effect.Domain as A
import Analysis.File
import Analysis.FlowInsensitive
import Analysis.Reference
import Control.Applicative (Alternative (..))
import Control.Carrier.Fresh.Church
import Control.Carrier.Reader
import Control.Carrier.State.Church
import Control.Effect.Labelled
import Control.Monad (zipWithM_)
import Control.Monad.Trans.Class
import Data.Function (fix)
import qualified Data.Set as Set

deadCodeFlowInsensitive
:: Ord term
=> (forall sig m
. (Has (A.Dom Unit) sig m, Has (A.Env A.MAddr) sig m, Has (Reader Reference) sig m, Has A.Statement sig m, HasLabelled A.Store (A.Store A.MAddr Unit) sig m, MonadFail m)
=> (term -> m Unit)
-> (term -> m Unit)
)
-> (term -> Set.Set term)
-> [File term]
-> ( Set.Set term
, A.MStore Unit
, [File (Either (Reference, String) (Set.Set Unit))]
)
deadCodeFlowInsensitive eval subterms
= run
. runState (\ dead (store, files) -> pure (dead, store, files)) Set.empty
. evalFresh 0
. A.runStoreState
. traverse (runFile eval subterms)

runFile
:: ( Has Fresh sig m
, Has (State (A.MStore Unit)) sig m
, Has (State (Set.Set term)) sig m
, Ord term
)
=> (forall sig m
. (Has (A.Dom Unit) sig m, Has (A.Env A.MAddr) sig m, Has (Reader Reference) sig m, Has A.Statement sig m, HasLabelled A.Store (A.Store A.MAddr Unit) sig m, MonadFail m)
=> (term -> m Unit)
-> (term -> m Unit)
)
-> (term -> Set.Set term)
-> File term
-> m (File (Either (Reference, String) (Set.Set Unit)))
runFile eval subterms file = traverse run file
where run term = do
modify (<> subterms term)
A.runStatement (const pure)
. runReader (fileRef file)
. A.runEnv @Unit
. runFail
. convergeTerm (A.runStore @Unit . runDomain . fix (cacheTerm . evalDead))
$ term
evalDead eval' subterm = do
modify (Set.delete subterm)
eval eval' subterm


data Unit = Unit
deriving (Eq, Ord, Show)


newtype DomainC m a = DomainC { runDomain :: m a }
deriving (Alternative, Applicative, Functor, Monad, MonadFail)

instance MonadTrans DomainC where
lift = DomainC


instance ( Alternative m
, Has (A.Env A.MAddr) sig m
, Has Fresh sig m
, HasLabelled A.Store (A.Store A.MAddr Unit) sig m
, MonadFail m
)
=> Algebra (A.Dom Unit :+: sig) (DomainC m) where
alg hdl sig ctx = case sig of
L (DVar _) -> pure (Unit <$ ctx)

L (DInt _) -> pure (Unit <$ ctx)

L DUnit -> pure (Unit <$ ctx)

L (DBool _) -> pure (Unit <$ ctx)
L (DIf _ t e) -> hdl (t <$ ctx) <|> hdl (e <$ ctx)

L (DString _) -> pure (Unit <$ ctx)

L (DAbs n b) -> do
addrs <- traverse A.alloc n
let args = Unit <$ n
zipWithM_ (A..=) addrs args
hdl (b args <$ ctx)
L (DApp _ _) -> pure (Unit <$ ctx)

L (_ :>>> t) -> pure (t <$ ctx)

L (DDie msg) -> fail (show msg)

R other -> DomainC (alg (runDomain . hdl) other ctx)
1 change: 0 additions & 1 deletion semantic-analysis/src/Analysis/Name.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
module Analysis.Name
( Name
-- * Constructors
Expand Down
Loading

0 comments on commit 36c6eae

Please sign in to comment.