Browse files

Merge branch 'master' of github.com:norm2782/NanoProlog

  • Loading branch information...
2 parents 8160be7 + 5e223cb commit 2f12bc36c33cce0c2dc87491c089b1f4024ee11e @doaitse doaitse committed May 24, 2011
View
2 .ghci
@@ -0,0 +1,2 @@
+:set -Wall
+:set -isrc
View
26 Makefile
@@ -1,27 +1,5 @@
-default:
- make clean
- make install
-
-jcu:
- make && jcu
-
dist:
- cabal check
+ cabal clean
cabal configure
+ cabal haddock
cabal sdist
-
-install:
- cabal install -fdevelopment
-
-clean:
- cabal clean
-
-run:
- jcu
-
-debug:
- DEBUG=1 jcu
-
-deps:
- rm -rf deps
- mkdir deps && cd deps && git clone https://github.com/snapframework/snap-auth.git && cd snap-auth && cabal install && cd .. && git clone https://github.com/ozataman/snap-extension-mongodb.git && cd snap-extension-mongodb && cabal install && cd ../..
View
13 NanoProlog.cabal
@@ -1,5 +1,5 @@
Name: NanoProlog
-Version: 0.1.2
+Version: 0.2
Synopsis: Very small interpreter for a Prolog-like language
Description: This package was developed to demonstrate the ideas behind
the Prolog language. It contains a very small interpreter
@@ -10,11 +10,12 @@ Description: This package was developed to demonstrate the ideas behind
License: BSD3
license-file: LICENSE
Author: Doaitse Swierstra, Jurriën Stutterheim
-Maintainer: Jurriën Stutterheim
+Maintainer: j.stutterheim@uu.nl
Stability: Experimental
Category: Language
Build-type: Simple
Cabal-version: >= 1.6
+Extra-Source-Files: README, royals.pro
Source-repository head
Type: git
@@ -25,12 +26,16 @@ Executable nano-prolog
Main-is: Main.hs
Build-depends:
- base >= 4 && < 5
+ base >= 4 && < 5,
+ uu-parsinglib >= 2.7.1
Library
Build-Depends: base >= 4.0 && < 5.0,
uu-parsinglib >= 2.7.1,
ListLike == 3.1.*,
containers == 0.4.*
Hs-Source-Dirs: src
- Exposed-modules: Language.Prolog.NanoProlog.Lib
+ Exposed-modules: Language.Prolog.NanoProlog.NanoProlog,
+ Language.Prolog.NanoProlog.Interpreter
+ Extensions: Rank2Types, FlexibleContexts, TypeSynonymInstances,
+ FlexibleInstances
View
0 src/royals.pro → royals.pro
File renamed without changes.
View
53 src/Language/Prolog/NanoProlog/Interpreter.hs
@@ -0,0 +1,53 @@
+module Language.Prolog.NanoProlog.Interpreter where
+
+import Language.Prolog.NanoProlog.NanoProlog
+import Text.ParserCombinators.UU
+import System.IO
+
+-- * Running the Interpreter
+-- ** The main interpreter
+-- | The `main` program prompt for a file with Prolog rules and call the main
+-- interpreter loop
+run :: IO ()
+run = do hSetBuffering stdin LineBuffering
+ putStrLn "File with rules?"
+ fn <- getLine
+ s <- readFile fn
+ let (rules, errors) = startParse (pList pRule) s
+ if null errors then do mapM_ print rules
+ loop rules
+ else do putStrLn "No rules parsed"
+ mapM_ print errors
+ run
+
+-- | `loop` ask for a goal, and enuartes all solutions found, each preceded by
+-- a trace conatining the rules applied in a tree-like fashion
+loop :: [Rule] -> IO ()
+loop rules = do putStrLn "goal? "
+ s <- getLine
+ unless (s == "quit") $
+ do let (goal, errors) = startParse pFun s
+ if null errors
+ then printSolutions (solve rules emptyEnv [("0",goal)])
+ else do putStrLn "Some goals were expected:"
+ mapM_ print errors
+ loop rules
+
+-- | `printSolutions` takes the result of a treewalk, which constructs
+-- all the proofs, and pairs them with their final
+-- substitutions. Alternative approaches in printing are to print the
+-- raw proofs, i.e. without applying the final substitution (remove
+-- the @subst env@ ). This nicely shows how the intermediate variables
+-- come into life. By including the test on the length the facts
+-- directly stemming from the data base are not printed. This makes
+-- the proofs much shorter, but a bit less complete.
+printSolutions :: Result -> IO ()
+printSolutions result = sequence_
+ [ do sequence_ [ putStrLn (prefix ++ " " ++ show (subst env pr))
+ | (prefix, pr@(p :<-: pp)) <- reverse proof
+-- , length pp >0
+ ]
+ putStr "substitution: "
+ putStrLn (show' env)
+ void getLine
+ | (proof, env) <- enumerateDepthFirst [] result ]
View
6 src/Language/Prolog/NanoProlog/Lib.hs → src/Language/Prolog/NanoProlog/NanoProlog.hs
@@ -3,7 +3,7 @@
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
-module Language.Prolog.NanoProlog.Lib (
+module Language.Prolog.NanoProlog.NanoProlog (
LowerCase
, Result(..)
, Rule((:<-:))
@@ -119,8 +119,8 @@ enumerateBreadthFirst :: Proofs -> [String] -> Result -> [(Proofs, Env)]
-- | `printEnv` prints a single solution, showing only the variables
-- that were introduced in the original goal
show' :: Env -> String
-show' env = intercalate ", " . filter (not.null) . map showBdg $ M.assocs env
- where showBdg (x, t) | isGlobVar x = x ++ " <- "++ showTerm t
+show' env = intercalate ", " . filter (not.null) . map showBdg $ M.assocs env
+ where showBdg (x, t) | isGlobVar x = x ++ " <- " ++ showTerm t
| otherwise = ""
showTerm t@(Var _) = showTerm (subst env t)
showTerm (Fun f []) = f
View
52 src/Main.hs
@@ -1,58 +1,10 @@
-{-# LANGUAGE Rank2Types #-}
-{-# LANGUAGE FlexibleContexts #-}
-{-# LANGUAGE TypeSynonymInstances #-}
-{-# LANGUAGE FlexibleInstances #-}
-
module Main where
-import Language.Prolog.NanoProlog.Lib
-import Text.ParserCombinators.UU
-import System.IO
+import Language.Prolog.NanoProlog.Interpreter (run)
-- * Running the Interpreter
-- ** The main interpreter
-- | The `main` program prompt for a file with Prolog rules and call the main
-- interpreter loop
main :: IO ()
-main = do hSetBuffering stdin LineBuffering
- putStrLn "File with rules?"
- fn <- getLine
- s <- readFile fn
- let (rules, errors) = startParse (pList pRule) s
- if null errors then do mapM_ print rules
- loop rules
- else do putStrLn "No rules parsed"
- mapM_ print errors
- main
-
--- | `loop` ask for a goal, and enuartes all solutions found, each preceded by
--- a trace conatining the rules applied in a tree-like fashion
-loop :: [Rule] -> IO ()
-loop rules = do putStrLn "goal? "
- s <- getLine
- unless (s == "quit") $
- do let (goal, errors) = startParse pFun s
- if null errors
- then printSolutions (solve rules emptyEnv [("0",goal)])
- else do putStrLn "Some goals were expected:"
- mapM_ print errors
- loop rules
-
--- | `printSolutions` takes the result of a treewalk, which constructs
--- all the proofs, and pairs them with their final
--- substitutions. Alternative approaches in printing are to print the
--- raw proofs, i.e. without applying the final substitution (remove
--- the @subst env@ ). This nicely shows how the intermediate variables
--- come into life. By including the test on the length the facts
--- directly stemming from the data base are not printed. This makes
--- the proofs much shorter, but a bit less complete.
-printSolutions :: Result -> IO ()
-printSolutions result = sequence_
- [ do sequence_ [ putStrLn (prefix ++ " " ++ show (subst env pr))
- | (prefix, pr@(p :<-: pp)) <- reverse proof
--- , length pp >0
- ]
- putStr "substitution: "
- putStrLn (show' env)
- void getLine
- | (proof, env) <- enumerateDepthFirst [] result ]
+main = run

0 comments on commit 2f12bc3

Please sign in to comment.