This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
このプログラムは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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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([],[]). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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):-!. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |