summaryrefslogtreecommitdiff
path: root/source/Syntax/Lexicon.hs
blob: 65072ee47d0dbd88625da98f207953093a600603 (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
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
{-# LANGUAGE NoImplicitPrelude #-}

-- | The 'Lexicon' describes the part of the grammar that extensible/dynamic.
--
-- The items of the 'Lexicon' are organized by their meaning and their
-- syntactic behaviour. They are typically represented as some kind of
-- pattern data which is then used to generate various production rules
-- for the concrete grammar. This representation makes inspection and
-- extension easier.
--

module Syntax.Lexicon
    ( module Syntax.Lexicon
    , pattern ConsSymbol
    , pattern PairSymbol
    , pattern CarrierSymbol
    , pattern ApplySymbol
    , pattern DomSymbol
    ) where


import Base
import Syntax.Abstract

import Data.List qualified as List
import Data.Sequence qualified as Seq
import Data.HashSet qualified as Set
import Data.HashMap.Strict qualified as HM
import Data.Text qualified as Text
import Text.Earley.Mixfix (Holey, Associativity(..))


data Lexicon = Lexicon
    { lexiconMixfix             :: Seq (HashMap (Holey Token) (Associativity, Marker))
    , lexiconConnectives        :: [[(Holey Token, Associativity)]]
    , lexiconPrefixPredicates   :: LexicalItems PrefixPredicate
    , lexiconStructFun          :: LexicalItems StructSymbol
    , lexiconRelationSymbols    :: LexicalItems RelationSymbol
    , lexiconVerbs              :: LexicalItems (SgPl LexicalPhrase)
    , lexiconAdjLs              :: LexicalItems LexicalPhrase
    , lexiconAdjRs              :: LexicalItems LexicalPhrase
    , lexiconNouns              :: LexicalItems (SgPl LexicalPhrase)
    , lexiconStructNouns        :: LexicalItems (SgPl LexicalPhrase)
    , lexiconFuns               :: LexicalItems (SgPl LexicalPhrase)
    } deriving (Show, Eq)

-- Projection returning the union of both left and right attributes.
--
lexiconAdjs :: Lexicon -> HashMap LexicalPhrase Marker
lexiconAdjs lexicon = lexiconAdjLs lexicon <> lexiconAdjRs lexicon

lookupOp :: FunctionSymbol -> Seq (HashMap FunctionSymbol (assoc, Marker)) -> Either String Marker
lookupOp f ops = case snd <$> firstJust (HM.lookup f) ops of
    Just m -> Right m
    Nothing -> Left (show f)

lookupLexicalItem :: (Hashable a, Show a) => a -> LexicalItems a -> Either String Marker
lookupLexicalItem a items = case HM.lookup a items of
    Just m -> Right m
    Nothing -> Left (show a)

type LexicalItems a = HashMap a Marker

builtins :: Lexicon
builtins = Lexicon
    { lexiconMixfix             = builtinMixfix
    , lexiconPrefixPredicates   = builtinPrefixPredicates
    , lexiconStructFun          = builtinStructOps
    , lexiconConnectives        = builtinConnectives
    , lexiconRelationSymbols    = builtinRelationSymbols
    , lexiconAdjLs              = mempty
    , lexiconAdjRs              = builtinAdjRs
    , lexiconVerbs              = builtinVerbs
    , lexiconNouns              = builtinNouns
    , lexiconStructNouns        = builtinStructNouns
    , lexiconFuns               = mempty
    }

-- INVARIANT: 10 precedence levels for now.
builtinMixfix :: Seq (HashMap FunctionSymbol (Associativity, Marker))
builtinMixfix = Seq.fromList $ (HM.fromList <$>)
    [ []
    , [binOp (Symbol "+") LeftAssoc "add", binOp (Command "union") LeftAssoc "union", binOp (Symbol "-") LeftAssoc "minus", binOp (Command "rminus") LeftAssoc "rminus", binOp (Command "monus") LeftAssoc "monus"]
    , [binOp (Command "relcomp") LeftAssoc "relcomp"]
    , [binOp (Command "circ") LeftAssoc "circ"]
    , [binOp (Command "mul") LeftAssoc "mul", binOp (Command "inter") LeftAssoc "inter", binOp (Command "rmul") LeftAssoc "rmul"]
    , [binOp (Command "setminus") LeftAssoc "setminus"]
    , [binOp (Command "times") RightAssoc "times"]
    , []
    , prefixOps
    , builtinIdentifiers
    ]
    where
        builtinIdentifiers :: [(FunctionSymbol, (Associativity, Marker))]
        builtinIdentifiers = identifier <$>
            [ "emptyset"
            , "naturals"
            , "integers"
            , "rationals"
            , "reals"
            , "unit"
            , "zero"
            ]

prefixOps :: [(FunctionSymbol, (Associativity, Marker))]
prefixOps =
    [ ([Just (Command "rfrac"), Just InvisibleBraceL, Nothing, Just InvisibleBraceR, Just InvisibleBraceL, Nothing, Just InvisibleBraceR], (NonAssoc, "rfrac"))
    , ([Just (Command "unions"), Just InvisibleBraceL, Nothing, Just InvisibleBraceR], (NonAssoc, "unions"))
    , ([Just (Command "cumul"), Just InvisibleBraceL, Nothing, Just InvisibleBraceR], (NonAssoc, "cumul"))
    , ([Just (Command "fst"), Just InvisibleBraceL, Nothing, Just InvisibleBraceR], (NonAssoc, "fst"))
    , ([Just (Command "snd"), Just InvisibleBraceL, Nothing, Just InvisibleBraceR], (NonAssoc, "snd"))
    , ([Just (Command "pow"), Just InvisibleBraceL, Nothing, Just InvisibleBraceR], (NonAssoc, "pow"))
    , ([Just (Command "neg"), Just InvisibleBraceL, Nothing, Just InvisibleBraceR], (NonAssoc, "neg"))
    , (ConsSymbol, (NonAssoc, "cons"))
    , (PairSymbol, (NonAssoc, "pair"))
    -- NOTE Is now defined and hence no longer necessary , (ApplySymbol, (NonAssoc, "apply"))
    ]


builtinStructOps :: LexicalItems StructSymbol
builtinStructOps = HM.fromList
    [ (CarrierSymbol, "carrier")
    ]

identifier :: Text -> (Holey Token, (Associativity, Marker))
identifier cmd = ([Just (Command cmd)], (NonAssoc, Marker cmd))


builtinRelationSymbols :: LexicalItems RelationSymbol
builtinRelationSymbols = HM.fromList
    [ (Symbol "=", "eq")
    , (Command "rless", "rless")
    , (Command "neq", "neq")
    , (Command "in", "elem")
    , (Command "notin", "notelem") -- Alternative to @\not\in@.
    ]

builtinPrefixPredicates :: LexicalItems PrefixPredicate
builtinPrefixPredicates = HM.fromList
    [ (PrefixPredicate "Cong" 4, "cong")
    , (PrefixPredicate "Betw" 3, "betw")
    ]


builtinConnectives :: [[(Holey Token, Associativity)]]
builtinConnectives =
    [ [binOp' (Command "iff") NonAssoc]
    , [binOp' (Command "implies") RightAssoc]
    , [binOp' (Command "lor") LeftAssoc]
    , [binOp' (Command "land") LeftAssoc]
    , [([Just (Command "lnot"), Nothing], NonAssoc)]
    ]


binOp :: Token -> Associativity -> Marker -> (Holey Token, (Associativity, Marker))
binOp tok assoc m = ([Nothing, Just tok, Nothing], (assoc, m))

binOp' :: Token -> Associativity -> (Holey Token, Associativity)
binOp' tok assoc = ([Nothing, Just tok, Nothing], assoc)

builtinAdjRs :: LexicalItems LexicalPhrase
builtinAdjRs = HM.fromList
    [ (unsafeReadPhrase "equal to ?", "eq")
    ]

builtinVerbs :: LexicalItems (SgPl LexicalPhrase)
builtinVerbs = HM.fromList
    [ (unsafeReadPhraseSgPl "equal[s/] ?", "eq")
    ]


-- Some of these do/should correspond to mathlib structures,
-- e.g.: lattice, complete lattice, ring, etc.
--
builtinNouns :: LexicalItems (SgPl LexicalPhrase)
builtinNouns = HM.mapKeys unsafeReadPhraseSgPl (HM.fromList
    -- Nullary
    [ ("set[/s]", "set")
    , ("point[/s]", "point")
    , ("element[/s] of ?", "elem")
    ])

_Onesorted :: SgPl LexicalPhrase
_Onesorted = unsafeReadPhraseSgPl "onesorted structure[/s]"

builtinStructNouns :: LexicalItems (SgPl LexicalPhrase)
builtinStructNouns = HM.singleton _Onesorted "onesorted_structure"


-- | Naïve splitting of lexical phrases to insert a variable slot for names in noun phrases,
-- as in /@there exists a linear form $h$ on $E$@/, where the underlying pattern is
-- /@linear form on ?@/. In this case we would get:
--
-- > splitOnVariableSlot (sg (unsafeReadPhraseSgPl "linear form[/s] on ?"))
-- > ==
-- > (unsafeReadPhrase "linear form", unsafeReadPhrase "on ?")
--
splitOnVariableSlot :: LexicalPhrase -> (LexicalPhrase, LexicalPhrase)
splitOnVariableSlot phrase = case prepositionIndices <> nonhyphenatedSlotIndices of
    [] -> (phrase, []) -- Place variable slot at the end.
    is -> List.splitAt (minimum is) phrase
    where
        prepositionIndices, slotIndices, nonhyphenatedSlotIndices :: [Int] -- Ascending.
        prepositionIndices = List.findIndices isPreposition phrase
        slotIndices = List.findIndices isNothing phrase
        nonhyphenatedSlotIndices = [i | i <- slotIndices, noHyphen (nth (i + 1) phrase)]

        isPreposition :: Maybe Token -> Bool
        isPreposition = \case
            Just (Word w) -> w `Set.member` prepositions
            _ -> False

        noHyphen :: Maybe (Maybe Token) -> Bool
        noHyphen = \case
            Just (Just (Word w)) -> Text.head w /= '-'
            -- If we arrive here, either the pattern is over (`Nothing`) or the next
            -- part of the pattern is not a word that starts with a hyphen.
            _ -> True


-- Preposition are a closed class, but this list is not yet exhaustive.
-- It can and should be extended when needed. The following list is a
-- selection of the prepositions found at
-- https://en.wikipedia.org/wiki/List_of_English_prepositions.
--
prepositions :: HashSet Text
prepositions = Set.fromList
    [ "about"
    , "above"
    , "across"
    , "after"
    , "against"
    , "along", "alongside"
    , "amid", "amidst"
    , "among"
    , "around"
    , "as"
    , "at"
    , "atop"
    , "before"
    , "behind"
    , "below"
    , "beneath"
    , "beside", "besides"
    , "between"
    , "beyond"
    , "but"
    , "by"
    , "except"
    , "for"
    , "from"
    , "in", "inside", "into"
    , "like"
    , "modulo", "mod"
    , "near"
    , "next"
    , "of"
    , "off"
    , "on"
    , "onto"
    , "opposite"
    , "out"
    , "over"
    , "past"
    , "per"
    , "sans"
    , "till"
    , "to"
    , "under"
    , "underneath"
    , "unlike"
    , "unto"
    , "up", "upon"
    , "versus"
    , "via"
    , "with"
    , "within"
    , "without"
    ]