Created
August 18, 2015 11:18
-
-
Save yuki-takeichi/65e37360aa02241b21c3 to your computer and use it in GitHub Desktop.
Prologで自然数を再帰的に定義する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nat(0). | |
nat(s(X)) :- nat(X). | |
/* even(x) odd(X) */ | |
plus(0, Y, Y). | |
plus(s(X), Y, s(Z)) :- plus(X, Y, Z). | |
le(X, Y) :- plus(X, _, Y). | |
lt(0, S(X)). | |
lt(X, s(Y)) :- le(X, Y). /* hmm */ | |
times(0, _, 0). | |
times(s(X), Y, Z) :- times(X, Y, Z1), plus(Z1, Y, Z). | |
quot(X, Y, 0, X) :- lt(X, Y). | |
quot(X, Y, s(Q), R) :- plus(Y, X1, X), quot(X1, Y, Q, R). | |
prime(s(X)) :- df(X, s(X)). | |
df(s(0), _). | |
df(s(s(M)), N) :- dnd(s(s(M)), N), df(s(M), N). | |
dnd(M, N) :- quot(N, M, _, s(_)). | |
hoge(0, 0). | |
hoge(I, s(N)) :- I > 0, J is I - 1, hoge(J, N). | |
/* hoge(I, s(N)) :- hoge(I - 1, N). */ | |
/* ? :- hoge(3, 0, X). | |
* X = s(s(s(0))). | |
* ? :- foo(X, s(s(s(0)))). | |
* X = 3. | |
*/ | |
foo(0, N). | |
foo(I, s(N)) :- foo(I + 1, N). | |
/* Rangeで指定した範囲の整数をinstance化する述語が欲しい */ | |
/* (universe of discourseを定めたい) */ | |
/* (それって全件網羅的に探索するのと変わらない?) */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://bach.istc.kobe-u.ac.jp/prolog/intro/nat.html
を参考にした。