Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor to make it cabal installable

  • Loading branch information...
commit 33c64702e6b044f0850bfe7c716767290677ed69 1 parent 1d600b1
Jurriën Stutterheim norm2782 authored
14 NanoProlog.cabal
View
@@ -3,7 +3,7 @@ Version: 0.1
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
- (@Language.prolog.Nanoprolog@) which can be run on its
+ (@Language.Prolog.Nanoprolog@) which can be run on its
own. It reads a file with definitions, and then prompts
for a goal. All possibe solutions are printed, preceded by
a tree showing which rules were applied in which order.
@@ -22,8 +22,14 @@ Source-repository head
Executable nano-prolog
Hs-source-dirs: src
- Main-is: Language/Prolog/NanoProlog.hs
+ Main-is: Main.hs
Build-depends:
- base >= 4 && < 5,
- uu-parsinglib >= 2.7.1
+ base >= 4 && < 5
+
+Library
+ Build-Depends: base >= 4.0 && < 5.0,
+ uu-parsinglib >= 2.7.1,
+ ListLike == 3.1.*
+ Hs-Source-Dirs: src
+ Exposed-modules: Language.Prolog.NanoProlog
43 src/Language/Prolog/NanoProlog.hs
View
@@ -1,8 +1,20 @@
-
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FlexibleContexts #-}
-module Language.Prolog.NanoProlog (Term(..),Rule((:<-:)),LowerCase,unify,subst,startParse,pRule,pTerm) where
+module Language.Prolog.NanoProlog (
+ Term(..)
+ , Rule((:<-:))
+ , LowerCase
+ , unify
+ , subst
+ , startParse
+ , pRule
+ , pTerm
+ , pFun
+ , printSolutions
+ , emptyEnv
+ , solve
+ ) where
import Data.ListLike.Base (ListLike)
import Data.List (intercalate)
@@ -67,33 +79,6 @@ solve rules e n (t:ts)
| rule@(c :<-: cs) <- tag n rules
]
--- * 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
- putStr "File with rules? "
- fn <- getLine
- s <- readFile fn
- let (rules, errors) = startParse (pList pRule) s
- if Prelude.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 putStr "goal? "
- s <- getLine
- unless (s == "quit") $
- do let (goal, errors) = startParse pFun s
- if null errors
- then printSolutions (print goal) ["0"] (solve rules emptyEnv 0 [goal])
- else do putStrLn "Some goals were expected:"
- mapM_ (putStrLn.show) errors
- loop rules
-
-- ** Printing the solutions
-- | `printSolutions` performs a depth-first walk over the `Result` tree, while accumulating the rules that were applied on the path which was traversed from the root to the current node. At a successful leaf tis contains the full proof
printSolutions :: IO () -> [String] -> Result -> IO ()
24 src/Language/Prolog/royals.pro
View
@@ -1,24 +0,0 @@
-ma(mien,juul).
-ma(juul,bea).
-ma(bea,alex).
-ma(bea,cons).
-oma(X,Z):-ma(X,Y),ouder(Y,Z).
-
-append(nil,X,X).
-append(cons(A,X), Y, cons(A,Z)):- append(X,Y,Z) .
-
-pa(alex,ale).
-pa(alex,ama).
-pa(alex,ari).
-ma(max,ale).
-ma(max,ama).
-ma(max,ari).
-
-ouder(X,Y) :- pa(X,Y).
-ouder(X,Y) :- ma(X,Y).
-
-voor(X,Y) :- ouder(X,Y).
-voor(X,Y) :- ouder(X,Z), voor(Z,Y).
-
-plus(zero,X,X).
-plus(succ(X), Y, succ(Z)) :- plus(X, Y,Z).
Please sign in to comment.
Something went wrong with that request. Please try again.