let rec map f a =
  match a with
    | App(Fun(Apply(r)), x :: xl) ->
        let x' = map f x in
        let xl' = mapl (map f) xl in
          if x == x' && xl == xl' then a else
            mk_apply mk_app r x' xl'
    | App(Fun(Abs), [x]) ->
        let x' = map f x in
          if x == x' then a else 
            mk_abs x'
    | _ ->
        f a