Skip to content

Instantly share code, notes, and snippets.

@jackmott
Created December 17, 2018 20:28
Show Gist options
  • Save jackmott/df2b5a1d73c77062eb69f1faa63544ca to your computer and use it in GitHub Desktop.
Save jackmott/df2b5a1d73c77062eb69f1faa63544ca to your computer and use it in GitHub Desktop.
ConstantFolding
let rec ConstantFolding node =
match node with
| Add (l,r) -> match (ConstantFolding(l),ConstantFolding(r)) with
| (Constant v1,Constant v2) -> Constant(v1+v2)
| (optL,optR) -> Add(optL,optR)
| Sub(l,r) -> match (ConstantFolding(l),ConstantFolding(r)) with
| (Constant v1,Constant v2) -> Constant(v1-v2)
| (optL,optR) -> Sub(optL,optR)
| X -> X
| Constant v -> Constant(v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment