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 / 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 / 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 / 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 / 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. */
(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 / 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
@PhDP
PhDP / harr1.cc
Last active August 29, 2015 14:16
First example of Harrison's "Handbook of Practical Logic and Automated Reasoning" in C++11. Even clang-format couldn't improve this...
#include <iostream>
#include <string>
#include <boost/variant.hpp>
struct Add;
struct Mult;
using Expr = boost::variant<int, std::string, boost::recursive_wrapper<Add>,
boost::recursive_wrapper<Mult>>;
@PhDP
PhDP / harr1.jl
Last active August 29, 2015 14:16
First example of Harrison's "Handbook of Practical Logic and Automated Reasoning" in Julia.
abstract Expr
type Const <: Expr; value::Int end
type Var <: Expr; name::String end
type Add <: Expr; left::Expr; right::Expr end
type Mult <: Expr; left::Expr; right::Expr end
add(x::Const, y::Const) = Const(x.value + y.value)
add(x::Const, y::Expr) = x.value == 0? y : Add(x, y)
add(x::Expr, y::Const) = add(y, x)
@PhDP
PhDP / harr1.hs
Last active August 29, 2015 14:16
First example of Harrison's "Handbook of Practical Logic and Automated Reasoning" in Haskell
data Expr =
Var String
| Const Int
| Add Expr Expr
| Mult Expr Expr
simplify1 :: Expr -> Expr
simplify1 e = case e of
Add (Const 0) x -> x
Add x (Const 0) -> x
Section "Device"
Identifier "nvidia"
Driver "nvidia"
Option "NoLogo" "true"
Option "DPI" "96 x 96"
# Specify Nvidia PCI device
BusID "PCI:1:0:0"
# Make sure X starts also when no outputs are connected to the Nvidia chip
Option "AllowEmptyInitialConfiguration"
EndSection