summaryrefslogtreecommitdiff
path: root/Library/TermNet.hs
blob: 11a6992e487c669277eb991b4bedbd58e3e6333a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
module Library.TermNet(
    TermNet,

    empty,
    getLeafList,
    getBranchList,

    termToTermString,
    thmToTermString,

    addThm,
    addThmFromNode
    ) where



import Data.Maybe
import Data.List
import qualified Data.Set as Set
import Data.Graph.Inductive.Graph( Node )
import qualified Data.Graph.Inductive.Graph as Graph
import Library.ProofGraph
import Library.WriteProof
import Library.Object
import Library.Theorem
import Library.Term
import Library.Parse
import Library.Semantic
import Library.Stack( Stack, at, (<:>) )
import qualified Library.Stack as Stack



data TermNet = Leaf [(Theorem, Node)] | Branch [(String, TermNet)] deriving (Eq, Show)



empty :: TermNet
empty = Branch []



isLeaf :: TermNet -> Bool
isLeaf (Leaf _) = True
isLeaf _ = False



isBranch :: TermNet -> Bool
isBranch (Branch _) = True
isBranch _ = False



getLeafList :: TermNet -> Maybe [(Theorem, Node)]
getLeafList net =
    case net of
        Leaf list -> Just list
        Branch list -> Nothing



getBranchList :: TermNet -> Maybe [(String, TermNet)]
getBranchList net =
    case net of 
        Leaf list -> Nothing
        Branch list -> Just list



genThm :: PGraph -> Node -> Theorem
genThm graph node =
    let gen = (\g n num -> let edge = filter (\x -> (fst . thd3 $ x) == num) (Graph.out g n)
                               node = (snd3 . head $ edge)
                               listing = write g node
                           in fromJust (((\(a,_,_,_) -> a) . fromJust $ (eval listing)) `at` 0))
        hypList = map (fromJust . objTerm) (fromJust . objList $ (gen graph node 2))
        con = fromJust . objTerm $ (gen graph node 1)
    in Theorem (Set.fromList hypList) con



termToTermString :: Term -> [String]
termToTermString term =
    case term of
        (TConst _ _) ->
            ["const"]

        (TApp func arg) ->
            ["app"] ++ (termToTermString func) ++ (termToTermString arg)

        (TAbs var body) ->
            ["abs"] ++ (termToTermString body)

        (TVar var) ->
            ["var"]



thmToTermString :: Theorem -> [String]
thmToTermString theorem =
    let hypList = Set.toList (thmHyp theorem)
        f = (\soFar hyp -> soFar ++ ["hyp"] ++ (termToTermString hyp))
    in (foldl' f [] hypList) ++ ["con"] ++ (termToTermString . thmCon $ theorem)



addThm :: TermNet -> Theorem -> Node -> (TermNet, [(Theorem, Node)])
addThm net theorem node =
    let stringThm = thmToTermString theorem
        add = (\net list ->
                let branchList = fromJust . getBranchList $ net
                in case list of
                        [] ->
                            (net, [])

                        x:[] ->
                            let (check, rest) = partition (\(y,z) -> y == x && isLeaf z) branchList
                            in if (check == [])
                                then (Branch ((x,(Leaf [(theorem,node)])):rest), [(theorem,node)])
                                else let leaf = snd . head $ check
                                         leafList = fromJust . getLeafList $ leaf
                                     in --if ((theorem,node) `elem` leafList)
                                        --then (Branch ((x,leaf):rest), leafList)
                                        --else
                                        (Branch ((x,(Leaf ((theorem,node):leafList))):rest), (theorem,node):leafList)

                        x:xs ->
                            let (check, rest) = partition (\(y,z) -> y == x && isBranch z) branchList
                            in if (check == [])
                                then let (net', resultList) = add empty xs
                                     in (Branch ((x,net'):rest), resultList)
                                else let nextStepDown = snd . head $ check
                                         (net', resultList) = add nextStepDown xs
                                     in (Branch ((head check):rest), resultList))

    in add net stringThm



addThmFromNode :: TermNet -> PGraph -> Node -> (TermNet, [(Theorem, Node)])
addThmFromNode net graph node =
    let theorem = genThm graph node
    in addThm net theorem node