Skip to content

Instantly share code, notes, and snippets.

View PhDP's full-sized avatar
🏠
Working from home

Philippe Desjardins-Proulx PhDP

🏠
Working from home
View GitHub Profile
@PhDP
PhDP / harr1.sc
Last active August 29, 2015 14:17
First example of Harrison's "Handbook of Practical Logic and Automated Reasoning" in Scala
sealed abstract class Expr
case class Variable(name: String) extends Expr
case class Const(value: Int) extends Expr
case class Add(left: Expr, right: Expr) extends Expr
case class Mult(left: Expr, right: Expr) extends Expr
object Simplify {
def one(e: Expr): Expr = e match {
case Add(Const(0), r) => r
case Add(l, Const(0)) => l
(use '[clojure.core.match :only [match]])
(defn evaluate [env [sym x y]]
(match [sym]
['Number] x
['Add] (+ (evaluate env x) (evaluate env y))
['Multiply] (* (evaluate env x) (evaluate env y))
['Variable] (env x)))
(def environment {"a" 3, "b" 4, "c" 5})
@PhDP
PhDP / formula.cpp
Created May 1, 2015 02:21
Playing with propositional logic / first-order logic in C++ with boost::variant.
#include <iostream>
#include <string>
#include <memory>
#include <set>
#include <boost/variant.hpp>
#include <format.h>
// Selection of symbols
/** A set of symbols for printing / parsing formula. */
@PhDP
PhDP / tokenize.cc
Created July 22, 2015 22:44
Playing with new C++ features to see if I can write a tokenizer / parser without boost.
#include <iostream>
#include <string>
#include <vector>
auto member_of(const std::vector<char> &cs) {
return [=](const char c) -> bool {
for (const char &x : cs) if (c == x) return true;
return false;
};
};
@PhDP
PhDP / fol.pegjs
Last active October 11, 2015 19:21
First-order logic parser file for PEG.js, can handle weighted formulas of the form FORMULA ; WEIGHT.
{
function getTerms(t, ts) {
if (t) {
var arr = [];
for (var i = 0; i < ts.length; ++i) {
arr.push(ts[i][2]);
}
return [t].concat(arr);
}
return [];
@PhDP
PhDP / set.cc
Last active January 25, 2016 02:18
unordered_set using the shared_ptr's value
#include <iostream>
#include <string>
#include <memory>
#include <unordered_set>
#include <unordered_map>
#include <boost/functional/hash.hpp>
class my_class {
std::string m_name;
@PhDP
PhDP / safe_sqrt.cc
Created January 28, 2016 06:46
Example of boost::optional to write a safe sqrt function.
// clang++ -std=c++11 safe_sqrt.cc
#include <iostream>
#include <cmath>
#include <boost/optional.hpp>
auto safe_sqrt(double x) -> boost::optional<double> {
if (x >= 0.0)
return std::sqrt(x);
return boost::none;
@PhDP
PhDP / bond.fsx
Created February 5, 2016 01:13 — forked from evelinag/bond.fsx
Analysing box office success of James Bond films using HTML type provider
#load "packages/FsLab/FsLab.fsx"
open FSharp.Data
open XPlot.GoogleCharts
let bondUrl = "https://en.wikipedia.org/w/index.php?title=List_of_James_Bond_films&oldid=688916363"
type BondProvider = HtmlProvider<"https://en.wikipedia.org/w/index.php?title=List_of_James_Bond_films&oldid=688916363">
let bondWiki = BondProvider.Load(bondUrl)
@PhDP
PhDP / permutations.cc
Created February 24, 2016 04:59
List all unique sets for a range of integers.
// clang++ -std=c++11 permutations.cc -o permutations
#include <iostream>
#include <ostream>
#include <set>
#include <vector>
template<typename T>
auto operator<<(std::ostream &os, const std::set<T> &xs) -> std::ostream& {
os << '{';
@PhDP
PhDP / expression.hh
Created March 12, 2016 19:09
C++11 template for math expressions
#ifndef EXPRESSION_HH__
#define EXPRESSION_HH__
#include <iostream>
#include <string>
#include <boost/variant.hpp>
#include <boost/lexical_cast.hpp>
namespace reasoning {