Created
December 31, 2023 20:37
-
-
Save roboguy13/273378baec460cf707ceb9fcd790e339 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
{-# LANGUAGE ImpredicativeTypes #-} | |
{-# LANGUAGE RankNTypes #-} | |
import Control.Lens | |
import Control.Applicative | |
data Expr | |
= Lit Int | |
| Add Expr Expr | |
| Sub Expr Expr | |
deriving (Show) | |
makePrisms ''Expr | |
replaceLits :: Expr -> Expr | |
replaceLits (Lit n) = Lit 0 | |
replaceLits e = | |
e & ((_Add `failing` _Sub).both %~ replaceLits) | |
data Expr' | |
= Lit' Int | |
| Add' Expr' Expr' | |
| Sub' Expr' Expr' | |
| If' Expr' Expr' Expr' | |
deriving (Show) | |
makePrisms ''Expr' | |
replaceLeftmostLit :: Expr' -> Expr' | |
replaceLeftmostLit (Lit' n) = Lit' 0 | |
replaceLeftmostLit e = | |
e & ((_Add'._1 `failing` _Sub'._1 `failing` _If'._1) %~ replaceLeftmostLit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment