Skip to content

Instantly share code, notes, and snippets.

@hsk
hsk / dnf+.pl
Last active March 22, 2023 07:06
dnf+
% Sound and Complete Flow Typing with Unions,
% Intersections and Negation
% https://ecs.wgtn.ac.nz/foswiki/pub/Main/TechnicalReportSeries/ECSTR12-20.pdf
:- op(1200,xfx,::=),op(650,xfx,∈),op(250,yf,*),op(600,xfy,[&,$]).
G∈{G}. G∈(G|_). G∈(_|G1):-G∈G1. G∈G.
syntax(G,E):-G=..[O|Gs],E=..[O|Es],maplist(syntax,Gs,Es),!.
syntax(G,E):-(G::=Gs),!,G1∈Gs,syntax(G1,E),!.
syntax(i,I):-integer(I),!.
syntax(E*,Ls):-maplist(syntax(E),Ls).
@hsk
hsk / cnf.v
Last active May 1, 2019 07:10
Coqで実装したので停止性と型安全性が保証できているので構文的に完全な cnf 変換
Definition x := nat.
Inductive t:=X:x->t|True:t|False:t|N:t->t|A:t*t->t|O:t*t->t.
Inductive n:=NX:x->n|NTrue:n|NFalse:n|NN:x->n|NA:n*n->n|NO:n*n->n.
Fixpoint t2n(t0:t):n :=
match t0 with
| N(a) => t2nneg a
| A(a,b) => NO(t2n a,t2n b)
| O(a,b) => NA(t2n a,t2n b)
| X x => NX x
| True => NTrue
@hsk
hsk / nnf.v
Last active December 26, 2023 09:37
構文的に完全なnnf変換
Definition x := nat.
Inductive t:=X:x->t|True:t|False:t|N:t->t|A:t*t->t|O:t*t->t.
Inductive n:=NX:x->n|NTrue:n|NFalse:n|NNX:x->n|NA:n*n->n|NO:n*n->n.
Fixpoint nnf(t0:t):n :=
match t0 with
| N(a) => neg a
| A(a,b) => NO(nnf a,nnf b)
| O(a,b) => NA(nnf a,nnf b)
| X x => NX x
| True => NTrue
@hsk
hsk / t4.pl
Created April 30, 2019 08:44
cnf
:- op(1100,xfy,&).
:-op(600,xfy,=>).
:-op(700,xfy,<=>).
n(-(-A),A1):-!,n(A,A1).
n(-(A&B),R):-!,n(-A,A1),n(-B,B1),d(A1|B1,R).
n(-(A|B),R):-!,n(-A,A1),n(-B,B1),d(A1&B1,R).
n(A&B,R):-!,n(A,A1),n(B,B1),d(A1&B1,R).
n(A|B,R):-!,n(A,A1),n(B,B1),d(A1|B1,R).
n(-true,false).
n(-false,true).
@hsk
hsk / fjv.pl
Created February 16, 2019 05:17
fjv.pl
:- module(fjv,[init/0,syntax/2,(<:)/2,fields/2,ftype/3,split/3,
mtype/3,(/-)/2,okall/0,run/1]).
% ------------------------ SYNTAX ------------------------
init :- op(920,xfx,[=>,=>*]),op(910,xfx,[/-]),
op(501,xfx,<:),op(500,xfy,∨),op(511,yfx,#),op(510,xfx,!),
op(1200,xfx,::=),op(650,xfx,∈),op(250,yf,*),op(999,fx,class).
:- init.
G∈{G}. G∈(G|_). G∈(_|G1):-G∈G1. G∈G.
syntax(G,E):-G=..[O|Gs],E=..[O|Es],maplist(syntax,Gs,Es),!.
syntax(G,E):-(G::=Gs),!,G1∈Gs,syntax(G1,E),!.
@hsk
hsk / Untitled.ipynb
Created December 20, 2018 16:08
Juypter で Prolog のファイル
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@hsk
hsk / swipl.py
Created December 20, 2018 14:37
Jupyter-Lab SWI-Prolog change files
from pyswip import Prolog
from pyswip import Functor
from pyswip.prolog import PrologError
DEFAULT_LIMIT = 10
ini = False
def init(prolog):
global ini
if ini: return prolog
ini = True
prolog.assertz("""get_labapp(A) :- retract(outs(A))""")
spaceN(N,R2):-findall(' ',between(1,N,_),R),concat_atom(R,R2).
pprint(FP,T) :- tell(FP),pprint(T),tell(user_output).
pprint(T) :- pprint1(0,T),writeln(.).
pprint1(_,T) :- atomic(T),print(T).
pprint1(N,T) :- format(atom(V),'~p',[T]),atom_length(V,N1),N2 is N1+N,N2 < 80,write(V).
pprint1(N,T) :- is_list(T), writeln('['),N1 is N+1,pplist(N1,T),printSpace(N),write(']').
pprint1(N,A:B) :- writeq(A),write(:),pprint1(N,B).
pprint1(N,A) :- A =..[P|Ps], write(P),write('('),ppparams(N,Ps),write(')').
ppparams(_,[]).
ppparams(N,[A]):-pprint1(N,A).
@hsk
hsk / w.js
Last active September 5, 2018 04:03
welsh & powell graph coloring register allocation
// Welsh-Powell-Algorithm
function coloring(es) {
var ns = neighbors(es),cs = {}, c = 0
ns.sort(([_a1,e1],[_a2,e2])=>e2.length-e1.length)
ns.forEach(([a1,e1],i)=>(a1 in cs || (cs[a1] = ++c),
ns.slice(i+1).forEach(([a,e])=>
a in cs||e1.includes(a)||e.some(v=>cs[v]==c)||(cs[a]=c))))
return [cs,es]
}
// 隣を求める
:- A=a{x:10,y:20},nb_set_dict(x,A,5),writeln(A).
:- expects_dialect(sicstus).
:- time(forall(between(0,100000,I),(b_setval(a,I),b_getval(a,_)))).
:- time(forall(between(0,100000,I),(nb_linkval(a,I),nb_getval(a,_)))).
:- time(forall(between(0,100000,I),(nb_setval(a,I),nb_getval(a,_)))).
:- time(forall(between(0,100000,I),(bb_put(a,I),bb_get(a,_)))).
:- time(forall(between(0,100000,I),(retractall(a(_)),assert(a(I)),a(_)))).
:- assert(a(0)),time(forall(between(0,100000,I),(retract(a(_)),assert(a(I)),a(_)))).
:- writeln(---).
:- b_setval(a,hoge(0)),time(forall(between(0,1000000,_),(b_getval(a,_)))).