Skip to content

Instantly share code, notes, and snippets.

use nom;
use nom::{
bytes::complete::{tag, take_until, take_while1},
branch::alt,
character::complete::{char, multispace0},
combinator::{map, cut},
error::VerboseError,
multi::{many1,separated_list0},
sequence::{preceded, terminated, tuple, separated_pair},
IResult,
@hsk
hsk / math.pl
Last active March 11, 2024 08:54
/*
このプログラムはswi-prologで実行することができます。
swiproogのインストールは以下のようにしてインストールできます:
$ brew install swi-prolog
$ apt install swi-prolog
このプログラムを実行するには以下のようにコマンドラインから実行します:
$ swipl math.pl
現代数学は一階述語論理を道具としてペアノ数などを定義していくことで構築されています。
http://www.f.waseda.jp/moriya/PUBLIC_HTML/social/natural_numbers.pdf
@hsk
hsk / simplesub1.scala
Created August 2, 2020 15:22
simple-sub1
import scala.collection.mutable.{Set => MutSet}
object simplesub1 extends App {
sealed trait Term
final case class Lit(value: Int) extends Term // as in: 27
final case class Var(name: String) extends Term // as in: x
final case class Lam(name: String, rhs: Term) extends Term // as in: λx . t
final case class App(lhs: Term, rhs: Term) extends Term // as in: s t
final case class Rcd(fields: List[(String, Term)]) extends Term // as in: { a : 0; b : true; ... }
final case class Sel(receiver: Term, fieldName: String) extends Term // as in: t .a
/*
https://letexpr.hatenablog.com/entry/2018/11/15/222959
%ast
term ::= string
| bool(qual,bool)
| if(term, term , term)
| (qual, term, term)
| let((string, string)=term, term)
| λ(qual, string: ltype, term)
| app(term, term).
@hsk
hsk / cse.pl
Last active August 17, 2020 07:22
Reaching Definition
% https://qiita.com/fukkun/items/05160e8dae0e15ae201e
reset:- nb_setval(cnt,0),retractall(tmp(_,_)).
genid(C1):- nb_getval(cnt,C),C1 is C+1,nb_setval(cnt,C1).
gentmp(T):- genid(C),atomic_list_concat([t,C],T).
gentmp(E,T):- tmp(E,T).
gentmp(E,T):- gentmp(T),assert(tmp(E,T)).
tran([N:(X=A+B)|C],[N:(T=A+B),NA:(X=T)|C_]) :- gentmp(A+B,T),atom_concat(N,*,NA),tran(C,C_).
tran([N:(X=A-B)|C],[N:(T=A-B),NA:(X=T)|C_]) :- gentmp(A-B,T),atom_concat(N,*,NA),tran(C,C_).
tran([N:I|C],[N:I|C_]):- tran(C,C_).
tran([],[]).
@hsk
hsk / parse.pl
Created October 7, 2019 12:28
操車場アルゴリズムのProlog実装
ops(+,1).
ops(*,2).
parse(O,S,[],R):-!,parse3(O,S,R).
parse(O,S,['('|C],R):-!,parse(O,['('|S],C,R).
parse(O,S,[')'|C],R):-!,parse1(O,S,C,R).
parse(O,S,[A|C],R):-atom_number(A,I),!,parse([I|O],S,C,R).
parse(O,S,[V|C],R):-ops(V,P),!,parse2(P,O,S,[V|C],R).
parse(_,_,_,_):-throw('InvalidToken').
parse1(_,[],_,_):-throw('ParenthesisMismatch').
parse1(O,['('|S],C,R):-parse(O,S,C,R).
@hsk
hsk / bnf7.ml
Created September 21, 2019 08:55
bnf7.ml
type 'a e = I of int | X of string | L of 'a e list | V of 'a
let rec bind f v = match v with
| None -> None
| Some v -> Some (f v)
let rec map2M f es gs = match es,gs with
| e::es,g::gs ->
begin match f e g with
| None -> None
| Some x ->
function or(...ps) {
return (x) => {
for(var i = 0; i< ps.length;i++)
if (ps[i](x)) return true
return false
}
}
function i(x) {
return Number.isInteger(x)
}
@hsk
hsk / dnf.pl
Last active May 10, 2019 21:46
nnf変換とdnf変換を同時に行う
% nnf変換とdnf変換を一発で計算する単純なプログラム
:- 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(x,X):-atom(X),!.
syntax(E*,Ls):-maplist(syntax(E),Ls).
t ::= x | t* | -t | (t | t) | t& t.
dod(D1|D2,D3,D1|D4):-!,dod(D2,D3,D4).
dod(-[],D2,D2):-!.
@hsk
hsk / memo.ml
Last active May 10, 2019 13:05
結果が大きくなるような計算用のmemo化 DNFなどを作る場合に有効なはず
type t = I of int | Add of t * t | Mul of t * t
type t1 = t
module TMap = Map.Make(struct
type t=t1
let eq_t t1 t2 =
if t1==t2 then true else
match t1,t2 with
| Add(t1,t2),Add(t3,t4) -> t1==t3 && t2==t4
| Mul(t1,t2),Mul(t3,t4) -> t1==t3 && t2==t4