# Mathematica command for type checking?

I have a program where users can enter mathematica commands that can evaluate to any of the standard numeric types (complex, real, int, etc.), or to boolean values. Moreover the users can enter functions that depend on variables. (For the sake of this post let's assume the variables are all of type complex.)

I want to know if there's a mathematica command that can type-check an arbitrary string. Specifically, I'd want something like

ReturnsComplex[string, assumptions]


that would return true for "1/x + 7x^2 + Sin[x]" but false for "E^x == 5", say. Does anyone know of such a command, or a way to implement one without a huge quantity of work? (Assume that I can identify all the variables in the string and substitute in if necessary.)

-
You may be interested in this question: Automatically check student answers in tests –  Jens Apr 27 '13 at 17:51

This is a simple approach that requires you to include the assumptions about all the identified variables in the second argument (I assume that's what you want to do):

returnsComplex[string_, assumptions_] := TrueQ[
Simplify[(ToExpression[string]) \[Element] Complexes, assumptions]]

returnsComplex["1/x+7x^2+Sin[x]", x \[Element] Complexes]

(* ==> True *)

returnsComplex["E^x\[Equal]5", x \[Element] Complexes]

(* ==> False *)

-
Wow, I feel stupid. Thanks, that's perfect. –  Twiffy Apr 27 '13 at 18:51