 
 	public func apply(type: Type) -> Type {
 		return type.analysis(
-			ifVariable: { self.elements[$0] ?? type },
+			ifVariable: { variable in find(self.elements, { v, _ in v == variable }).map { self.elements[$0].1 } ?? type },
 			ifConstructed: {
 				$0.analysis(
 					ifUnit: type,

//  Copyright (c) 2015 Rob Rix. All rights reserved.

public struct Substitution: DictionaryLiteralConvertible, Equatable, Printable {
	public init<S: SequenceType where S.Generator.Element == Dictionary<Variable, Type>.Element>(_ sequence: S) {
		self.elements = [:] + sequence
	}


	public func compose(other: Substitution) -> Substitution {
		return Substitution(other.elements + elements)
	}

	public var variables: Set<Variable> {
		return Set(elements.keys)
	}

	public var occurringVariables: Set<Variable> {
		let replacementVariables = reduce(lazy(elements.values).map { $0.freeVariables }, Set(), uncurry(Set.union))
		return variables.intersect(replacementVariables)
	}



	// MARK: Private

	private let elements: [(Variable, Type)]
}


