Skip to content

Instantly share code, notes, and snippets.

View takanuva's full-sized avatar

Paulo Torrens takanuva

View GitHub Profile
@takanuva
takanuva / kent-class10.hs
Created March 24, 2023 10:00
Functional Programming - class 10
import Control.Concurrent
mergesortM :: (Ord a) => [a] -> IO [a]
mergesortM xs = do
c <- newChan
mergesortP xs c
result <- readChan c
return result
mergesortP :: (Ord a) => [a] -> Chan [a] -> IO ()
@takanuva
takanuva / kent-class8.hs
Created March 6, 2023 09:32
Functional Programming - class 8
{-
IO operations can be composed through >>=, which is:
(>>=) :: IO a -> (a -> IO b) -> IO b
The other building block for IOs is the return operation:
return :: a -> IO a
-}
@takanuva
takanuva / kent-class6.hs
Created February 20, 2023 09:59
Functional Programming - Class 6
foo = Just (not True)
bar = Just not
-- not :: Bool -> Bool
-- Just :: a -> Maybe a
-- Just not :: Maybe (Bool -> Bool)
--
baz = Just . not
-- Remember that the dot operator joins two functions
@takanuva
takanuva / church.js
Created January 6, 2023 21:23
Factorial and Fibonacci using Church-encoded numbers
// Can we write programs without control flow, variables, or even recursion?
// Well, of course we can! Church did that with pen and paper! :)
const to_num = (n) => n((x) => x + 1)(0)
const ZERO = (f) => (x) => x
const ONE = (f) => (x) => f(x)
const TWO = (f) => (x) => f(f(x))
const THREE = (f) => (x) => f(f(f(x)))
const FOUR = (f) => (x) => f(f(f(f(x))))
@takanuva
takanuva / agt.h
Last active July 25, 2022 19:57
Simple generic print() and scan() macros
/*******************************************************************************
* Copyright 2022 Paulo Torrens *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to *
* deal in the Software without restriction, including without limitation the *
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or *
* sell copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
@takanuva
takanuva / Church
Last active March 11, 2020 16:42
Conversion between Scott encoding and Church endocing for naturals in CoC
\/(N: *) ->
\/(z: N) ->
\/(s: N -> N) ->
N
@takanuva
takanuva / excerpt.hs
Created February 12, 2019 22:38
CPS for the CoC
-- The language of sorts (S, T, U)
data Sort = Prop
| SortPi1 String Type Sort
| SortPi2 String Sort Sort
-- The language of types (A, B, D)
data Type = TypeBound Int
| TypeApplication1 Type Term
| TypeApplication2 Type Type
| TypeLambda1 String Type Type
@takanuva
takanuva / proj.v
Created August 1, 2018 17:01
Admissibility of strong projections
(*
Prove that, due to the free theorem for impredicative sigma types, strong
projection is a valid extension. Based on Dan Doel's Agda version:
https://hub.darcs.net/dolio/agda-share/browse/ParamInduction.agda.
*)
Definition Rel (A: Prop) (B: Prop) :=
A -> B -> Prop.
Definition Sigma (T: Prop) (U: T -> Prop): Prop :=
@takanuva
takanuva / Free.hs
Last active March 21, 2018 22:03
Test with Free monads
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleContexts #-}
import Control.Monad
import Control.Monad.Trans.Free
import Control.Monad.Trans.Class
--------------------------------------------------
@takanuva
takanuva / Commands to compile:
Created March 1, 2014 05:40
Integrating Java and C++ with GCC/CNI :)
#!/bin/bash
# Compile Main for Java bytecode
gcj -C Main.java
# Compile Main for native
gcj -findirect-dispatch -fno-indirect-classes -fpic -c Main.class -o java.o
# Generate header file
gcjh -cp . Main