kowainik / kowainik/stan

AST patterns for node equality

Open
#265 0 comments 0 reactions 0 assignees View on GitHub
analysis :alembic: hie
Dominant language
Haskell
Stars
597
Forks
53
PR merge metrics
No merged PRs in 30d

Description

Often it's required to compare different parts of AST on equality to provide various inspections. For this, we need to provide new constructors for `PatternAst` and be able to compare patterns. For example, it would be desirable to specify a pattern for the expression:

```haskell
a && a
```

Which works on the following cases:

```haskell
True && True
x && x
not y && not y
foo bar Nothing baz && foo bar Nothing baz
```

Not sure, how to implement this, but a few ideas:

- [ ] Introduce the `PatternAstLabel Text` constructor where you can specify a tag for expression. So the above expression can be written as a pattern `op (label "a") andOp (label "a")`.
- [ ] Change the implementation of `hieMatchPatternAst` in such a way, that it works with `Map Text (HieAST TypeIndex)`. The idea is that when trying to match `PatternAstLabel` there could be 2 cases:
1. Such a label is not in `Map` yet: insert it into `Map`.
2. Such a label is already in `Map`: `lookup` in `Map` and compare current with what is there.
- [x] We need to figure out how to compare two `Node`s of `HieAST`. We can't use `==` because it will compare source positions, but we are not interested in them.

This is just an initial idea, but I think it will allow us to specify more custom rules and capture more patterns.

**UPDATE:** HIE AST comparison was implemented in #269

Contributor guide

Open the contributing guide

Research direction

Start by reading the PatternAst design and the hieMatchPatternAst implementation described in the issue, then inspect the HIE AST comparison work referenced in #269. The intended result is a way to label a pattern and compare repeated AST nodes while ignoring source positions, but the remaining scope is not specified.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.