| 1 | {- | |
|---|
| 2 | Module :$Header$ |
|---|
| 3 | Description : utilitary functions used throughout the CMDL interface |
|---|
| 4 | Copyright : uni-bremen and DFKI |
|---|
| 5 | License : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt |
|---|
| 6 | Maintainer : r.pascanu@jacobs-university.de |
|---|
| 7 | Stability : provisional |
|---|
| 8 | Portability : portable |
|---|
| 9 | |
|---|
| 10 | PGIP.Utils contains different basic functions that are |
|---|
| 11 | used throughout the CMDL interface and could not be found in |
|---|
| 12 | Prelude |
|---|
| 13 | |
|---|
| 14 | -} |
|---|
| 15 | |
|---|
| 16 | module PGIP.DataTypesUtils |
|---|
| 17 | ( getAllNodes |
|---|
| 18 | , obtainGoalNodeList |
|---|
| 19 | , getAllGoalNodes |
|---|
| 20 | , getAllGoalEdges |
|---|
| 21 | , getTh |
|---|
| 22 | , baseChannels |
|---|
| 23 | , genErrorMsg |
|---|
| 24 | , genMessage |
|---|
| 25 | , generatePrompter |
|---|
| 26 | , add2hist |
|---|
| 27 | , getIdComorphism |
|---|
| 28 | ) where |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | import Interfaces.DataTypes |
|---|
| 32 | import Interfaces.Utils |
|---|
| 33 | import Interfaces.History |
|---|
| 34 | import PGIP.Utils |
|---|
| 35 | import PGIP.DataTypes |
|---|
| 36 | import Common.Result |
|---|
| 37 | import Data.List |
|---|
| 38 | import Data.Graph.Inductive.Graph |
|---|
| 39 | import Static.GTheory |
|---|
| 40 | import Static.DevGraph |
|---|
| 41 | import Proofs.TheoremHideShift |
|---|
| 42 | import System.IO |
|---|
| 43 | |
|---|
| 44 | import Proofs.AbstractState |
|---|
| 45 | import Proofs.TheoremHideShift |
|---|
| 46 | |
|---|
| 47 | import Static.GTheory |
|---|
| 48 | import Static.DevGraph |
|---|
| 49 | |
|---|
| 50 | import Common.Result |
|---|
| 51 | |
|---|
| 52 | import Data.Graph.Inductive.Graph |
|---|
| 53 | import Data.List |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | import Common.Result |
|---|
| 57 | import Logic.Comorphism |
|---|
| 58 | import Logic.Grothendieck |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | add2hist :: [UndoRedoElem] -> CMDL_State -> CMDL_State |
|---|
| 62 | add2hist descr st |
|---|
| 63 | = let intst = add2history [] (intState st) descr |
|---|
| 64 | in st { |
|---|
| 65 | intState = intst } |
|---|
| 66 | |
|---|
| 67 | -- | Given a list of selected theory generate an Id comorphism to the |
|---|
| 68 | -- first selected theory |
|---|
| 69 | getIdComorphism :: [Int_NodeInfo] -> Maybe AnyComorphism |
|---|
| 70 | getIdComorphism ls |
|---|
| 71 | = case ls of |
|---|
| 72 | [] -> Nothing |
|---|
| 73 | (Element st _):_ -> |
|---|
| 74 | case sublogicOfTheory st of |
|---|
| 75 | (G_sublogics lid sub) -> Just $ Comorphism (mkIdComorphism lid sub) |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | -- | Generates the string containing the prompter |
|---|
| 79 | generatePrompter :: CMDL_State -> String |
|---|
| 80 | generatePrompter st |
|---|
| 81 | = case i_state $ intState st of |
|---|
| 82 | Nothing -> prompterHead $ prompter st |
|---|
| 83 | Just ist -> |
|---|
| 84 | let pst = prompter st |
|---|
| 85 | els = case elements ist of |
|---|
| 86 | [] -> [] |
|---|
| 87 | el:[] -> case el of |
|---|
| 88 | Element sm _ ->"."++(theoryName sm) |
|---|
| 89 | el:_ -> case el of |
|---|
| 90 | Element sm _ ->"." ++(theoryName sm)++ ".." |
|---|
| 91 | cm = case elements ist of |
|---|
| 92 | [] -> [] |
|---|
| 93 | _-> case cComorphism ist of |
|---|
| 94 | Nothing -> [] |
|---|
| 95 | Just cm' -> |
|---|
| 96 | case getIdComorphism $ elements ist of |
|---|
| 97 | Nothing -> [] |
|---|
| 98 | Just ocm -> |
|---|
| 99 | case cm' == ocm of |
|---|
| 100 | True -> [] |
|---|
| 101 | False -> "*" |
|---|
| 102 | in (delExtension $ fileLoaded pst) ++ els ++ cm ++ (prompterHead pst) |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | -- | Given a list of node names and the list of all nodes |
|---|
| 106 | -- the function returns all the nodes that have their name |
|---|
| 107 | -- in the name list but are also goals |
|---|
| 108 | obtainGoalNodeList :: CMDL_State -> [String] -> [LNode DGNodeLab] |
|---|
| 109 | -> ([String],[LNode DGNodeLab]) |
|---|
| 110 | obtainGoalNodeList state input ls |
|---|
| 111 | = let (l1,l2) = obtainNodeList input ls |
|---|
| 112 | l2' = filter (\(nb,nd) -> |
|---|
| 113 | let nwth = getTh Dont_translate nb state |
|---|
| 114 | in case nwth of |
|---|
| 115 | Nothing -> False |
|---|
| 116 | Just th -> nodeContainsGoals (nb,nd) th) l2 |
|---|
| 117 | in (l1,l2') |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | -- | Returns the list of all nodes that are goals, |
|---|
| 121 | -- taking care of the up to date status |
|---|
| 122 | getAllGoalNodes :: CMDL_State -> [LNode DGNodeLab] |
|---|
| 123 | getAllGoalNodes st |
|---|
| 124 | = case i_state $ intState st of |
|---|
| 125 | Nothing -> [] |
|---|
| 126 | Just ist -> |
|---|
| 127 | filter (\(nb,nd) -> |
|---|
| 128 | let nwth = getTh Dont_translate nb st |
|---|
| 129 | in case nwth of |
|---|
| 130 | Nothing -> False |
|---|
| 131 | Just th -> nodeContainsGoals (nb,nd) th) $ |
|---|
| 132 | getAllNodes $ ist |
|---|
| 133 | |
|---|
| 134 | -- | Returns the list of all goal edges taking care of the |
|---|
| 135 | -- up to date status |
|---|
| 136 | getAllGoalEdges :: CMDL_State -> [LEdge DGLinkLab] |
|---|
| 137 | getAllGoalEdges st |
|---|
| 138 | = case i_state $ intState st of |
|---|
| 139 | Nothing -> [] |
|---|
| 140 | Just ist -> |
|---|
| 141 | filter edgeContainsGoals $ getAllEdges $ ist |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | --local function that computes the theory of a node |
|---|
| 145 | --that takes into consideration translated theories in |
|---|
| 146 | --the selection too and returns the theory as a string |
|---|
| 147 | getTh :: CMDL_UseTranslation -> Int -> CMDL_State -> Maybe G_theory |
|---|
| 148 | getTh useTrans x st |
|---|
| 149 | = let |
|---|
| 150 | -- compute the theory for a given node |
|---|
| 151 | fn n = case i_state $ intState st of |
|---|
| 152 | Nothing -> Nothing |
|---|
| 153 | Just ist -> |
|---|
| 154 | case computeTheory False (i_libEnv ist) (i_ln ist) n of |
|---|
| 155 | Result _ (Just (_, th)) -> Just th |
|---|
| 156 | _ -> Nothing |
|---|
| 157 | |
|---|
| 158 | in |
|---|
| 159 | case useTrans of |
|---|
| 160 | Dont_translate -> fn x |
|---|
| 161 | Do_translate -> |
|---|
| 162 | case i_state $ intState st of |
|---|
| 163 | Nothing -> Nothing |
|---|
| 164 | Just ist -> |
|---|
| 165 | case elements ist of |
|---|
| 166 | [] -> fn x |
|---|
| 167 | _ -> |
|---|
| 168 | case find (\y -> case y of |
|---|
| 169 | Element _ z -> z == x) $ |
|---|
| 170 | elements ist of |
|---|
| 171 | Nothing -> fn x |
|---|
| 172 | Just _ -> |
|---|
| 173 | case cComorphism ist of |
|---|
| 174 | Nothing -> fn x |
|---|
| 175 | Just cm -> |
|---|
| 176 | case fn x of |
|---|
| 177 | Nothing -> Nothing |
|---|
| 178 | Just sth-> |
|---|
| 179 | case mapG_theory cm sth of |
|---|
| 180 | Result _ Nothing -> Just sth |
|---|
| 181 | Result _ (Just sth') -> Just sth' |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | -- | Generates the base channels to be used (stdin and stdout) |
|---|
| 185 | baseChannels :: [CMDL_Channel] |
|---|
| 186 | baseChannels |
|---|
| 187 | = let ch_in = CMDL_Channel { |
|---|
| 188 | chName = "stdin", |
|---|
| 189 | chType = ChStdin, |
|---|
| 190 | chHandler = stdin, |
|---|
| 191 | chSocket = Nothing, |
|---|
| 192 | chProperties = ChRead |
|---|
| 193 | } |
|---|
| 194 | ch_out = CMDL_Channel { |
|---|
| 195 | chName = "stdout", |
|---|
| 196 | chType = ChStdout, |
|---|
| 197 | chHandler = stdout, |
|---|
| 198 | chSocket = Nothing, |
|---|
| 199 | chProperties = ChWrite |
|---|
| 200 | } |
|---|
| 201 | in ch_in : ch_out : [] |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | genErrorMsg :: String -> CMDL_State -> CMDL_State |
|---|
| 205 | genErrorMsg msg st |
|---|
| 206 | = st { |
|---|
| 207 | output = CMDL_Message { |
|---|
| 208 | outputMsg = [], |
|---|
| 209 | warningMsg = [], |
|---|
| 210 | errorMsg = msg |
|---|
| 211 | } |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | genMessage :: String -> String -> CMDL_State -> CMDL_State |
|---|
| 215 | genMessage warnings msg st |
|---|
| 216 | = st{ |
|---|
| 217 | output = CMDL_Message { |
|---|
| 218 | outputMsg = msg, |
|---|
| 219 | warningMsg = warnings, |
|---|
| 220 | errorMsg = [] |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | |
|---|