Skip to content

Instantly share code, notes, and snippets.

@loguntsov
Created April 6, 2015 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loguntsov/da467cc9ad5da9f32ff7 to your computer and use it in GitHub Desktop.
Save loguntsov/da467cc9ad5da9f32ff7 to your computer and use it in GitHub Desktop.
module(simplification).
-compile([export_all]).
zeroA({add,E,{num,0}}) -> E;
zeroA({add,{num,0},E}) -> E;
zeroA(E) -> E.
mulO({mul,E,{num,1}}) -> E;
mulO({mul,{num,1},E}) -> E;
mulO(E) -> E.
mulZ({mul,_,{num,0}}) -> {num,0};
mulZ({mul,{num,0},_}) -> {num,0};
mulZ(E) -> E.
compose([])-> fun (E) -> E end;
compose([Rule|Rules]) ->
fun (E) -> (compose(Rules))(Rule(E)) end.
rules() -> [ fun zeroA/1, fun mulO/1, fun mulZ/1].
simp(F,{add,E1,E2}) ->
F({add,simp(F,E1),simp(F,E2)});
simp(F,{mul,E1,E2}) ->
F({mul,simp(F,E1),simp(F,E2)});
simp(_F,E) -> E.
simplify(E) -> simp(compose(rules()),E).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment