Skip to content

Instantly share code, notes, and snippets.

@googleson78
Last active March 8, 2021 12:10
Show Gist options
  • Save googleson78/55cb4a2632bf51f45991f59a54adb0ec to your computer and use it in GitHub Desktop.
Save googleson78/55cb4a2632bf51f45991f59a54adb0ec to your computer and use it in GitHub Desktop.
if-then-else which can return different types in the two branches
module Bla where
-- declare constants to minimise example
-- just assume Set == Type
postulate
-- there is a type called String
String : Set
-- it has a value called 'hey' in it
hey : String
-- there is a type called Int
Int : Set
-- it has a value called 'one' in it
one : Int
-- declare booleans
data Bool : Set where
ff tt : Bool
-- calculate which type of the given two we should return based on a boolean
-- actually we can use the traditional if-then-else instead
-- but we need to generalise it a bit, so I omitted doing so
If : Bool -> Set -> Set -> Set
If ff A B = B
If tt A B = A
if_then_else_ :
-- for any two types 'A' and 'B'
{A B : Set}
-- and a boolean value 'b'
(b : Bool) ->
-- given values of those types
A -> B ->
-- return the value of the correct type
If b A B
if ff then t else e = e
if tt then t else e = t
-- example usage
-- > if ff then hey else one
-- one
-- > if tt then hey else one
-- hey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment