How to add fields that only cache something to ADT?
Often I'm in the need of adding fields to an ADT that only memoize some redundant information. But I haven't figured out completely how to do it nicely and efficiently. The best way to show the problem is to make an example. Suppose we're working with untyped lambda terms: type VSym = String data Lambda = Var VSym | App Lambda Lambda | Abs VSym Lambda And from time to time we need to compute the set of free variables of a term: fv :: Lambda -> Set VSym fv (Var v) = Set.singleton v fv (App s t) = (fv s) `Set.union` (fv t) fv (Abs v t) = v `Set.delete` (fv t) Soon we realize that repeated