Skip to content

Instantly share code, notes, and snippets.

@roboguy13
Created December 31, 2023 20:37
Show Gist options
  • Save roboguy13/273378baec460cf707ceb9fcd790e339 to your computer and use it in GitHub Desktop.
Save roboguy13/273378baec460cf707ceb9fcd790e339 to your computer and use it in GitHub Desktop.
{-# 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