Skip to content

Instantly share code, notes, and snippets.

View dannypsnl's full-sized avatar

Lîm Tsú-thuàn dannypsnl

View GitHub Profile
@dannypsnl
dannypsnl / Tyck.hs
Last active November 18, 2022 12:00
simple polymorphic type checker in haskell
{-# LANGUAGE LambdaCase #-}
module Tyck
( Term (..),
Type (..),
emptyContext,
emptySolution,
topInfer,
infer,
check,
@dannypsnl
dannypsnl / all-factors.rkt
Created October 9, 2022 20:06
all factors
#lang racket
(define prime-list '(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59))
(define (factor-list n)
(define p-factor-list
(for/list ([p prime-list]
#:when (= (remainder n p) 0))
p))
(println p-factor-list)
@dannypsnl
dannypsnl / strictly-positive.rkt
Last active September 20, 2022 18:10
strictly positive checker
#lang racket
(require syntax/parse/define
(for-syntax syntax/stx))
(begin-for-syntax
(define-syntax-class type
#:datum-literals (->)
(pattern (~literal Type)
#:attr occurs (λ (D) #f))
(pattern x:id
@dannypsnl
dannypsnl / encasulate-future.rkt
Created September 15, 2022 10:20
use future in weird way
#lang racket/base
(require racket/future
racket/async-channel
racket/function
racket/match)
(define process-map (make-hasheq))
(struct pid ())
(struct process (id fu ch))
@dannypsnl
dannypsnl / list-to-tree.rkt
Created September 4, 2022 22:23
Convert a list that element has depth information to tree
#lang typed/racket
#|
tree definitions
|#
(struct Tree ([v : String] [sub-tree : (Listof Tree)])
#:property prop:custom-write
(λ (v port mode) (if (empty? (Tree-sub-tree v))
(fprintf port "~a" (Tree-v v))
(fprintf port "~a ~a" (Tree-sub-tree v) (Tree-v v)))))
@dannypsnl
dannypsnl / c-calc.rkt
Last active January 22, 2025 16:20
Compile to C and get back immediately
#lang racket
(require syntax/parse/define
(for-syntax syntax/stx
racket/string
racket/system)
ffi/unsafe)
(begin-for-syntax
(define-syntax-class c/ty
#:datum-literals (double)
@dannypsnl
dannypsnl / refer-type.rkt
Last active August 19, 2022 03:48
improving the type refer situation
#lang curly-fn racket
(require syntax/parse/define
(for-syntax racket/syntax
syntax/stx))
(define-syntax-parser List
[(_ ty) (with-disappeared-uses
(record-disappeared-uses/recur #'ty)
#''(List ty))])
(define-syntax-parser Integer
@dannypsnl
dannypsnl / compile.rkt
Created August 9, 2022 10:58
Build compiler incrementally
#lang typed/racket
(define-type Prog (Listof Instruction))
(define-type Regsiter (U Symbol))
(define-type ImmediateValue (U Integer))
(struct Instruction () #:transparent)
(struct mov Instruction
([register : Regsiter]
@dannypsnl
dannypsnl / SPropSketch.hs
Created June 15, 2022 05:34 — forked from atennapel/SPropSketch.hs
SProp sketch
{-# LANGUAGE PatternSynonyms #-}
type Ix = Int
type Lvl = Int
type ULvl = Int
data Sort = Type ULvl | Prop
deriving (Show, Eq)
data SortType = SType | SProp
@dannypsnl
dannypsnl / sdf-ode.rkt
Created June 13, 2022 13:13
Software Design for Flexibility: simple ODE
#lang racket
(define (%history ts xs)
(λ args
(cond ((null? args) `(%history ,ts ,xs))
((eq? (first args) 'x)
(list-ref xs (second args)))
((eq? (first args) 't)
(list-ref ts (second args)))
((eq? (first args) 'extend)