Skip to content

Instantly share code, notes, and snippets.

View kamahen's full-sized avatar

Peter Ludemann kamahen

View GitHub Profile
@kamahen
kamahen / tree_path.pl
Last active March 8, 2021 19:44
calculate path into a binary tree for some element
% -*- mode: Prolog -*-
% See https://twitter.com/krismicinski/status/1368611779107053570?s=03
% gives a program in Racket; I've written my version in
% non-deterministic Prolog.
% Tested with SWI-Prolog 8.3.20
% (You can also try this out at https://swish.swi-prolog.org/)
% The problem is (using Racket terminology):
@kamahen
kamahen / coverage.pl
Created July 12, 2021 17:57
Hack on top of swipl library(test_cover) for more granular coverage
% -*- mode: Prolog -*-
/*
TODO: generalize this code to work in any file (currently, file name is hard-coded)
This code depends on a modification to ~/src/swipl-devel/packages/plunit/test_cover.pl:
$ git diff
diff --git a/test_cover.pl b/test_cover.pl
index 5290499..24d8874 100644
@kamahen
kamahen / d_wire.pl
Created July 18, 2021 14:59
Test data for protobufs:proto_meta_field_name/4 indexing
wire_codes(RequestWireStream) :-
RequestWireStream=[10,16,100,101,115,99,114,105,112,116,111,114,46,112,114,111,116,111,26,8,8,3,16,17,24,3,34,0,122,185,133,3,10,16,100,101,115,99,114,105,112,116,111,114,46,112,114,111,116,111,18,15,103,111,111,103,108,101,46,112,114,111,116,111,98,117,102,34,77,10,17,70,105,108,101,68,101,115,99,114,105,112,116,111,114,83,101,116,18,56,10,4,102,105,108,101,24,1,32,3,40,11,50,36,46,103,111,111,103,108,101,46,112,114,111,116,111,98,117,102,46,70,105,108,101,68,101,115,99,114,105,112,116,111,114,80,114,111,116,111,82,4,102,105,108,101,34,228,4,10,19,70,105,108,101,68,101,115,99,114,105,112,116,111,114,80,114,111,116,111,18,18,10,4,110,97,109,101,24,1,32,1,40,9,82,4,110,97,109,101,18,24,10,7,112,97,99,107,97,103,101,24,2,32,1,40,9,82,7,112,97,99,107,97,103,101,18,30,10,10,100,101,112,101,110,100,101,110,99,121,24,3,32,3,40,9,82,10,100,101,112,101,110,100,101,110,99,121,18,43,10,17,112,117,98,108,105,99,95,100,101,112,101,110,100,101,110,99,121,24,10,32,3,40,5,82,16,112,117,98,10
:- det(valid_termpos/2).
%! valid_termpos(+Term, +TermPos) is semidet.
% Checks that a Term has an appropriate TermPos.
% This should always succeed:
% read_term(Term, [subterm_positions(TermPos)]),
% valid_termpos(Term, TermPos)
valid_termpos(_Term, Var), var(Var) => true. % trivial default: "unknown" position
valid_termpos(Atom, _From-_To), atom(Atom) => true.
valid_termpos(Number, _From-_To), number(Number) => true.
valid_termpos(Var, _From-_To), var(Var) => true.
@kamahen
kamahen / pcre2_mingw.c
Created April 5, 2022 20:16
Output some PCRE2 values for debugging PCRE2 with MinGW
#include <stdio.h>
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
static void config_uint(const char *descr, uint32_t flag) {
uint32_t value;
uint32_t len = pcre2_config_8(flag, NULL);
int rc = pcre2_config_8(flag, &value);
if (rc >= 0) {
printf("%s (len=%d rc=%d) 0x%08x: 0x%08x\n", descr, len, rc, flag, value);
@kamahen
kamahen / archive.pl
Last active June 3, 2022 17:40
library(archive) snapshot 2022-06-03
/* Part of SWI-Prolog
Author: Jan Wielemaker and Matt Lilley
E-mail: J.Wielemaker@cs.vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2012-2019, VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2000-2022, University of Amsterdam
VU University Amsterdam
SWI-Prolog Solutions b.v.
All rights reserved.
@kamahen
kamahen / chatgpt_solve_visitor_problem.rkt
Created March 19, 2023 23:15
ChatGPT writes Racket code to solve the visitor problem
; Define the hosts and visitors as symbols
(define hosts '(akira bhaskar charlemagne))
(define visitors '(delancy erika fei-fei))
; Define the availability of each host
(define availability
'((akira (1 2 3))
(bhaskar (1 3 4))
(charlemagne (2 4))))
@kamahen
kamahen / compilation.output
Last active June 2, 2023 19:50
SWIPL wasm build with error
2: Running test set "gmp" ...................../home/peter/src/swipl-devel/build.wasm/src/swipl.js:1
2: var Module=typeof Module!="undefined"?Module:{};var moduleOverrides=Object.assign({},Module);var arguments_=[];var thisProgram="./this.program";var quit_=(status,toThrow)=>{throw toThrow};var ENVIRONMENT_IS_WEB=typeof window=="object";var ENVIRONMENT_IS_WORKER=typeof importScripts=="function";var ENVIRONMENT_IS_NODE=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string";var scriptDirectory="";function locateFile(path){if(Module["locateFile"]){return Module["locateFile"](path,scriptDirectory)}return scriptDirectory+path}var read_,readAsync,readBinary,setWindowTitle;if(ENVIRONMENT_IS_NODE){var fs=require("fs");var nodePath=require("path");if(ENVIRONMENT_IS_WORKER){scriptDirectory=nodePath.dirname(scriptDirectory)+"/"}else{scriptDirectory=__dirname+"/"}read_=(filename,binary)=>{filename=isFileURI(filename)?new URL(filename):nodePath.normalize(filename);return fs.rea
@kamahen
kamahen / deadfish.pl
Created October 6, 2023 16:45
deadfish code using dict/DCG and library(yall)
% Deadfish is one of the best known non Turing-complete programming languages.
% It has only one accumulator (which starts at 0) to store data, and only four commands:
%
% i - Increment the accumulator
% s - Square the accumulator
% d - Decrement the accumulator
% o - Output the accumulator
%
% Create a program that will input a number and output Deadfish
% code to display the number. It must work for any integer from 0 to 255.