Git Product home page Git Product logo

programming-in-idris's Introduction

Programming in Idris

Implicit Argument
-- explicit
append: (elem: Type) -> (n: Nat) -> (m: Nat) ->
		Vect n elem -> Vect m elem -> Vect (n + m) elem
-- > append Char 2 2 ['a', 'b'] ['c', 'd']
-- > append _ _ _ ['a', 'b'] ['c', 'd']

-- implicit
append: Vect n elem -> Vect m elem -> Vect (n + m) elem		-- unbound
append: {elem: Type} -> {n: Nat} -> {m: Nat} ->
		Vect n elem -> Vect m elem -> Vect (n + m) elem	 	-- bound
-- > append ['a', 'b'] ['c', 'd']
Use Implicit in Function
-- plain
length: Vect n elem -> Nat
length [] = Z
length (x :: xs) = 1 + length xs

-- dependent
length: Vect n elem -> Nat
length {n} xs = n

-- dependent with pattern matching
createEmpties: Vect n (Vect 0 a)
createEmpties {n = Z} = []
createEmpties {n = (S k)} = [] :: createEmpties
Basic Data Types
-- enumerated types (enum)
data Bool = False | True
data Direction = North | East | South | West

-- union types (parameterized enum)
data Shape = Triangle Double Double
			| Rectangle Double Double
			| Circle Double
data Shape: Type where
	Triangle: Double -> Double -> Shape
	Rectangle: Double -> Double -> Shape
	Circle: Double -> Shape
			
-- recursive types (self-defining types)
data Nat = Z | S Nat
data Infinite = Forever Infinite

-- generic types (type-parameterized)
data Maybe valtype = Nothing | Just valtype
data Either a b = Left a | Right b
Pattern Naming and Reuse
insert x orig@(Node left val right)
	= case compare x val of
		LT => Node (insert x left) val right
		EQ => orig
		GT => Node left val (insert x right)
Dependent Data Types
data PowerSouce = Petrol | Pedal

data Vehicle: PowerSource -> Type where		-- dependent types / families of types
	Bicycle: Vehicle Pedal
	Car: (fuel: Nat) -> Vehicle Petrol
	Bus: (fuel: Nat) -> Vehicle Petrol
	
data Vect: Nat -> Type -> Type where
	Nil: Vect Z a
	(::): (x: a) -> (xs: Vect k a) -> Vect (S k) a
	
data DataStore: Type where
	MkData: (size: Nat) ->
		    (items: Vect size String) ->	-- computation from parameters
		    DataStore
Dependent Pairs
anyVect: (n: Nat ** Vect n String)		-- second element computed from the first
anyVect: (n ** Vect n String)			-- type inference
Type-level Calculation
-- type calculation functions
StringOrInt: Bool -> Type
StringOrInt False = String
StringOrInt True = Int
-- usage
getStringOrInt: (isInt: Bool) -> StringOrInt isInt
getStringOrInt False = "Ninety Four"
getStringOrInt True = 94

-- or use case expression directly
valToString': (isInt: Bool) -> (case isInt of False => String True => Int) -> String
valToString' False y = trim y
valToString' True y = cast y
Interfaces
Eq Matter where
		(==) Solid Solid = True
		(==) Liquid Liquid = True
		(==) Gas Gas = True
		(==) _ _ = False
		
		(/=) x y = not (x == y)
		
-- default methods
interface Eq a where
		(==): a -> a -> Bool
		(/=): a -> a -> Bool
		
		(==) x y = not (x /= y)
		(/=) x y = not (x == y)

programming-in-idris's People

Watchers

Tao Tao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.