논리, 제어 및 예외

논리, 제어 및 예외

실행 흐름을 제어하고 예외를 다루는 술어들을 설명해요.

출처: GNU Prolog Manual - Logic, control and exceptions

본문

abort/0, stop/0, top_level/0, break/0, halt/1, halt/0

Templates

abort
stop
top_level
break
halt(+integer)
halt

Description

  • abort — 현재 실행을 중단. 이 실행이 최상위(top-level) 아래에서 시작됐으면 제어가 최상위로 돌아가고 {execution aborted} 메시지가 표시됨. 그렇지 않으면, 예를 들어 initialization/1 지시자(7.1.14절)로 시작된 실행이면, abort/0halt(1)과 동일(아래 참조).
  • stop — 현재 실행을 중지. 이 실행이 최상위 아래에서 시작됐으면 제어가 최상위로 돌아감. 그렇지 않으면 stop/0halt(0)과 동일(아래 참조).
  • top_level — 새 재귀 최상위(배너 표시 포함)를 시작. 이 새 최상위를 끝내려면 파일 끝 키 시퀀스(Ctl-D)나 그 항 표현인 end_of_file.을 입력하면 됨.
  • break — 재귀 최상위를 호출(배너는 표시되지 않음). 이 새 레벨을 끝내려면 파일 끝 키 시퀀스(Ctl-D)나 그 항 표현인 end_of_file.을 입력하면 됨.
  • halt(Status) — GNU Prolog 프로세스가 반환 코드 Status로 즉시 셸로 종료하게 함.
  • halthalt(0)과 동일.

Errors

  • Status가 변수 — instantiation_error
  • Status가 변수도 정수도 아님 — type_error(integer, Status)

Portability: halt/1halt/0은 ISO 술어. abort/0, stop/0, top_level/0, break/0은 GNU Prolog 술어.

false/0, once/1, (+)/1 - not provable, call/2-11, call_with_args/1-11, call_det/2, forall/2

Templates

false
once(+callable_term)
\+(+callable_term)
call(+callable_term, +term,…, +term)
call_with_args(+atom, +term,…, +term)
call_det(+callable_term, ?boolean)
forall(+callable_term, +callable_term)

Description

  • false — 항상 실패하고 백트래킹을 강제. fail/0 제어 구조(7.2.1절)와 동일.
  • once(Goal)call(Goal)이 성공하면 성공. 그러나 Goal의 모든 대안이 컷되므로 once/1은 백트래킹 시 재실행되지 않음. once(Goal)call(Goal), !과 동일.
  • \+ Goalcall(Goal)이 실패하면 성공하고 그 외에는 실패. 이 내장 술어는 실패에 의한 부정(negation by failure)을 제공.
  • call(Closure, Arg1,…, ArgN) — Closure의 인자들(있으면)에 Arg1,…, ArgN(1 ≤ N ≤ 10) 추가 인자를 붙여 만든 Goal에 대해 call(Goal)을 호출.
  • call_with_args(Functor, Arg1,…, ArgN) — functor가 Functor이고 인자가 Arg1,…, ArgN(0 ≤ N ≤ 10)인 목표를 호출.
  • call_det(Goal, Deterministic)call(Goal)이 성공하면 성공하고, Goal이 선택점(choice-point)을 만들지 않았으면 Deterministic을 true와, 그 외에는 false와 통일.
  • forall(Condition, Action) — Condition의 모든 대안 바인딩에 대해 Action이 증명될 수 있으면 성공. \+(Condition, \+ Action)과 동일.

\+는 미리 정의된 접두 연산자예요(8.14.10절).

Errors

  • Goal(또는 Condition 또는 Action)이 변수 — instantiation_error
  • Goal(또는 Condition 또는 Action)이 변수도 callable 항도 아님 — type_error(callable, Goal)
  • Goal의 술어 지시자 Pred가 기존 프로시저에 대응하지 않고 unknown Prolog 플래그 값이 error(8.22.1절) — existence_error(procedure, Pred)
  • Functor가 변수 — instantiation_error
  • Functor가 변수도 원자도 아님 — type_error(atom, Functor)
  • Deterministic이 변수도 boolean도 아님 — type_error(boolean, Deterministic)
  • call/2-11의 경우 Goal의 결과 항수(Closure의 항수 + N)가 max_arity 플래그(8.22.1절)보다 큰 정수 — representation_error(max_arity)

Portability: false/0, call/2-8, once/1, (\+)/1은 ISO 술어. call/9-11, call_with_args/1-11, call_det/2, forall/2는 GNU Prolog 술어.

repeat/0

Templates

repeat

Description repeat는 무한한 백트래킹 선택의 시퀀스를 생성해요. 목적은 어떤 테스트가 참이 될 때까지 어떤 방식으로 생성된 요소(예: 스트림에서 읽어)에 대해 어떤 동작을 반복적으로 수행하는 것이에요. repeat 루프는 프로그램의 논리에 기여할 수 없어요. 동작이 부작용을 포함할 때만 의미가 있어요. repeat 루프를 더 자연스러운 꼬리 재귀 공식 대신 사용하는 유일한 이유는 효율성이에요: 테스트가 실패해 돌아갈 때 Prolog 엔진이 repeat/0 호출 이후 소비된 작업 저장소를 즉시 재확보하기 때문이에요.

Errors: 없음.

Portability: ISO 술어.

between/3, for/3

Templates

between(+integer, +integer, ?integer)
for(?integer, +integer, +integer)

Description between(Lower, Upper, Counter)는 Counter를 Lower, Lower+1,…, Upper 값으로 인스턴스화하는 백트래킹 선택의 시퀀스를 생성해요.

for(Counter, Lower, Upper)between(Lower, Upper, Counter)와 동일. 이 술어는 구식이며 새 코드는 between/3을 사용해야 해요.

Errors

  • Counter가 변수도 정수도 아님 — type_error(integer, Counter)
  • Lower가 변수 — instantiation_error
  • Lower가 변수도 정수도 아님 — type_error(integer, Lower)
  • Upper가 변수 — instantiation_error
  • Upper가 변수도 정수도 아님 — type_error(integer, Upper)

Portability: GNU Prolog 술어.

더 알아보기