일반적으로 SO에 대해 질문 할 때 시도를 공유하는 것이 좋습니다. 사람들은 일반적으로 문제에 대한 완벽한 해결책을 제공하지 않습니다. 특정 질문이 있거나 문제에 접근하는 방법을 알았을 때 대답합니다.
일반적인 접근법에 대한 몇 가지 힌트를 공유 하겠지만 은 완전한 해결책이 아닙니다입니다. 먼저 AST를 어떤 식 으로든 표현해야합니다. F #에서는 차별화 된 노동 조합을 사용하여이를 수행 할 수 있습니다.
type Expr =
| Function of string * Expr list
| Variable of string
| Value of int
통일 (변수 명
string
로 표현
Expr
할당) 타입
(Expr * Expr) list
통일적 할 표현의리스트를 취하고 변수 할당을 반환하는 함수이다
:
다음은 변수 값 함수 애플리케이션을 지원
let rec unify exprs =
match exprs with
// Unify two variables - if they are equal, we continue unifying
// the rest of the constraints, otherwise the algorithm fails
| (Variable s1, Variable s2)::remaining ->
if s1 = s2 then unify remaining
else failwith "cannot unify variables"
// Unify variable with some other expression - unify the remaining
// constraints and add new assignment to the result
| (Variable s, expr)::remaining
| (expr, Variable s)::remaining ->
let rest = unify remaining
// (s, expr) is a tuple meaning that we assign 'expr' to variable 's'
(s, expr)::rest
// TODO: Handle remaining cases here!
| _ -> failwith "cannot unify..."
몇 가지 경우를 추가해야합니다. 가장 중요한 것은, Function
와 Function
을 통합하면 ...을 remaining
목록에 새 제약 모든 인수 식을 추가 한 후 (그렇지 않으면 실패) 함수 이름이 동일한 지 확인해야한다는 것을 의미
귀하의 시도를 공유하는 데 관심이 있으십니까? –
거친 일 이겠지만 [F # source] (https://github.com/fsharp/fsharp)를 확인 했습니까? 또는 [참조한 기사] (http://dl.acm.org/citation.cfm?id=357169)를 구입할 수 있습니다. – Daniel
@ 대니얼 무료 온라인 (예 : http://www.nsl.com/misc/papers/martelli-montanari.pdf)에서 사용할 수 있으므로 ACM에서 기사를 구매할 필요가 없을 것입니다. 그러나 AFAIK, 표준 통합 알고리즘이며 WikiPedia에 잘 설명되어 있습니다. http://en.wikipedia.org/wiki/Unification_algorithm –