-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.hs
More file actions
283 lines (232 loc) · 8.62 KB
/
main.hs
File metadata and controls
283 lines (232 loc) · 8.62 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
module Main where
import Data.List (find, intercalate)
import Data.Maybe (listToMaybe)
import Data.Set (Set)
import System.IO (hFlush, stdout)
import Text.Read (readMaybe)
import qualified Data.Set as Set
-- A simple LCF-style theorem prover in Haskell.
------------------------
-- 1. Inference rules --
------------------------
data Formula
= Var !String -- x₁, x₂, ...
| Imp !Formula !Formula -- A → B
deriving (Eq, Ord, Read)
instance Show Formula where
show (Var x) = x
show (Imp a b) = "(" ++ show a ++ " → " ++ show b ++ ")"
data Theorem
= Theorem !(Set Formula) !Formula -- Γ ⊢ A
deriving (Eq, Ord, Read)
instance Show Theorem where
show (Theorem gamma a) =
let gammaStr = if Set.null gamma then "" else intercalate "\n" (map show $ Set.toList gamma) ++ "\n"
in gammaStr ++ "______________________________________Goal\n" ++ show a
--
-- ─────
-- A ⊢ A
assume :: Formula -> Theorem
assume a = Theorem (Set.singleton a) a
-- Γ ⊢ B
-- ───────────────
-- Γ - {A} ⊢ A → B
introRule :: Formula -> Theorem -> Theorem
introRule a (Theorem gamma b) = Theorem (gamma `Set.difference` Set.singleton a) (Imp a b)
-- Γ ⊢ A → B Δ ⊢ A
-- ────────────────
-- Γ ∪ Δ ⊢ B
elimRule :: Theorem -> Theorem -> Maybe Theorem
elimRule (Theorem gamma imp) (Theorem delta a) = case imp of
Var _ -> Nothing
Imp _ b ->
if imp == Imp a b
then Just $ Theorem (gamma `Set.union` delta) b
else Nothing
----------------------------------
-- 2. Goal-directed proof state --
----------------------------------
type Justification = [Theorem] -> Maybe Theorem
type Tactic = Goal -> Maybe GoalState
newtype Goal = Goal Theorem
deriving (Eq, Ord, Read)
instance Show Goal where
show (Goal thm) = show thm
data GoalState
= GoalState
{ goals :: ![Goal]
, justification :: !Justification
}
proves :: Theorem -> Formula -> Bool
proves (Theorem _ a) b = a == b
-- Applies the given tactic to the first goal in the goal state.
by :: Tactic -> GoalState -> Maybe GoalState
by tactic curState = case goals curState of
-- No goals left to prove.
[] -> Nothing
(goal:rest) -> do
newState <- tactic goal
return $ GoalState
{ goals = goals newState ++ rest
, justification = combineJustification newState
}
where
-- The new justification first applies the new justification to the new goals, and then the old
-- justification to the result of the new justification and the remaining goals.
-- FIXME (2025-05-26): Perhaps we can simply use [goals newState] and [goals curState] instead of the splitAt?
combineJustification newState thms = do
let (topGoals, remainingGoals) = splitAt (length $ goals newState) thms
thm <- justification newState topGoals
justification curState (thm : remainingGoals)
----------------
-- 3. Tactics --
----------------
assumption :: Tactic
assumption (Goal (Theorem gamma a))
| Set.member a gamma = Just $ GoalState
{ goals = []
, justification = \_ -> return $ assume a
}
| otherwise = Nothing
introTactic :: Tactic
introTactic (Goal (Theorem gamma (Var _))) = Nothing
introTactic (Goal (Theorem gamma (Imp a b))) = do
let newGamma = Set.insert a gamma
let subGoals = [Goal (Theorem newGamma b)]
return $ GoalState
{ goals = subGoals
, justification = justification'
}
where
justification' thms = do
thm <- find (`proves` b) thms
return $ introRule a thm
elimTactic :: Formula -> Tactic
elimTactic assm (Goal (Theorem gamma a)) = do
let imp = Imp assm a -- A → B
let impThm = Theorem gamma imp -- Γ ⊢ A → B
let assmThm = Theorem gamma assm -- Γ ⊢ A
return $ GoalState
{ goals = [Goal impThm, Goal assmThm]
, justification = justification' imp assm
}
where
justification' imp assm thms = do
impThm <- find (`proves` imp) thms
assmThm <- find (`proves` assm) thms
elimRule impThm assmThm
-----------------
-- 4. Commands --
-----------------
type History = [GoalState]
type Result a = Either String a
-- Get the current goal state, if it exists.
currentState :: History -> Result GoalState
currentState hist = case hist of
[] -> Left "No current goal state."
(x:_) -> Right x
-- Set the current goal.
g :: History -> Formula -> Result History
g hist form = do
let goal = Goal $ Theorem Set.empty form
return [GoalState { goals = [goal], justification = listToMaybe }]
top :: History -> Result (Theorem, History)
top hist = do
goalState <- currentState hist
case justification goalState [] of
Nothing -> Left "Theorem not yet proven."
Just thm -> Right (thm, hist)
-- Return the current goal.
p :: History -> Result (Goal, History)
p hist = do
goalState <- currentState hist
case listToMaybe $ goals goalState of
Nothing -> Left "QED."
Just goal -> Right (goal, hist)
-- Apply a tactic to the current goal.
e :: History -> Tactic -> Result History
e hist tac = do
curState <- currentState hist
case by tac curState of
Nothing -> Left "Failed to apply tactic."
Just newState -> Right (newState : hist)
-- Undo the last tactic.
b :: History -> Result History
b hist = do
case hist of
(_ : oldHist) -> Right oldHist
[] -> Left "No previous goal state to revert to."
------------------------------
-- 5. Interactive interface --
------------------------------
splitFirst :: String -> (String, String)
splitFirst str = case words str of
[] -> ("", "")
(x:xs) -> (x, unwords xs)
main :: IO ()
main = do
putStrLn "LCF-style theorem prover"
putStrLn "Type 'help' for a list of commands."
loop [] -- The empty list denotes the initial history (which is empty)
where
loop :: History -> IO ()
loop hist = do
putStr "> " >> hFlush stdout
input <- getLine
case splitFirst input of
("help", rest) -> handleHelpCommand hist rest
("top", rest) -> handleTopCommand hist rest
("g", rest) -> handleGCommand hist rest
("p", rest) -> handlePCommand hist rest
("e", rest) -> handleECommand hist rest
("b", rest) -> handleBCommand hist rest
-- Catch-all for unknown commands.
(cmd, rest) -> putStrLn ("Unknown command '" <> cmd <> "'. Type 'help' for a list of commands.") >> loop hist
handleHelpCommand hist input = case input of
"" -> do
putStrLn "Available commands:"
putStrLn " g <formula> Set the current goal to the given formula."
putStrLn " p Print the current goal."
putStrLn " e <tactic> Apply a tactic to the current goal."
putStrLn " b Undo the last tactic."
putStrLn " top Show the proven theorem."
putStrLn " help Show this help message."
putStrLn ""
putStrLn "<tactic> ::= 'intro' | 'elim' , <formula> | 'assumption' ;"
putStrLn "<formula> ::= 'Var' , <string> | 'Imp' , <formula> , <formula> | '(' , <formula> , ')' ;"
putStrLn "<string> ::= '\"' , ? any sequence of characters ? , '\"' ;"
loop hist
_ -> putStrLn "Unexpected input for 'help' command. Usage: 'help'" >> loop hist
handleTopCommand hist input = case top hist of
Left err -> putStrLn err >> loop hist
Right (thm, newHist) -> do
print thm
loop newHist
handleGCommand hist input = case readMaybe input :: Maybe Formula of
Nothing -> putStrLn "Invalid formula." >> loop hist
Just form -> case g hist form of
Left err -> putStrLn err >> loop hist
Right newHist -> handlePCommand newHist "" >> loop newHist
handlePCommand hist "" = case p hist of
Left err -> putStrLn err >> loop hist
Right (goal, newHist) -> do
print goal
loop newHist
handlePCommand hist _input = putStrLn "Unexpected input for 'p' command. Usage: 'p'" >> loop hist
handleECommand hist input = case tacticFun of
Nothing -> putStrLn "Invalid tactic." >> loop hist
Just tacticFun -> case e hist tacticFun of
Left err -> putStrLn err >> loop hist
Right newHist -> handlePCommand newHist "" >> loop newHist
where
tacticFun = case splitFirst input of
("intro", rest) -> return introTactic
("elim", rest) -> do
form <- readMaybe rest :: Maybe Formula
return $ elimTactic form
("assumption", _) -> return assumption
_input -> Nothing
handleBCommand hist "" = case b hist of
Left err -> putStrLn err >> loop hist
Right newHist -> handlePCommand newHist "" >> loop newHist
handleBCommand hist _input = putStrLn "Unexpected input for 'b' command. Usage: 'b'" >> loop hist