Create runtime_files directory for some benchmarks
[nofib.git] / real / prolog / Main.hs
1 --
2 -- Prolog interpreter top level module
3 -- Mark P. Jones November 1990
4 --
5 -- uses Haskell B. version 0.99.3
6 --
7 module Main(main) where
8
9 import PrologData
10 import Parse
11 import Interact
12 import Subst
13 import Engine
14 import Version
15 import Data.List(nub)--1.3
16
17 import System.IO.Error (catchIOError)
18
19 --- Command structure and parsing:
20
21 data Command = Fact Clause | Query [Term] | Show | Error | Quit | NoChange
22
23 command :: Parser Command
24 command = just (sptok "bye" `orelse` sptok "quit") `doo` (\quit->Quit)
25 `orelse`
26 just (okay NoChange)
27 `orelse`
28 just (sptok "??") `doo` (\show->Show)
29 `orelse`
30 just clause `doo` Fact
31 `orelse`
32 just (sptok "?-" `seQ` termlist) `doo` (\(q,ts)->Query ts)
33 `orelse`
34 okay Error
35
36 --- Main program read-solve-print loop:
37
38 signOn :: String
39 signOn = "Mini Prolog Version 1.5 (" ++ version ++ ")\n\n"
40
41 main = --echo False abort
42 putStr signOn >>
43 putStr ("Reading " ++ stdlib) >>
44 catchIOError (readFile stdlib)
45 (\fail -> putStr "...not found\n" >> return "")
46 >>= \ is ->
47 if null is then
48 interpreter []
49 else
50 let parse = map clause (lines is)
51 clauses = [ r | ((r,""):_) <- parse ]
52 reading = ['.'| c <- clauses] ++ "done\n"
53 in
54 putStr reading >>
55 interpreter clauses
56
57 stdlib :: String
58 stdlib = "runtime_files/stdlib"
59
60 interpreter :: [Clause] -> IO ()
61 interpreter lib = getContents >>= \ is ->
62 putStr (loop startDb is)
63 where startDb = foldl addClause emptyDb lib
64
65 loop :: Database -> String -> String
66 loop db = readln "> " (exec db . fst . head . command)
67
68 exec :: Database -> Command -> String -> String
69 exec db (Fact r) = skip (loop (addClause db r))
70 exec db (Query q) = demonstrate db q
71 exec db Show = writeln (show db) (loop db)
72 exec db Error = writeln "I don't understand\n" (loop db)
73 exec db Quit = writeln "Thank you and goodbye\n" end
74 exec db NoChange = skip (loop db)
75
76 --- Handle printing of solutions etc...
77
78 solution :: [Id] -> Subst -> [String]
79 solution vs s = [ show (Var i) ++ " = " ++ show v
80 | (i,v) <- [ (i,s i) | i<-vs ], v /= Var i ]
81
82 demonstrate :: Database -> [Term] -> Interactive
83 demonstrate db q = printOut (map (solution vs) (prove db q))
84 where vs = (nub . concat . map varsIn) q
85 printOut [] = writeln "no.\n" (loop db)
86 printOut ([]:bs) = writeln "yes.\n" (loop db)
87 printOut (b:bs) = writeln (doLines b) (nextReqd bs)
88 doLines = foldr1 (\xs ys -> xs ++ "\n" ++ ys)
89 nextReqd bs = writeln " "
90 (readch (\c->if c==';'
91 then writeln ";\n" (printOut bs)
92 else writeln "\n" (loop db)) "")
93
94 --- End of Main.hs