Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add predicates to coq-lib to convert elpi strings and lists to coq strings and lists. #530

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions coq-builtin.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,38 @@ typeabbrev field-attributes (list field-attribute).
% retrocompatibility macro for Coq v8.10
macro @coercion! :- [coercion reversible].

pred int->bools i:int, o:list bool.
pred int->bools.unpadded i:int, o:list bool.
int->bools.unpadded 0 [] :- !.
int->bools.unpadded INT BITS :-
if ({ calc (INT mod 2) } == 1) (BIT is tt) (BIT is ff),
BITS = [BIT | { int->bools.unpadded { calc (INT div 2) } }].
int->bools INT BITS :-
int->bools.unpadded INT BITSSHORT,
std.length BITSSHORT SIZE,
std.map { std.iota { calc (8 - SIZE) } } (x\ r\ r = ff) PADDING,
std.append BITSSHORT PADDING BITS.

pred string->ascii i:string, o:list (list bool).
string->ascii.aux INDEX STRING BS :-
INT is rhc (substring STRING INDEX 1),
int->bools INT BS.
string->ascii S BSS :-
LENGTH is size S,
std.map {std.iota LENGTH} (x\ r\ string->ascii.aux x S r) BSS.

pred string->stringterm i:string, o:term.
pred string->stringterm.aux i:list bool, i:term, o:term.
string->stringterm.aux X ACC RES :-
std.map X (x\ r\ if (x == tt) (r = {{ true }}) (r = {{ false }})) BITSTERM,
RES = app [{{ String }}, app [{{ Ascii }} | BITSTERM], ACC].
string->stringterm STRING T :-
string->ascii STRING BSS,
foldr BSS {{ EmptyString }} string->stringterm.aux T.

pred list->listterm i:list term, o:term.
list->listterm TS T :- foldr TS {{ [] }} (x\ a\ r\ r = {{ cons lp:x lp:a }}) T.


% Attributes for a record field. Can be left unspecified, see defaults
% below.
Expand Down
32 changes: 32 additions & 0 deletions elpi/coq-lib.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,38 @@ coq.with-TC Class Instance->Clause Code :-
map Instances Instance->Clause Hyps, !,
Hyps => Code.

pred coq.int->bools i:int, o:list bool.
coq.int->bools INT BITS :-
coq.int->bools.aux INT BITSSHORT,
std.length BITSSHORT SIZE,
map { std.iota { calc (8 - SIZE) } } (x\ r\ r = ff) PADDING,
std.append BITSSHORT PADDING BITS.
pred coq.int->bools.aux i:int, o:list bool.
coq.int->bools.aux 0 [] :- !.
coq.int->bools.aux INT BITS :-
if ({ calc (INT mod 2) } == 1) (BIT is tt) (BIT is ff),
BITS = [BIT | { coq.int->bools.aux { calc (INT div 2) } }].

pred coq.string->ascii i:string, o:list (list bool).
coq.string->ascii S BSS :-
LENGTH is size S,
map {std.iota LENGTH} (x\ r\ coq.string->ascii.aux x S r) BSS.
coq.string->ascii.aux INDEX STRING BS :-
INT is rhc (substring STRING INDEX 1),
coq.int->bools INT BS.

pred coq.string->stringterm i:string, o:term.
coq.string->stringterm STRING T :-
coq.string->ascii STRING BSS,
foldr BSS {{ EmptyString }} coq.string->stringterm.aux T.
pred coq.string->stringterm.aux i:list bool, i:term, o:term.
coq.string->stringterm.aux X ACC RES :-
map X (x\ r\ if (x == tt) (r = {{ true }}) (r = {{ false }})) BITSTERM,
RES = app [{{ String }}, app [{{ Ascii }} | BITSTERM], ACC].

pred coq.list->listterm i:list term, o:term.
coq.list->listterm TS T :- std.foldr TS {{ [] }} (x\ a\ r\ r = {{ cons lp:x lp:a }}) T.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pred coq.parse-attributes i:list attribute, i:list attribute-signature, o:list prop.
Expand Down
Loading