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

public func constraints(graph: Graph<Node>) -> (Term, constraints: ConstraintSet) {
	let parameters = Node.parameters(graph)
	let returns = Node.returns(graph)
	let returnType: Term = reduce(returns, nil) { $0.0.map { .product(Term(), $0) } ?? Term() } ?? .Unit
	return (simplify(reduce(parameters, returnType) {
		.function(Term(), $0.0)
	}), [])
}


private func simplify(type: Term) -> Term {
	return Substitution(
		(type.freeVariables
			|>	sorted
			|>	reverse
			|>	enumerate
			|>	lazy)
			.map {
				($1, Term(integerLiteral: $0))
			}).apply(type)
}


import Manifold
import Prelude

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

public func constraints(graph: Graph<Node>) -> (Term, constraints: ConstraintSet) {
	let parameters = Node.parameters(graph)
	let returns = Node.returns(graph)
	let returnType: Term = reduce(returns, nil) { $0.0.map { .product(Term(), $0) } ?? Term() } ?? .Unit
	return (simplify(reduce(parameters, returnType) {
		.function(Term(), $0.0)
	}), [])
}


private func simplify(type: Term) -> Term {
	return Substitution(
		(type.freeVariables
			|>	(flip(sorted) <| { $0.value < $1.value })
			|>	reverse
			|>	enumerate
			|>	lazy)
			.map {
				($1, Term(integerLiteral: $0))
			}).apply(type)
}


import Manifold
import Prelude

--- 
+++ 
@@ -13,7 +13,7 @@
 private func simplify(type: Term) -> Term {
 	return Substitution(
 		(type.freeVariables
-			|>	(flip(sorted) <| { $0.value < $1.value })
+			|>	sorted
 			|>	reverse
 			|>	enumerate
 			|>	lazy)
Use Comparable to sort the free variables.
