/* license: GNU Lesser General Public License Version 2.1 or later */ /* ------------------------------------------------------------------------- */ % [count L X R] counts the occurrences of X in L func count list A, A -> int. count [] _ 0. count [A | TL] A R :- !, count TL A X, R is (X + 1). count [_ | TL] A R :- count TL A R. func expected-found A, A ->. expected-found Expected Found :- if (Expected = Found) true (halt "Assertion error" "\nExpected :" Expected "\nFound :" Found). func last-no-error list A -> A. last-no-error A B :- (std.last [] _ :- !, fail) => std.last A B. % [find L F R] returns the first R in L such that (F R) is valid func find list A, (pred i:A) -> A. find [R | _] F R :- F R, !. find [_ | L] F R :- find L F R. func find-opt list A, (pred i:A) -> (option A). find-opt [] _ none. find-opt [R | _] F (some R) :- F R, !. find-opt [_ | L] F R :- find-opt L F R. func args->str-list list argument -> list string. args->str-list L Res :- std.map L (x\r\ str r = x) Res. func or bool, bool -> bool. or ff ff ff :- !. or _ _ tt. func neg bool -> bool. neg tt ff. neg ff tt. func fail->bool prop -> bool. fail->bool P ff :- P, !. fail->bool _ tt. func sep. sep :- coq.say "---------------------------------". func do (list (func)) ->. do []. do [P|PS] :- P, do PS. func if-true (pred), (func) ->. if-true A B :- if A B true. func if-false (pred), (func) ->. if-false A B :- if A true B. func std.findall-unary (pred o:A) -> list A. std.findall-unary P L :- std.findall (P _) V, std.map V (x\y\ P y = x) L. func print-repeat-aux int, string -> string. print-repeat-aux 0 _ S :- !, coq.say S. print-repeat-aux N A S :- N > 0, N1 is N - 1, S' is A ^ S, print-repeat-aux N1 A S'. func print-repeat int, string ->. print-repeat I S :- print-repeat-aux I S "". func split-at-not-fatal int, list A -> list A, list A. split-at-not-fatal 0 L [] L :- !. split-at-not-fatal N [X|XS] [X|LN] LM :- !, N1 is N - 1, split-at-not-fatal N1 XS LN LM. func undup-same list A -> list A. undup-same [] []. undup-same [X|Xs] [X|Ys] :- std.forall Xs (x\ not (x == X)), !, undup-same Xs Ys. undup-same [_|Xs] Ys :- undup-same Xs Ys. :index (1) func is-coq-term any ->. is-coq-term (sort _). is-coq-term (global _). is-coq-term (pglobal _ _). is-coq-term (app _). is-coq-term (fun _ _ _). is-coq-term (prod _ _ _ ). is-coq-term (fix _ _ _ _ ). is-coq-term (match _ _ _). is-coq-term (let _ _ _ _). is-coq-term (primitive _). kind nat type. type z nat. type s nat -> nat. type inf nat. :index (1) func add-nat nat, nat -> nat. add-nat inf _ inf :- !. add-nat _ inf inf :- !. add-nat z N N. add-nat (s N) M (s P) :- add-nat N M P. func max-nat nat, nat -> nat. max-nat z N N :- !. max-nat N z N :- !. max-nat inf _ inf :- !. max-nat _ inf inf :- !. max-nat (s N) (s M) (s P) :- max-nat N M P. func min-nat nat, nat -> nat. min-nat z _ z :- !. min-nat _ z z :- !. min-nat inf A A :- !. min-nat A inf A :- !. min-nat (s N) (s M) (s P) :- min-nat N M P. func length-nat list A -> nat. length-nat [] z. length-nat [_|L] (s N) :- length-nat L N. func count-prod term -> nat. count-prod (prod _ _ B) (s N) :- !, pi x\ count-prod (B x) N. count-prod _ z. func close-prop (A -> list prop) -> list prop. close-prop (x\ []) [] :- !. close-prop (x\ [X | Xs x]) [X| Xs'] :- !, close-prop Xs Xs'. close-prop (x\ [X x | Xs x]) [pi x\ X x | Xs'] :- !, close-prop Xs Xs'. func close-prop-no-prune (A -> list prop) -> list prop. close-prop-no-prune (x\ []) [] :- !. close-prop-no-prune (x\ [X x | Xs x]) [pi x\ X x | Xs'] :- !, close-prop-no-prune Xs Xs'. % [close-term-ty (x\ L) Ty R] Ty is the type of x func close-term-ty (term -> list prop), term -> list prop. close-term-ty (x\ []) _ [] :- !. close-term-ty (x\ [X | Xs x]) Ty [X| Xs'] :- !, close-term-ty Xs Ty Xs'. close-term-ty (x\ [X x | Xs x]) Ty [@pi-decl `x` Ty x\ X x | Xs'] :- !, close-term-ty Xs Ty Xs'. func close-term-no-prune-ty (term -> list prop), term -> list prop. close-term-no-prune-ty (x\ []) _ [] :- !. close-term-no-prune-ty (x\ [X x | Xs x]) Ty [@pi-decl `x` Ty x\ X x | Xs'] :- !, close-term-no-prune-ty Xs Ty Xs'.