| 1 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 2 |
/* ------------------------------------------------------------------------- */ |
| 3 |
|
| 4 |
% [count L X R] counts the occurrences of X in L |
| 5 |
func count list A, A -> int. |
| 6 |
count [] _ 0. |
| 7 |
count [A | TL] A R :- !, count TL A X, R is (X + 1). |
| 8 |
count [_ | TL] A R :- count TL A R. |
| 9 |
|
| 10 |
func expected-found A, A ->. |
| 11 |
expected-found Expected Found :- |
| 12 |
if (Expected = Found) true |
| 13 |
(halt "Assertion error" |
| 14 |
"\nExpected :" Expected |
| 15 |
"\nFound :" Found). |
| 16 |
|
| 17 |
func last-no-error list A -> A. |
| 18 |
last-no-error A B :- |
| 19 |
(std.last [] _ :- !, fail) => std.last A B. |
| 20 |
|
| 21 |
% [find L F R] returns the first R in L such that (F R) is valid |
| 22 |
func find list A, (pred i:A) -> A. |
| 23 |
find [R | _] F R :- F R, !. |
| 24 |
find [_ | L] F R :- find L F R. |
| 25 |
|
| 26 |
func find-opt list A, (pred i:A) -> (option A). |
| 27 |
find-opt [] _ none. |
| 28 |
find-opt [R | _] F (some R) :- F R, !. |
| 29 |
find-opt [_ | L] F R :- find-opt L F R. |
| 30 |
|
| 31 |
func args->str-list list argument -> list string. |
| 32 |
args->str-list L Res :- |
| 33 |
std.map L (x\r\ str r = x) Res. |
| 34 |
|
| 35 |
func or bool, bool -> bool. |
| 36 |
or ff ff ff :- !. |
| 37 |
or _ _ tt. |
| 38 |
|
| 39 |
func neg bool -> bool. |
| 40 |
neg tt ff. |
| 41 |
neg ff tt. |
| 42 |
|
| 43 |
func fail->bool prop -> bool. |
| 44 |
fail->bool P ff :- P, !. |
| 45 |
fail->bool _ tt. |
| 46 |
|
| 47 |
func sep. |
| 48 |
sep :- coq.say "---------------------------------". |
| 49 |
|
| 50 |
func do (list (func)) ->. |
| 51 |
do []. |
| 52 |
do [P|PS] :- P, do PS. |
| 53 |
|
| 54 |
func if-true (pred), (func) ->. |
| 55 |
if-true A B :- if A B true. |
| 56 |
|
| 57 |
func if-false (pred), (func) ->. |
| 58 |
if-false A B :- if A true B. |
| 59 |
|
| 60 |
func std.findall-unary (pred o:A) -> list A. |
| 61 |
std.findall-unary P L :- |
| 62 |
std.findall (P _) V, |
| 63 |
std.map V (x\y\ P y = x) L. |
| 64 |
|
| 65 |
func print-repeat-aux int, string -> string. |
| 66 |
print-repeat-aux 0 _ S :- !, coq.say S. |
| 67 |
print-repeat-aux N A S :- N > 0, N1 is N - 1, |
| 68 |
S' is A ^ S, print-repeat-aux N1 A S'. |
| 69 |
|
| 70 |
func print-repeat int, string ->. |
| 71 |
print-repeat I S :- print-repeat-aux I S "". |
| 72 |
|
| 73 |
func split-at-not-fatal int, list A -> list A, list A. |
| 74 |
split-at-not-fatal 0 L [] L :- !. |
| 75 |
split-at-not-fatal N [X|XS] [X|LN] LM :- !, N1 is N - 1, |
| 76 |
split-at-not-fatal N1 XS LN LM. |
| 77 |
|
| 78 |
func undup-same list A -> list A. |
| 79 |
undup-same [] []. |
| 80 |
undup-same [X|Xs] [X|Ys] :- |
| 81 |
std.forall Xs (x\ not (x == X)), !, |
| 82 |
undup-same Xs Ys. |
| 83 |
undup-same [_|Xs] Ys :- undup-same Xs Ys. |
| 84 |
|
| 85 |
:index (1) |
| 86 |
func is-coq-term any ->. |
| 87 |
is-coq-term (sort _). |
| 88 |
is-coq-term (global _). |
| 89 |
is-coq-term (pglobal _ _). |
| 90 |
is-coq-term (app _). |
| 91 |
is-coq-term (fun _ _ _). |
| 92 |
is-coq-term (prod _ _ _ ). |
| 93 |
is-coq-term (fix _ _ _ _ ). |
| 94 |
is-coq-term (match _ _ _). |
| 95 |
is-coq-term (let _ _ _ _). |
| 96 |
is-coq-term (primitive _). |
| 97 |
|
| 98 |
kind nat type. |
| 99 |
type z nat. |
| 100 |
type s nat -> nat. |
| 101 |
type inf nat. |
| 102 |
|
| 103 |
:index (1) |
| 104 |
func add-nat nat, nat -> nat. |
| 105 |
add-nat inf _ inf :- !. |
| 106 |
add-nat _ inf inf :- !. |
| 107 |
add-nat z N N. |
| 108 |
add-nat (s N) M (s P) :- add-nat N M P. |
| 109 |
|
| 110 |
func max-nat nat, nat -> nat. |
| 111 |
max-nat z N N :- !. |
| 112 |
max-nat N z N :- !. |
| 113 |
max-nat inf _ inf :- !. |
| 114 |
max-nat _ inf inf :- !. |
| 115 |
max-nat (s N) (s M) (s P) :- max-nat N M P. |
| 116 |
|
| 117 |
func min-nat nat, nat -> nat. |
| 118 |
min-nat z _ z :- !. |
| 119 |
min-nat _ z z :- !. |
| 120 |
min-nat inf A A :- !. |
| 121 |
min-nat A inf A :- !. |
| 122 |
min-nat (s N) (s M) (s P) :- min-nat N M P. |
| 123 |
|
| 124 |
func length-nat list A -> nat. |
| 125 |
length-nat [] z. |
| 126 |
length-nat [_|L] (s N) :- length-nat L N. |
| 127 |
|
| 128 |
func count-prod term -> nat. |
| 129 |
count-prod (prod _ _ B) (s N) :- !, pi x\ count-prod (B x) N. |
| 130 |
count-prod _ z. |
| 131 |
|
| 132 |
func close-prop (A -> list prop) -> list prop. |
| 133 |
close-prop (x\ []) [] :- !. |
| 134 |
close-prop (x\ [X | Xs x]) [X| Xs'] :- !, close-prop Xs Xs'. |
| 135 |
close-prop (x\ [X x | Xs x]) [pi x\ X x | Xs'] :- !, close-prop Xs Xs'. |
| 136 |
|
| 137 |
func close-prop-no-prune (A -> list prop) -> list prop. |
| 138 |
close-prop-no-prune (x\ []) [] :- !. |
| 139 |
close-prop-no-prune (x\ [X x | Xs x]) [pi x\ X x | Xs'] :- !, |
| 140 |
close-prop-no-prune Xs Xs'. |
| 141 |
|
| 142 |
% [close-term-ty (x\ L) Ty R] Ty is the type of x |
| 143 |
func close-term-ty (term -> list prop), term -> list prop. |
| 144 |
close-term-ty (x\ []) _ [] :- !. |
| 145 |
close-term-ty (x\ [X | Xs x]) Ty [X| Xs'] :- !, close-term-ty Xs Ty Xs'. |
| 146 |
close-term-ty (x\ [X x | Xs x]) Ty [@pi-decl `x` Ty x\ X x | Xs'] :- !, |
| 147 |
close-term-ty Xs Ty Xs'. |
| 148 |
|
| 149 |
func close-term-no-prune-ty (term -> list prop), term -> list prop. |
| 150 |
close-term-no-prune-ty (x\ []) _ [] :- !. |
| 151 |
close-term-no-prune-ty (x\ [X x | Xs x]) Ty [@pi-decl `x` Ty x\ X x | Xs'] :- !, |
| 152 |
close-term-no-prune-ty Xs Ty Xs'. |