Source

Opifex / src / Transformation / CWCPS / CWCPS_EtaReduction.ml

(*
 * Opifex
 *
 * Copyrights(C) 2012 by Pawel Wieczorek <wieczyk at gmail>
 *)

open CWCPS_AST
open CWCPS_Transformation
open Batteries

(*************************************************************************************************
 * Unused definitions
 ************************************************************************************************)

module EtaReduction = struct

    let transform_definition sb (deflabel, defname, defargs, inexpr) =
        let expected_args = List.map (fun v -> VEXPR_Variable v) defargs in
        match inexpr with
            | EXPR_App (_, (VEXPR_Variable fname as fname'), actual_args)
              when expected_args = actual_args ->
                (defname, fname') :: sb

            | _ -> 
                sb

    let transformation = function
        | EXPR_Fix (reclabel, definitions, in_expr) ->
            let sb = List.fold_left transform_definition [] definitions in
            Transformation.Substitute sb

        | _ ->
            Transformation.NoChange

end

(*************************************************************************************************
 * 
 ************************************************************************************************)

let topdown_transformations = 
    [ EtaReduction.transformation
    ]

let topdown =
    Transformation.topdowns
        topdown_transformations