Skip to content

Instantly share code, notes, and snippets.

@paf31
Last active December 29, 2015 16:29
Show Gist options
  • Save paf31/7697483 to your computer and use it in GitHub Desktop.
Save paf31/7697483 to your computer and use it in GitHub Desktop.
data Zero = Zero
data Succ a = Succ a
pred = \s -> case s of Succ a -> a
meters = \n ->
{ multiply: \v -> v { value = n * v.value, meters = Succ (v.meters) }
, divide: \v -> v { value = n / v.value, meters = pred (v.meters) }
}
seconds = \n ->
{ multiply: \v -> v { value = n * v.value, seconds = Succ (v.seconds) }
, divide: \v -> v { value = n / v.value, seconds = pred (v.seconds) }
}
(*.) = \n m ->
{ multiply: \x -> m.multiply (n.multiply x)
, divide: \x -> m.divide (n.divide x)
}
(+.) = \f g ->
{ multiply: \x ->
{ var v1 = f.multiply x;
var v2 = g.multiply x;
return v2 { value = v1.value + v2.value }; }
, divide: \x ->
{ var v1 = f.divide x;
var v2 = g.divide x;
return v2 { value = v1.value + v2.value }; }
}
(~) = \f g ->
{ multiply: \x ->
{ var v1 = f.multiply x;
var v2 = g.multiply x;
return v2 { value = v1.value - v2.value }; }
, divide: \x ->
{ var v1 = f.divide x;
var v2 = g.divide x;
return v2 { value = v1.value - v2.value }; }
}
(/.) = \n m ->
{ multiply: \x -> m.divide (n.multiply x)
, divide: \x -> m.multiply (n.divide x)
}
test = seconds 1 /. seconds 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment