| 1 |
namespace nes { |
| 2 |
|
| 3 |
func ns->modpath prop -> modpath. |
| 4 |
ns->modpath (ns _ M) M. |
| 5 |
|
| 6 |
func open-ns->string prop -> string. |
| 7 |
open-ns->string (open-ns S _) S. |
| 8 |
|
| 9 |
func begin-ns string, list string, list prop -> list prop. |
| 10 |
begin-ns NS Path In Out :- |
| 11 |
if (Path = []) |
| 12 |
(Fresh is NS ^ "_aux_" ^ {std.any->string {new_int} }, coq.env.begin-module Fresh none) |
| 13 |
true, |
| 14 |
coq.env.begin-module NS none, |
| 15 |
coq.env.current-path CP, |
| 16 |
Clause = open-ns NS CP, |
| 17 |
Out = [Clause | In], |
| 18 |
@local! => |
| 19 |
coq.elpi.accumulate current "NES.db" |
| 20 |
(clause _ (after "open-ns:begin") Clause). |
| 21 |
|
| 22 |
func subpath list string, prop ->. |
| 23 |
subpath Path (ns Sub _) :- std.appendR _Prefix Path Sub, !. |
| 24 |
|
| 25 |
func submod modpath, prop ->. |
| 26 |
submod Mod (ns _ SubMod) :- |
| 27 |
coq.modpath->path SubMod SubPath, |
| 28 |
coq.modpath->path Mod ModPath, |
| 29 |
std.appendR ModPath _Suffix SubPath, !. |
| 30 |
|
| 31 |
func undup list A, list A -> list A. |
| 32 |
undup [] _ []. |
| 33 |
undup [X|XS] Seen YS :- std.mem! Seen X, !, undup XS Seen YS. |
| 34 |
undup [X|XS] Seen [X|YS] :- undup XS [X|Seen] YS. |
| 35 |
|
| 36 |
% end-ns ID Stack ClauseIn ClauseOut |
| 37 |
func end-ns string, list string, list prop -> list prop. |
| 38 |
end-ns NS Stack In Out :- In => std.do! [ |
| 39 |
std.rev Stack Path, |
| 40 |
std.append Path [NS|END_] PathNoEnd, |
| 41 |
std.findall (ns PathNoEnd M_) AllNS, |
| 42 |
coq.env.end-module M, |
| 43 |
% stuff inside M |
| 44 |
std.filter AllNS (submod M) SubmodNS, |
| 45 |
% since the current program still sees the clauses that will be dropped |
| 46 |
% after closing M |
| 47 |
undup SubmodNS [] SubmodNSNodup, |
| 48 |
|
| 49 |
coq.locate-module NS M, |
| 50 |
if (Path = []) |
| 51 |
(std.do! [coq.env.end-module M_aux, coq.env.export-module M_aux, Local = @global!]) |
| 52 |
(Local = @local!), |
| 53 |
% NES.Open can put clauses in scope |
| 54 |
std.append Path [NS] NewPath, |
| 55 |
New = [ns NewPath M | SubmodNSNodup], |
| 56 |
std.append In New Out, |
| 57 |
std.forall New (c\ Local => coq.elpi.accumulate current "NES.db" (clause _ _ c)), |
| 58 |
]. |
| 59 |
|
| 60 |
:index (_ 1) |
| 61 |
func iter-> list A, list A, (func A, list A, list prop -> list prop), list prop -> list prop. |
| 62 |
iter-> _ [] _ O O :- coq.error "No elements". |
| 63 |
iter-> INIT [X] F In Out :- !, F X INIT In Out. |
| 64 |
iter-> INIT [X|XS] F In Out :- F X {std.append XS INIT} In Mid, iter-> INIT XS F Mid Out. |
| 65 |
|
| 66 |
:index (_ 1) |
| 67 |
func iter<- list A, list A, (func A, list A, list prop -> list prop), list prop -> list prop. |
| 68 |
iter<- _ [] _ _ _ :- coq.error "No elements". |
| 69 |
iter<- INIT [X] F In Out :- !, F X INIT In Out. |
| 70 |
iter<- INIT [X|XS] F In Out :- iter<- INIT XS F In Mid, F X {std.append XS INIT} Mid Out. |
| 71 |
|
| 72 |
% Panics unless [S] well-formed. |
| 73 |
func string->non-empty-ns string -> list string. |
| 74 |
string->non-empty-ns S L :- std.do! [ |
| 75 |
Ident = "[a-zA-Z_][a-zA-Z_0-9']*", % non-unicode Coq identifiers |
| 76 |
Path is "^\\(" ^ Ident ^ "\\.\\)*" ^ Ident ^ "$", |
| 77 |
if (rex.match Path S) true (string->ns.err S), |
| 78 |
rex.split "\\." S L, |
| 79 |
if (std.mem L "_") (string->ns.err S) true, |
| 80 |
]. |
| 81 |
func string->ns.err string ->. |
| 82 |
string->ns.err S :- |
| 83 |
coq.error {calc ("NES: Bad <DotSeparatedPath>: \"" ^ S ^ "\"")}. |
| 84 |
|
| 85 |
% Panics unless [S] empty, or well-formed. |
| 86 |
func string->ns string -> list string. |
| 87 |
string->ns "" [] :- !. |
| 88 |
string->ns S L :- string->non-empty-ns S L. |
| 89 |
|
| 90 |
func ns->string list string -> string. |
| 91 |
ns->string NS S :- std.string.concat "." NS S. |
| 92 |
|
| 93 |
func begin-path list string -> list prop. |
| 94 |
begin-path [] [] :- std.do! [ |
| 95 |
coq.begin-synterp-group "nes.begin-path" Group, |
| 96 |
coq.end-synterp-group Group, |
| 97 |
]. |
| 98 |
begin-path ([_|_] as Path) Out :- std.do! [ |
| 99 |
coq.begin-synterp-group "nes.begin-path" Group, |
| 100 |
coq.env.current-path CP, |
| 101 |
if (open-ns _ NSCP) (std.assert! (NSCP = CP) "NS: cannot begin a namespace inside a module that is inside a namespace") true, |
| 102 |
std.map {std.findall (open-ns Y_ P_)} open-ns->string Stack, |
| 103 |
if (Stack = []) true (std.do! [ |
| 104 |
coq.locate-all {std.string.concat "." Path} L, |
| 105 |
if (std.do! [ |
| 106 |
std.mem L (loc-modpath M), |
| 107 |
coq.modpath->path M MP, |
| 108 |
MP = {std.append CP Path} |
| 109 |
]) |
| 110 |
(iter-> [] Stack end-ns [] _, iter<- [] Stack begin-ns [] _) |
| 111 |
true, |
| 112 |
]), |
| 113 |
iter<- Stack {std.rev Path} begin-ns [] Out, |
| 114 |
|
| 115 |
open-super-path Path [], |
| 116 |
coq.end-synterp-group Group, |
| 117 |
]. |
| 118 |
|
| 119 |
func std.time-do! list prop ->. |
| 120 |
std.time-do! []. |
| 121 |
std.time-do! [P|PS] :- |
| 122 |
std.time P T, coq.say P "\ntakes" T "\n", |
| 123 |
!, |
| 124 |
std.time-do! PS. |
| 125 |
|
| 126 |
func end-path list string -> list prop. |
| 127 |
end-path [] [] :- std.do! [ |
| 128 |
coq.begin-synterp-group "nes.end-path" Group, |
| 129 |
coq.end-synterp-group Group, |
| 130 |
]. |
| 131 |
end-path ([_|_] as Path) Out :- std.do! [ |
| 132 |
coq.begin-synterp-group "nes.end-path" Group, |
| 133 |
std.map {std.findall (open-ns X_ P_)} nes.open-ns->string Stack, |
| 134 |
std.assert! (std.appendR {std.rev Path} Bottom Stack) "NES: Ending a namespace that is not begun", |
| 135 |
nes.iter-> Bottom {std.rev Path} nes.end-ns [] Out, |
| 136 |
coq.end-synterp-group Group, |
| 137 |
]. |
| 138 |
|
| 139 |
|
| 140 |
func open-path list string ->. |
| 141 |
open-path Path :- std.do! [ |
| 142 |
coq.begin-synterp-group "nes.open-path" Group, |
| 143 |
std.map {std.findall (ns Path M_)} nes.ns->modpath Mods, |
| 144 |
std.forall Mods coq.env.import-module, |
| 145 |
coq.end-synterp-group Group, |
| 146 |
]. |
| 147 |
|
| 148 |
:index (1) |
| 149 |
func open-super-path list string, list string ->. |
| 150 |
open-super-path [] _. |
| 151 |
open-super-path [P|PS] ACC :- |
| 152 |
std.append ACC [P] Cur, |
| 153 |
open-path Cur, |
| 154 |
open-super-path PS Cur. |
| 155 |
|
| 156 |
% Currently open namespace, or []. |
| 157 |
func current-path -> list string. |
| 158 |
current-path NS :- std.do! [ |
| 159 |
std.map {std.findall (open-ns X_ P_)} nes.open-ns->string Stack, |
| 160 |
std.rev Stack NS, |
| 161 |
]. |
| 162 |
|
| 163 |
% Find an existing namespace, or panic. |
| 164 |
func resolve string -> list string. |
| 165 |
resolve S Path :- std.do! [ |
| 166 |
if (Top = "^_\\.", rex.match Top S) (std.do! [ |
| 167 |
rex.replace Top "" S S', |
| 168 |
string->non-empty-ns S' NS, |
| 169 |
if (ns NS _) (Path = NS) (resolve.err S'), |
| 170 |
])( |
| 171 |
resolve.walk S {current-path} {string->non-empty-ns S} Path |
| 172 |
), |
| 173 |
]. |
| 174 |
func resolve.walk string, list string, list string -> list string. |
| 175 |
resolve.walk S Ctx SP Path :- std.do! [ |
| 176 |
std.append Ctx SP Candidate, |
| 177 |
( ns Candidate _, Path = Candidate |
| 178 |
; Ctx = [], resolve.err S |
| 179 |
; resolve.walk S {std.drop-last 1 Ctx} SP Path ), |
| 180 |
]. |
| 181 |
func resolve.err string ->. |
| 182 |
resolve.err S :- coq.error "NES: Namespace not found:" S. |
| 183 |
|
| 184 |
|
| 185 |
} |