Advertisement
4da

IIS coursework

4da
Nov 3rd, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Control.Monad
  2. import Data.List
  3.  
  4. -- data structures declaration
  5.  
  6. data Feature = MkFeat String deriving (Show, Eq)
  7. data Type = MkType String | RootType | Unkn deriving (Show, Eq)
  8. data Object = MkObj String [Feature] Type | LeafObj String [Feature] Type deriving (Show)
  9.  
  10. -- data Hypothesis = Match | HType Type | Check deriving (Show, Eq)
  11.  
  12.  
  13. data Unification = UniRes [Feature] [Feature] Bool deriving (Show)
  14.  
  15. -- (1) [Feature] - list of unified features
  16. -- (2) [Feature] - list of features failed to unify
  17. -- Bool - was object matched?
  18.  
  19.  
  20. -- objects declaration
  21.  
  22. objList :: [Object]
  23. objList =
  24.   [
  25.     MkObj "Anilyn"      [MkFeat "Heavier-water",  MkFeat "Unsolv"]        (MkType "Amins"),
  26.     MkObj "Trietilamin" [MkFeat "Lighter-water",  MkFeat "Danger"]       (MkType "Amins"),
  27.     MkObj "Piperidin"   [MkFeat "Solvable"]                              (MkType "Amins"),
  28.     MkObj "Etanol"      [MkFeat "Lighter-water", MkFeat "Fuel"]          (MkType "Spirits"),
  29.     MkObj "Metanol"     [MkFeat "Danger",        MkFeat "Heavier-water"] (MkType "Spirits"),
  30.     MkObj "CU(OH)2"     [MkFeat "Racids",        MkFeat "Industry"]      (MkType "Bases"),
  31.     MkObj "LIOH"        [MkFeat "Solvable",      MkFeat "Danger"]        (MkType "Bases"),
  32.     MkObj "H2S04"       [MkFeat "Danger",        MkFeat "Industry"]      (MkType "Acids"),
  33.     MkObj "Si203"       [MkFeat "Racids",        MkFeat "Unsolvable"]    (MkType "Acids"),
  34.     MkObj "K2S04"       [MkFeat "Industry",      MkFeat "Solvable"]      (MkType "Salts"),
  35.     MkObj "Na2C03"      [MkFeat "Industry",      MkFeat "Fuel"]          (MkType "Salts"),
  36.    
  37.     MkObj "Amins"       [MkFeat "Ammonia-deriv"]                         (MkType "Organic"),
  38.     MkObj "Spirits"     [MkFeat "Water-deriv"]                           (MkType "Organic") ,
  39.     MkObj "Bases"       [MkFeat "Metal-ions-deriv"]                      (MkType "Non-organic"),
  40.     MkObj "Acids"       [MkFeat "Soury"]                                 (MkType "Non-organic"),
  41.     MkObj "Salts"       [MkFeat "Cons-Anions-KO"]                        (MkType "Non-organic"),
  42.                        
  43.     MkObj "Organic"     [MkFeat "Substance", MkFeat "Carbon-deriv"]      RootType,
  44.     MkObj "Non-organic" [MkFeat "Substance", MkFeat "Minerals-contain"]  RootType  
  45.   ]
  46.  
  47. -- number of last leaf object
  48. leafObjNum = 11
  49.  
  50.  
  51. getName (MkObj name _ _ ) = name
  52.  
  53. getReply :: String -> IO Bool
  54. getReply s = do
  55.   putStrLn ("Is " ++ s ++ " ?")
  56.   a <- getLine
  57.   return (a == "y" || a == "Y" || a == "yes")
  58.  
  59. type_choice :: Feature -> IO Bool
  60. type_choice (MkFeat f) = do
  61.   a <- getReply f
  62.   return (a)
  63.  
  64. checkFeat :: [Feature] -> IO Bool
  65. checkFeat [] = return (True)
  66. checkFeat (x : xs) = do
  67.   a <- type_choice x
  68.   b <- checkFeat(xs)
  69.   return (a && b)
  70.  
  71. iand :: IO Bool -> IO Bool -> IO Bool  
  72. iand a b = do
  73.   ca <- a
  74.   cb <- b
  75.   return (ca && cb)
  76.  
  77. -- Get the name of the object
  78.  
  79. nameOf :: Object -> String
  80. nameOf (MkObj nm _ _) = nm
  81.  
  82. typeOf :: Object -> Type
  83. typeOf (MkObj _ _ t) = t
  84.  
  85. featsOf :: Object -> [Feature]
  86. featsOf (MkObj _ fts (MkType _)) = fts
  87. featsOf (MkObj _ fts RootType) = [] ++ fts
  88. -- nameOf (LeafObj nm _ _) = nm
  89.  
  90. --------------------------------------------------------------                          
  91. findObj :: String -> [Object] -> Object
  92.  
  93. findObj nm (obj : xs) =
  94.   if (nameOf obj == nm)
  95.   then (obj)
  96.   else (findObj nm xs)
  97.        
  98. --------------------------------------------------------------
  99.  
  100. contAll :: (Eq a) => [a] -> [a] -> Bool
  101. contAll _ [] = True
  102. contAll arr (x:xs) = any (x == ) arr && (contAll arr xs)
  103.  
  104. --------------------------------------------------------------
  105.  
  106. findFeat :: [Feature] -> Feature -> Bool
  107. findFeat (x:xs) feat =
  108.   if x == feat
  109.   then True
  110.   else
  111.     findFeat xs feat
  112. findFeat [] _ = False    
  113.        
  114. --------------------------------------------------------------
  115.  
  116. getUniRes :: Unification -> Bool
  117. getUniRes (UniRes _ _ res) = res
  118.  
  119. getUniFeatPos :: Unification -> [Feature]
  120. getUniFeatPos  (UniRes fp _ _) = fp
  121.  
  122. getUniFeatNeg :: Unification -> [Feature]
  123. getUniFeatNeg  (UniRes _ fn _) = fn
  124.  
  125. --------------------------------------------------------------
  126.  
  127. -- Walk through Unification Features and ask user for missing ones
  128.  
  129. upUni :: [Feature] -> Unification -> IO Unification
  130. upUni [] uf = return uf
  131. upUni (f:fx) (UniRes fp fn _) =
  132.   if ((findFeat fp f) == False) && ((findFeat fn f) == False)
  133.      then
  134.     do
  135.       res <- (type_choice f)
  136.              
  137.       if (res == True)
  138.         then upUni fx (UniRes (fp ++ (f:[])) fn True)
  139.         else
  140.         return (UniRes fp (fn ++ (f:[])) False)
  141.        
  142.   else
  143.     upUni fx (UniRes fp fn True)
  144.    
  145. --------------------------------------------------------------    
  146. -- Make false unification of all objects with specified types
  147.  
  148. falseUnify :: Object -> [Feature]
  149.  
  150. falseUnify (MkObj name feats t) =
  151.   foldl
  152.   (\y x -> if ((typeOf x) == t && (nameOf x) /= name) == True
  153.                 then y ++ ((featsOf x) \\ feats)
  154.                 else y)
  155.   []
  156.   objList
  157.  
  158. --------------------------------------------------------------
  159.  
  160. -- 1 Object - hypothesis object
  161. -- 2 [Feature] - unified features
  162. -- 3 [Feature] - features failed to unify
  163.  
  164. hypsolve :: Object -> [Feature] -> [Feature] -> IO Unification
  165.  
  166. hypsolve (MkObj name (feat:xs) RootType) fp fn =    
  167.   if (contAll fp (feat:xs)) == True --
  168.   then do
  169.     -- putStrLn "hypsolve - True"
  170.     return (UniRes fp fn True)
  171.   else
  172.     -- if (contAll fn (feat:xs)) == True --
  173.     if (length (intersect fn (feat:xs)) /= 0)
  174.     then
  175.       return (UniRes fp fn False)
  176.     else
  177.      
  178.       do
  179.         uf <- upUni (feat:xs)
  180.               (UniRes
  181.                fp
  182.                fn
  183.                True)
  184.              
  185.         if (getUniRes uf == True)
  186.           then
  187.           return (
  188.             (UniRes (getUniFeatPos uf)
  189.              ((getUniFeatNeg uf) ++ falseUnify (findObj name objList))
  190.              (getUniRes uf)))
  191.           else
  192.           return (
  193.             (UniRes (getUniFeatPos uf)
  194.              (getUniFeatNeg uf)
  195.              (getUniRes uf)))
  196.  
  197. hypsolve (MkObj name (feat:xs) (MkType tp)) fp fn =    
  198.   if (contAll fp (feat:xs)) == True --
  199.   then do
  200.     -- putStrLn "hypsolve - True"
  201.     return (UniRes fp fn True)
  202.   else
  203.     if (length (intersect fn (feat:xs)) /= 0)
  204.     -- if (contAll fn (feat:xs)) == True --
  205.     -- if (any (fn
  206.        
  207.     then
  208.       return (UniRes fp fn False)
  209.     else
  210.  
  211.     do
  212.       hr <- hypsolve (findObj tp objList) fp fn
  213.       let ur = getUniRes hr
  214.      
  215.       if (ur == False)
  216.         then do
  217.         -- putStrLn "hypsolve - False"
  218.         return (UniRes (getUniFeatPos hr)
  219.                      (getUniFeatNeg hr) False)
  220.         else
  221.        
  222.         do
  223.           -- putStr "falseUnify for "
  224.           -- putStrLn name
  225.           -- print (falseUnify (findObj name objList))
  226.           uf <- upUni (feat:xs) (UniRes
  227.                            ((getUniFeatPos hr))
  228.                            ((getUniFeatNeg hr)
  229.                             -- ++ falseUnify (findObj name objList)
  230.                            )
  231.                            True)
  232.                
  233.           if (getUniRes uf == True)
  234.             then
  235.             return (
  236.               (UniRes (getUniFeatPos uf)
  237.                ((getUniFeatNeg uf) ++ falseUnify (findObj name objList))
  238.                (getUniRes uf)))
  239.             else
  240.             return (
  241.               (UniRes (getUniFeatPos uf)
  242.                (getUniFeatNeg uf)
  243.                (getUniRes uf)))
  244.        
  245. solve :: [Feature] -> [Feature] -> Int -> IO Object
  246.  
  247. solve fp fn num =
  248.  
  249.   if (num == leafObjNum)
  250.   then return (MkObj "No solution" [] RootType)
  251.   else
  252.    
  253.   do
  254.   putStrLn ("trying: " ++ nameOf (objList !! num))
  255.   -- print (objList !! num)  
  256.   -- print fp  
  257.   -- print fn
  258.   -- putStrLn " "
  259.   hr <- (hypsolve (objList !! num) fp fn)
  260.  
  261.   if (getUniRes hr == True)
  262.     then
  263.     do
  264.     -- putStr ("result is : ")
  265.     -- print (nameOf (objList !! num))
  266.  
  267.     return (objList !! num)
  268.     else
  269.       solve (getUniFeatPos hr) (getUniFeatNeg hr) (num + 1)
  270.  
  271. getSolve =
  272.   do
  273.     x <- solve [] [] 0
  274.     putStrLn ("Result: " ++ (nameOf x))
  275.     return ()
  276.        
  277.   -- return ()
  278.    
  279. main =  
  280.   getSolve
  281.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement