| 1 |
/* Entry point for all derivations */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
namespace derive { |
| 6 |
|
| 7 |
pred dep o:string, o:string. |
| 8 |
dep X Y :- dep1 X Y. |
| 9 |
dep X Y :- dep1 X Z, dep Z Y. |
| 10 |
|
| 11 |
func selected string ->. |
| 12 |
selected Name :- get-option "only" Map, !, |
| 13 |
Map ==> (get-option Name _; (get-option X _, dep X Name)), !. |
| 14 |
selected _. |
| 15 |
|
| 16 |
:index (_ 1) |
| 17 |
func chain string, list derive ->. |
| 18 |
chain _ []. |
| 19 |
chain T [derive Name _ _|FS] :- not(selected Name), !, |
| 20 |
chain T FS. |
| 21 |
chain T [derive _ _ AlreadyDone|FS] :- ((pi x\ stop x :- !, fail) ==> AlreadyDone), !, |
| 22 |
chain T FS. |
| 23 |
chain T [derive _ F _|FS] :- get-option "only" _, !, % request this one |
| 24 |
F _, |
| 25 |
chain T FS. |
| 26 |
chain T [derive _ F _|FS] :- % all are selected, we can fail |
| 27 |
((pi x\ stop x :- !, fail) ==> F _), !, |
| 28 |
chain T FS. |
| 29 |
chain T [derive _ _ _|FS] :- |
| 30 |
chain T FS. |
| 31 |
|
| 32 |
func toposort list derive -> list derive. |
| 33 |
toposort L SL :- |
| 34 |
std.findall (dep1 _ _) Deps, |
| 35 |
topo L Deps SL. |
| 36 |
|
| 37 |
func not-a-src list prop, derive ->. |
| 38 |
not-a-src Deps (derive A _ _) :- not(std.mem! Deps (dep1 A _)). |
| 39 |
|
| 40 |
:index (1) |
| 41 |
func tgt-is-not-in list derive, prop ->. |
| 42 |
tgt-is-not-in [] _. |
| 43 |
tgt-is-not-in [derive Tgt _ _|_] (dep1 _ Tgt) :- !, fail. |
| 44 |
tgt-is-not-in [_|L] D :- tgt-is-not-in L D. |
| 45 |
|
| 46 |
func topo list derive, list prop -> list derive. |
| 47 |
topo [] _ [] :- !. |
| 48 |
topo L Deps SL :- |
| 49 |
std.partition L (not-a-src Deps) LNoDeps Other, |
| 50 |
if (LNoDeps = []) (coq.error "derive: no topological order:" L Deps) true, |
| 51 |
std.filter Deps (tgt-is-not-in LNoDeps) NewDeps, |
| 52 |
topo Other NewDeps SOther, |
| 53 |
std.append LNoDeps SOther SL. |
| 54 |
|
| 55 |
func main string ->. |
| 56 |
main TypeName :- get-option "module" M, !, |
| 57 |
if (M = "") (Mod = TypeName) (Mod = M), |
| 58 |
coq.env.begin-module Mod none, |
| 59 |
main-derive TypeName tt, |
| 60 |
coq.env.end-module _. |
| 61 |
main TypeName :- |
| 62 |
main-derive TypeName ff. |
| 63 |
|
| 64 |
func main-derive string, bool ->. |
| 65 |
main-derive TypeName InModule :- |
| 66 |
if (get-option "prefix" PFX) |
| 67 |
(Prefix = PFX) |
| 68 |
(if (InModule is ff) (Prefix is TypeName ^ "_") (Prefix = "")), |
| 69 |
std.findall (derivation TypeName Prefix _) L, |
| 70 |
std.map L (x\r\ x = derivation _ _ r) DL, |
| 71 |
toposort DL SortedDL, |
| 72 |
chain TypeName SortedDL. |
| 73 |
|
| 74 |
} |