Created
December 23, 2013 17:19
-
-
Save chris-taylor/8100978 to your computer and use it in GitHub Desktop.
Simple field extensions in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FieldExtension where | |
{- | |
This module lets you define the exact Fibonacci function using the mathematically | |
elegant definition in terms of phi = (1 + sqrt 5) / 2, without losing precision | |
due to calculations being carried out in floating point. | |
phi :: Q5 | |
phi = 0.5 :+ 0.5 | |
fib :: Integral a => a -> a | |
fib n = let a :+ b = phi^n in round $ 2 * b | |
-} | |
import Data.Ratio | |
class Nat n where | |
nat :: f n -> Int | |
withType :: (a -> a) -> a | |
withType f = f undefined | |
withNat :: Nat n => (Int -> Ext n) -> Ext n | |
withNat foo = withType (foo . nat) | |
data Ext a = Rational :+ Rational deriving (Eq) | |
instance Nat a => Show (Ext a) where | |
show z@(a :+ b) = show a ++ " + " ++ show b ++ " sqrt " ++ show (nat z) | |
instance Nat a => Num (Ext a) where | |
(a :+ b) + (c :+ d) = withNat $ \_ -> (a + c) :+ (b + d) | |
(a :+ b) * (c :+ d) = withNat $ \n -> (a * c + fromIntegral n * b * d) :+ (a * d + b * c) | |
(a :+ b) - (c :+ d) = withNat $ \_ -> (a - c) :+ (b - d) | |
abs (a :+ b) = abs a :+ abs b | |
signum (a :+ b) = signum a :+ signum b | |
fromInteger n = fromInteger n :+ 0 | |
data I2 | |
data I3 | |
data I5 | |
data I6 | |
data I7 | |
data I8 | |
data I10 | |
data I11 | |
data I12 | |
data I13 | |
data I14 | |
data I15 | |
data I17 | |
data I18 | |
data I19 | |
data I20 | |
data I21 | |
data I22 | |
data I23 | |
data I24 | |
data I26 | |
data I27 | |
data I28 | |
data I29 | |
data I30 | |
data I31 | |
data I32 | |
data I33 | |
data I34 | |
data I35 | |
data I37 | |
data I38 | |
data I39 | |
data I40 | |
data I41 | |
data I42 | |
data I43 | |
data I44 | |
data I45 | |
data I46 | |
data I47 | |
data I48 | |
data I50 | |
data I51 | |
data I52 | |
data I53 | |
data I54 | |
data I55 | |
data I56 | |
data I57 | |
data I58 | |
data I59 | |
data I60 | |
data I61 | |
data I62 | |
data I63 | |
data I65 | |
data I66 | |
data I67 | |
data I68 | |
data I69 | |
data I70 | |
data I71 | |
data I72 | |
data I73 | |
data I74 | |
data I75 | |
data I76 | |
data I77 | |
data I78 | |
data I79 | |
data I80 | |
data I82 | |
data I83 | |
data I84 | |
data I85 | |
data I86 | |
data I87 | |
data I88 | |
data I89 | |
data I90 | |
data I91 | |
data I92 | |
data I93 | |
data I94 | |
data I95 | |
data I96 | |
data I97 | |
data I98 | |
data I99 | |
instance Nat I2 where nat _ = 2 | |
instance Nat I3 where nat _ = 3 | |
instance Nat I5 where nat _ = 5 | |
instance Nat I6 where nat _ = 6 | |
instance Nat I7 where nat _ = 7 | |
instance Nat I8 where nat _ = 8 | |
instance Nat I10 where nat _ = 10 | |
instance Nat I11 where nat _ = 11 | |
instance Nat I12 where nat _ = 12 | |
instance Nat I13 where nat _ = 13 | |
instance Nat I14 where nat _ = 14 | |
instance Nat I15 where nat _ = 15 | |
instance Nat I17 where nat _ = 17 | |
instance Nat I18 where nat _ = 18 | |
instance Nat I19 where nat _ = 19 | |
instance Nat I20 where nat _ = 20 | |
instance Nat I21 where nat _ = 21 | |
instance Nat I22 where nat _ = 22 | |
instance Nat I23 where nat _ = 23 | |
instance Nat I24 where nat _ = 24 | |
instance Nat I26 where nat _ = 26 | |
instance Nat I27 where nat _ = 27 | |
instance Nat I28 where nat _ = 28 | |
instance Nat I29 where nat _ = 29 | |
instance Nat I30 where nat _ = 30 | |
instance Nat I31 where nat _ = 31 | |
instance Nat I32 where nat _ = 32 | |
instance Nat I33 where nat _ = 33 | |
instance Nat I34 where nat _ = 34 | |
instance Nat I35 where nat _ = 35 | |
instance Nat I37 where nat _ = 37 | |
instance Nat I38 where nat _ = 38 | |
instance Nat I39 where nat _ = 39 | |
instance Nat I40 where nat _ = 40 | |
instance Nat I41 where nat _ = 41 | |
instance Nat I42 where nat _ = 42 | |
instance Nat I43 where nat _ = 43 | |
instance Nat I44 where nat _ = 44 | |
instance Nat I45 where nat _ = 45 | |
instance Nat I46 where nat _ = 46 | |
instance Nat I47 where nat _ = 47 | |
instance Nat I48 where nat _ = 48 | |
instance Nat I50 where nat _ = 50 | |
instance Nat I51 where nat _ = 51 | |
instance Nat I52 where nat _ = 52 | |
instance Nat I53 where nat _ = 53 | |
instance Nat I54 where nat _ = 54 | |
instance Nat I55 where nat _ = 55 | |
instance Nat I56 where nat _ = 56 | |
instance Nat I57 where nat _ = 57 | |
instance Nat I58 where nat _ = 58 | |
instance Nat I59 where nat _ = 59 | |
instance Nat I60 where nat _ = 60 | |
instance Nat I61 where nat _ = 61 | |
instance Nat I62 where nat _ = 62 | |
instance Nat I63 where nat _ = 63 | |
instance Nat I65 where nat _ = 65 | |
instance Nat I66 where nat _ = 66 | |
instance Nat I67 where nat _ = 67 | |
instance Nat I68 where nat _ = 68 | |
instance Nat I69 where nat _ = 69 | |
instance Nat I70 where nat _ = 70 | |
instance Nat I71 where nat _ = 71 | |
instance Nat I72 where nat _ = 72 | |
instance Nat I73 where nat _ = 73 | |
instance Nat I74 where nat _ = 74 | |
instance Nat I75 where nat _ = 75 | |
instance Nat I76 where nat _ = 76 | |
instance Nat I77 where nat _ = 77 | |
instance Nat I78 where nat _ = 78 | |
instance Nat I79 where nat _ = 79 | |
instance Nat I80 where nat _ = 80 | |
instance Nat I82 where nat _ = 82 | |
instance Nat I83 where nat _ = 83 | |
instance Nat I84 where nat _ = 84 | |
instance Nat I85 where nat _ = 85 | |
instance Nat I86 where nat _ = 86 | |
instance Nat I87 where nat _ = 87 | |
instance Nat I88 where nat _ = 88 | |
instance Nat I89 where nat _ = 89 | |
instance Nat I90 where nat _ = 90 | |
instance Nat I91 where nat _ = 91 | |
instance Nat I92 where nat _ = 92 | |
instance Nat I93 where nat _ = 93 | |
instance Nat I94 where nat _ = 94 | |
instance Nat I95 where nat _ = 95 | |
instance Nat I96 where nat _ = 96 | |
instance Nat I97 where nat _ = 97 | |
instance Nat I98 where nat _ = 98 | |
instance Nat I99 where nat _ = 99 | |
type Q2 = Ext I2 | |
type Q3 = Ext I3 | |
type Q5 = Ext I5 | |
type Q6 = Ext I6 | |
type Q7 = Ext I7 | |
type Q8 = Ext I8 | |
type Q10 = Ext I10 | |
type Q11 = Ext I11 | |
type Q12 = Ext I12 | |
type Q13 = Ext I13 | |
type Q14 = Ext I14 | |
type Q15 = Ext I15 | |
type Q17 = Ext I17 | |
type Q18 = Ext I18 | |
type Q19 = Ext I19 | |
type Q20 = Ext I20 | |
type Q21 = Ext I21 | |
type Q22 = Ext I22 | |
type Q23 = Ext I23 | |
type Q24 = Ext I24 | |
type Q26 = Ext I26 | |
type Q27 = Ext I27 | |
type Q28 = Ext I28 | |
type Q29 = Ext I29 | |
type Q30 = Ext I30 | |
type Q31 = Ext I31 | |
type Q32 = Ext I32 | |
type Q33 = Ext I33 | |
type Q34 = Ext I34 | |
type Q35 = Ext I35 | |
type Q37 = Ext I37 | |
type Q38 = Ext I38 | |
type Q39 = Ext I39 | |
type Q40 = Ext I40 | |
type Q41 = Ext I41 | |
type Q42 = Ext I42 | |
type Q43 = Ext I43 | |
type Q44 = Ext I44 | |
type Q45 = Ext I45 | |
type Q46 = Ext I46 | |
type Q47 = Ext I47 | |
type Q48 = Ext I48 | |
type Q50 = Ext I50 | |
type Q51 = Ext I51 | |
type Q52 = Ext I52 | |
type Q53 = Ext I53 | |
type Q54 = Ext I54 | |
type Q55 = Ext I55 | |
type Q56 = Ext I56 | |
type Q57 = Ext I57 | |
type Q58 = Ext I58 | |
type Q59 = Ext I59 | |
type Q60 = Ext I60 | |
type Q61 = Ext I61 | |
type Q62 = Ext I62 | |
type Q63 = Ext I63 | |
type Q65 = Ext I65 | |
type Q66 = Ext I66 | |
type Q67 = Ext I67 | |
type Q68 = Ext I68 | |
type Q69 = Ext I69 | |
type Q70 = Ext I70 | |
type Q71 = Ext I71 | |
type Q72 = Ext I72 | |
type Q73 = Ext I73 | |
type Q74 = Ext I74 | |
type Q75 = Ext I75 | |
type Q76 = Ext I76 | |
type Q77 = Ext I77 | |
type Q78 = Ext I78 | |
type Q79 = Ext I79 | |
type Q80 = Ext I80 | |
type Q82 = Ext I82 | |
type Q83 = Ext I83 | |
type Q84 = Ext I84 | |
type Q85 = Ext I85 | |
type Q86 = Ext I86 | |
type Q87 = Ext I87 | |
type Q88 = Ext I88 | |
type Q89 = Ext I89 | |
type Q90 = Ext I90 | |
type Q91 = Ext I91 | |
type Q92 = Ext I92 | |
type Q93 = Ext I93 | |
type Q94 = Ext I94 | |
type Q95 = Ext I95 | |
type Q96 = Ext I96 | |
type Q97 = Ext I97 | |
type Q98 = Ext I98 | |
type Q99 = Ext I99 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment