In Idris, is “Eq a” a type, and can I supply a value for it?

后端 未结 1 1746
感情败类
感情败类 2021-01-18 10:46

In the following, example1 is standard syntax (as documented), with Eq a as a constraint. In example2, I specify Eq a dir

1条回答
  •  再見小時候
    2021-01-18 11:40

    You could use named implementations:

    [eq_nat] Eq Nat where { ... }
    ...
    example_of_example2 : Type
    example_of_example2 = example2 Nat eq_nat 3 3
    

    A useful application of named implementations are defining multiple implementation of Monoid:

    [PlusNatSemi] Semigroup Nat where
      (<+>) x y = x + y
    
    [MultNatSemi] Semigroup Nat where
      (<+>) x y = x * y
    
    [PlusNatMonoid] Monoid Nat using PlusNatSemi where
      neutral = 0
    
    [MultNatMonoid] Monoid Nat using MultNatSemi where
      neutral = 1
    

    To comment : To get the default instance

    getEq : (a : Type) -> {auto inst : Eq a} -> Eq a
    getEq a {inst} = inst
    

    Then you can have your default instance:

    getEq (List Nat) -- => implementation of Eq...
    

    0 讨论(0)
提交回复
热议问题