Skip to content

Incorporating negative integers into patterns - #43

Merged
hshkk merged 2 commits into
mainfrom
negation-patterns
Nov 24, 2026
Merged

hshkk merged 2 commits into
mainfrom
negation-patterns

Conversation

@hshkk

@hshkk hshkk commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

This pull request intends to close Issue #42. For a description of the issue, please refer to that link.

Some tests were added to verify that the issue is truly gone. Now, the parser (assuming at least L B) accepts definitions like the following.

fun :: [Int] -> [Int]
fun (-1:_) = [-1]
fun xs     = []

The resulting abstract syntax tree is as follows. Note the lack of a LHsPNeg constructor like I implied in Issue #42, since we actually don't want something that general. For instance, (- x) is not a valid pattern in Haskell.

fromList 
    [ LHsSig "fun" 
          (LHsTyFn 
              (LHsTyList (LHsTyCon "Int" (fromList []))) 
              (LHsTyList (LHsTyCon "Int" (fromList []))))
    , LHsEqn "fun" 
          (fromList [LHsPListCons (LHsPInt (-1)) LHsPWild]) 
          (LHsList (fromList [LHsNeg (LHsInt 1)]))
    , LHsEqn "fun" 
          (fromList [LHsPVar "xs"]) 
          (LHsList (fromList []))
    ]

@hshkk hshkk added the implementation Implementation of a new feature label Nov 21, 2026
@hshkk hshkk linked an issue Nov 21, 2026 that may be closed by this pull request
@hshkk
hshkk requested a review from RacsO-A November 22, 2026 23:15
Comment on lines +179 to +180
(Just ("Unexpected '" <> bad <> "'"))
(underline file (pos +> Text.length bad) "not sure how to interpret this symbol")

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message for lexical errors was slightly revised.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The placement of these errors might be revised once we implement errors more properly.

Comment on lines +112 to +114
PatCon :: { L LHsPat }
: '-' int { pneg $1 $2 }

@hshkk hshkk Nov 24, 2026 •

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main addition is a PatCon nonterminal that is subordinate to Pat, but greater in precedence over PatAtom. PatCon contains pattern constructors (added by D) and negation patterns (added by B, as seen here).

@hshkk
hshkk requested review from leoSallos and removed request for RacsO-A November 24, 2026 04:07
@hshkk
hshkk merged commit 529dd4b into main Nov 24, 2026
2 checks passed
@hshkk
hshkk deleted the negation-patterns branch November 24, 2026 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

implementation Implementation of a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to parse negative integers in patterns

2 participants