summaryrefslogtreecommitdiff
path: root/Command.hs
blob: 6df88f86ea4a6eb05036634783b0ef866c18c34c (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
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
module Command (
	name,
	number,
	absTerm,
	absThm,
	appTerm,
	appThm,
	assume,
	axiom,
	betaConv,
	constant,
	constTerm,
	deductAntisym,
	defineConst,
	defineTypeOp,
	eqMp,
	opType,
	refl,
	subst,
	thm,
	typeOp,
	var,
	varTerm,
	varType
    ) where

-- deliberately not included:
-- cons, nil, def, ref, remove, pop

-- all functions here deal exclusively with arguments
-- and results from/to the stack

import Data.List
import Data.Maybe
import qualified Data.Set as Set
import qualified Data.Map as Map
import TypeVar
import Term
import Theorem
import Object
import Parse


name :: String -> Maybe Name
name str =
	if (not . isName $ str)
	then Nothing
	else let wordlist = (separateBy '.') . removeEscChars . removeQuotes $ str
	         name = Name (init wordlist) (last wordlist)
	     in Just name


number :: String -> Maybe Number
number num =
	if (not . isNumber $ num)
	then Nothing
	else Just (read num)


absTerm :: Term -> Var -> Term
absTerm term var =
	TAbs (TVar var) term


absThm :: Theorem -> Var -> Maybe Theorem
absThm thm var =
	if (Set.member (TVar var) (thmHyp thm))
	then Nothing
	else Just (Theorem (thmHyp thm) (mkEquals (TAbs (TVar var) (getlhs . thmCon $ thm))
		                                      (TAbs (TVar var) (getrhs . thmCon $ thm))))


appTerm :: Term -> Term -> Term
appTerm term1 term2 =
	TApp term2 term1


appThm :: Theorem -> Theorem -> Theorem
appThm thm1 thm2 =
	Theorem (Set.union (thmHyp thm1) (thmHyp thm2))
	        (mkEquals (TApp (getlhs . thmCon $ thm2) (getlhs . thmCon $ thm1))
                      (TApp (getrhs . thmCon $ thm2) (getrhs . thmCon $ thm1)))


assume :: Term -> Maybe Theorem
assume term =
	if (typeOf term /= typeBool)
	then Nothing
	else Just (Theorem (Set.singleton term) term)


axiom :: Term -> [Term] -> Maybe Theorem
axiom term termlist =
	if (not (all ((== typeBool) . typeOf) termlist))
	then Nothing
	else Just (Theorem (Set.fromList termlist) term)


betaConv :: Term -> Theorem
betaConv term =
	Theorem (Set.empty)
            (mkEquals term
                      (substitute ([], [(tVar . tAbsVar . tAppLeft $ term, tAppRight $ term)])
                                  (tAbsTerm . tAppLeft $ term)))


constant :: Name -> Const
constant name =
	Const name


constTerm :: Type -> Const -> Term
constTerm ty c =
	TConst c ty


deductAntisym :: Theorem -> Theorem -> Theorem
deductAntisym x y =
	Theorem (Set.union (Set.delete (thmCon $ x) (thmHyp $ y))
                       (Set.delete (thmCon $ y) (thmHyp $ x)))
            (mkEquals (thmCon $ y) (thmCon $ x))


defineConst :: Term -> Name -> Maybe (Theorem, Const)
defineConst term name =
	if (freeVars term /= Set.empty || typeVarsInTerm term /= typeVarsInType (typeOf term))
	then Nothing
	else let constant = Const name
	         constTerm = TConst constant (typeOf term)
	         theorem = Theorem Set.empty (mkEquals constTerm term)
	     in Just (theorem, constant)


defineTypeOp :: Theorem -> [Name] -> Name -> Name -> Name -> Maybe (Theorem, Theorem, Const, Const, TypeOp)
defineTypeOp thm namelist r a n =
	if ((typeVarsInTerm . tAppLeft . thmCon $ thm) /= (Set.fromList . map (TypeVar) $ namelist) ||
		(length namelist) /= (length . nub $ namelist))
	then Nothing
	else let rep = Const r
	         abst = Const a
	         op = TypeOp n
	         rtype = typeOf . tAppRight . thmCon $ thm
	         atype = AType (map (TypeVar) namelist) op
	         r' = TVar (Var (Name [] "r'") rtype)
	         a' = TVar (Var (Name [] "a'") atype)
	         reptype = typeFunc atype rtype
	         abstype = typeFunc rtype atype
	         repTerm = TConst rep reptype
	         absTerm = TConst abst abstype
	         rthm = Theorem Set.empty 
	                        (mkEquals (TApp (tAppLeft . thmCon $ thm) r')
	         	                      (mkEquals (TApp repTerm (TApp absTerm r')) r'))
	         athm = Theorem Set.empty
	                        (mkEquals (TApp absTerm (TApp repTerm a')) a')
	     in Just (rthm, athm, rep, abst, op)


eqMp :: Theorem -> Theorem -> Maybe Theorem
eqMp thm1 thm2 =
	if (thmCon thm1 /= (getlhs . thmCon $ thm2))
	then Nothing
	else Just (Theorem (Set.union (thmHyp thm1) (thmHyp thm2))
	                   (getrhs . thmCon $ thm2))


opType :: [Type] -> TypeOp -> Type
opType typelist tyOp =
	AType typelist tyOp


refl :: Term -> Theorem
refl term =
	Theorem Set.empty (mkEquals term term)


subst :: Theorem -> [Object] -> Theorem
subst thm list =
	let s = makeSubst list
	in Theorem (Set.map (substitute s) (thmHyp thm))
               (substitute s (thmCon thm))


thm :: Term -> [Term] -> Theorem -> Maybe Theorem
thm term termlist oldthm =
	if ((term /= thmCon oldthm) || (Set.fromList termlist /= thmHyp oldthm))
	then Nothing
	else Just (Theorem (Set.fromList (alphaConvertList (Set.toList . thmHyp $ oldthm) termlist))
                       (alphaConvert (thmCon oldthm) term))


typeOp :: Name -> TypeOp
typeOp name =
	TypeOp name


var :: Type -> Name -> Maybe Var
var ty name =
	if (nameSpace name /= [])
	then Nothing
	else Just (Var name ty)


varTerm :: Var -> Term
varTerm var =
	TVar var


varType :: Name -> Maybe Type
varType name =
	if (nameSpace name /= [])
	then Nothing
	else Just (TypeVar name)