Skip to content

Commit

Permalink
console and graph packages split off.
Browse files Browse the repository at this point in the history
  • Loading branch information
Erdwolf authored and Erdwolf committed Sep 6, 2011
1 parent 6066ed5 commit 8267db8
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 8 deletions.
2 changes: 1 addition & 1 deletion makefile
Expand Up @@ -4,7 +4,7 @@ tests:
(cd specs; runghc -i../src Specs) (cd specs; runghc -i../src Specs)


shell: shell:
ghci -isrc -outputdir dist/build Prolog GraphViz Quote IsString -XOverloadedStrings -XQuasiQuotes ghci -isrc -iprolog-graph -outputdir dist/build Prolog GraphViz Quote IsString -XOverloadedStrings -XQuasiQuotes


bench: bench:
( cd bench; \ ( cd bench; \
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions prolog-console/Main.hs
@@ -0,0 +1,2 @@
module Main (main) where
import Console
19 changes: 19 additions & 0 deletions prolog-console/prolog-console.cabal
@@ -0,0 +1,19 @@
Name: prolog-console
Version: 0.1
Synopsis: A Prolog interpreter written in Haskell.
License: PublicDomain
Author: Matthias Bartsch
Maintainer: bartsch@cs.uni-bonn.de
Category: Language
Build-type: Simple

Cabal-version: >=1.2

Executable hsprolog
Main-is: Main.hs

Build-depends:
base >=4 && <5,
parsec >= 3.1.1,
readline >= 1.0.1.0,
prolog == 0.1.*
14 changes: 9 additions & 5 deletions src/GraphViz.hs → prolog-graph/GraphViz.hs
Expand Up @@ -14,7 +14,11 @@ import Data.GraphViz (preview, runGraphviz, setDirectedness, graphToDot, Graphvi
import Data.GraphViz.Attributes.Colors (Color(X11Color), X11Color(..)) import Data.GraphViz.Attributes.Colors (Color(X11Color), X11Color(..))
import Data.GraphViz.Attributes.HTML import Data.GraphViz.Attributes.HTML


import Prolog import qualified Data.Text.Lazy

import Language.Prolog

htmlStr = HtmlStr . Data.Text.Lazy.pack




-- Graphical output of derivation tree -- Graphical output of derivation tree
Expand Down Expand Up @@ -64,11 +68,11 @@ ensureNode node label =
then graph then graph
else Graph.insNode (node, label) graph else Graph.insNode (node, label) graph


makeNodeLabel _ [] = [HtmlStr "[]"] makeNodeLabel _ [] = [htmlStr "[]"]
makeNodeLabel _ gs = [HtmlStr $ intercalate "," $ map show gs] makeNodeLabel _ gs = [htmlStr $ intercalate "," $ map show gs]


makeEdgeLabel [] _ = [HtmlFont [HtmlPointSize 8] [HtmlStr "{}"]] makeEdgeLabel [] _ = [HtmlFont [HtmlPointSize 8] [htmlStr "{}"]]
makeEdgeLabel u _ = [HtmlFont [HtmlPointSize 8] $ intersperse (HtmlNewline []) [HtmlStr $ show v ++ " = " ++ show t | (v,t) <- u]] makeEdgeLabel u _ = [HtmlFont [HtmlPointSize 8] $ intersperse (HtmlNewline []) [htmlStr $ show v ++ " = " ++ show t | (v,t) <- u]]


modifyLabel node f = do modifyLabel node f = do
modify $ Graph.gmap $ \cxt@(in_,node',label,out) -> modify $ Graph.gmap $ \cxt@(in_,node',label,out) ->
Expand Down
18 changes: 18 additions & 0 deletions prolog-graph/Main.hs
@@ -0,0 +1,18 @@
module Main where

import Control.Applicative ((<$>))
import Control.Arrow ((>>>),(<<<))
import Language.Prolog (consult, parseQuery)
import ParseArgs (parseArgs)
import GraphViz (resolveTree, resolveTreeToFile)

main = do
(queryString, files, output) <- parseArgs
p <- concat <$> mapM ((abortOnError=<<) . consult) files
q <- abortOnError $ parseQuery queryString
resolveTreeToFile output p q
--case output of
-- Nothing -> resolveTree p q {- FIXME Never shown since thread terminated. -}
-- Just file -> resolveTreeToFile file p q >> return ()

abortOnError = either (error . show) return
31 changes: 31 additions & 0 deletions prolog-graph/ParseArgs.hs
@@ -0,0 +1,31 @@
{-# LANGUAGE DeriveDataTypeable #-}
module ParseArgs (
parseArgs
) where

import System.Console.CmdArgs
import System.Environment (getProgName)

data Options = Options
{ query :: String
, file :: [String]
, output :: String
, positional :: [String]
}
deriving (Data, Typeable)

options = Options
{ query = def &= typ "QUERY" &= help "Set Prolog query (If not set, first positional argument is used)"
, file = def &= typ "FILE" &= help "Consult file before executing query"
, output = "graph.png" &= typ "FILE" &= help "Save generated image to file (default: 'graph.png')"
, positional = def &= args &= typ "QUERY [FILE]..."
}
&= versionArg [ignore]
&= helpArg [name "h"]

parseArgs = do
opts <- getProgName >>= cmdArgs . ((options &=) . program)
return $ case opts of
Options q fs o [] -> (q, fs, o)
Options _ fs o [q] -> (q, fs, o)
Options _ fs o (q:fs') -> (q, fs++fs', o)
22 changes: 22 additions & 0 deletions prolog-graph/prolog-graph.cabal
@@ -0,0 +1,22 @@
Name: prolog-graph
Version: 0.1
Synopsis: A Prolog interpreter written in Haskell.
License: PublicDomain
Author: Matthias Bartsch
Maintainer: bartsch@cs.uni-bonn.de
Category: Language
Build-type: Simple

Cabal-version: >=1.2

Executable hsprolog-graph
Main-is: Main.hs

Build-depends:
base >=4 && <5,
prolog == 0.1.*,
fgl >= 5.4.2.4,
mtl >= 2.0.1.0,
cmdargs >= 0.8,
text >= 0.11.1.5,
graphviz
1 change: 1 addition & 0 deletions prolog.cabal
Expand Up @@ -15,6 +15,7 @@ Library
Language.Prolog.Quote Language.Prolog.Quote
Other-modules: Prolog Other-modules: Prolog
Interpreter Interpreter
Database
Unifier Unifier
Parser Parser
Syntax Syntax
Expand Down
4 changes: 3 additions & 1 deletion src/Parser.hs
@@ -1,5 +1,5 @@
module Parser module Parser
( consult ( consult, parseQuery
, program, whitespace, comment, clause, terms, term, bottom, vname , program, whitespace, comment, clause, terms, term, bottom, vname
) where ) where


Expand All @@ -17,6 +17,8 @@ consult = fmap consultString . readFile
consultString :: String -> Either ParseError Program consultString :: String -> Either ParseError Program
consultString = parse (whitespace >> program <* eof) "(input)" consultString = parse (whitespace >> program <* eof) "(input)"


parseQuery = parse (whitespace >> terms <* eof) "(query)"

program = many (clause <* char '.' <* whitespace) program = many (clause <* char '.' <* whitespace)


whitespace = skipMany (comment <|> skip space <?> "") whitespace = skipMany (comment <|> skip space <?> "")
Expand Down
2 changes: 1 addition & 1 deletion src/Prolog.hs
Expand Up @@ -10,7 +10,7 @@ module Prolog
, runNoGraphT , runNoGraphT
, resolve, resolve_ , resolve, resolve_
, (+++) , (+++)
, consult , consult, parseQuery
, program, whitespace, comment, clause, terms, term, bottom, vname , program, whitespace, comment, clause, terms, term, bottom, vname
) )
where where
Expand Down

0 comments on commit 8267db8

Please sign in to comment.