Skip to content

Instantly share code, notes, and snippets.

View nachivpn's full-sized avatar

Nacho nachivpn

View GitHub Profile
@nachivpn
nachivpn / STLCBetaWHNF.agda
Last active June 18, 2021 13:37
Beta-WHNF NbE for STLC
module STLCBetaWHNF where
open import Data.Sum
open import Data.Unit using (⊤ ; tt)
open import Data.Product
--------------------
-- Term syntax, etc.
--------------------
@nachivpn
nachivpn / logicincs.md
Last active October 24, 2021 11:00
Logic in CS, beyond DAT060/DIT202

Master's Courses

¬[_] : ∀ {a b : Name} → Dec (a ≡ b) → Set
¬[ (yes x₁) ] = ⊥
¬[ (no x₁) ] = ⊤
data _,_∈x_ (n : Name) (p : Pointer) : Env → Set where
zero : ∀ {xs} → n , p ∈x ((n , p) ∷ xs)
suc : ∀ {n' : Name} { p' : Pointer} {xs} {pr : ¬[ n == n' ]}
→ n , p ∈x xs → n , p ∈x ((n' , p') ∷ xs)
private
@nachivpn
nachivpn / scoped_belongsto.agda
Last active October 14, 2017 14:59
scoped_belongsto.agda
-- scope sensitive belongs
-- Unlike ∈, it does not allow scope insensitive proof of belongs
_∈s_ : (Name × Pointer) → Env → Set
(n , p) ∈s [] = ⊥
(n , p) ∈s ((n' , p') ∷ ρ') with n ==n n' | p ==n p'
... | false | _ = ( n , p ) ∈s ρ'
... | _ | false = ⊥
... | true | true = ⊤
private
@nachivpn
nachivpn / LeftRight.java
Last active July 30, 2017 18:33
LeftRight.java
public final static class Left<LL, LR> extends Either<LL, LR>{
LL l;
public Left(LL l) {
this.l = l;
}
@Override
public <T> T match(Function<LL, T> a, Function<LR, T> b) {
return a.apply(l);
}
@Override
@nachivpn
nachivpn / InventorySample.java
Last active July 30, 2017 13:59
InventorySample.java
public static void main(String args[]){
//bike product
Map<String,List<String>> productDetails = new HashMap<>();
List<String> bikeComponentNames = new ArrayList<>();
bikeComponentNames.add("wheel");
bikeComponentNames.add("wheel");
bikeComponentNames.add("frame");
productDetails.put("bike", bikeComponentNames);
//componets
@nachivpn
nachivpn / ProductPrice.java
Last active July 30, 2017 18:45
ProductPrice.java
Map<String,Component> components;
public List<String> getComponentNames(String product){
return productDetails.get(product);
}
//given a product, return the sum of prices of it's components
//if anything fails, return that specific exception
public Either<Exception,Double> getProductPrice(String product){
return getComponentNames(product)
.stream()
//from product name string, get componenet as Either<Exception,Component>
@nachivpn
nachivpn / getComponentPriceWDisc.java
Last active July 30, 2017 18:46
getComponentPriceWDisc.java
//return component price from Either<Exception, Component>, but if any step fails, return exception which occured
public Either<Exception,Double> getComponentPrice(Either<Exception, Component> response){
return response
//extract component id
.fmap(result -> result.getId())
//find price
.bind(this::findOriginalPrice)
//apply discount if applicable
.fmap(this::discountedPrice);
}
@nachivpn
nachivpn / discountedPrice.java
Last active July 30, 2017 18:37
discountedPrice.java
Double discount;
//apply discount to a given price and return discounted price
public Double discountedPrice(Double originalPrice){
Optional<Double> optionalDiscount = Optional.ofNullable(discount);
return optionalDiscount
.map(discount -> originalPrice - originalPrice * discount)
.orElse(originalPrice);
}
@nachivpn
nachivpn / getComponentPrice.java
Last active July 30, 2017 18:36
getComponentPrice.java
//return the price of component in Either<Exception, Component> if price is available, else return exception
public Either<Exception,Double> getComponentPrice(Either<Exception, Component> response){
return response.bind(result -> findOriginalPrice(result.getId()));
}