Skip to content
Snippets Groups Projects
Commit 5f63a6aa authored by chrg's avatar chrg
Browse files

Work done!

parent 3fbfca47
No related branches found
No related tags found
No related merge requests found
Showing
with 508 additions and 334 deletions
...@@ -4,60 +4,63 @@ from dataclasses import dataclass ...@@ -4,60 +4,63 @@ from dataclasses import dataclass
@dataclass @dataclass
class ReducePath: class ReducePath:
path: list[bool] path: list[bool]
index: int = 0 reasons: list[str]
@classmethod
def empty(cls):
return cls([], [])
def explore(self, choice): def explore(self, choice):
self.index = 0 self.reasons = []
self.path.append(choice) self.path.append(choice)
return self return self
def left(self): def left(self):
return self.index - len(self.path) return len(self.reasons) - len(self.path)
def dispensable(self): def check(self, reason: str):
self.index += 1 self.reasons.append(reason)
try: try:
return self.path[self.index - 1] return self.path[len(self.reasons) - 1]
except IndexError: except IndexError:
return False return False
def reduce(predicate, rtree): def reduce(predicate, reducer, i):
r = ReducePath([]) r = ReducePath.empty()
i = rtree(r) j = i
it = rtree(r.explore(True)) ix = reducer(i, r.explore(True))
# While we don't consume all choices going down the true branch # While we don't consume all choices going down the true branch
while r.left() >= 0: while r.left() >= 0:
if predicate(it): print(r.reasons[len(r.path) - 1], "...", end="")
if predicate(ix):
# If true update the valid input # If true update the valid input
i = it print(" Yes")
j = ix
else: else:
# If false we have to go down the left branch. # If false we have to go down the left branch.
print(" No")
r.path[-1] = False r.path[-1] = False
it = rtree(r.explore(True)) ix = reducer(i, r.explore(True))
return i return j
def debug(predicate): def debug(predicate):
def newpred(i): def newpred(i):
t = predicate(i) t = predicate(i)
print(t, i) print(" test", i, "...", end="")
return t return t
return newpred return newpred
def reduce_list(items: list): def reduce_set(i: set, r) -> set:
def rtree(r: ReducePath) -> list: result: set = set()
results = [] for e in sorted(i):
for i in items: if not r.check(f"remove {e}?"):
if r.dispensable(): result.add(e)
continue return result
results.append(i)
return results
return rtree
if __name__ == "__main__": if __name__ == "__main__":
reduce(debug(lambda a: 1 in a), reduce_list([1, 2, 3])) reduce(debug(lambda a: "a" in a), reduce_set, {"a", "b", "c"})
--failure-report .hspec-failures --failure-report .hspec-failures
--rerun
--rerun-all-on-success --rerun-all-on-success
...@@ -15,6 +15,7 @@ import Control.Monad.State ...@@ -15,6 +15,7 @@ import Control.Monad.State
import Data.Foldable import Data.Foldable
import Data.Function import Data.Function
import Data.Functor import Data.Functor
import Data.IORef (atomicModifyIORef', atomicWriteIORef, newIORef)
import Data.List.NonEmpty qualified as NE import Data.List.NonEmpty qualified as NE
import Data.RPath qualified as RPath import Data.RPath qualified as RPath
import Data.Text qualified as Text import Data.Text qualified as Text
...@@ -28,6 +29,7 @@ import System.Directory ...@@ -28,6 +29,7 @@ import System.Directory
import System.Exit import System.Exit
import System.FilePath import System.FilePath
import System.IO import System.IO
import System.IO.Error (tryIOError)
import System.IO.Temp import System.IO.Temp
import System.Process.Typed import System.Process.Typed
import Text.Pretty.Simple import Text.Pretty.Simple
...@@ -92,6 +94,14 @@ run = do ...@@ -92,6 +94,14 @@ run = do
, help "check the output for the validity code and output debug information" , help "check the output for the validity code and output debug information"
] ]
debug <-
flag False True $
fold
[ long "debug"
, short 'D'
, help "keep debug versions of all copies"
]
fixpoint <- fixpoint <-
flag False True $ flag False True $
fold fold
...@@ -128,6 +138,8 @@ run = do ...@@ -128,6 +138,8 @@ run = do
) )
usingLoggerT (filterBySeverity Info msgSeverity $ cmapM fmt logTextStderr) do usingLoggerT (filterBySeverity Info msgSeverity $ cmapM fmt logTextStderr) do
counter <- liftIO (newIORef (0 :: Int))
let let
test f = process D ("test " <> Text.pack f) do test f = process D ("test " <> Text.pack f) do
err <- liftIO $ runProcess (proc cmd []) err <- liftIO $ runProcess (proc cmd [])
...@@ -153,14 +165,21 @@ run = do ...@@ -153,14 +165,21 @@ run = do
check f l c = process D "Checking predictate" do check f l c = process D "Checking predictate" do
let xs = NE.nonEmpty (filter ((RPath.Yes ==) . RPath.choice) l) let xs = NE.nonEmpty (filter ((RPath.Yes ==) . RPath.choice) l)
let decided = length l - RPath.numberOfUndecided l let decided = length l - RPath.numberOfUndecided l
count <- liftIO (atomicModifyIORef' counter (\a -> (a + 1, a)))
logInfo logInfo
( "Checking D=" ( "Checking (num: "
<> Text.pack (show count)
<> ") D="
<> Text.pack (show decided <> "/" <> show (length l)) <> Text.pack (show decided <> "/" <> show (length l))
<> ": " <> ": "
<> Text.pack (maybe "-" ((\(r, p) -> r <> " at " <> show p) . RPath.label . NE.last) xs) <> Text.pack (maybe "-" ((\(r, p) -> r <> " at " <> show p) . RPath.label . NE.last) xs)
) )
output f c output f c
res <- test f res <- test f
when debug $ liftIO do
copyFile f ("case-" <> show count <.> "c")
ret <- case res of ret <- case res of
ExitSuccess -> do ExitSuccess -> do
liftIO $ copyFile f (f <.> "last") liftIO $ copyFile f (f <.> "last")
...@@ -169,7 +188,9 @@ run = do ...@@ -169,7 +188,9 @@ run = do
| Just x == validity -> do | Just x == validity -> do
folder <- liftIO do folder <- liftIO do
temp <- getCanonicalTemporaryDirectory temp <- getCanonicalTemporaryDirectory
folder <- createTempDirectory temp "rtree-c" createTempDirectory temp "rtree-c"
logWarning $ "Produced invalid code, see " <> Text.pack folder <> " for details"
liftIO do
withFile (folder </> "reduction.path") WriteMode \h -> do withFile (folder </> "reduction.path") WriteMode \h -> do
forM (takeWhile ((/= RPath.Undecided) . RPath.choice) l) \(RPath.AnnotatedChoice c' (msg, pos)) -> do forM (takeWhile ((/= RPath.Undecided) . RPath.choice) l) \(RPath.AnnotatedChoice c' (msg, pos)) -> do
hPutStr h [RPath.debugShowChoice c'] hPutStr h [RPath.debugShowChoice c']
...@@ -178,10 +199,10 @@ run = do ...@@ -178,10 +199,10 @@ run = do
hPutStr h " at " hPutStr h " at "
hPrint h pos hPrint h pos
copyFile cmd (folder </> takeFileName cmd) copyFile cmd (folder </> takeFileName cmd)
copyFile (f <.> "last") (folder </> takeFileName (f <.> "last"))
copyFile f (folder </> takeFileName f) copyFile f (folder </> takeFileName f)
tryIOError do
copyFile (f <.> "last") (folder </> takeFileName (f <.> "last"))
pure folder pure folder
logWarning $ "Produced invalid code, see " <> Text.pack folder <> " for details"
when pedandic do when pedandic do
logError "Pedandic, stoping now." logError "Pedandic, stoping now."
liftIO exitFailure liftIO exitFailure
......
ghcid --command='cabal repl rtree-c-test' -r --reload=test/cases ghcid --command='cabal repl rtree-c-test' -W -r --reload=test/cases
This diff is collapsed.
...@@ -2,5 +2,6 @@ static int a = 0; ...@@ -2,5 +2,6 @@ static int a = 0;
int main () { int main () {
for (a = 0;;) { for (a = 0;;) {
break;
} }
} }
float fabs(float);
float testx(float);
float testy(float a);
int main() {
return 0;
}
void printf(const char* fmt,...);
int main(void) {
printf("Hello, %s %s", "World", "!");
return 0;
}
struct point { int x; int y; }; struct point { int x, y; };
struct point p1 = { 0, 0 }; struct point p1 = { 0, 0 };
......
CTranslUnit
[ CFDefExt
( CFunDef
[ CTypeSpec
( CIntType () )
]
( CDeclr
( Just
( Ident "add" 1651297 () )
)
[ CFunDeclr
( CFunParamsNew
[ CDecl
[ CTypeSpec
( CIntType () )
]
[ CDeclarationItem
( CDeclr
( Just
( Ident "a" 97 () )
) [] Nothing [] ()
) Nothing Nothing
] ()
, CDecl
[ CTypeSpec
( CIntType () )
]
[ CDeclarationItem
( CDeclr
( Just
( Ident "b" 98 () )
) [] Nothing [] ()
) Nothing Nothing
] ()
] False
) [] ()
] Nothing [] ()
) []
( CCompound []
[ CBlockStmt
( CReturn
( Just
( CBinary CAddOp
( CVar
( Ident "a" 97 () ) ()
)
( CVar
( Ident "b" 98 () ) ()
) ()
)
) ()
)
] ()
) ()
)
, CFDefExt
( CFunDef
[ CTypeSpec
( CIntType () )
]
( CDeclr
( Just
( Ident "main" 232419565 () )
)
[ CFunDeclr
( CFunParamsNew [] False ) [] ()
] Nothing [] ()
) []
( CCompound []
[ CBlockStmt
( CReturn
( Just
( CCall
( CVar
( Ident "add" 1651297 () ) ()
)
[ CConst
( CIntConst 10 () )
, CConst
( CIntConst 23 () )
] ()
)
) ()
)
] ()
) ()
)
] ()
\ No newline at end of file
// 0 remove function add at ("test/cases/small/add.c": line 1)
// 0 remove parameter at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1)
// 0 remove return statement at ("test/cases/small/add.c": line 2)
// 1 remove return statement at ("test/cases/small/add.c": line 6)
int add(int a)
{
return a;
}
int main()
{
}
// 0 remove function add at ("test/cases/small/add.c": line 1) // 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 0 remove parameter at ("test/cases/small/add.c": line 1) // 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1) // 1 remove parameter at ("test/cases/small/add.c": line 1)
// 1 remove return statement at ("test/cases/small/add.c": line 2) // 1 remove parameter at ("test/cases/small/add.c": line 1)
// 0 Remove compound at ("test/cases/small/add.c": line 1)
// 0 remove return statement at ("test/cases/small/add.c": line 6) // 0 remove return statement at ("test/cases/small/add.c": line 6)
// 0 do without param at ("test/cases/small/add.c": line 6)
int add(int a) int add()
{ {
} }
int main() int main()
{ {
return add(10); return add();
} }
// 0 remove function add at ("test/cases/small/add.c": line 1)
// 0 remove parameter at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1)
// 1 remove return statement at ("test/cases/small/add.c": line 2)
// 0 remove return statement at ("test/cases/small/add.c": line 6)
// 1 do without param at ("test/cases/small/add.c": line 6)
int add(int a)
{
}
int main()
{
return add(0);
}
// 0 remove function add at ("test/cases/small/add.c": line 1) // 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1) // 1 remove parameter at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1) // 1 remove parameter at ("test/cases/small/add.c": line 1)
// 0 Remove compound at ("test/cases/small/add.c": line 1)
// 1 remove return statement at ("test/cases/small/add.c": line 6) // 1 remove return statement at ("test/cases/small/add.c": line 6)
// 0 Remove compound at ("test/cases/small/add.c": line 5)
int add() int add()
{ {
......
// 0 remove function add at ("test/cases/small/add.c": line 1) // 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 0 remove parameter at ("test/cases/small/add.c": line 1) // 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1) // 1 remove parameter at ("test/cases/small/add.c": line 1)
// 1 remove return statement at ("test/cases/small/add.c": line 2) // 1 remove parameter at ("test/cases/small/add.c": line 1)
// 0 Remove compound at ("test/cases/small/add.c": line 1)
// 1 remove return statement at ("test/cases/small/add.c": line 6) // 1 remove return statement at ("test/cases/small/add.c": line 6)
// 1 Remove compound at ("test/cases/small/add.c": line 5)
int add(int a) int add()
{ {
} }
int main() int main()
......
// 0 remove function add at ("test/cases/small/add.c": line 1) // 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1) // 1 remove parameter at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1) // 1 remove parameter at ("test/cases/small/add.c": line 1)
// 1 Remove compound at ("test/cases/small/add.c": line 1)
// 0 remove return statement at ("test/cases/small/add.c": line 6) // 0 remove return statement at ("test/cases/small/add.c": line 6)
int add() int add()
......
// 0 remove function add at ("test/cases/small/add.c": line 1) // 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1) // 1 remove parameter at ("test/cases/small/add.c": line 1)
// 0 remove parameter at ("test/cases/small/add.c": line 1) // 1 remove parameter at ("test/cases/small/add.c": line 1)
// 1 Remove compound at ("test/cases/small/add.c": line 1)
// 1 remove return statement at ("test/cases/small/add.c": line 6) // 1 remove return statement at ("test/cases/small/add.c": line 6)
// 0 Remove compound at ("test/cases/small/add.c": line 5)
int add(int b) int add()
{ {
} }
int main() int main()
......
// 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1)
// 1 Remove compound at ("test/cases/small/add.c": line 1)
// 1 remove return statement at ("test/cases/small/add.c": line 6)
// 1 Remove compound at ("test/cases/small/add.c": line 5)
int add()
{
}
int main()
{
}
// 0 remove function add 41 at ("test/cases/small/add.c": line 1)
// 1 remove function add 41 at ("test/cases/small/add.c": line 1)
// 0 Remove compound at ("test/cases/small/add.c": line 5)
int main()
{
}
// 0 remove function add at ("test/cases/small/add.c": line 1)
// 1 remove parameter at ("test/cases/small/add.c": line 1)
// 0 remove parameter at ("test/cases/small/add.c": line 1)
// 0 remove return statement at ("test/cases/small/add.c": line 6)
// 0 do without param at ("test/cases/small/add.c": line 6)
int add(int b)
{
}
int main()
{
return add(23);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment