Skip to content

Add CSE#2716

Open
nikita-volkov wants to merge 4 commits into
dhall-lang:mainfrom
nikita-volkov:cse
Open

Add CSE#2716
nikita-volkov wants to merge 4 commits into
dhall-lang:mainfrom
nikita-volkov:cse

Conversation

@nikita-volkov
Copy link
Copy Markdown
Collaborator

Addresses #2715

(LLM-generated and edited by me).

Comment on lines +53 to +61
cse :: forall a. Ord a => Expr Void a -> Expr Void a
cse expr = go freshNames candidates expr
where
-- Count occurrences of each subexpression using cosmosOf to traverse all levels
counts :: Map (Expr Void a) Int
counts = Map.fromListWith (+)
[ (sub, 1)
| sub <- Lens.foldMapOf (Lens.cosmosOf subExpressions) (:[]) expr
]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this will not correctly handle the following case:

let x  = 1

let y = nonTrivialExpressionOf x

let x = 2

in  nonTrivialExpressionOf x

If I understand correctly, the logic would count both occurrences of nonTrivialExpressionOf x as the same sub-expression even though x would differ in both cases.

My intuition here is that this would require explicit recursion over the syntax tree to correctly handle this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It actually won't because such issues are sidestepped by excluding expressions containing variables from candidates. See 1 and 2.

I've added docs and a test to highlight this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants