Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / type_inference.pl
Last active January 22, 2020 19:10
A simple demo of type inference in SWI-Prolog
:- initialization(main).
:- set_prolog_flag('double_quotes','chars').
main :- Term = (member(Z,A),append(A,B,C),dif(A,[true,false])),has_type(Term:Type,Types),writeln('Term with types to infer:'),writeln(Term),writeln('Types of variables in this term:'),writeln(Types).
greater_than(A,B) :-
A > B.
matches_any_([],B) :- false.
matches_any_([A|A1],B) :-
@jarble
jarble / search_redirector.js
Created February 23, 2020 03:39
A Greasemonkey script with a "whitelist" of keywords
// ==UserScript==
// @name Search redirector
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author Anderson Green
// @match *://*/*
// @grant none
// ==/UserScript==
@jarble
jarble / generic_example.frag
Last active January 11, 2023 21:41
Generic programming in GLSL (parametric polymorphism, template metaprogramming) https://www.reddit.com/r/glsl/comments/mmxves/generic_programming_parametric_polymorphism/
//I wish there were a better way to do this!
#define func(type,name,param1,param2,body) type name(type param1,type param2) {body}
#define generic(name,param1,param2,body) func(float,name,param1,param2,body) func(vec2,name,param1,param2,body) func(vec3,name,param1,param2,body) func(vec4,name,param1,param2,body)
//define two "generic" functions using this macro
generic(add,a,b,
return a + b;
)
generic(sub,a,b,
const replaceOnce = require('replace-once');
var str = 'I have a cat, a cathy, and a catch.';
var find = ['cat', 'cathy', 'catch'];
var replace = ['catch', 'catch', 'cathy'];
console.log(replaceOnce(str, find, replace, 'gi'));
//=> 'I have a catch, a catchhy, and a catchch.'
@jarble
jarble / replace_matching_sublist.erl
Created September 27, 2021 18:57
Replacing matching sublists in Erlang
-module(helloworld).
-import(lists,[append/2]).
-export([start/0]).
replace_if_match(X) ->
case X of
[A, "equals", B | Tail ] ->
[[A,"==",B],replace_if_match(Tail)];
[A, "is", B | Tail ] ->
[[A,"==",B],replace_if_match(Tail)];