Skip to content

Instantly share code, notes, and snippets.

@jarble
jarble / partial_evaluator.pl
Last active September 25, 2019 00:50
A partial evaluator for non-recursive Prolog programs
:- initialization(main).
:- set_prolog_flag(double_quotes,chars).
main :- recursive_partial_eval((is_between(3,4,5) -> false,is_between(1,C,3);is_between(3,4,5);is_between(3,D,4)),B),writeln(B).
recursive_partial_eval(A,B) :-
find_all_clauses_(A,A1),
(A==A1,A1=B;recursive_partial_eval(A1,B)).
find_all_clauses_(A,A) :- var(A).
find_all_clauses_(A,B) :- nonvar(A),find_all_clauses(A,B).
@jarble
jarble / adaptive_parser.py
Created August 26, 2019 02:23
A simple adaptive parser in Python
import copy
import sys
import subprocess
from tokenize import tokenize
from io import BytesIO
def tokenize_input(the_input):
return ([a.string for a in list(tokenize(BytesIO(the_input.encode('utf-8')).readline)) if a.string not in ["\n","","utf-8"]])
def matches_pattern(string,pattern):
@jarble
jarble / pseudocode_parser_generator.py
Last active August 11, 2019 17:41
This Python script generates a "pseudocode parser" for the Nearley parser generator. It's based on an adaptive parsing algorithm or "adaptive grammar."
import copy
import sys
import subprocess
def matches_pattern(string,pattern):
if len(string) != len(pattern):
return False
for i in range(0,len(string)):
if is_expr_(pattern[i]):
for j in range(0,len(string)):
@jarble
jarble / type_inference.pl
Last active February 17, 2019 05:19
A simple example of type inference in Prolog using Constraint Handling Rules
:- initialization(main).
:- set_prolog_flag('double_quotes','chars'). ;this is for SWI-Prolog
:- use_module(library(chr)).
:- chr_constraint type/2.
type(A,B) \ type(A,B) <=> true.
type((A;B),C) ==> C = bool,type(A,bool),type(B,bool).
type((A,B),C) ==> C = bool,type(A,bool),type(B,bool).
type(A>B,C) ==> C = bool,type(A,number),type(B,number).
type(A<B,C) ==> C = bool,type(A,number),type(B,number).
type(A+B,C) ==> C = number,type(A,number),type(B,number).
@jarble
jarble / prolog_translator.jison
Created January 13, 2019 09:28
A Prolog-to-Python translator using list comprehensions
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
[0-9]+("."[0-9]+)?\b return 'NUMBER'
\"([^\\\"]|\\.)*\" return 'STRING_LITERAL'
"forall" return 'forall'
"copy_term" return 'copy_term'
"if" return "if"
@jarble
jarble / duplicate_code_detection.pl
Last active August 21, 2021 21:17
An example of duplicate code detection in Prolog
:- initialization(main).
:- set_prolog_flag(double_quotes, chars).
main :-
duplicate_code(
(G<A;A>B+1;C>D+1),
Output),
writeln(Output).
%find a duplicated term within a term
@jarble
jarble / closure_example.pl
Last active February 8, 2024 09:05
An example of closures (or "nested predicates") in Prolog
%Since SWI-Prolog does not have a built-in implementation of closures, I wrote my own implementation here.
:- initialization(main).
:- set_prolog_flag('double_quotes','chars').
main :- predicate_with_nested(1,C),writeln(C).
call_local(Definition,Params) :-
copy_term(Definition,(Params :- Body)),
call(Body).
<!DOCTYPE html>
<html>
<body>
<input type="text" id="input_box"/> <button id = "speak">Click me</button><br/><br/>Repeat how many times:<input type="text" id="repeat"/>
<script type = "text/javascript">
var speak = function(){for(var i = 0; i < document.getElementById("repeat").value; i++)
window.speechSynthesis.speak(new SpeechSynthesisUtterance(document.getElementById('input_box').value));};
@jarble
jarble / chr_metainterpreter.pl
Last active November 8, 2018 23:07
This is an attempt to write a meta-interpreter for Constraint Handling Rules.
:- initialization(main).
:- set_prolog_flag('double_quotes','chars').
:- use_module(library(chr)).
:- chr_constraint is_true/1.
is_true(X) \ is_true(X) <=> true.
is_true(X ==> Y),is_true(X1) ==> copy_term((X -> Y),(X2 -> Y1)),(X2=X1,(is_true(X1)->is_true(Y1));X2\=X1),writeln(Y).
is_true((X;Y)) ==> is_true(X);is_true(Y).
is_true((X,Y)) ==> is_true(X),is_true(Y).
is_true(is_true(X)) ==> is_true(X).
:- initialization(main).
main :- recursive_simplify_expr(((x+y+1)^2 = (x-y)^2),Output),writeln(["Output: ", Output]).
%mul([add([mul([add([mul([x,x]),mul([x,y2])])]),mul([add([mul([x,y1]),mul([y1,y2])])])])])=0
simplify_expr(mul([A|Rest]),mul([1|[A|Rest]])) :-
atom(A).
simplify_expr(mul([A]),A).
simplify_expr(A/B,A1/B1) :-
simplify_expr(A,A1),simplify_expr(B,B1).